Skip to content

Commit 8960f96

Browse files
committed
retry update if there a conflict
Signed-off-by: Chetan Banavikalmutt <[email protected]>
1 parent a03aec0 commit 8960f96

File tree

2 files changed

+33
-16
lines changed

2 files changed

+33
-16
lines changed

test/e2e/fixture/fixture.go

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import (
3131
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
3232
"k8s.io/apimachinery/pkg/labels"
3333
"k8s.io/apimachinery/pkg/types"
34+
"k8s.io/client-go/util/retry"
3435
)
3536

3637
const (
@@ -129,8 +130,10 @@ func EnsureDeletion(ctx context.Context, kclient KubeClient, obj KubeObject) err
129130

130131
// After X seconds, give up waiting for the child objects to be deleted, and remove any finalizers on the object
131132
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 {
134137
return err
135138
}
136139
}
@@ -151,6 +154,33 @@ func EnsureDeletion(ctx context.Context, kclient KubeClient, obj KubeObject) err
151154
return fmt.Errorf("EnsureDeletion: timeout waiting for deletion of %s/%s", key.Namespace, key.Name)
152155
}
153156

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+
154184
func CleanUp(ctx context.Context, principalClient KubeClient, managedAgentClient KubeClient, autonomousAgentClient KubeClient, clusterDetails *ClusterDetails) error {
155185

156186
var list argoapp.ApplicationList
@@ -295,19 +325,6 @@ func CleanUp(ctx context.Context, principalClient KubeClient, managedAgentClient
295325
}
296326
}
297327

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-
311328
// Delete all repositories from the managed agent
312329
repoList = corev1.SecretList{}
313330
err = managedAgentClient.List(ctx, "argocd", &repoList, repoListOpts)

test/e2e/resync_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ func (suite *ResyncTestSuite) TearDownTest() {
4545
}
4646

4747
// Ensure that all the components are running after runnings the tests
48-
if !fixture.IsProcessRunning("process") {
48+
if !fixture.IsProcessRunning("principal") {
4949
err := fixture.StartProcess("principal")
5050
requires.NoError(err)
5151
}

0 commit comments

Comments
 (0)