Skip to content

Commit 1c9e5b4

Browse files
authored
Merge pull request #166 from brendandburns/coverage
Improve coverage, fix some bugs.
2 parents d7d3dad + 620fac5 commit 1c9e5b4

File tree

5 files changed

+360
-167
lines changed

5 files changed

+360
-167
lines changed

src/config.ts

Lines changed: 80 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -14,49 +14,6 @@ import { Cluster, Context, newClusters, newContexts, newUsers, User } from './co
1414
import { ExecAuth } from './exec_auth';
1515

1616
export class KubeConfig {
17-
// Only public for testing.
18-
public static findHomeDir(): string | null {
19-
if (process.env.HOME) {
20-
try {
21-
fs.accessSync(process.env.HOME);
22-
return process.env.HOME;
23-
// tslint:disable-next-line:no-empty
24-
} catch (ignore) {}
25-
}
26-
if (process.platform !== 'win32') {
27-
return null;
28-
}
29-
if (process.env.HOMEDRIVE && process.env.HOMEPATH) {
30-
const dir = path.join(process.env.HOMEDRIVE, process.env.HOMEPATH);
31-
try {
32-
fs.accessSync(dir);
33-
return dir;
34-
// tslint:disable-next-line:no-empty
35-
} catch (ignore) {}
36-
}
37-
if (process.env.USERPROFILE) {
38-
try {
39-
fs.accessSync(process.env.USERPROFILE);
40-
return process.env.USERPROFILE;
41-
// tslint:disable-next-line:no-empty
42-
} catch (ignore) {}
43-
}
44-
return null;
45-
}
46-
47-
// Only really public for testing...
48-
public static findObject(list: any[], name: string, key: string) {
49-
for (const obj of list) {
50-
if (obj.name === name) {
51-
if (obj[key]) {
52-
return obj[key];
53-
}
54-
return obj;
55-
}
56-
}
57-
return null;
58-
}
59-
6017
private static authenticators: Authenticator[] = [
6118
new CloudAuth(),
6219
new ExecAuth(),
@@ -106,7 +63,7 @@ export class KubeConfig {
10663
if (!this.contexts) {
10764
return null;
10865
}
109-
return KubeConfig.findObject(this.contexts, name, 'context');
66+
return findObject(this.contexts, name, 'context');
11067
}
11168

11269
public getCurrentCluster(): Cluster | null {
@@ -117,16 +74,20 @@ export class KubeConfig {
11774
return this.getCluster(context.cluster);
11875
}
11976

120-
public getCluster(name: string): Cluster {
121-
return KubeConfig.findObject(this.clusters, name, 'cluster');
77+
public getCluster(name: string): Cluster | null {
78+
return findObject(this.clusters, name, 'cluster');
12279
}
12380

124-
public getCurrentUser() {
125-
return this.getUser(this.getCurrentContextObject().user);
81+
public getCurrentUser(): User | null {
82+
const ctx = this.getCurrentContextObject();
83+
if (!ctx) {
84+
return null;
85+
}
86+
return this.getUser(ctx.user);
12687
}
12788

128-
public getUser(name: string): User {
129-
return KubeConfig.findObject(this.users, name, 'user');
89+
public getUser(name: string): User | null {
90+
return findObject(this.users, name, 'user');
13091
}
13192

13293
public loadFromFile(file: string) {
@@ -138,7 +99,7 @@ export class KubeConfig {
13899

139100
this.applyOptions(opts);
140101

141-
if (user.username) {
102+
if (user && user.username) {
142103
opts.auth = `${user.username}:${user.password}`;
143104
}
144105
}
@@ -233,7 +194,7 @@ export class KubeConfig {
233194
this.loadFromFile(process.env.KUBECONFIG);
234195
return;
235196
}
236-
const home = KubeConfig.findHomeDir();
197+
const home = findHomeDir();
237198
if (home) {
238199
const config = path.join(home, '.kube', 'config');
239200
try {
@@ -260,8 +221,8 @@ export class KubeConfig {
260221
} catch (ignore) {}
261222

262223
this.loadFromClusterAndUser(
263-
{name: 'cluster', server: 'http://localhost:8080'} as Cluster,
264-
{name: 'user'} as User,
224+
{ name: 'cluster', server: 'http://localhost:8080' } as Cluster,
225+
{ name: 'user' } as User,
265226
);
266227
}
267228

@@ -280,31 +241,21 @@ export class KubeConfig {
280241
return this.getContextObject(this.currentContext);
281242
}
282243

283-
private bufferFromFileOrString(file?: string, data?: string): Buffer | null {
284-
if (file) {
285-
return fs.readFileSync(file);
286-
}
287-
if (data) {
288-
return Buffer.from(base64.decode(data), 'utf-8');
289-
}
290-
return null;
291-
}
292-
293244
private applyHTTPSOptions(opts: request.Options | https.RequestOptions) {
294245
const cluster = this.getCurrentCluster();
295246
const user = this.getCurrentUser();
296247
if (!user) {
297248
return;
298249
}
299-
const ca = cluster != null ? this.bufferFromFileOrString(cluster.caFile, cluster.caData) : null;
250+
const ca = cluster != null ? bufferFromFileOrString(cluster.caFile, cluster.caData) : null;
300251
if (ca) {
301252
opts.ca = ca;
302253
}
303-
const cert = this.bufferFromFileOrString(user.certFile, user.certData);
254+
const cert = bufferFromFileOrString(user.certFile, user.certData);
304255
if (cert) {
305256
opts.cert = cert;
306257
}
307-
const key = this.bufferFromFileOrString(user.keyFile, user.keyData);
258+
const key = bufferFromFileOrString(user.keyFile, user.keyData);
308259
if (key) {
309260
opts.key = key;
310261
}
@@ -349,17 +300,17 @@ export interface ApiType {
349300
}
350301

351302
export interface ApiConstructor<T extends ApiType> {
352-
new (server: string): T;
303+
new(server: string): T;
353304
}
354305

355306
// This class is deprecated and will eventually be removed.
356307
export class Config {
357308
public static SERVICEACCOUNT_ROOT =
358-
'/var/run/secrets/kubernetes.io/serviceaccount';
309+
'/var/run/secrets/kubernetes.io/serviceaccount';
359310
public static SERVICEACCOUNT_CA_PATH =
360-
Config.SERVICEACCOUNT_ROOT + '/ca.crt';
311+
Config.SERVICEACCOUNT_ROOT + '/ca.crt';
361312
public static SERVICEACCOUNT_TOKEN_PATH =
362-
Config.SERVICEACCOUNT_ROOT + '/token';
313+
Config.SERVICEACCOUNT_ROOT + '/token';
363314

364315
public static fromFile(filename: string): api.Core_v1Api {
365316
return Config.apiFromFile(filename, api.Core_v1Api);
@@ -400,3 +351,61 @@ export class Config {
400351
return kc.makeApiClient(apiClientType);
401352
}
402353
}
354+
355+
// This is public really only for testing.
356+
export function bufferFromFileOrString(file ?: string, data ?: string): Buffer | null {
357+
if (file) {
358+
return fs.readFileSync(file);
359+
}
360+
if (data) {
361+
return Buffer.from(base64.decode(data), 'utf-8');
362+
}
363+
return null;
364+
}
365+
366+
// Only public for testing.
367+
export function findHomeDir(): string | null {
368+
if (process.env.HOME) {
369+
try {
370+
fs.accessSync(process.env.HOME);
371+
return process.env.HOME;
372+
// tslint:disable-next-line:no-empty
373+
} catch (ignore) { }
374+
}
375+
if (process.platform !== 'win32') {
376+
return null;
377+
}
378+
if (process.env.HOMEDRIVE && process.env.HOMEPATH) {
379+
const dir = path.join(process.env.HOMEDRIVE, process.env.HOMEPATH);
380+
try {
381+
fs.accessSync(dir);
382+
return dir;
383+
// tslint:disable-next-line:no-empty
384+
} catch (ignore) { }
385+
}
386+
if (process.env.USERPROFILE) {
387+
try {
388+
fs.accessSync(process.env.USERPROFILE);
389+
return process.env.USERPROFILE;
390+
// tslint:disable-next-line:no-empty
391+
} catch (ignore) {}
392+
}
393+
return null;
394+
}
395+
396+
export interface Named {
397+
name: string;
398+
}
399+
400+
// Only really public for testing...
401+
export function findObject<T extends Named>(list: T[], name: string, key: string): T | null {
402+
for (const obj of list) {
403+
if (obj.name === name) {
404+
if (obj[key]) {
405+
return obj[key];
406+
}
407+
return obj;
408+
}
409+
}
410+
return null;
411+
}

0 commit comments

Comments
 (0)