Skip to content

Commit

Permalink
Merge pull request #4320 from calvin0327/fixed-service-conflict
Browse files Browse the repository at this point in the history
karmada-operator: resolve resource version conflict when updating serivce
  • Loading branch information
karmada-bot committed Nov 25, 2023
2 parents f6b9849 + 4e09f5e commit dd9f54c
Showing 1 changed file with 16 additions and 9 deletions.
25 changes: 16 additions & 9 deletions operator/pkg/util/apiclient/idempotency.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package apiclient
import (
"context"
"errors"
"fmt"
"strings"

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

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

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

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

0 comments on commit dd9f54c

Please sign in to comment.