Skip to content

Commit

Permalink
feat: allow user to add extra headers to subsequent krm requests (#146)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
alewitt2 authored Feb 2, 2021
1 parent bb25596 commit 86b0b79
Showing 1 changed file with 28 additions and 22 deletions.
50 changes: 28 additions & 22 deletions lib/KubeResourceMeta.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
Expand Down Expand Up @@ -69,74 +70,79 @@ 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 = {}) {
return this.patch(name, ns, jPatch, reqOpt);
}

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 }]));
}

};

0 comments on commit 86b0b79

Please sign in to comment.