Skip to content

Commit

Permalink
Merge pull request kubernetes-client#692 from briceruzand/current-con…
Browse files Browse the repository at this point in the history
…text-namespace

Set current context namespace in loadFromCluster()
  • Loading branch information
k8s-ci-robot authored Jul 14, 2021
2 parents 0428941 + 578854c commit 45c2726
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
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,
},
];
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

0 comments on commit 45c2726

Please sign in to comment.