From 9efaac289be06f2540ed0949996c2e207fdb98ef Mon Sep 17 00:00:00 2001 From: Panu Horsmalahti Date: Thu, 21 Oct 2021 10:53:26 +0300 Subject: [PATCH] Move getRequestOptions to JsonApi. Signed-off-by: Panu Horsmalahti --- src/common/k8s-api/json-api.ts | 8 ++++++-- src/common/k8s-api/kube-api.ts | 32 ++++++++++++++++---------------- 2 files changed, 22 insertions(+), 18 deletions(-) diff --git a/src/common/k8s-api/json-api.ts b/src/common/k8s-api/json-api.ts index dde8f5f5a2..5ed9d9406f 100644 --- a/src/common/k8s-api/json-api.ts +++ b/src/common/k8s-api/json-api.ts @@ -52,6 +52,7 @@ export interface JsonApiConfig { apiBase: string; serverAddress: string; debug?: boolean; + getRequestOptions?: () => RequestInit; } export class JsonApi { static reqInitDefault: RequestInit = { @@ -68,10 +69,13 @@ export class JsonApi { this.config = Object.assign({}, JsonApi.configDefault, config); this.reqInit = merge({}, JsonApi.reqInitDefault, reqInit); this.parseResponse = this.parseResponse.bind(this); + this.getRequestOptions = config.getRequestOptions; } public onData = new EventEmitter<[D, Response]>(); public onError = new EventEmitter<[JsonApiErrorParsed, Response]>(); + + private getRequestOptions?: () => RequestInit; get(path: string, params?: P, reqInit: RequestInit = {}) { return this.request(path, params, { ...reqInit, method: "get" }); @@ -79,7 +83,7 @@ export class JsonApi { getResponse(path: string, params?: P, init: RequestInit = {}): Promise { let reqUrl = `${this.config.serverAddress}${this.config.apiBase}${path}`; - const reqInit: RequestInit = merge({}, this.reqInit, init); + const reqInit: RequestInit = merge({}, this.reqInit, this.getRequestOptions?.(), init); const { query } = params || {} as P; if (!reqInit.method) { @@ -113,7 +117,7 @@ export class JsonApi { protected async request(path: string, params?: P, init: RequestInit = {}) { let reqUrl = `${this.config.serverAddress}${this.config.apiBase}${path}`; - const reqInit: RequestInit = merge({}, this.reqInit, init); + const reqInit: RequestInit = merge({}, this.reqInit, this.getRequestOptions?.(), init); const { data, query } = params || {} as P; if (data && !reqInit.body) { diff --git a/src/common/k8s-api/kube-api.ts b/src/common/k8s-api/kube-api.ts index 5426f63343..723cfa9bba 100644 --- a/src/common/k8s-api/kube-api.ts +++ b/src/common/k8s-api/kube-api.ts @@ -109,6 +109,7 @@ export interface IRemoteKubeApiConfig { } user: { token?: string; + getToken?: () => string; clientCertificateData?: string; clientKeyData?: string; } @@ -135,9 +136,13 @@ export function forCluster(cluster: ILocalKubeApiConfig, k export function forRemoteCluster(config: IRemoteKubeApiConfig, kubeClass: KubeObjectConstructor): KubeApi { const reqInit: RequestInit = {}; - if (config.user.token) { + if (config.user.token && config.user.getToken) { + throw new Error("Provide either user.token or user.getToken"); + } + + if (config.user.token || config.user.getToken) { reqInit.headers = { - "Authorization": `Bearer ${config.user.token}` + "Authorization": `Bearer ${config.user.token ?? config.user.getToken()}` }; } @@ -167,6 +172,13 @@ export function forRemoteCluster(config: IRemoteKubeApiCon serverAddress: config.cluster.server, apiBase: "", debug: isDevelopment, + ...(config.user.getToken ? { + getRequestOptions: () => ({ + headers: { + "Authorization": `Bearer ${config.user.getToken()}` + } + }) + } : {}) }, reqInit); return new KubeApi({ @@ -195,9 +207,6 @@ export type KubeApiWatchOptions = { abortController?: AbortController watchId?: string; retry?: boolean; - - // Request options that are applied for the initial watch request and also subsequent retry requests - getRequestOptions?: () => RequestInit; }; export class KubeApi { @@ -489,12 +498,7 @@ export class KubeApi { watch(opts: KubeApiWatchOptions = { namespace: "", retry: false }): () => void { let errorReceived = false; let timedRetry: NodeJS.Timeout; - const { - abortController: { abort, signal } = new AbortController(), - namespace, - callback = noop, - retry, - getRequestOptions } = opts; + const { abortController: { abort, signal } = new AbortController(), namespace, callback = noop, retry } = opts; const { watchId = `${this.kind.toLowerCase()}-${this.watchId++}` } = opts; signal.addEventListener("abort", () => { @@ -503,11 +507,7 @@ export class KubeApi { }); const watchUrl = this.getWatchUrl(namespace); - const responsePromise = this.request.getResponse( - watchUrl, - null, - merge({ signal, timeout: 600_000 }, getRequestOptions?.()) - ); + const responsePromise = this.request.getResponse(watchUrl, null,{ signal, timeout: 600_000 }); logger.info(`[KUBE-API] watch (${watchId}) ${retry === true ? "retried" : "started"} ${watchUrl}`);