@@ -83,14 +83,34 @@ const getArgocdAppManifest = (release: HelmRelease, values: Record<string, any>,
83
83
}
84
84
}
85
85
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
+
86
100
const removeApplication = async ( release : HelmRelease ) : Promise < void > => {
87
101
const name = getAppName ( release )
88
102
if ( ! ( await isResourcePresent ( 'application' , name , 'argocd' ) ) ) return
89
103
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
+ }
94
114
}
95
115
96
116
const writeApplicationManifest = async ( release : HelmRelease , otomiVersion : string ) : Promise < void > => {
0 commit comments