Skip to content

Commit

Permalink
Add execution event
Browse files Browse the repository at this point in the history
Signed-off-by: wawa0210 <[email protected]>
  • Loading branch information
wawa0210 committed Oct 11, 2021
1 parent 6aff31e commit 98adeb4
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 22 deletions.
12 changes: 6 additions & 6 deletions pkg/controllers/binding/binding_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,15 +94,15 @@ 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
}

err = helper.RemoveOrphanWorks(c.Client, works)
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
}

Expand All @@ -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
}
Expand Down
12 changes: 6 additions & 6 deletions pkg/controllers/binding/cluster_resource_binding_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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
}
Expand Down
8 changes: 0 additions & 8 deletions pkg/controllers/binding/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 7 additions & 2 deletions pkg/controllers/execution/execution_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}

Expand Down
14 changes: 14 additions & 0 deletions pkg/util/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

0 comments on commit 98adeb4

Please sign in to comment.