Skip to content

Commit 182b886

Browse files
authored
Merge pull request #507 from brendandburns/types
Require typedef in the style. Add some missing return types.
2 parents 4d40e96 + dfd12b1 commit 182b886

File tree

11 files changed

+81
-60
lines changed

11 files changed

+81
-60
lines changed

src/cache.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,14 @@ export class ListWatch<T extends KubernetesObject> implements ObjectCache<T>, In
3333
await this.doneHandler(null);
3434
}
3535

36-
public on(verb: string, cb: ObjectCallback<T>) {
36+
public on(verb: string, cb: ObjectCallback<T>): void {
3737
if (this.callbackCache[verb] === undefined) {
3838
throw new Error(`Unknown verb: ${verb}`);
3939
}
4040
this.callbackCache[verb].push(cb);
4141
}
4242

43-
public off(verb: string, cb: ObjectCallback<T>) {
43+
public off(verb: string, cb: ObjectCallback<T>): void {
4444
if (this.callbackCache[verb] === undefined) {
4545
throw new Error(`Unknown verb: ${verb}`);
4646
}
@@ -72,7 +72,7 @@ export class ListWatch<T extends KubernetesObject> implements ObjectCache<T>, In
7272
return this.resourceVersion;
7373
}
7474

75-
private async doneHandler(err: any) {
75+
private async doneHandler(err: any): Promise<any> {
7676
if (err) {
7777
this.callbackCache[ERROR].forEach((elt: ObjectCallback<T>) => elt(err));
7878
return;
@@ -93,7 +93,7 @@ export class ListWatch<T extends KubernetesObject> implements ObjectCache<T>, In
9393
);
9494
}
9595

96-
private addOrUpdateItems(items: T[]) {
96+
private addOrUpdateItems(items: T[]): void {
9797
items.forEach((obj: T) => {
9898
addOrUpdateObject(
9999
this.objects,
@@ -107,7 +107,7 @@ export class ListWatch<T extends KubernetesObject> implements ObjectCache<T>, In
107107
});
108108
}
109109

110-
private indexObj(obj: T) {
110+
private indexObj(obj: T): void {
111111
let namespaceList = this.indexCache[obj.metadata!.namespace!] as T[];
112112
if (!namespaceList) {
113113
namespaceList = [];
@@ -116,7 +116,7 @@ export class ListWatch<T extends KubernetesObject> implements ObjectCache<T>, In
116116
addOrUpdateObject(namespaceList, obj);
117117
}
118118

119-
private watchHandler(phase: string, obj: T, watchObj?: any) {
119+
private watchHandler(phase: string, obj: T, watchObj?: any): void {
120120
switch (phase) {
121121
case 'ADDED':
122122
case 'MODIFIED':
@@ -172,7 +172,7 @@ export function addOrUpdateObject<T extends KubernetesObject>(
172172
obj: T,
173173
addCallback?: Array<ObjectCallback<T>>,
174174
updateCallback?: Array<ObjectCallback<T>>,
175-
) {
175+
): void {
176176
const ix = findKubernetesObject(objects, obj);
177177
if (ix === -1) {
178178
objects.push(obj);
@@ -202,7 +202,7 @@ export function deleteObject<T extends KubernetesObject>(
202202
objects: T[],
203203
obj: T,
204204
deleteCallback?: Array<ObjectCallback<T>>,
205-
) {
205+
): void {
206206
const ix = findKubernetesObject(objects, obj);
207207
if (ix !== -1) {
208208
objects.splice(ix, 1);

src/cloud_auth.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,10 @@ export class CloudAuth implements Authenticator {
2626
return user.authProvider.name === 'azure' || user.authProvider.name === 'gcp';
2727
}
2828

29-
public async applyAuthentication(user: User, opts: request.Options | https.RequestOptions) {
29+
public async applyAuthentication(
30+
user: User,
31+
opts: request.Options | https.RequestOptions,
32+
): Promise<void> {
3033
const token = this.getToken(user);
3134
if (token) {
3235
opts.headers!.Authorization = `Bearer ${token}`;
@@ -41,7 +44,7 @@ export class CloudAuth implements Authenticator {
4144
return config['access-token'];
4245
}
4346

44-
private isExpired(config: Config) {
47+
private isExpired(config: Config): boolean {
4548
const token = config['access-token'];
4649
const expiry = config.expiry;
4750
if (!token) {
@@ -58,7 +61,7 @@ export class CloudAuth implements Authenticator {
5861
return false;
5962
}
6063

61-
private updateAccessToken(config: Config) {
64+
private updateAccessToken(config: Config): void {
6265
let cmd = config['cmd-path'];
6366
if (!cmd) {
6467
throw new Error('Token is expired!');

src/config.ts

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -71,27 +71,27 @@ export class KubeConfig {
7171
this.users = [];
7272
}
7373

74-
public getContexts() {
74+
public getContexts(): Context[] {
7575
return this.contexts;
7676
}
7777

78-
public getClusters() {
78+
public getClusters(): Cluster[] {
7979
return this.clusters;
8080
}
8181

82-
public getUsers() {
82+
public getUsers(): User[] {
8383
return this.users;
8484
}
8585

86-
public getCurrentContext() {
86+
public getCurrentContext(): string {
8787
return this.currentContext;
8888
}
8989

90-
public setCurrentContext(context: string) {
90+
public setCurrentContext(context: string): void {
9191
this.currentContext = context;
9292
}
9393

94-
public getContextObject(name: string) {
94+
public getContextObject(name: string): Context | null {
9595
if (!this.contexts) {
9696
return null;
9797
}
@@ -122,13 +122,13 @@ export class KubeConfig {
122122
return findObject(this.users, name, 'user');
123123
}
124124

125-
public loadFromFile(file: string, opts?: Partial<ConfigOptions>) {
125+
public loadFromFile(file: string, opts?: Partial<ConfigOptions>): void {
126126
const rootDirectory = path.dirname(file);
127127
this.loadFromString(fs.readFileSync(file, 'utf8'), opts);
128128
this.makePathsAbsolute(rootDirectory);
129129
}
130130

131-
public async applytoHTTPSOptions(opts: https.RequestOptions) {
131+
public async applytoHTTPSOptions(opts: https.RequestOptions): Promise<void> {
132132
const user = this.getCurrentUser();
133133

134134
await this.applyOptions(opts);
@@ -138,7 +138,7 @@ export class KubeConfig {
138138
}
139139
}
140140

141-
public async applyToRequest(opts: request.Options) {
141+
public async applyToRequest(opts: request.Options): Promise<void> {
142142
const cluster = this.getCurrentCluster();
143143
const user = this.getCurrentUser();
144144

@@ -156,22 +156,22 @@ export class KubeConfig {
156156
}
157157
}
158158

159-
public loadFromString(config: string, opts?: Partial<ConfigOptions>) {
159+
public loadFromString(config: string, opts?: Partial<ConfigOptions>): void {
160160
const obj = yaml.safeLoad(config);
161161
this.clusters = newClusters(obj.clusters, opts);
162162
this.contexts = newContexts(obj.contexts, opts);
163163
this.users = newUsers(obj.users, opts);
164164
this.currentContext = obj['current-context'];
165165
}
166166

167-
public loadFromOptions(options: any) {
167+
public loadFromOptions(options: any): void {
168168
this.clusters = options.clusters;
169169
this.contexts = options.contexts;
170170
this.users = options.users;
171171
this.currentContext = options.currentContext;
172172
}
173173

174-
public loadFromClusterAndUser(cluster: Cluster, user: User) {
174+
public loadFromClusterAndUser(cluster: Cluster, user: User): void {
175175
this.clusters = [cluster];
176176
this.users = [user];
177177
this.currentContext = 'loaded-context';
@@ -184,7 +184,7 @@ export class KubeConfig {
184184
];
185185
}
186186

187-
public loadFromCluster(pathPrefix: string = '') {
187+
public loadFromCluster(pathPrefix: string = ''): void {
188188
const host = process.env.KUBERNETES_SERVICE_HOST;
189189
const port = process.env.KUBERNETES_SERVICE_PORT;
190190
const clusterName = 'inCluster';
@@ -231,7 +231,7 @@ export class KubeConfig {
231231
this.currentContext = contextName;
232232
}
233233

234-
public mergeConfig(config: KubeConfig) {
234+
public mergeConfig(config: KubeConfig): void {
235235
this.currentContext = config.currentContext;
236236
config.clusters.forEach((cluster: Cluster) => {
237237
this.addCluster(cluster);
@@ -244,7 +244,7 @@ export class KubeConfig {
244244
});
245245
}
246246

247-
public addCluster(cluster: Cluster) {
247+
public addCluster(cluster: Cluster): void {
248248
if (!this.clusters) {
249249
this.clusters = [];
250250
}
@@ -256,7 +256,7 @@ export class KubeConfig {
256256
this.clusters.push(cluster);
257257
}
258258

259-
public addUser(user: User) {
259+
public addUser(user: User): void {
260260
if (!this.users) {
261261
this.users = [];
262262
}
@@ -268,7 +268,7 @@ export class KubeConfig {
268268
this.users.push(user);
269269
}
270270

271-
public addContext(ctx: Context) {
271+
public addContext(ctx: Context): void {
272272
if (!this.contexts) {
273273
this.contexts = [];
274274
}
@@ -280,7 +280,7 @@ export class KubeConfig {
280280
this.contexts.push(ctx);
281281
}
282282

283-
public loadFromDefault(opts?: Partial<ConfigOptions>) {
283+
public loadFromDefault(opts?: Partial<ConfigOptions>): void {
284284
if (process.env.KUBECONFIG && process.env.KUBECONFIG.length > 0) {
285285
const files = process.env.KUBECONFIG.split(path.delimiter);
286286
this.loadFromFile(files[0], opts);
@@ -323,7 +323,7 @@ export class KubeConfig {
323323
);
324324
}
325325

326-
public makeApiClient<T extends ApiType>(apiClientType: ApiConstructor<T>) {
326+
public makeApiClient<T extends ApiType>(apiClientType: ApiConstructor<T>): T {
327327
const cluster = this.getCurrentCluster();
328328
if (!cluster) {
329329
throw new Error('No active cluster!');
@@ -334,7 +334,7 @@ export class KubeConfig {
334334
return apiClient;
335335
}
336336

337-
public makePathsAbsolute(rootDirectory: string) {
337+
public makePathsAbsolute(rootDirectory: string): void {
338338
this.clusters.forEach((cluster: Cluster) => {
339339
if (cluster.caFile) {
340340
cluster.caFile = makeAbsolutePath(rootDirectory, cluster.caFile);
@@ -364,11 +364,11 @@ export class KubeConfig {
364364
return JSON.stringify(configObj);
365365
}
366366

367-
private getCurrentContextObject() {
367+
private getCurrentContextObject(): Context | null {
368368
return this.getContextObject(this.currentContext);
369369
}
370370

371-
private applyHTTPSOptions(opts: request.Options | https.RequestOptions) {
371+
private applyHTTPSOptions(opts: request.Options | https.RequestOptions): void {
372372
const cluster = this.getCurrentCluster();
373373
const user = this.getCurrentUser();
374374
if (!user) {
@@ -392,7 +392,7 @@ export class KubeConfig {
392392
}
393393
}
394394

395-
private async applyAuthorizationHeader(opts: request.Options | https.RequestOptions) {
395+
private async applyAuthorizationHeader(opts: request.Options | https.RequestOptions): Promise<void> {
396396
const user = this.getCurrentUser();
397397
if (!user) {
398398
return;
@@ -413,7 +413,7 @@ export class KubeConfig {
413413
}
414414
}
415415

416-
private async applyOptions(opts: request.Options | https.RequestOptions) {
416+
private async applyOptions(opts: request.Options | https.RequestOptions): Promise<void> {
417417
this.applyHTTPSOptions(opts);
418418
await this.applyAuthorizationHeader(opts);
419419
}
@@ -428,9 +428,9 @@ type ApiConstructor<T extends ApiType> = new (server: string) => T;
428428

429429
// This class is deprecated and will eventually be removed.
430430
export class Config {
431-
public static SERVICEACCOUNT_ROOT = '/var/run/secrets/kubernetes.io/serviceaccount';
432-
public static SERVICEACCOUNT_CA_PATH = Config.SERVICEACCOUNT_ROOT + '/ca.crt';
433-
public static SERVICEACCOUNT_TOKEN_PATH = Config.SERVICEACCOUNT_ROOT + '/token';
431+
public static SERVICEACCOUNT_ROOT: string = '/var/run/secrets/kubernetes.io/serviceaccount';
432+
public static SERVICEACCOUNT_CA_PATH: string = Config.SERVICEACCOUNT_ROOT + '/ca.crt';
433+
public static SERVICEACCOUNT_TOKEN_PATH: string = Config.SERVICEACCOUNT_ROOT + '/token';
434434

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

src/exec_auth.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,10 @@ export class ExecAuth implements Authenticator {
3636
);
3737
}
3838

39-
public async applyAuthentication(user: User, opts: request.Options | https.RequestOptions) {
39+
public async applyAuthentication(
40+
user: User,
41+
opts: request.Options | https.RequestOptions,
42+
): Promise<void> {
4043
const credential = this.getCredential(user);
4144
if (!credential) {
4245
return;

src/file_auth.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@ export class FileAuth implements Authenticator {
1313
return user.authProvider && user.authProvider.config && user.authProvider.config.tokenFile;
1414
}
1515

16-
public async applyAuthentication(user: User, opts: request.Options | https.RequestOptions) {
16+
public async applyAuthentication(
17+
user: User,
18+
opts: request.Options | https.RequestOptions,
19+
): Promise<void> {
1720
if (this.token == null) {
1821
this.refreshToken(user.authProvider.config.tokenFile);
1922
}
@@ -25,7 +28,7 @@ export class FileAuth implements Authenticator {
2528
}
2629
}
2730

28-
private refreshToken(filePath: string) {
31+
private refreshToken(filePath: string): void {
2932
// TODO make this async?
3033
this.token = fs.readFileSync(filePath).toString('UTF-8');
3134
this.lastRead = new Date();

src/informer.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ export const DELETE: string = 'delete';
1818
export const ERROR: string = 'error';
1919

2020
export interface Informer<T> {
21-
on(verb: string, fn: ObjectCallback<T>);
22-
off(verb: string, fn: ObjectCallback<T>);
21+
on(verb: string, fn: ObjectCallback<T>): void;
22+
off(verb: string, fn: ObjectCallback<T>): void;
2323
start(): Promise<void>;
2424
}
2525

src/oidc_auth.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ export class OpenIDConnectAuth implements Authenticator {
4040
}
4141

4242
// public for testing purposes.
43-
private currentTokenExpiration = 0;
43+
private currentTokenExpiration: number = 0;
4444
public isAuthProvider(user: User): boolean {
4545
if (!user.authProvider) {
4646
return false;
@@ -58,7 +58,7 @@ export class OpenIDConnectAuth implements Authenticator {
5858
user: User,
5959
opts: request.Options | https.RequestOptions,
6060
overrideClient?: any,
61-
) {
61+
): Promise<void> {
6262
const token = await this.getToken(user, overrideClient);
6363
if (token) {
6464
opts.headers!.Authorization = `Bearer ${token}`;
@@ -102,7 +102,7 @@ export class OpenIDConnectAuth implements Authenticator {
102102
return user.authProvider.config['id-token'];
103103
}
104104

105-
private async getClient(user: User) {
105+
private async getClient(user: User): Promise<Client> {
106106
const oidcIssuer = await Issuer.discover(user.authProvider.config['idp-issuer-url']);
107107
return new oidcIssuer.Client({
108108
client_id: user.authProvider.config['client-id'],

0 commit comments

Comments
 (0)