Skip to content

Commit

Permalink
Update: add selfLink annotation before sending events to razeedash (#167
Browse files Browse the repository at this point in the history
)

* Chore: remove unused code

* Update: use annotations.selfLink for sentSelfLinks

* Update: add selfLink annotation during eventHandler

* Update: use correct krm

* Upgrade: Use new selfLink update

* Chore: remove tests for removed code

* Chore: update tests for new annotations selfLink

* Chore: fix lint error
  • Loading branch information
alewitt2 authored Aug 26, 2020
1 parent bdd0143 commit 48a6f2b
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 41 deletions.
17 changes: 0 additions & 17 deletions src/controllers/Util.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
* limitations under the License.
*/
const objectPath = require('object-path');
const hash = require('object-hash');
const fs = require('fs-extra');

const { KubeClass, KubeApiConfig } = require('@razee/kubernetes-util');
Expand Down Expand Up @@ -113,21 +112,6 @@ module.exports = class Util {
return util[razeedashUrl];
}

// addHash - will add object hash to attribute 'razeehash' if
static addHash(o) {
if (Array.isArray(o)) {
o.forEach(Util.addHash);
} else if (Array.isArray(o.items)) {
o.items.forEach(Util.addHash);
} else {
let hashCode = hash(o);
if (objectPath.has(o, 'type')) {
objectPath.set(o, 'razeehash', hashCode);
}
}
return o;
}

// prepObject2Send - strip/redact attributes and apply razeehash code if needed
static prepObject2Send(o, level = 'lite') {
if (Array.isArray(o)) {
Expand Down Expand Up @@ -167,7 +151,6 @@ module.exports = class Util {
} else if (objectPath.get(o, ['spec', 'jobTemplate', 'spec', 'template', 'spec', 'containers'])) { // cronJob
Util.clearContainerEnvs(objectPath.get(o, ['spec', 'jobTemplate', 'spec', 'template', 'spec', 'containers'], []));
}
Util.addHash(o);
}
return o;
}
Expand Down
5 changes: 4 additions & 1 deletion src/controllers/Watch.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ function createWatch(watchableKrm, querySelector = {}, detailLevel, globalWatch
};
options.requestOptions.qs = querySelector;
WatchManager.ensureWatch(options, (eventObj) => {
let metadata = { name: objectPath.get(eventObj, 'object.metadata.name'), namespace: objectPath.get(eventObj, 'object.metadata.namespace') };
objectPath.set(eventObj, 'object.metadata.annotations.selfLink', watchableKrm.uri(metadata));

let preppedEventObj = Util.prepObject2Send(eventObj, detailLevel);
util.dsa.send(preppedEventObj);
}, globalWatch);
Expand Down Expand Up @@ -77,7 +80,7 @@ async function watch() {
}
}
} catch (e) {
util.error(`Could not validate watches. ${e}`);
util.error('Could not validate watches.', e);
success = false;
}
return success;
Expand Down
2 changes: 1 addition & 1 deletion src/razeedash/Sender.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ module.exports = class RazeedashSender {
resourceArrays.forEach((a) => {
a.forEach((e) => {
if (e) {
let selfLink = objectPath.get(e, 'object.metadata.selfLink');
let selfLink = objectPath.get(e, 'object.metadata.annotations.selfLink');
if (selfLink && !this._sentSelflinks[selfLink]) {
this._sentSelflinks[selfLink] = true;
result.push(e);
Expand Down
8 changes: 6 additions & 2 deletions test/Sender-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ const testResource = {
metadata: {
name: 'kubernetes-dashboard',
namespace: 'kube-system',
selfLink: '/api/v1/namespaces/kube-system/endpoints/kubernetes-dashboard',
uid: 'b3156476-fca8-11e8-9f10-3a7d3a0f8cf2',
resourceVersion: '10739693',
creationTimestamp: '2018-12-10T18:23:32Z',
Expand All @@ -33,6 +32,9 @@ const testResource = {
'k8s-app': 'kubernetes-dashboard',
'kubernetes.io/cluster-service': 'true',
'razee/watch-resource': 'lite'
},
annotations: {
selfLink: '/api/v1/namespaces/kube-system/endpoints/kubernetes-dashboard',
}
}
}
Expand All @@ -43,7 +45,6 @@ const otherResource = {
metadata: {
name: 'hello-world',
namespace: 'kube-system',
selfLink: '/api/v1/namespaces/kube-system/endpoints/hello-world',
uid: 'b3156476-fca8-11e8-9f10-3xxxxxxxx2',
resourceVersion: '10739693',
creationTimestamp: '2018-12-10T18:23:32Z',
Expand All @@ -52,6 +53,9 @@ const otherResource = {
'k8s-app': 'hello-world',
'kubernetes.io/cluster-service': 'true',
'razee/watch-resource': 'lite'
},
annotations: {
selfLink: '/api/v1/namespaces/kube-system/endpoints/hello-world',
}
}
}
Expand Down
20 changes: 0 additions & 20 deletions test/Util-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -218,26 +218,6 @@ describe('Util', () => {
});
});
// ===========================================================================
describe('addHash', () => {
// ---------- Success ----------
it('handles an object with array of "items"', () => {
let result = Util.addHash({ items: [{ type: 'gismo' }] });
assert.equal(JSON.stringify(result), '{"items":[{"type":"gismo","razeehash":"e181ea047cdac260b67e1f538c77622b553ad744"}]}');
});
it('handles an array', () => {
let result = Util.addHash([{ type: 'gismo' }]);
assert.equal(JSON.stringify(result), '[{"type":"gismo","razeehash":"e181ea047cdac260b67e1f538c77622b553ad744"}]');
});
it('handles an object', () => {
let result = Util.addHash({ type: 'gismo' });
assert.equal(JSON.stringify(result), '{"type":"gismo","razeehash":"e181ea047cdac260b67e1f538c77622b553ad744"}');
});
it('object does not have type attr then it should not get razeehash attr', () => {
let result = Util.addHash({ stuff: 'gismo' });
assert.equal(JSON.stringify(result), '{"stuff":"gismo"}');
});
});
// ===========================================================================
describe('prepObject2Send', () => {
// ---------- Success ----------
it('handles an object with array of "items"', () => {
Expand Down

0 comments on commit 48a6f2b

Please sign in to comment.