diff --git a/pkg/detector/detector.go b/pkg/detector/detector.go index a36e7b427c27..e01e36f19781 100644 --- a/pkg/detector/detector.go +++ b/pkg/detector/detector.go @@ -21,6 +21,7 @@ import ( "k8s.io/client-go/dynamic" "k8s.io/client-go/tools/cache" "k8s.io/client-go/tools/record" + "k8s.io/client-go/util/retry" "k8s.io/klog/v2" "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" @@ -427,14 +428,21 @@ func (d *ResourceDetector) ApplyPolicy(object *unstructured.Unstructured, object return err } bindingCopy := binding.DeepCopy() - operationResult, err := controllerutil.CreateOrUpdate(context.TODO(), d.Client, bindingCopy, func() error { - // Just update necessary fields, especially avoid modifying Spec.Clusters which is scheduling result, if already exists. - bindingCopy.Labels = binding.Labels - bindingCopy.OwnerReferences = binding.OwnerReferences - bindingCopy.Finalizers = binding.Finalizers - bindingCopy.Spec.Resource = binding.Spec.Resource - bindingCopy.Spec.ReplicaRequirements = binding.Spec.ReplicaRequirements - bindingCopy.Spec.Replicas = binding.Spec.Replicas + var operationResult controllerutil.OperationResult + err = retry.RetryOnConflict(retry.DefaultRetry, func() (err error) { + operationResult, err = controllerutil.CreateOrUpdate(context.TODO(), d.Client, bindingCopy, func() error { + // Just update necessary fields, especially avoid modifying Spec.Clusters which is scheduling result, if already exists. + bindingCopy.Labels = binding.Labels + bindingCopy.OwnerReferences = binding.OwnerReferences + bindingCopy.Finalizers = binding.Finalizers + bindingCopy.Spec.Resource = binding.Spec.Resource + bindingCopy.Spec.ReplicaRequirements = binding.Spec.ReplicaRequirements + bindingCopy.Spec.Replicas = binding.Spec.Replicas + return nil + }) + if err != nil { + return err + } return nil }) if err != nil { @@ -483,14 +491,21 @@ func (d *ResourceDetector) ApplyClusterPolicy(object *unstructured.Unstructured, return err } bindingCopy := binding.DeepCopy() - operationResult, err := controllerutil.CreateOrUpdate(context.TODO(), d.Client, bindingCopy, func() error { - // Just update necessary fields, especially avoid modifying Spec.Clusters which is scheduling result, if already exists. - bindingCopy.Labels = binding.Labels - bindingCopy.OwnerReferences = binding.OwnerReferences - bindingCopy.Finalizers = binding.Finalizers - bindingCopy.Spec.Resource = binding.Spec.Resource - bindingCopy.Spec.ReplicaRequirements = binding.Spec.ReplicaRequirements - bindingCopy.Spec.Replicas = binding.Spec.Replicas + var operationResult controllerutil.OperationResult + err = retry.RetryOnConflict(retry.DefaultRetry, func() (err error) { + operationResult, err = controllerutil.CreateOrUpdate(context.TODO(), d.Client, bindingCopy, func() error { + // Just update necessary fields, especially avoid modifying Spec.Clusters which is scheduling result, if already exists. + bindingCopy.Labels = binding.Labels + bindingCopy.OwnerReferences = binding.OwnerReferences + bindingCopy.Finalizers = binding.Finalizers + bindingCopy.Spec.Resource = binding.Spec.Resource + bindingCopy.Spec.ReplicaRequirements = binding.Spec.ReplicaRequirements + bindingCopy.Spec.Replicas = binding.Spec.Replicas + return nil + }) + if err != nil { + return err + } return nil }) if err != nil { diff --git a/pkg/util/helper/mcs.go b/pkg/util/helper/mcs.go index 3020511840f5..824d2481d231 100644 --- a/pkg/util/helper/mcs.go +++ b/pkg/util/helper/mcs.go @@ -7,6 +7,7 @@ import ( apierrors "k8s.io/apimachinery/pkg/api/errors" "k8s.io/apimachinery/pkg/labels" "k8s.io/apimachinery/pkg/util/errors" + "k8s.io/client-go/util/retry" "k8s.io/klog/v2" "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" @@ -15,11 +16,18 @@ import ( // CreateOrUpdateEndpointSlice creates a EndpointSlice object if not exist, or updates if it already exist. func CreateOrUpdateEndpointSlice(client client.Client, endpointSlice *discoveryv1.EndpointSlice) error { runtimeObject := endpointSlice.DeepCopy() - operationResult, err := controllerutil.CreateOrUpdate(context.TODO(), client, runtimeObject, func() error { - runtimeObject.AddressType = endpointSlice.AddressType - runtimeObject.Endpoints = endpointSlice.Endpoints - runtimeObject.Labels = endpointSlice.Labels - runtimeObject.Ports = endpointSlice.Ports + var operationResult controllerutil.OperationResult + err := retry.RetryOnConflict(retry.DefaultRetry, func() (err error) { + operationResult, err = controllerutil.CreateOrUpdate(context.TODO(), client, runtimeObject, func() error { + runtimeObject.AddressType = endpointSlice.AddressType + runtimeObject.Endpoints = endpointSlice.Endpoints + runtimeObject.Labels = endpointSlice.Labels + runtimeObject.Ports = endpointSlice.Ports + return nil + }) + if err != nil { + return err + } return nil }) if err != nil {