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 16, 2021
1 parent 6aff31e commit ada5ca6
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 23 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"
)
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, 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 @@ -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, workv1alpha2.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, 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())
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, workv1alpha2.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, 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 @@ -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, workv1alpha2.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, 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())
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, workv1alpha2.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
13 changes: 10 additions & 3 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,16 +96,22 @@ 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, workv1alpha1.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, 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 ada5ca6

Please sign in to comment.