Skip to content

Commit

Permalink
Merge pull request #368 from brendandburns/configit
Browse files Browse the repository at this point in the history
Fix bugs in programmatic cluster config. Add unit test.
  • Loading branch information
k8s-ci-robot authored Nov 19, 2019
2 parents 2109605 + cf6c628 commit 34cfa68
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ export class KubeConfig {
*/
public 'currentContext': string;

constructor() {
this.contexts = [];
this.clusters = [];
this.users = [];
}

public getContexts() {
return this.contexts;
}
Expand Down Expand Up @@ -224,6 +230,9 @@ export class KubeConfig {
}

public addCluster(cluster: Cluster) {
if (!this.clusters) {
this.clusters = [];
}
this.clusters.forEach((c: Cluster, ix: number) => {
if (c.name === cluster.name) {
throw new Error(`Duplicate cluster: ${c.name}`);
Expand All @@ -233,6 +242,9 @@ export class KubeConfig {
}

public addUser(user: User) {
if (!this.users) {
this.users = [];
}
this.users.forEach((c: User, ix: number) => {
if (c.name === user.name) {
throw new Error(`Duplicate user: ${c.name}`);
Expand All @@ -242,6 +254,9 @@ export class KubeConfig {
}

public addContext(ctx: Context) {
if (!this.contexts) {
this.contexts = [];
}
this.contexts.forEach((c: Context, ix: number) => {
if (c.name === ctx.name) {
throw new Error(`Duplicate context: ${c.name}`);
Expand Down
25 changes: 25 additions & 0 deletions src/config_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1123,6 +1123,31 @@ describe('KubeConfig', () => {
});
});

describe('Programmatic', () => {
it('should be able to generate a valid config from code', () => {
const kc = new KubeConfig();
kc.addCluster({
name: 'testCluster',
server: `https://localhost:9889`,
skipTLSVerify: true,
});
kc.addUser({
token: 'token',
username: 'username',
name: 'testUser',
});
kc.addContext({
cluster: 'testCluster',
name: 'test',
user: 'testUser',
});
kc.setCurrentContext('test');

expect(kc.getCurrentCluster()!.name).to.equal('testCluster');
expect(kc.getCurrentUser()!.username).to.equal('username');
});
});

describe('BufferOrFile', () => {
it('should load from root if present', () => {
const data = 'some data for file';
Expand Down

0 comments on commit 34cfa68

Please sign in to comment.