Skip to content

Commit bb4a757

Browse files
authored
feat: argo app removal (#1727)
1 parent 460276e commit bb4a757

File tree

1 file changed

+24
-4
lines changed

1 file changed

+24
-4
lines changed

src/cmd/apply-as-apps.ts

+24-4
Original file line numberDiff line numberDiff line change
@@ -83,14 +83,34 @@ const getArgocdAppManifest = (release: HelmRelease, values: Record<string, any>,
8383
}
8484
}
8585

86+
const setFinalizers = async (name: string) => {
87+
d.info(`Setting finalizers for ${name}`)
88+
const resPatch =
89+
await $`kubectl -n argocd patch application ${name} -p '{"metadata": {"finalizers": ["resources-finalizer.argocd.argoproj.io"]}}' --type merge`
90+
if (resPatch.exitCode !== 0) {
91+
throw new Error(`Failed to set finalizers for ${name}: ${resPatch.stderr}`)
92+
}
93+
}
94+
95+
const getFinalizers = async (name: string): Promise<string[]> => {
96+
const res = await $`kubectl -n argocd get application ${name} -o jsonpath='{.metadata.finalizers}'`
97+
return res.stdout ? JSON.parse(res.stdout) : []
98+
}
99+
86100
const removeApplication = async (release: HelmRelease): Promise<void> => {
87101
const name = getAppName(release)
88102
if (!(await isResourcePresent('application', name, 'argocd'))) return
89103

90-
// TODO: do we always want to remove finalizers?
91-
await $`kubectl -n argocd patch application ${name} -p '{"metadata": {"finalizers": null}}' --type merge`
92-
const resDelete = await $`kubectl -n argocd delete application ${name}`
93-
d.info(resDelete.stdout.toString())
104+
try {
105+
const finalizers = await getFinalizers(name)
106+
if (!finalizers.includes('resources-finalizer.argocd.argoproj.io')) {
107+
await setFinalizers(name)
108+
}
109+
const resDelete = await $`kubectl -n argocd delete application ${name}`
110+
d.info(resDelete.stdout.toString().trim())
111+
} catch (e) {
112+
d.error(`Failed to delete application ${name}: ${e.message}`)
113+
}
94114
}
95115

96116
const writeApplicationManifest = async (release: HelmRelease, otomiVersion: string): Promise<void> => {

0 commit comments

Comments
 (0)