@@ -92,27 +92,24 @@ const getArgocdAppManifest = (release: HelmRelease, values: Record<string, any>,
92
92
const setFinalizers = async ( name : string ) => {
93
93
d . info ( `Setting finalizers for ${ name } ` )
94
94
const resPatch =
95
- await $ `kubectl -n argocd patch application ${ name } -p '{"metadata": {"finalizers": ["resources-finalizer.argocd.argoproj.io"]}}' --type merge`
95
+ await $ `kubectl -n argocd patch applications.argoproj.io ${ name } -p '{"metadata": {"finalizers": ["resources-finalizer.argocd.argoproj.io"]}}' --type merge`
96
96
if ( resPatch . exitCode !== 0 ) {
97
97
throw new Error ( `Failed to set finalizers for ${ name } : ${ resPatch . stderr } ` )
98
98
}
99
99
}
100
100
101
101
const getFinalizers = async ( name : string ) : Promise < string [ ] > => {
102
- const res = await $ `kubectl -n argocd get application ${ name } -o jsonpath='{.metadata.finalizers}'`
102
+ const res = await $ `kubectl -n argocd get applications.argoproj.io ${ name } -o jsonpath='{.metadata.finalizers}'`
103
103
return res . stdout ? JSON . parse ( res . stdout ) : [ ]
104
104
}
105
105
106
- const removeApplication = async ( release : HelmRelease ) : Promise < void > => {
107
- const name = getAppName ( release )
108
- if ( ! ( await isResourcePresent ( 'application' , name , 'argocd' ) ) ) return
109
-
106
+ const removeApplication = async ( name : string ) : Promise < void > => {
110
107
try {
111
108
const finalizers = await getFinalizers ( name )
112
109
if ( ! finalizers . includes ( 'resources-finalizer.argocd.argoproj.io' ) ) {
113
110
await setFinalizers ( name )
114
111
}
115
- const resDelete = await $ `kubectl -n argocd delete application ${ name } `
112
+ const resDelete = await $ `kubectl -n argocd delete applications.argoproj.io ${ name } `
116
113
d . info ( resDelete . stdout . toString ( ) . trim ( ) )
117
114
} catch ( e ) {
118
115
d . error ( `Failed to delete application ${ name } : ${ e . message } ` )
@@ -149,6 +146,11 @@ async function patchArgocdResources(release: HelmRelease, values: Record<string,
149
146
}
150
147
}
151
148
149
+ const getApplications = async ( ) : Promise < string [ ] > => {
150
+ const res = await $ `kubectl get application.argoproj.io -n argocd -oname`
151
+ return res . stdout . split ( '\n' )
152
+ }
153
+
152
154
const writeApplicationManifest = async ( release : HelmRelease , otomiVersion : string ) : Promise < void > => {
153
155
const appName = `${ release . namespace } -${ release . name } `
154
156
const applicationPath = `${ appsDir } /${ appName } .yaml`
@@ -186,6 +188,7 @@ export const applyAsApps = async (argv: HelmArguments): Promise<void> => {
186
188
const errors : Array < any > = [ ]
187
189
// Generate JSON object with all helmfile releases defined in helmfile.d
188
190
const releases : [ ] = JSON . parse ( res . stdout . toString ( ) )
191
+ const currentApplications = await getApplications ( )
189
192
await Promise . allSettled (
190
193
releases . map ( async ( release : HelmRelease ) => {
191
194
try {
@@ -197,7 +200,11 @@ export const applyAsApps = async (argv: HelmArguments): Promise<void> => {
197
200
198
201
if ( release . installed ) await writeApplicationManifest ( release , otomiVersion )
199
202
else {
200
- await removeApplication ( release )
203
+ const appName = getAppName ( release )
204
+ const resourceName = `application.argoproj.io/${ appName } `
205
+ if ( currentApplications . includes ( resourceName ) ) {
206
+ await removeApplication ( appName )
207
+ }
201
208
}
202
209
} catch ( e ) {
203
210
errors . push ( e )
0 commit comments