|
22 | 22 |
|
23 | 23 | const objectPath = require('object-path'); |
24 | 24 | const clone = require('clone'); |
| 25 | +const yaml = require('js-yaml'); |
25 | 26 |
|
26 | 27 | const BaseController = require('./BaseController'); |
27 | 28 |
|
@@ -178,8 +179,12 @@ module.exports = class CompositeController extends BaseController { |
178 | 179 | modeUsed = 'Apply'; |
179 | 180 | res = await this.apply(krm, child); |
180 | 181 | } |
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 | + } |
183 | 188 | } catch (e) { |
184 | 189 | res = e; |
185 | 190 | } |
@@ -221,6 +226,7 @@ module.exports = class CompositeController extends BaseController { |
221 | 226 | } |
222 | 227 | } else if (!exists) { |
223 | 228 | this.log.info(`${selfLink} no longer applied.. Reconcile ${reconcile.toLowerCase()}.. leaving on cluster`); |
| 229 | + await this._patchChild(selfLink); |
224 | 230 | let res = await this.patchSelf({ |
225 | 231 | status: { |
226 | 232 | children: { |
@@ -250,4 +256,28 @@ module.exports = class CompositeController extends BaseController { |
250 | 256 | this.log.debug(`Delete ${res.statusCode} ${opt.uri || opt.url}`); |
251 | 257 | return { statusCode: res.statusCode, body: res.body }; |
252 | 258 | } |
| 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 | + } |
253 | 283 | }; |
0 commit comments