Skip to content

Fix bugs in programmatic cluster config. Add unit test. #368

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 1 commit into from
Nov 19, 2019
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
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) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Would it not be better to put this in the constructor? I think it would be more logical and easier to follow too.

Copy link
Contributor

Choose a reason for hiding this comment

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

Not a blocker just curious before I add my lgtm (and it merges)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The fields involved are public, so it's always possible that someone will overwrite them with something undefined or null, so the check here is safer.

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