Skip to content

Set current context namespace in loadFromCluster() #692

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -221,11 +221,17 @@ export class KubeConfig {
},
},
];
const namespaceFile = `${pathPrefix}${Config.SERVICEACCOUNT_NAMESPACE_PATH}`;
let namespace: string | undefined;
if (fileExists(namespaceFile)) {
namespace = fs.readFileSync(namespaceFile, 'utf8');
}
this.contexts = [
{
cluster: clusterName,
name: contextName,
user: userName,
namespace,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this be namespace: namespace?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh, right typescript just repeats it for you, nevermind...

},
];
this.currentContext = contextName;
Expand Down Expand Up @@ -448,6 +454,7 @@ export class Config {
public static SERVICEACCOUNT_ROOT: string = '/var/run/secrets/kubernetes.io/serviceaccount';
public static SERVICEACCOUNT_CA_PATH: string = Config.SERVICEACCOUNT_ROOT + '/ca.crt';
public static SERVICEACCOUNT_TOKEN_PATH: string = Config.SERVICEACCOUNT_ROOT + '/token';
public static SERVICEACCOUNT_NAMESPACE_PATH: string = Config.SERVICEACCOUNT_ROOT + '/namespace';

public static fromFile(filename: string): api.CoreV1Api {
return Config.apiFromFile(filename, api.CoreV1Api);
Expand Down
8 changes: 8 additions & 0 deletions src/config_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1151,10 +1151,12 @@ describe('KubeConfig', () => {
}
const token = 'token';
const cert = 'cert';
const namespace = 'myNamespace';
mockfs({
'/var/run/secrets/kubernetes.io/serviceaccount': {
'ca.crt': cert,
token,
namespace,
},
});

Expand All @@ -1181,6 +1183,12 @@ describe('KubeConfig', () => {
'/var/run/secrets/kubernetes.io/serviceaccount/token',
);
}
const contextName = kc.getCurrentContext();
const currentContext = kc.getContextObject(contextName);
expect(currentContext).to.not.be.null;
if (currentContext) {
expect(currentContext.namespace).to.equal('myNamespace');
}
});

it('should load from cluster with http port', () => {
Expand Down