Skip to content

Commit 402ce65

Browse files
kdai7Katie Dai
andauthored
add parent selfLink to children (#360)
* add parent selfLink to children * if different parent still apply other children Co-authored-by: Katie Dai <kdai7@ibm.com>
1 parent a09b239 commit 402ce65

File tree

2 files changed

+39
-2
lines changed

2 files changed

+39
-2
lines changed

lib/BaseController.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -596,6 +596,10 @@ module.exports = class BaseController {
596596
}
597597

598598
if (liveResource) {
599+
let currentParent = objectPath.get(liveResource, ['metadata', 'annotations', 'deploy.razee.io.parent']);
600+
if (currentParent && (currentParent != this.selfLink)) {
601+
return { statusCode: 200, body: 'Multiple Parents' };
602+
}
599603
let debug = objectPath.get(liveResource, ['metadata', 'labels', 'deploy.razee.io/debug'], 'false');
600604
if (debug.toLowerCase() === 'true') {
601605
this.log.warn(`${uri}: Debug enabled on resource: skipping modifying resource - adding annotation deploy.razee.io/pending-configuration.`);
@@ -633,6 +637,8 @@ module.exports = class BaseController {
633637
}
634638
objectPath.set(file, ['metadata', 'annotations', 'deploy.razee.io/last-applied-configuration'], JSON.stringify(original));
635639
}
640+
objectPath.set(file, ['metadata', 'annotations', 'deploy.razee.io.parent'], this.selfLink);
641+
636642
if (mode.toLowerCase() === 'strategicmergepatch') {
637643
let res = await krm.strategicMergePatch(name, namespace, file, opt);
638644
this._logger.debug(`StrategicMergePatch ${res.statusCode} ${uri}`);
@@ -658,6 +664,7 @@ module.exports = class BaseController {
658664
if (objectPath.get(file, ['metadata', 'annotations']) === null) {
659665
objectPath.set(file, ['metadata', 'annotations'], {});
660666
}
667+
objectPath.set(file, ['metadata', 'annotations', 'deploy.razee.io.parent'], this.selfLink);
661668
if (mode.toLowerCase() === 'additivemergepatch') {
662669
// Set last applied with a warning.
663670
objectPath.set(file, ['metadata', 'annotations', 'deploy.razee.io/last-applied-configuration'], additiveMergPatchWarning);

lib/CompositeController.js

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
const objectPath = require('object-path');
2424
const clone = require('clone');
25+
const yaml = require('js-yaml');
2526

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

@@ -178,8 +179,12 @@ module.exports = class CompositeController extends BaseController {
178179
modeUsed = 'Apply';
179180
res = await this.apply(krm, child);
180181
}
181-
await this.addChildren({ uid: childUid, selfLink: childUri, 'deploy.razee.io/Reconcile': reconcile, 'Impersonate-User': impersonateUser });
182-
this.log.info(`${modeUsed} ${res.statusCode} ${childUri}`);
182+
if (res.body == 'Multiple Parents') {
183+
this.log.warn(`Child already managed by another parent. Skipping apply for ${childUri}`);
184+
} else {
185+
await this.addChildren({ uid: childUid, selfLink: childUri, 'deploy.razee.io/Reconcile': reconcile, 'Impersonate-User': impersonateUser });
186+
this.log.info(`${modeUsed} ${res.statusCode} ${childUri}`);
187+
}
183188
} catch (e) {
184189
res = e;
185190
}
@@ -221,6 +226,7 @@ module.exports = class CompositeController extends BaseController {
221226
}
222227
} else if (!exists) {
223228
this.log.info(`${selfLink} no longer applied.. Reconcile ${reconcile.toLowerCase()}.. leaving on cluster`);
229+
await this._patchChild(selfLink);
224230
let res = await this.patchSelf({
225231
status: {
226232
children: {
@@ -250,4 +256,28 @@ module.exports = class CompositeController extends BaseController {
250256
this.log.debug(`Delete ${res.statusCode} ${opt.uri || opt.url}`);
251257
return { statusCode: res.statusCode, body: res.body };
252258
}
259+
260+
async _patchChild(child) {
261+
this.log.info(`Patch ${child}`);
262+
const opt = { uri: child, simple: false, resolveWithFullResponse: true, method: 'GET' };
263+
const get = await this.kubeResourceMeta.request(opt);
264+
const file = yaml.loadAll(get.body)[0];
265+
const childApiVersion = objectPath.get(file, 'apiVersion');
266+
const childKind = objectPath.get(file, 'kind');
267+
const namespace = objectPath.get(file, ['metadata', 'namespace']);
268+
const name = objectPath.get(file, ['metadata', 'name']);
269+
const krm = await this.kubeClass.getKubeResourceMeta(childApiVersion, childKind, 'update');
270+
271+
const patchObj = {
272+
metadata: {
273+
annotations: {
274+
'deploy.razee.io.parent': null
275+
}
276+
}
277+
};
278+
let res = await krm.mergePatch(name, namespace, patchObj, {simple: false, resolveWithFullResponse: true});
279+
280+
return { statusCode: res.statusCode, body: res.body };
281+
282+
}
253283
};

0 commit comments

Comments
 (0)