Skip to content

Commit dd9f54c

Browse files
authored
Merge pull request #4320 from calvin0327/fixed-service-conflict
karmada-operator: resolve resource version conflict when updating serivce
2 parents f6b9849 + 4e09f5e commit dd9f54c

File tree

1 file changed

+16
-9
lines changed

1 file changed

+16
-9
lines changed

operator/pkg/util/apiclient/idempotency.go

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package apiclient
1919
import (
2020
"context"
2121
"errors"
22+
"fmt"
2223
"strings"
2324

2425
admissionregistrationv1 "k8s.io/api/admissionregistration/v1"
@@ -90,19 +91,25 @@ func CreateOrUpdateSecret(client clientset.Interface, secret *corev1.Secret) err
9091
func CreateOrUpdateService(client clientset.Interface, service *corev1.Service) error {
9192
_, err := client.CoreV1().Services(service.GetNamespace()).Create(context.TODO(), service, metav1.CreateOptions{})
9293
if err != nil {
93-
if apierrors.IsAlreadyExists(err) {
94-
_, err := client.CoreV1().Services(service.GetNamespace()).Update(context.TODO(), service, metav1.UpdateOptions{})
95-
return err
94+
if !apierrors.IsAlreadyExists(err) {
95+
// Ignore if the Service is invalid with this error message:
96+
// Service "apiserver" is invalid: provided Port is already allocated.
97+
if apierrors.IsInvalid(err) && strings.Contains(err.Error(), errAllocated.Error()) {
98+
klog.V(2).ErrorS(err, "failed to create or update serivce", "service", klog.KObj(service))
99+
return nil
100+
}
101+
return fmt.Errorf("unable to create Service: %v", err)
96102
}
97103

98-
// Ignore if the Service is invalid with this error message:
99-
// Service "apiserver" is invalid: provided Port is already allocated.
100-
if apierrors.IsInvalid(err) && strings.Contains(err.Error(), errAllocated.Error()) {
101-
klog.V(2).ErrorS(err, "failed to create or update serivce", "service", klog.KObj(service))
102-
return nil
104+
older, err := client.CoreV1().Services(service.GetNamespace()).Get(context.TODO(), service.GetName(), metav1.GetOptions{})
105+
if err != nil {
106+
return err
103107
}
104108

105-
return err
109+
service.ResourceVersion = older.ResourceVersion
110+
if _, err := client.CoreV1().Services(service.GetNamespace()).Update(context.TODO(), service, metav1.UpdateOptions{}); err != nil {
111+
return fmt.Errorf("unable to update Service: %v", err)
112+
}
106113
}
107114

108115
klog.V(5).InfoS("Successfully created or updated service", "service", service.GetName())

0 commit comments

Comments
 (0)