From 4e09f5e6b5d90db8a214942bbe494564e4c9fc00 Mon Sep 17 00:00:00 2001 From: calvin Date: Fri, 24 Nov 2023 17:47:42 +0800 Subject: [PATCH] fixed resource version conflict when updating serivce Signed-off-by: calvin --- operator/pkg/util/apiclient/idempotency.go | 25 ++++++++++++++-------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/operator/pkg/util/apiclient/idempotency.go b/operator/pkg/util/apiclient/idempotency.go index 2f8ffa311..2cd27f2d1 100644 --- a/operator/pkg/util/apiclient/idempotency.go +++ b/operator/pkg/util/apiclient/idempotency.go @@ -19,6 +19,7 @@ package apiclient import ( "context" "errors" + "fmt" "strings" admissionregistrationv1 "k8s.io/api/admissionregistration/v1" @@ -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{}) + 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) + } + + older, err := client.CoreV1().Services(service.GetNamespace()).Get(context.TODO(), service.GetName(), metav1.GetOptions{}) + if err != nil { return 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 + 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) } - - return err } klog.V(5).InfoS("Successfully created or updated service", "service", service.GetName())