diff --git a/pkg/controllers/binding/binding_controller.go b/pkg/controllers/binding/binding_controller.go index a560e160bc55..d32e85285386 100644 --- a/pkg/controllers/binding/binding_controller.go +++ b/pkg/controllers/binding/binding_controller.go @@ -94,7 +94,7 @@ func (c *ResourceBindingController) syncBinding(binding *workv1alpha2.ResourceBi if err != nil { klog.Errorf("Failed to find orphan works by resourceBinding(%s/%s). Error: %v.", binding.GetNamespace(), binding.GetName(), err) - c.EventRecorder.Event(binding, corev1.EventTypeWarning, eventReasonCleanupWorkFailed, err.Error()) + c.EventRecorder.Event(binding, corev1.EventTypeWarning, util.EventReasonCleanupWorkFailed, err.Error()) return controllerruntime.Result{Requeue: true}, err } @@ -102,7 +102,7 @@ func (c *ResourceBindingController) syncBinding(binding *workv1alpha2.ResourceBi if err != nil { klog.Errorf("Failed to remove orphan works by resourceBinding(%s/%s). Error: %v.", binding.GetNamespace(), binding.GetName(), err) - c.EventRecorder.Event(binding, corev1.EventTypeWarning, eventReasonCleanupWorkFailed, err.Error()) + c.EventRecorder.Event(binding, corev1.EventTypeWarning, util.EventReasonCleanupWorkFailed, err.Error()) return controllerruntime.Result{Requeue: true}, err } @@ -117,23 +117,23 @@ func (c *ResourceBindingController) syncBinding(binding *workv1alpha2.ResourceBi 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()) + c.EventRecorder.Event(binding, corev1.EventTypeWarning, util.EventReasonSyncWorkFailed, err.Error()) return controllerruntime.Result{Requeue: true}, err } 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) + c.EventRecorder.Event(binding, corev1.EventTypeNormal, util.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()) + c.EventRecorder.Event(binding, corev1.EventTypeWarning, util.EventReasonAggregateStatusFailed, err.Error()) return controllerruntime.Result{Requeue: true}, err } 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) + c.EventRecorder.Event(binding, corev1.EventTypeNormal, util.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 5660233a632a..7e74b3e1779a 100644 --- a/pkg/controllers/binding/cluster_resource_binding_controller.go +++ b/pkg/controllers/binding/cluster_resource_binding_controller.go @@ -93,14 +93,14 @@ func (c *ClusterResourceBindingController) syncBinding(binding *workv1alpha2.Clu works, err := helper.FindOrphanWorks(c.Client, "", binding.Name, clusterNames, apiextensionsv1.ClusterScoped) if err != nil { klog.Errorf("Failed to find orphan works by ClusterResourceBinding(%s). Error: %v.", binding.GetName(), err) - c.EventRecorder.Event(binding, corev1.EventTypeWarning, eventReasonCleanupWorkFailed, err.Error()) + c.EventRecorder.Event(binding, corev1.EventTypeWarning, util.EventReasonCleanupWorkFailed, err.Error()) return controllerruntime.Result{Requeue: true}, err } err = helper.RemoveOrphanWorks(c.Client, works) if err != nil { klog.Errorf("Failed to remove orphan works by clusterResourceBinding(%s). Error: %v.", binding.GetName(), err) - c.EventRecorder.Event(binding, corev1.EventTypeWarning, eventReasonCleanupWorkFailed, err.Error()) + c.EventRecorder.Event(binding, corev1.EventTypeWarning, util.EventReasonCleanupWorkFailed, err.Error()) return controllerruntime.Result{Requeue: true}, err } @@ -113,22 +113,22 @@ func (c *ClusterResourceBindingController) syncBinding(binding *workv1alpha2.Clu 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()) + c.EventRecorder.Event(binding, corev1.EventTypeWarning, util.EventReasonSyncWorkFailed, err.Error()) return controllerruntime.Result{Requeue: true}, err } msg := fmt.Sprintf("Sync work of clusterResourceBinding(%s) successful.", binding.GetName()) klog.V(4).Infof(msg) - c.EventRecorder.Event(binding, corev1.EventTypeNormal, eventReasonSyncWorkSucceed, msg) + c.EventRecorder.Event(binding, corev1.EventTypeNormal, util.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()) + c.EventRecorder.Event(binding, corev1.EventTypeWarning, util.EventReasonAggregateStatusFailed, err.Error()) return controllerruntime.Result{Requeue: true}, err } msg = fmt.Sprintf("Update clusterResourceBinding(%s) with AggregatedStatus successfully.", binding.Name) klog.V(4).Infof(msg) - c.EventRecorder.Event(binding, corev1.EventTypeNormal, eventReasonAggregateStatusSucceed, msg) + c.EventRecorder.Event(binding, corev1.EventTypeNormal, util.EventReasonAggregateStatusSucceed, msg) return controllerruntime.Result{}, nil } diff --git a/pkg/controllers/binding/common.go b/pkg/controllers/binding/common.go index a938585faf42..6af7cae14940 100644 --- a/pkg/controllers/binding/common.go +++ b/pkg/controllers/binding/common.go @@ -24,14 +24,6 @@ import ( "github.com/karmada-io/karmada/pkg/util/overridemanager" ) -const ( - eventReasonCleanupWorkFailed = "CleanupWorkFailed" - eventReasonSyncWorkFailed = "SyncWorkFailed" - eventReasonSyncWorkSucceed = "SyncWorkSucceed" - eventReasonAggregateStatusFailed = "AggregateStatusFailed" - eventReasonAggregateStatusSucceed = "AggregateStatusSucceed" -) - var workPredicateFn = builder.WithPredicates(predicate.Funcs{ CreateFunc: func(e event.CreateEvent) bool { return false diff --git a/pkg/controllers/execution/execution_controller.go b/pkg/controllers/execution/execution_controller.go index d2a55e3556d2..b8aace801261 100644 --- a/pkg/controllers/execution/execution_controller.go +++ b/pkg/controllers/execution/execution_controller.go @@ -4,6 +4,7 @@ import ( "context" "fmt" + corev1 "k8s.io/api/core/v1" apierrors "k8s.io/apimachinery/pkg/api/errors" "k8s.io/apimachinery/pkg/api/meta" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -95,13 +96,17 @@ func (c *Controller) SetupWithManager(mgr controllerruntime.Manager) error { func (c *Controller) syncWork(cluster *clusterv1alpha1.Cluster, work *workv1alpha1.Work) (controllerruntime.Result, error) { if !util.IsClusterReady(&cluster.Status) { - klog.Errorf("Stop sync work(%s/%s) for cluster(%s) as cluster not ready.", work.Namespace, work.Name, cluster.Name) + msg := fmt.Sprintf("Stop sync work(%s/%s) for cluster(%s) as cluster not ready.", work.Namespace, work.Name, cluster.Name) + klog.Errorf(msg) + c.EventRecorder.Event(work, corev1.EventTypeNormal, util.EventReasonSyncWorkFailed, msg) return controllerruntime.Result{Requeue: true}, fmt.Errorf("cluster(%s) not ready", cluster.Name) } err := c.syncToClusters(cluster, work) if err != nil { - klog.Errorf("Failed to sync work(%s) to cluster(%s): %v", work.Name, cluster.Name, err) + msg := fmt.Sprintf("Failed to sync work(%s) to cluster(%s): %v", work.Name, cluster.Name, err) + klog.Errorf(msg) + c.EventRecorder.Event(work, corev1.EventTypeNormal, util.EventReasonSyncWorkFailed, msg) return controllerruntime.Result{Requeue: true}, err } diff --git a/pkg/util/constants.go b/pkg/util/constants.go index 3bf34559e152..e8ea9bd85b9a 100644 --- a/pkg/util/constants.go +++ b/pkg/util/constants.go @@ -117,3 +117,17 @@ const ( // NamespaceKarmadaSystem is the karmada system namespace. NamespaceKarmadaSystem = "karmada-system" ) + +// Define event type used by karmada system. +const ( + //EventReasonCleanupWorkFailed indicates that Cleanup work failed. + EventReasonCleanupWorkFailed = "CleanupWorkFailed" + //EventReasonSyncWorkFailed indicates that Sync work failed. + EventReasonSyncWorkFailed = "SyncWorkFailed" + //EventReasonSyncWorkSucceed indicates that Sync work succeed. + EventReasonSyncWorkSucceed = "SyncWorkSucceed" + //EventReasonAggregateStatusFailed indicates that aggregate status failed. + EventReasonAggregateStatusFailed = "AggregateStatusFailed" + //EventReasonAggregateStatusFailed indicates that aggregate status succeed. + EventReasonAggregateStatusSucceed = "AggregateStatusSucceed" +)