Skip to content

Commit f988e12

Browse files
committed
修改Pod注解遇到冲突自动重试
1 parent ceefe6c commit f988e12

File tree

2 files changed

+17
-14
lines changed

2 files changed

+17
-14
lines changed

pkg/kube/pod.go

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,13 @@ import (
88
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
99
)
1010

11-
func SetPodAnnotation(ctx context.Context, cachedPod client.Object, name, value string) error {
12-
pod := &corev1.Pod{}
13-
if err := apiReader.Get(ctx, client.ObjectKeyFromObject(cachedPod), pod); err != nil {
14-
return err
15-
}
16-
if pod.Annotations == nil {
17-
pod.Annotations = make(map[string]string)
18-
}
19-
pod.Annotations[name] = value
20-
return apiClient.Update(ctx, pod)
11+
func SetPodAnnotation(ctx context.Context, pod *corev1.Pod, name, value string) error {
12+
return update(ctx, pod, func() {
13+
if pod.Annotations == nil {
14+
pod.Annotations = make(map[string]string)
15+
}
16+
pod.Annotations[name] = value
17+
}, false, false)
2118
}
2219

2320
func AddPodFinalizer(ctx context.Context, pod client.Object, finalizerName string) error {

pkg/kube/retry.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,23 @@ import (
1010
)
1111

1212
func Update(ctx context.Context, obj client.Object, mutate func()) error {
13-
return update(ctx, obj, mutate, false)
13+
return update(ctx, obj, mutate, false, true)
1414
}
1515

1616
func UpdateStatus(ctx context.Context, obj client.Object, mutate func()) error {
17-
return update(ctx, obj, mutate, true)
17+
return update(ctx, obj, mutate, true, true)
1818
}
1919

20-
func update(ctx context.Context, obj client.Object, mutate func(), updateStatus bool) error {
20+
func update(ctx context.Context, obj client.Object, mutate func(), updateStatus, useCache bool) error {
2121
return wait.ExponentialBackoff(retry.DefaultBackoff, func() (done bool, err error) {
2222
key := client.ObjectKeyFromObject(obj)
23-
err = apiClient.Get(ctx, key, obj)
23+
var reader client.Reader
24+
if useCache {
25+
reader = apiClient
26+
} else {
27+
reader = apiReader
28+
}
29+
err = reader.Get(ctx, key, obj)
2430
if err != nil {
2531
return false, err
2632
}

0 commit comments

Comments
 (0)