Merge pull request #1355 from pigletfly/fix-retry

Fix add retry on createorupdate
This commit is contained in:
karmada-bot 2022-02-12 10:54:50 +08:00 committed by GitHub
commit d898f8469c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 44 additions and 21 deletions

View File

@ -21,6 +21,7 @@ import (
"k8s.io/client-go/dynamic" "k8s.io/client-go/dynamic"
"k8s.io/client-go/tools/cache" "k8s.io/client-go/tools/cache"
"k8s.io/client-go/tools/record" "k8s.io/client-go/tools/record"
"k8s.io/client-go/util/retry"
"k8s.io/klog/v2" "k8s.io/klog/v2"
"sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
@ -427,14 +428,21 @@ func (d *ResourceDetector) ApplyPolicy(object *unstructured.Unstructured, object
return err return err
} }
bindingCopy := binding.DeepCopy() bindingCopy := binding.DeepCopy()
operationResult, err := controllerutil.CreateOrUpdate(context.TODO(), d.Client, bindingCopy, func() error { var operationResult controllerutil.OperationResult
// Just update necessary fields, especially avoid modifying Spec.Clusters which is scheduling result, if already exists. err = retry.RetryOnConflict(retry.DefaultRetry, func() (err error) {
bindingCopy.Labels = binding.Labels operationResult, err = controllerutil.CreateOrUpdate(context.TODO(), d.Client, bindingCopy, func() error {
bindingCopy.OwnerReferences = binding.OwnerReferences // Just update necessary fields, especially avoid modifying Spec.Clusters which is scheduling result, if already exists.
bindingCopy.Finalizers = binding.Finalizers bindingCopy.Labels = binding.Labels
bindingCopy.Spec.Resource = binding.Spec.Resource bindingCopy.OwnerReferences = binding.OwnerReferences
bindingCopy.Spec.ReplicaRequirements = binding.Spec.ReplicaRequirements bindingCopy.Finalizers = binding.Finalizers
bindingCopy.Spec.Replicas = binding.Spec.Replicas 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 return nil
}) })
if err != nil { if err != nil {
@ -483,14 +491,21 @@ func (d *ResourceDetector) ApplyClusterPolicy(object *unstructured.Unstructured,
return err return err
} }
bindingCopy := binding.DeepCopy() bindingCopy := binding.DeepCopy()
operationResult, err := controllerutil.CreateOrUpdate(context.TODO(), d.Client, bindingCopy, func() error { var operationResult controllerutil.OperationResult
// Just update necessary fields, especially avoid modifying Spec.Clusters which is scheduling result, if already exists. err = retry.RetryOnConflict(retry.DefaultRetry, func() (err error) {
bindingCopy.Labels = binding.Labels operationResult, err = controllerutil.CreateOrUpdate(context.TODO(), d.Client, bindingCopy, func() error {
bindingCopy.OwnerReferences = binding.OwnerReferences // Just update necessary fields, especially avoid modifying Spec.Clusters which is scheduling result, if already exists.
bindingCopy.Finalizers = binding.Finalizers bindingCopy.Labels = binding.Labels
bindingCopy.Spec.Resource = binding.Spec.Resource bindingCopy.OwnerReferences = binding.OwnerReferences
bindingCopy.Spec.ReplicaRequirements = binding.Spec.ReplicaRequirements bindingCopy.Finalizers = binding.Finalizers
bindingCopy.Spec.Replicas = binding.Spec.Replicas 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 return nil
}) })
if err != nil { if err != nil {

View File

@ -7,6 +7,7 @@ import (
apierrors "k8s.io/apimachinery/pkg/api/errors" apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/labels" "k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/util/errors" "k8s.io/apimachinery/pkg/util/errors"
"k8s.io/client-go/util/retry"
"k8s.io/klog/v2" "k8s.io/klog/v2"
"sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" "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. // CreateOrUpdateEndpointSlice creates a EndpointSlice object if not exist, or updates if it already exist.
func CreateOrUpdateEndpointSlice(client client.Client, endpointSlice *discoveryv1.EndpointSlice) error { func CreateOrUpdateEndpointSlice(client client.Client, endpointSlice *discoveryv1.EndpointSlice) error {
runtimeObject := endpointSlice.DeepCopy() runtimeObject := endpointSlice.DeepCopy()
operationResult, err := controllerutil.CreateOrUpdate(context.TODO(), client, runtimeObject, func() error { var operationResult controllerutil.OperationResult
runtimeObject.AddressType = endpointSlice.AddressType err := retry.RetryOnConflict(retry.DefaultRetry, func() (err error) {
runtimeObject.Endpoints = endpointSlice.Endpoints operationResult, err = controllerutil.CreateOrUpdate(context.TODO(), client, runtimeObject, func() error {
runtimeObject.Labels = endpointSlice.Labels runtimeObject.AddressType = endpointSlice.AddressType
runtimeObject.Ports = endpointSlice.Ports runtimeObject.Endpoints = endpointSlice.Endpoints
runtimeObject.Labels = endpointSlice.Labels
runtimeObject.Ports = endpointSlice.Ports
return nil
})
if err != nil {
return err
}
return nil return nil
}) })
if err != nil { if err != nil {