Skip to content

Commit 45c2726

Browse files
authored
Merge pull request #692 from briceruzand/current-context-namespace
Set current context namespace in loadFromCluster()
2 parents 0428941 + 578854c commit 45c2726

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

src/config.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,11 +221,17 @@ export class KubeConfig {
221221
},
222222
},
223223
];
224+
const namespaceFile = `${pathPrefix}${Config.SERVICEACCOUNT_NAMESPACE_PATH}`;
225+
let namespace: string | undefined;
226+
if (fileExists(namespaceFile)) {
227+
namespace = fs.readFileSync(namespaceFile, 'utf8');
228+
}
224229
this.contexts = [
225230
{
226231
cluster: clusterName,
227232
name: contextName,
228233
user: userName,
234+
namespace,
229235
},
230236
];
231237
this.currentContext = contextName;
@@ -448,6 +454,7 @@ export class Config {
448454
public static SERVICEACCOUNT_ROOT: string = '/var/run/secrets/kubernetes.io/serviceaccount';
449455
public static SERVICEACCOUNT_CA_PATH: string = Config.SERVICEACCOUNT_ROOT + '/ca.crt';
450456
public static SERVICEACCOUNT_TOKEN_PATH: string = Config.SERVICEACCOUNT_ROOT + '/token';
457+
public static SERVICEACCOUNT_NAMESPACE_PATH: string = Config.SERVICEACCOUNT_ROOT + '/namespace';
451458

452459
public static fromFile(filename: string): api.CoreV1Api {
453460
return Config.apiFromFile(filename, api.CoreV1Api);

src/config_test.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1151,10 +1151,12 @@ describe('KubeConfig', () => {
11511151
}
11521152
const token = 'token';
11531153
const cert = 'cert';
1154+
const namespace = 'myNamespace';
11541155
mockfs({
11551156
'/var/run/secrets/kubernetes.io/serviceaccount': {
11561157
'ca.crt': cert,
11571158
token,
1159+
namespace,
11581160
},
11591161
});
11601162

@@ -1181,6 +1183,12 @@ describe('KubeConfig', () => {
11811183
'/var/run/secrets/kubernetes.io/serviceaccount/token',
11821184
);
11831185
}
1186+
const contextName = kc.getCurrentContext();
1187+
const currentContext = kc.getContextObject(contextName);
1188+
expect(currentContext).to.not.be.null;
1189+
if (currentContext) {
1190+
expect(currentContext.namespace).to.equal('myNamespace');
1191+
}
11841192
});
11851193

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

0 commit comments

Comments
 (0)