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 18, 2021
1 parent c018f79 commit 757e13d
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 24 deletions.
10 changes: 10 additions & 0 deletions pkg/apis/work/v1alpha1/events.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package v1alpha1

// Define events for work objects.
const (
// EventReasonSyncWorkFailed indicates that Sync work failed.
EventReasonSyncWorkFailed = "SyncFailed"

// EventReasonSyncWorkSucceed indicates that Sync work succeed.
EventReasonSyncWorkSucceed = "SyncSucceed"
)
15 changes: 15 additions & 0 deletions pkg/apis/work/v1alpha2/events.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package v1alpha2

// Define events for ResourceBinding and ClusterResourceBinding objects.
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"
)
13 changes: 6 additions & 7 deletions pkg/controllers/binding/binding_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,15 +97,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, workv1alpha2.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, workv1alpha2.EventReasonCleanupWorkFailed, err.Error())
return controllerruntime.Result{Requeue: true}, err
}

Expand All @@ -120,24 +120,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, workv1alpha2.EventReasonSyncWorkFailed, err.Error())
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)
c.EventRecorder.Event(binding, corev1.EventTypeNormal, workv1alpha2.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, workv1alpha2.EventReasonAggregateStatusFailed, err.Error())
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)
c.EventRecorder.Event(binding, corev1.EventTypeNormal, workv1alpha2.EventReasonAggregateStatusSucceed, msg)
}
if len(errs) > 0 {
return controllerruntime.Result{Requeue: true}, errors.NewAggregate(errs)
Expand Down
13 changes: 6 additions & 7 deletions pkg/controllers/binding/cluster_resource_binding_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,14 +96,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, workv1alpha2.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, workv1alpha2.EventReasonCleanupWorkFailed, err.Error())
return controllerruntime.Result{Requeue: true}, err
}

Expand All @@ -116,23 +116,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, workv1alpha2.EventReasonSyncWorkFailed, err.Error())
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)
c.EventRecorder.Event(binding, corev1.EventTypeNormal, workv1alpha2.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, workv1alpha2.EventReasonAggregateStatusFailed, err.Error())
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)
c.EventRecorder.Event(binding, corev1.EventTypeNormal, workv1alpha2.EventReasonAggregateStatusSucceed, msg)
}
if len(errs) > 0 {
return controllerruntime.Result{Requeue: true}, errors.NewAggregate(errs)
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 @@ -101,10 +102,14 @@ func (c *Controller) syncWork(cluster *clusterv1alpha1.Cluster, work *workv1alph

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.EventTypeWarning, workv1alpha1.EventReasonSyncWorkFailed, msg)
return controllerruntime.Result{Requeue: true}, err
}

msg := fmt.Sprintf("Sync work (%s) to cluster(%s) successful.", work.Name, cluster.Name)
klog.V(4).Infof(msg)
c.EventRecorder.Event(work, corev1.EventTypeNormal, workv1alpha1.EventReasonSyncWorkSucceed, msg)
return controllerruntime.Result{}, nil
}

Expand Down

0 comments on commit 757e13d

Please sign in to comment.