@@ -31,6 +31,7 @@ import (
31
31
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
32
32
"k8s.io/apimachinery/pkg/labels"
33
33
"k8s.io/apimachinery/pkg/types"
34
+ "k8s.io/client-go/util/retry"
34
35
)
35
36
36
37
const (
@@ -129,8 +130,10 @@ func EnsureDeletion(ctx context.Context, kclient KubeClient, obj KubeObject) err
129
130
130
131
// After X seconds, give up waiting for the child objects to be deleted, and remove any finalizers on the object
131
132
if len (obj .GetFinalizers ()) > 0 {
132
- obj .SetFinalizers (nil )
133
- if err := kclient .Update (ctx , obj , metav1.UpdateOptions {}); err != nil {
133
+ err := EnsureUpdate (ctx , kclient , obj , func (obj KubeObject ) {
134
+ obj .SetFinalizers (nil )
135
+ })
136
+ if err != nil {
134
137
return err
135
138
}
136
139
}
@@ -151,6 +154,33 @@ func EnsureDeletion(ctx context.Context, kclient KubeClient, obj KubeObject) err
151
154
return fmt .Errorf ("EnsureDeletion: timeout waiting for deletion of %s/%s" , key .Namespace , key .Name )
152
155
}
153
156
157
+ // EnsureUpdate will ensure that the object is updated by retrying if there is a conflict.
158
+ func EnsureUpdate (ctx context.Context , kclient KubeClient , obj KubeObject , updateFn func (obj KubeObject )) error {
159
+ return retry .RetryOnConflict (retry .DefaultRetry , func () error {
160
+ key := types.NamespacedName {Name : obj .GetName (), Namespace : obj .GetNamespace ()}
161
+ err := kclient .Get (ctx , key , obj , metav1.GetOptions {})
162
+ if err != nil {
163
+ if errors .IsNotFound (err ) {
164
+ return nil
165
+ }
166
+ return err
167
+ }
168
+
169
+ // Apply the update function to the object
170
+ updateFn (obj )
171
+
172
+ err = kclient .Update (ctx , obj , metav1.UpdateOptions {})
173
+ if err != nil {
174
+ if errors .IsNotFound (err ) {
175
+ return nil
176
+ }
177
+ return err
178
+ }
179
+
180
+ return nil
181
+ })
182
+ }
183
+
154
184
func CleanUp (ctx context.Context , principalClient KubeClient , managedAgentClient KubeClient , autonomousAgentClient KubeClient , clusterDetails * ClusterDetails ) error {
155
185
156
186
var list argoapp.ApplicationList
@@ -295,19 +325,6 @@ func CleanUp(ctx context.Context, principalClient KubeClient, managedAgentClient
295
325
}
296
326
}
297
327
298
- // Delete all repositories from the autonomous agent
299
- repoList = corev1.SecretList {}
300
- err = autonomousAgentClient .List (ctx , "argocd" , & repoList , repoListOpts )
301
- if err != nil {
302
- return err
303
- }
304
- for _ , repo := range repoList .Items {
305
- err = EnsureDeletion (ctx , autonomousAgentClient , & repo )
306
- if err != nil {
307
- return err
308
- }
309
- }
310
-
311
328
// Delete all repositories from the managed agent
312
329
repoList = corev1.SecretList {}
313
330
err = managedAgentClient .List (ctx , "argocd" , & repoList , repoListOpts )
0 commit comments