Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure parent cleanup and ability to become parent of an orphan #378

Merged
merged 4 commits into from
Jan 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion lib/BaseController.js
Original file line number Diff line number Diff line change
Expand Up @@ -598,8 +598,23 @@ 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' };
// Check to see if the current parent still exists. If it doesnt, it can be taken over.
try {
await this.kubeResourceMeta.request({ uri: currentParent, json: true });
// Current parent still exists, abort with `Multiple Parents` response
return { statusCode: 200, body: 'Multiple Parents' };
}
catch( e ) {
if( e.statusCode === 404 ) {
// Current parent is gone, continue and assume parent role
this.log.info( `parent ${currentParent} no longer exists, asserting ownership for new parent ${this.selfLink}` );
}
else {
throw e;
}
}
}

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
33 changes: 20 additions & 13 deletions lib/CompositeController.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,21 +37,28 @@ module.exports = class CompositeController extends BaseController {
// if cleanup fails, do not return successful response => Promise.reject(err) or throw Error(err)
let children = objectPath.get(this.data, ['object', 'status', 'children'], {});
let res = await Promise.all(Object.entries(children).map(async ([selfLink, child]) => {
let reconcile = objectPath.get(child, ['deploy.razee.io/Reconcile'], this.reconcileDefault);
if (reconcile.toLowerCase() == 'true') {
try {
try {
let reconcile = objectPath.get(child, ['deploy.razee.io/Reconcile'], this.reconcileDefault);
if (reconcile.toLowerCase() == 'true') {
// If child is to be Reconciled, delete it
this.log.info(`finalizer: ${selfLink} no longer applied.. Reconcile ${reconcile.toLowerCase()}.. removing from cluster`);
await this._deleteChild(selfLink);
let res = await this.patchSelf({
status: {
children: {
[selfLink]: null
}
}
}, { status: true });
objectPath.set(this.data, 'object', res);
} catch (e) {
return Promise.reject({ selfLink: selfLink, action: 'delete', state: 'fail', error: e.message || e });
}
else {
// If child is NOT to be Reconciled, remove the parent reference
this.log.info(`finalizer: ${selfLink} no longer applied.. Reconcile ${reconcile.toLowerCase()}.. leaving on cluster`);
await this._patchChild(selfLink);
}
let res = await this.patchSelf({
status: {
children: {
[selfLink]: null
}
}
}, { status: true });
objectPath.set(this.data, 'object', res);
} catch (e) {
return Promise.reject({ selfLink: selfLink, action: 'delete', state: 'fail', error: e.message || e });
}
}));
return res;
Expand Down