From 86b0b79c39200934827a8447e0128514f3f1ac2d Mon Sep 17 00:00:00 2001 From: Alex Lewitt <48691328+alewitt2@users.noreply.github.com> Date: Tue, 2 Feb 2021 15:36:31 -0500 Subject: [PATCH] feat: allow user to add extra headers to subsequent krm requests (#146) * feat: allow user to add extra headers to all future krm requests avoid having to pass in extra reqOpts that will be used for all future requests * all more granular control using reqOpt * Update lib/KubeResourceMeta.js --- lib/KubeResourceMeta.js | 50 +++++++++++++++++++++++------------------ 1 file changed, 28 insertions(+), 22 deletions(-) diff --git a/lib/KubeResourceMeta.js b/lib/KubeResourceMeta.js index b6f5ca8..37158c9 100644 --- a/lib/KubeResourceMeta.js +++ b/lib/KubeResourceMeta.js @@ -22,6 +22,7 @@ module.exports = class KubeResourceMeta { this._path = path; this._resourceMeta = rm; this._kubeApiConfig = kubeApiConfig; + this._extraHeaders = {}; this._logger = require('./bunyan-api').createLogger('KubeResourceMeta'); } @@ -69,45 +70,53 @@ module.exports = class KubeResourceMeta { get kubeApiConfig() { return this._kubeApiConfig; } + get extraHeaders() { + return this._extraHeaders; + } hasVerb(verb) { return this.verbs.some(v => verb == v); } + addHeader(key, value) { + objectPath.set(this._extraHeaders, ['headers', key], value); + } + removeHeader(key) { + objectPath.del(this._extraHeaders, ['headers', key]); + if (Object.keys(objectPath.get(this._extraHeaders, 'headers', {})).length === 0) { + objectPath.del(this._extraHeaders, 'headers'); + } + } async request(reqOpt) { this._logger.debug(`Request ${reqOpt.method || 'GET'} ${reqOpt.uri || reqOpt.url}`); - return request(merge(this._kubeApiConfig, reqOpt)); + return request(merge.all([this._kubeApiConfig, this._extraHeaders, reqOpt])); } async get(name, ns, reqOpt = {}) { - let uri = this.uri({ name: name, namespace: ns }); + const uri = this.uri({ name: name, namespace: ns }); this._logger.debug(`Get ${uri}`); - reqOpt = merge(reqOpt, { uri: uri, json: true }); - return request.get(merge(this._kubeApiConfig, reqOpt)); + return request.get(merge.all([this._kubeApiConfig, this._extraHeaders, reqOpt, { uri: uri, json: true }])); } async put(file, reqOpt = {}) { - let uri = this.uri({ name: objectPath.get(file, 'metadata.name'), namespace: objectPath.get(file, 'metadata.namespace') }); + const uri = this.uri({ name: objectPath.get(file, 'metadata.name'), namespace: objectPath.get(file, 'metadata.namespace') }); this._logger.debug(`Put ${uri}`); - reqOpt = merge(reqOpt, { uri: uri, json: file }); - return request.put(merge(this._kubeApiConfig, reqOpt)); + return request.put(merge.all([this._kubeApiConfig, this._extraHeaders, reqOpt, { uri: uri, json: file }])); } async post(file, reqOpt = {}) { - let uri = this.uri({ namespace: objectPath.get(file, 'metadata.namespace') }); + const uri = this.uri({ namespace: objectPath.get(file, 'metadata.namespace') }); this._logger.debug(`Post ${uri}`); - reqOpt = merge(reqOpt, { uri: uri, json: file }); - return request.post(merge(this._kubeApiConfig, reqOpt)); + return request.post(merge.all([this._kubeApiConfig, this._extraHeaders, reqOpt, { uri: uri, json: file }])); } async patch(name, ns, jPatch, reqOpt = {}) { - let uri = this.uri({ name: name, namespace: ns, status: reqOpt.status }); + const uri = this.uri({ name: name, namespace: ns, status: reqOpt.status }); this._logger.debug(`Json Patch ${uri}`); reqOpt = merge(reqOpt, { uri: uri, json: jPatch }); objectPath.set(reqOpt, ['headers', 'content-type'], objectPath.get(reqOpt, ['headers', 'content-type']) || 'application/json-patch+json'); - let opt = merge(this._kubeApiConfig, reqOpt); - return request.patch(opt); + return request.patch(merge.all([this._kubeApiConfig, this._extraHeaders, reqOpt])); } async jsonPatch(name, ns, jPatch, reqOpt = {}) { @@ -115,28 +124,25 @@ module.exports = class KubeResourceMeta { } async mergePatch(name, ns, mPatch, reqOpt = {}) { - let uri = this.uri({ name: name, namespace: ns, status: reqOpt.status }); + const uri = this.uri({ name: name, namespace: ns, status: reqOpt.status }); this._logger.debug(`MergePatch ${uri}`); reqOpt = merge(reqOpt, { uri: uri, json: mPatch }); objectPath.set(reqOpt, ['headers', 'content-type'], objectPath.get(reqOpt, ['headers', 'content-type']) || 'application/merge-patch+json'); - let opt = merge(this._kubeApiConfig, reqOpt); - return request.patch(opt); + return request.patch(merge.all([this._kubeApiConfig, this._extraHeaders, reqOpt])); } async strategicMergePatch(name, ns, smPatch, reqOpt = {}) { - let uri = this.uri({ name: name, namespace: ns, status: reqOpt.status }); + const uri = this.uri({ name: name, namespace: ns, status: reqOpt.status }); this._logger.debug(`StrategicMergePatch ${uri}`); reqOpt = merge(reqOpt, { uri: uri, json: smPatch }); objectPath.set(reqOpt, ['headers', 'content-type'], objectPath.get(reqOpt, ['headers', 'content-type']) || 'application/strategic-merge-patch+json'); - let opt = merge(this._kubeApiConfig, reqOpt); - return request.patch(opt); + return request.patch(merge.all([this._kubeApiConfig, this._extraHeaders, reqOpt])); } async delete(name, ns, reqOpt = {}) { - let uri = this.uri({ name: name, namespace: ns }); + const uri = this.uri({ name: name, namespace: ns }); this._logger.debug(`Delete ${uri}`); - reqOpt = merge(reqOpt, { uri: uri, json: true }); - return request.delete(merge(this._kubeApiConfig, reqOpt)); + return request.delete(merge.all([this._kubeApiConfig, this._extraHeaders, reqOpt, { uri: uri, json: true }])); } };