From 6dd4e5be304bd16e417a6778ffa487825b6bce0d Mon Sep 17 00:00:00 2001 From: Anatolii Bazko Date: Tue, 10 Sep 2024 10:15:19 +0200 Subject: [PATCH] chore: Update CRD before deletion in 2 steps Signed-off-by: Anatolii Bazko --- src/api/kube-client.ts | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/src/api/kube-client.ts b/src/api/kube-client.ts index 14f0960da..dfa60a83d 100644 --- a/src/api/kube-client.ts +++ b/src/api/kube-client.ts @@ -1038,12 +1038,13 @@ export class KubeClient { } } - async replaceCustomResourceDefinition(crd: V1CustomResourceDefinition): Promise { + async replaceCustomResourceDefinition(name: string, crd: V1CustomResourceDefinition): Promise { const k8sApi = this.kubeConfig.makeApiClient(ApiextensionsV1Api) try { - const response = await k8sApi.readCustomResourceDefinition(crd.metadata!.name!) + const response = await k8sApi.readCustomResourceDefinition(name) + crd.metadata!.resourceVersion = (response.body as any).metadata.resourceVersion - await k8sApi.replaceCustomResourceDefinition(crd.metadata!.name!, crd) + await k8sApi.replaceCustomResourceDefinition(name, crd) } catch (e: any) { throw this.wrapK8sClientError(e) } @@ -1096,18 +1097,19 @@ export class KubeClient { return } - crd = await this.getCustomResourceDefinition(crdName) // 1. Disable conversion webhook crd.spec.conversion = null + await this.replaceCustomResourceDefinition(crdName, crd) - // 2. Patch CRD to unblock potential invalid resource + // 2. Patch CRD to unblock potential invalid resource error + crd = await this.getCustomResourceDefinition(crdName) for (let i = 0; i < crd.spec.versions.length; i++) { if (crd.spec.versions[i].schema?.openAPIV3Schema?.properties?.spec) { crd.spec.versions[i].schema.openAPIV3Schema.properties.spec = {type: 'object', properties: {}} } } - await this.replaceCustomResourceDefinition(crd) + await this.replaceCustomResourceDefinition(crdName, crd) // 3. Delete resources let resources = await this.listClusterCustomObject(apiGroup, version, plural)