Skip to content
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
16 changes: 8 additions & 8 deletions src/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,14 @@ export class ListWatch<T extends KubernetesObject> implements ObjectCache<T>, In
await this.doneHandler(null);
}

public on(verb: string, cb: ObjectCallback<T>) {
public on(verb: string, cb: ObjectCallback<T>): void {
if (this.callbackCache[verb] === undefined) {
throw new Error(`Unknown verb: ${verb}`);
}
this.callbackCache[verb].push(cb);
}

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

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

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

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

private watchHandler(phase: string, obj: T, watchObj?: any) {
private watchHandler(phase: string, obj: T, watchObj?: any): void {
switch (phase) {
case 'ADDED':
case 'MODIFIED':
Expand Down Expand Up @@ -172,7 +172,7 @@ export function addOrUpdateObject<T extends KubernetesObject>(
obj: T,
addCallback?: Array<ObjectCallback<T>>,
updateCallback?: Array<ObjectCallback<T>>,
) {
): void {
const ix = findKubernetesObject(objects, obj);
if (ix === -1) {
objects.push(obj);
Expand Down Expand Up @@ -202,7 +202,7 @@ export function deleteObject<T extends KubernetesObject>(
objects: T[],
obj: T,
deleteCallback?: Array<ObjectCallback<T>>,
) {
): void {
const ix = findKubernetesObject(objects, obj);
if (ix !== -1) {
objects.splice(ix, 1);
Expand Down
9 changes: 6 additions & 3 deletions src/cloud_auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ export class CloudAuth implements Authenticator {
return user.authProvider.name === 'azure' || user.authProvider.name === 'gcp';
}

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

private isExpired(config: Config) {
private isExpired(config: Config): boolean {
const token = config['access-token'];
const expiry = config.expiry;
if (!token) {
Expand All @@ -58,7 +61,7 @@ export class CloudAuth implements Authenticator {
return false;
}

private updateAccessToken(config: Config) {
private updateAccessToken(config: Config): void {
let cmd = config['cmd-path'];
if (!cmd) {
throw new Error('Token is expired!');
Expand Down
54 changes: 27 additions & 27 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,27 +71,27 @@ export class KubeConfig {
this.users = [];
}

public getContexts() {
public getContexts(): Context[] {
return this.contexts;
}

public getClusters() {
public getClusters(): Cluster[] {
return this.clusters;
}

public getUsers() {
public getUsers(): User[] {
return this.users;
}

public getCurrentContext() {
public getCurrentContext(): string {
return this.currentContext;
}

public setCurrentContext(context: string) {
public setCurrentContext(context: string): void {
this.currentContext = context;
}

public getContextObject(name: string) {
public getContextObject(name: string): Context | null {
if (!this.contexts) {
return null;
}
Expand Down Expand Up @@ -122,13 +122,13 @@ export class KubeConfig {
return findObject(this.users, name, 'user');
}

public loadFromFile(file: string, opts?: Partial<ConfigOptions>) {
public loadFromFile(file: string, opts?: Partial<ConfigOptions>): void {
const rootDirectory = path.dirname(file);
this.loadFromString(fs.readFileSync(file, 'utf8'), opts);
this.makePathsAbsolute(rootDirectory);
}

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

await this.applyOptions(opts);
Expand All @@ -138,7 +138,7 @@ export class KubeConfig {
}
}

public async applyToRequest(opts: request.Options) {
public async applyToRequest(opts: request.Options): Promise<void> {
const cluster = this.getCurrentCluster();
const user = this.getCurrentUser();

Expand All @@ -156,22 +156,22 @@ export class KubeConfig {
}
}

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

public loadFromOptions(options: any) {
public loadFromOptions(options: any): void {
this.clusters = options.clusters;
this.contexts = options.contexts;
this.users = options.users;
this.currentContext = options.currentContext;
}

public loadFromClusterAndUser(cluster: Cluster, user: User) {
public loadFromClusterAndUser(cluster: Cluster, user: User): void {
this.clusters = [cluster];
this.users = [user];
this.currentContext = 'loaded-context';
Expand All @@ -184,7 +184,7 @@ export class KubeConfig {
];
}

public loadFromCluster(pathPrefix: string = '') {
public loadFromCluster(pathPrefix: string = ''): void {
const host = process.env.KUBERNETES_SERVICE_HOST;
const port = process.env.KUBERNETES_SERVICE_PORT;
const clusterName = 'inCluster';
Expand Down Expand Up @@ -231,7 +231,7 @@ export class KubeConfig {
this.currentContext = contextName;
}

public mergeConfig(config: KubeConfig) {
public mergeConfig(config: KubeConfig): void {
this.currentContext = config.currentContext;
config.clusters.forEach((cluster: Cluster) => {
this.addCluster(cluster);
Expand All @@ -244,7 +244,7 @@ export class KubeConfig {
});
}

public addCluster(cluster: Cluster) {
public addCluster(cluster: Cluster): void {
if (!this.clusters) {
this.clusters = [];
}
Expand All @@ -256,7 +256,7 @@ export class KubeConfig {
this.clusters.push(cluster);
}

public addUser(user: User) {
public addUser(user: User): void {
if (!this.users) {
this.users = [];
}
Expand All @@ -268,7 +268,7 @@ export class KubeConfig {
this.users.push(user);
}

public addContext(ctx: Context) {
public addContext(ctx: Context): void {
if (!this.contexts) {
this.contexts = [];
}
Expand All @@ -280,7 +280,7 @@ export class KubeConfig {
this.contexts.push(ctx);
}

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

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

public makePathsAbsolute(rootDirectory: string) {
public makePathsAbsolute(rootDirectory: string): void {
this.clusters.forEach((cluster: Cluster) => {
if (cluster.caFile) {
cluster.caFile = makeAbsolutePath(rootDirectory, cluster.caFile);
Expand Down Expand Up @@ -364,11 +364,11 @@ export class KubeConfig {
return JSON.stringify(configObj);
}

private getCurrentContextObject() {
private getCurrentContextObject(): Context | null {
return this.getContextObject(this.currentContext);
}

private applyHTTPSOptions(opts: request.Options | https.RequestOptions) {
private applyHTTPSOptions(opts: request.Options | https.RequestOptions): void {
const cluster = this.getCurrentCluster();
const user = this.getCurrentUser();
if (!user) {
Expand All @@ -392,7 +392,7 @@ export class KubeConfig {
}
}

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

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

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

public static fromFile(filename: string): api.CoreV1Api {
return Config.apiFromFile(filename, api.CoreV1Api);
Expand Down
5 changes: 4 additions & 1 deletion src/exec_auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,10 @@ export class ExecAuth implements Authenticator {
);
}

public async applyAuthentication(user: User, opts: request.Options | https.RequestOptions) {
public async applyAuthentication(
user: User,
opts: request.Options | https.RequestOptions,
): Promise<void> {
const credential = this.getCredential(user);
if (!credential) {
return;
Expand Down
7 changes: 5 additions & 2 deletions src/file_auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ export class FileAuth implements Authenticator {
return user.authProvider && user.authProvider.config && user.authProvider.config.tokenFile;
}

public async applyAuthentication(user: User, opts: request.Options | https.RequestOptions) {
public async applyAuthentication(
user: User,
opts: request.Options | https.RequestOptions,
): Promise<void> {
if (this.token == null) {
this.refreshToken(user.authProvider.config.tokenFile);
}
Expand All @@ -25,7 +28,7 @@ export class FileAuth implements Authenticator {
}
}

private refreshToken(filePath: string) {
private refreshToken(filePath: string): void {
// TODO make this async?
this.token = fs.readFileSync(filePath).toString('UTF-8');
this.lastRead = new Date();
Expand Down
4 changes: 2 additions & 2 deletions src/informer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ export const DELETE: string = 'delete';
export const ERROR: string = 'error';

export interface Informer<T> {
on(verb: string, fn: ObjectCallback<T>);
off(verb: string, fn: ObjectCallback<T>);
on(verb: string, fn: ObjectCallback<T>): void;
off(verb: string, fn: ObjectCallback<T>): void;
start(): Promise<void>;
}

Expand Down
6 changes: 3 additions & 3 deletions src/oidc_auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export class OpenIDConnectAuth implements Authenticator {
}

// public for testing purposes.
private currentTokenExpiration = 0;
private currentTokenExpiration: number = 0;
public isAuthProvider(user: User): boolean {
if (!user.authProvider) {
return false;
Expand All @@ -58,7 +58,7 @@ export class OpenIDConnectAuth implements Authenticator {
user: User,
opts: request.Options | https.RequestOptions,
overrideClient?: any,
) {
): Promise<void> {
const token = await this.getToken(user, overrideClient);
if (token) {
opts.headers!.Authorization = `Bearer ${token}`;
Expand Down Expand Up @@ -102,7 +102,7 @@ export class OpenIDConnectAuth implements Authenticator {
return user.authProvider.config['id-token'];
}

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