From 981201ce610b49d938b71bc953040cf0be1cbb9a Mon Sep 17 00:00:00 2001 From: dddddai Date: Thu, 14 Oct 2021 15:36:45 +0800 Subject: [PATCH] aggregate status no matter if ensured work or not Signed-off-by: dddddai --- pkg/controllers/binding/binding_controller.go | 25 +++++++++++-------- .../cluster_resource_binding_controller.go | 24 +++++++++++------- 2 files changed, 30 insertions(+), 19 deletions(-) diff --git a/pkg/controllers/binding/binding_controller.go b/pkg/controllers/binding/binding_controller.go index 23892cfd0436..c2743e588d40 100644 --- a/pkg/controllers/binding/binding_controller.go +++ b/pkg/controllers/binding/binding_controller.go @@ -10,6 +10,7 @@ import ( "k8s.io/apimachinery/pkg/api/meta" "k8s.io/apimachinery/pkg/labels" "k8s.io/apimachinery/pkg/types" + "k8s.io/apimachinery/pkg/util/errors" "k8s.io/client-go/dynamic" "k8s.io/client-go/tools/record" "k8s.io/klog/v2" @@ -115,29 +116,33 @@ func (c *ResourceBindingController) syncBinding(binding *workv1alpha2.ResourceBi binding.GetNamespace(), binding.GetName(), err) return controllerruntime.Result{Requeue: true}, err } - + var errs []error err = ensureWork(c.Client, workload, c.OverrideManager, binding, apiextensionsv1.NamespaceScoped) if err != nil { klog.Errorf("Failed to transform resourceBinding(%s/%s) to works. Error: %v.", binding.GetNamespace(), binding.GetName(), err) c.EventRecorder.Event(binding, corev1.EventTypeWarning, eventReasonSyncWorkFailed, err.Error()) - return controllerruntime.Result{Requeue: true}, err + errs = append(errs, err) + } else { + msg := fmt.Sprintf("Sync work of resourceBinding(%s/%s) successful.", binding.Namespace, binding.Name) + klog.V(4).Infof(msg) + c.EventRecorder.Event(binding, corev1.EventTypeNormal, eventReasonSyncWorkSucceed, msg) } - msg := fmt.Sprintf("Sync work of resourceBinding(%s/%s) successful.", binding.Namespace, binding.Name) - klog.V(4).Infof(msg) - c.EventRecorder.Event(binding, corev1.EventTypeNormal, eventReasonSyncWorkSucceed, msg) err = helper.AggregateResourceBindingWorkStatus(c.Client, binding, workload) if err != nil { klog.Errorf("Failed to aggregate workStatuses to resourceBinding(%s/%s). Error: %v.", binding.GetNamespace(), binding.GetName(), err) c.EventRecorder.Event(binding, corev1.EventTypeWarning, eventReasonAggregateStatusFailed, err.Error()) - return controllerruntime.Result{Requeue: true}, err + errs = append(errs, err) + } else { + msg := fmt.Sprintf("Update resourceBinding(%s/%s) with AggregatedStatus successfully.", binding.Namespace, binding.Name) + klog.V(4).Infof(msg) + c.EventRecorder.Event(binding, corev1.EventTypeNormal, eventReasonAggregateStatusSucceed, msg) + } + if len(errs) > 0 { + return controllerruntime.Result{Requeue: true}, errors.NewAggregate(errs) } - msg = fmt.Sprintf("Update resourceBinding(%s/%s) with AggregatedStatus successfully.", binding.Namespace, binding.Name) - klog.V(4).Infof(msg) - c.EventRecorder.Event(binding, corev1.EventTypeNormal, eventReasonAggregateStatusSucceed, msg) - return controllerruntime.Result{}, nil } diff --git a/pkg/controllers/binding/cluster_resource_binding_controller.go b/pkg/controllers/binding/cluster_resource_binding_controller.go index e7abc0ece7a1..6512624a8bea 100644 --- a/pkg/controllers/binding/cluster_resource_binding_controller.go +++ b/pkg/controllers/binding/cluster_resource_binding_controller.go @@ -10,6 +10,7 @@ import ( "k8s.io/apimachinery/pkg/api/meta" "k8s.io/apimachinery/pkg/labels" "k8s.io/apimachinery/pkg/types" + "k8s.io/apimachinery/pkg/util/errors" "k8s.io/client-go/dynamic" "k8s.io/client-go/tools/record" "k8s.io/klog/v2" @@ -111,26 +112,31 @@ func (c *ClusterResourceBindingController) syncBinding(binding *workv1alpha2.Clu klog.Errorf("Failed to fetch workload for clusterResourceBinding(%s). Error: %v.", binding.GetName(), err) return controllerruntime.Result{Requeue: true}, err } - + var errs []error err = ensureWork(c.Client, workload, c.OverrideManager, binding, apiextensionsv1.ClusterScoped) if err != nil { klog.Errorf("Failed to transform clusterResourceBinding(%s) to works. Error: %v.", binding.GetName(), err) c.EventRecorder.Event(binding, corev1.EventTypeWarning, eventReasonSyncWorkFailed, err.Error()) - return controllerruntime.Result{Requeue: true}, err + errs = append(errs, err) + } else { + msg := fmt.Sprintf("Sync work of clusterResourceBinding(%s) successful.", binding.GetName()) + klog.V(4).Infof(msg) + c.EventRecorder.Event(binding, corev1.EventTypeNormal, eventReasonSyncWorkSucceed, msg) } - msg := fmt.Sprintf("Sync work of clusterResourceBinding(%s) successful.", binding.GetName()) - klog.V(4).Infof(msg) - c.EventRecorder.Event(binding, corev1.EventTypeNormal, eventReasonSyncWorkSucceed, msg) err = helper.AggregateClusterResourceBindingWorkStatus(c.Client, binding, workload) if err != nil { klog.Errorf("Failed to aggregate workStatuses to clusterResourceBinding(%s). Error: %v.", binding.GetName(), err) c.EventRecorder.Event(binding, corev1.EventTypeWarning, eventReasonAggregateStatusFailed, err.Error()) - return controllerruntime.Result{Requeue: true}, err + errs = append(errs, err) + } else { + msg := fmt.Sprintf("Update clusterResourceBinding(%s) with AggregatedStatus successfully.", binding.Name) + klog.V(4).Infof(msg) + c.EventRecorder.Event(binding, corev1.EventTypeNormal, eventReasonAggregateStatusSucceed, msg) + } + if len(errs) > 0 { + return controllerruntime.Result{Requeue: true}, errors.NewAggregate(errs) } - msg = fmt.Sprintf("Update clusterResourceBinding(%s) with AggregatedStatus successfully.", binding.Name) - klog.V(4).Infof(msg) - c.EventRecorder.Event(binding, corev1.EventTypeNormal, eventReasonAggregateStatusSucceed, msg) return controllerruntime.Result{}, nil }