diff --git a/src/config.ts b/src/config.ts index db889d1c785..20f5b46e761 100644 --- a/src/config.ts +++ b/src/config.ts @@ -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; @@ -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); diff --git a/src/config_test.ts b/src/config_test.ts index 144f02dcdb7..8798f71fdeb 100644 --- a/src/config_test.ts +++ b/src/config_test.ts @@ -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, }, }); @@ -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', () => {