Skip to content

Commit

Permalink
add parent selfLink to children (#360)
Browse files Browse the repository at this point in the history
* add parent selfLink to children

* if different parent still apply other children

Co-authored-by: Katie Dai <kdai7@ibm.com>
  • Loading branch information
kdai7 and Katie Dai authored Nov 16, 2022
1 parent a09b239 commit 402ce65
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 2 deletions.
7 changes: 7 additions & 0 deletions lib/BaseController.js
Original file line number Diff line number Diff line change
Expand Up @@ -596,6 +596,10 @@ module.exports = class BaseController {
}

if (liveResource) {
let currentParent = objectPath.get(liveResource, ['metadata', 'annotations', 'deploy.razee.io.parent']);
if (currentParent && (currentParent != this.selfLink)) {
return { statusCode: 200, body: 'Multiple Parents' };
}
let debug = objectPath.get(liveResource, ['metadata', 'labels', 'deploy.razee.io/debug'], 'false');
if (debug.toLowerCase() === 'true') {
this.log.warn(`${uri}: Debug enabled on resource: skipping modifying resource - adding annotation deploy.razee.io/pending-configuration.`);
Expand Down Expand Up @@ -633,6 +637,8 @@ module.exports = class BaseController {
}
objectPath.set(file, ['metadata', 'annotations', 'deploy.razee.io/last-applied-configuration'], JSON.stringify(original));
}
objectPath.set(file, ['metadata', 'annotations', 'deploy.razee.io.parent'], this.selfLink);

if (mode.toLowerCase() === 'strategicmergepatch') {
let res = await krm.strategicMergePatch(name, namespace, file, opt);
this._logger.debug(`StrategicMergePatch ${res.statusCode} ${uri}`);
Expand All @@ -658,6 +664,7 @@ module.exports = class BaseController {
if (objectPath.get(file, ['metadata', 'annotations']) === null) {
objectPath.set(file, ['metadata', 'annotations'], {});
}
objectPath.set(file, ['metadata', 'annotations', 'deploy.razee.io.parent'], this.selfLink);
if (mode.toLowerCase() === 'additivemergepatch') {
// Set last applied with a warning.
objectPath.set(file, ['metadata', 'annotations', 'deploy.razee.io/last-applied-configuration'], additiveMergPatchWarning);
Expand Down
34 changes: 32 additions & 2 deletions lib/CompositeController.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

const objectPath = require('object-path');
const clone = require('clone');
const yaml = require('js-yaml');

const BaseController = require('./BaseController');

Expand Down Expand Up @@ -178,8 +179,12 @@ module.exports = class CompositeController extends BaseController {
modeUsed = 'Apply';
res = await this.apply(krm, child);
}
await this.addChildren({ uid: childUid, selfLink: childUri, 'deploy.razee.io/Reconcile': reconcile, 'Impersonate-User': impersonateUser });
this.log.info(`${modeUsed} ${res.statusCode} ${childUri}`);
if (res.body == 'Multiple Parents') {
this.log.warn(`Child already managed by another parent. Skipping apply for ${childUri}`);
} else {
await this.addChildren({ uid: childUid, selfLink: childUri, 'deploy.razee.io/Reconcile': reconcile, 'Impersonate-User': impersonateUser });
this.log.info(`${modeUsed} ${res.statusCode} ${childUri}`);
}
} catch (e) {
res = e;
}
Expand Down Expand Up @@ -221,6 +226,7 @@ module.exports = class CompositeController extends BaseController {
}
} else if (!exists) {
this.log.info(`${selfLink} no longer applied.. Reconcile ${reconcile.toLowerCase()}.. leaving on cluster`);
await this._patchChild(selfLink);
let res = await this.patchSelf({
status: {
children: {
Expand Down Expand Up @@ -250,4 +256,28 @@ module.exports = class CompositeController extends BaseController {
this.log.debug(`Delete ${res.statusCode} ${opt.uri || opt.url}`);
return { statusCode: res.statusCode, body: res.body };
}

async _patchChild(child) {
this.log.info(`Patch ${child}`);
const opt = { uri: child, simple: false, resolveWithFullResponse: true, method: 'GET' };
const get = await this.kubeResourceMeta.request(opt);
const file = yaml.loadAll(get.body)[0];
const childApiVersion = objectPath.get(file, 'apiVersion');
const childKind = objectPath.get(file, 'kind');
const namespace = objectPath.get(file, ['metadata', 'namespace']);
const name = objectPath.get(file, ['metadata', 'name']);
const krm = await this.kubeClass.getKubeResourceMeta(childApiVersion, childKind, 'update');

const patchObj = {
metadata: {
annotations: {
'deploy.razee.io.parent': null
}
}
};
let res = await krm.mergePatch(name, namespace, patchObj, {simple: false, resolveWithFullResponse: true});

return { statusCode: res.statusCode, body: res.body };

}
};

0 comments on commit 402ce65

Please sign in to comment.