Skip to content

Commit fcc15e1

Browse files
committed
Add several new options for creating KubeConfig objects.
1 parent a7ff580 commit fcc15e1

File tree

2 files changed

+80
-1
lines changed

2 files changed

+80
-1
lines changed

node-client/src/config.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,26 @@ export class KubeConfig {
131131
this.currentContext = obj['current-context'];
132132
}
133133

134+
public loadFromOptions(options: any) {
135+
this.clusters = options.clusters;
136+
this.contexts = options.contexts;
137+
this.users = options.users;
138+
this.currentContext = options.currentContext;
139+
}
140+
141+
public loadFromClusterAndUser(cluster: Cluster, user: User) {
142+
this.clusters = [cluster];
143+
this.users = [user];
144+
this.currentContext = 'loaded-context';
145+
this.contexts = [
146+
{
147+
cluster: cluster.name,
148+
user: user.name,
149+
name: this.currentContext,
150+
} as Context,
151+
];
152+
}
153+
134154
public loadFromCluster() {
135155
const host = process.env.KUBERNETES_SERVICE_HOST;
136156
const port = process.env.KUBERNETES_SERVICE_PORT;

node-client/src/config_test.ts

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ import * as base64 from 'base-64';
44
import { expect } from 'chai';
55
import * as https from 'https';
66

7-
import { Config, KubeConfig } from './config';
7+
import { KubeConfig } from './config';
8+
import { Cluster, Context, User } from './config_types';
89

910
const kcFileName = 'testdata/kubeconfig.yaml';
1011

@@ -44,6 +45,64 @@ describe('KubeConfig', () => {
4445
expect(obj3).to.equal(null);
4546
});
4647
});
48+
49+
describe('loadFromClusterAndUser', () => {
50+
it('should load from cluster and user', () => {
51+
const kc = new KubeConfig();
52+
const cluster = {
53+
name: 'foo',
54+
server: 'http://server.com',
55+
} as Cluster;
56+
57+
const user = {
58+
name: 'my-user',
59+
password: 'some-password',
60+
} as User;
61+
62+
kc.loadFromClusterAndUser(cluster, user);
63+
64+
const clusterOut = kc.getCurrentCluster();
65+
expect(clusterOut).to.equal(cluster);
66+
67+
const userOut = kc.getCurrentUser();
68+
expect(userOut).to.equal(user);
69+
});
70+
});
71+
72+
describe('clusterConstructor', () => {
73+
it('should load from options', () => {
74+
const cluster = {
75+
name: 'foo',
76+
server: 'http://server.com',
77+
} as Cluster;
78+
79+
const user = {
80+
name: 'my-user',
81+
password: 'some-password',
82+
} as User;
83+
84+
const context = {
85+
name: 'my-context',
86+
user: user.name,
87+
cluster: cluster.name,
88+
};
89+
90+
const kc = new KubeConfig();
91+
kc.loadFromOptions({
92+
clusters: [cluster],
93+
users: [user],
94+
contexts: [context],
95+
currentContext: context.name,
96+
});
97+
98+
const clusterOut = kc.getCurrentCluster();
99+
expect(clusterOut).to.equal(cluster);
100+
101+
const userOut = kc.getCurrentUser();
102+
expect(userOut).to.equal(user);
103+
});
104+
});
105+
47106
describe('loadFromFile', () => {
48107
it('should load the kubeconfig file properly', () => {
49108
const kc = new KubeConfig();

0 commit comments

Comments
 (0)