Fix add retry on createorupdate
Signed-off-by: pigletfly <wangbing.adam@gmail.com>
This commit is contained in:
parent
e494bda2da
commit
eef56c31d2
|
@ -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,7 +428,9 @@ func (d *ResourceDetector) ApplyPolicy(object *unstructured.Unstructured, object
|
|||
return err
|
||||
}
|
||||
bindingCopy := binding.DeepCopy()
|
||||
operationResult, err := controllerutil.CreateOrUpdate(context.TODO(), d.Client, bindingCopy, func() error {
|
||||
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
|
||||
|
@ -437,6 +440,11 @@ func (d *ResourceDetector) ApplyPolicy(object *unstructured.Unstructured, object
|
|||
bindingCopy.Spec.Replicas = binding.Spec.Replicas
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
klog.Errorf("Failed to apply policy(%s) for object: %s. error: %v", policy.Name, objectKey, err)
|
||||
return err
|
||||
|
@ -483,7 +491,9 @@ func (d *ResourceDetector) ApplyClusterPolicy(object *unstructured.Unstructured,
|
|||
return err
|
||||
}
|
||||
bindingCopy := binding.DeepCopy()
|
||||
operationResult, err := controllerutil.CreateOrUpdate(context.TODO(), d.Client, bindingCopy, func() error {
|
||||
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
|
||||
|
@ -493,6 +503,11 @@ func (d *ResourceDetector) ApplyClusterPolicy(object *unstructured.Unstructured,
|
|||
bindingCopy.Spec.Replicas = binding.Spec.Replicas
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
klog.Errorf("Failed to apply cluster policy(%s) for object: %s. error: %v", policy.Name, objectKey, err)
|
||||
return err
|
||||
|
|
|
@ -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,13 +16,20 @@ 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 {
|
||||
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 {
|
||||
klog.Errorf("Failed to create/update EndpointSlice %s/%s. Error: %v", endpointSlice.GetNamespace(), endpointSlice.GetName(), err)
|
||||
return err
|
||||
|
|
Loading…
Reference in New Issue