Skip to content

Commit

Permalink
chore: Update CRD before deletion in 2 steps
Browse files Browse the repository at this point in the history
Signed-off-by: Anatolii Bazko <[email protected]>
  • Loading branch information
tolusha committed Sep 10, 2024
1 parent 82dfd2c commit 6dd4e5b
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions src/api/kube-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1038,12 +1038,13 @@ export class KubeClient {
}
}

async replaceCustomResourceDefinition(crd: V1CustomResourceDefinition): Promise<void> {
async replaceCustomResourceDefinition(name: string, crd: V1CustomResourceDefinition): Promise<void> {
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)
}
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 6dd4e5b

Please sign in to comment.