Skip to content

Commit

Permalink
Publish events when ClusterCreated or ClusterRemoved
Browse files Browse the repository at this point in the history
  • Loading branch information
sarataha committed Oct 31, 2023
1 parent 2e4ddbd commit 19700a6
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 1 deletion.
7 changes: 7 additions & 0 deletions config/rbac/role.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@ rules:
- get
- list
- watch
- apiGroups:
- ""
resources:
- events
verbs:
- create
- patch
- apiGroups:
- ""
resources:
Expand Down
19 changes: 18 additions & 1 deletion internal/controller/automatedclusterdiscovery_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,20 +44,31 @@ import (

const k8sManagedByLabel = "app.kubernetes.io/managed-by"

type eventRecorder interface {
Event(object runtime.Object, eventtype, reason, message string)
}

// AutomatedClusterDiscoveryReconciler reconciles a AutomatedClusterDiscovery object
type AutomatedClusterDiscoveryReconciler struct {
client.Client
Scheme *runtime.Scheme
Scheme *runtime.Scheme
EventRecorder eventRecorder

AKSProvider func(string) providers.Provider
}

// event emits a Kubernetes event and forwards the event to the event recorder
func (r *AutomatedClusterDiscoveryReconciler) event(obj *clustersv1alpha1.AutomatedClusterDiscovery, eventtype, reason, message string) {
r.EventRecorder.Event(obj, eventtype, reason, message)
}

//+kubebuilder:rbac:groups=clusters.weave.works,resources=automatedclusterdiscoveries,verbs=get;list;watch;create;update;patch;delete
//+kubebuilder:rbac:groups=clusters.weave.works,resources=automatedclusterdiscoveries/status,verbs=get;update;patch
//+kubebuilder:rbac:groups=clusters.weave.works,resources=automatedclusterdiscoveries/finalizers,verbs=update
//+kubebuilder:rbac:groups=gitops.weave.works,resources=gitopsclusters,verbs=get;list;watch;create;update;patch;delete
//+kubebuilder:rbac:groups="",resources=secrets,verbs=get;list;watch;create;update;patch;delete
//+kubebuilder:rbac:groups="",resources=configmaps,verbs=get;list;watch
//+kubebuilder:rbac:groups="",resources=events,verbs=create;patch

// Reconcile is part of the main kubernetes reconciliation loop which aims to
// move the current state of the cluster closer to the desired state.
Expand Down Expand Up @@ -204,6 +215,9 @@ func (r *AutomatedClusterDiscoveryReconciler) reconcileClusters(ctx context.Cont

inventoryResources = append(inventoryResources, clusterRef)

// publish event for ClusterCreated
r.event(cd, corev1.EventTypeNormal, "ClusterCreated", fmt.Sprintf("Cluster %s created", cluster.Name))

secret := newSecret(types.NamespacedName{
Name: secretName,
Namespace: cd.Namespace,
Expand Down Expand Up @@ -257,6 +271,9 @@ func (r *AutomatedClusterDiscoveryReconciler) reconcileClusters(ctx context.Cont
if err := r.Client.Delete(ctx, cluster); err != nil {
return inventoryResources, fmt.Errorf("failed to delete cluster: %w", err)
}

// publish event for ClusterRemoved
r.event(cd, corev1.EventTypeNormal, "ClusterRemoved", fmt.Sprintf("Cluster %s removed", cluster.GetName()))
}
}

Expand Down
41 changes: 41 additions & 0 deletions internal/controller/automatedclusterdiscovery_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ func TestAutomatedClusterDiscoveryReconciler(t *testing.T) {
AKSProvider: func(providerID string) providers.Provider {
return &testProvider
},
EventRecorder: &mockEventRecorder{},
}

assert.NoError(t, reconciler.SetupWithManager(mgr))
Expand Down Expand Up @@ -192,6 +193,7 @@ func TestAutomatedClusterDiscoveryReconciler(t *testing.T) {
AKSProvider: func(providerID string) providers.Provider {
return &testProvider
},
EventRecorder: &mockEventRecorder{},
}

assert.NoError(t, reconciler.SetupWithManager(mgr))
Expand Down Expand Up @@ -263,6 +265,7 @@ func TestAutomatedClusterDiscoveryReconciler(t *testing.T) {
AKSProvider: func(providerID string) providers.Provider {
return &testProvider
},
EventRecorder: &mockEventRecorder{},
}
assert.NoError(t, reconciler.SetupWithManager(mgr))

Expand Down Expand Up @@ -334,6 +337,7 @@ func TestAutomatedClusterDiscoveryReconciler(t *testing.T) {
AKSProvider: func(providerID string) providers.Provider {
return &testProvider
},
EventRecorder: &mockEventRecorder{},
}
assert.NoError(t, reconciler.SetupWithManager(mgr))

Expand Down Expand Up @@ -398,6 +402,7 @@ func TestAutomatedClusterDiscoveryReconciler(t *testing.T) {
AKSProvider: func(providerID string) providers.Provider {
return &testProvider
},
EventRecorder: &mockEventRecorder{},
}

assert.NoError(t, reconciler.SetupWithManager(mgr))
Expand Down Expand Up @@ -462,6 +467,7 @@ func TestAutomatedClusterDiscoveryReconciler(t *testing.T) {
AKSProvider: func(providerID string) providers.Provider {
return &testProvider
},
EventRecorder: &mockEventRecorder{},
}
assert.NoError(t, reconciler.SetupWithManager(mgr))

Expand Down Expand Up @@ -537,6 +543,7 @@ func TestReconcilingWithAnnotationChange(t *testing.T) {
AKSProvider: func(providerID string) providers.Provider {
return &stubProvider{}
},
EventRecorder: &mockEventRecorder{},
}
assert.NoError(t, reconciler.SetupWithManager(mgr))

Expand Down Expand Up @@ -565,6 +572,40 @@ func TestReconcilingWithAnnotationChange(t *testing.T) {
assert.Equal(t, aksCluster.Status.LastHandledReconcileAt, "testing")
}

func TestEvent(t *testing.T) {
mockEventRecorder := &mockEventRecorder{}

reconciler := &AutomatedClusterDiscoveryReconciler{
EventRecorder: mockEventRecorder,
}

obj := &clustersv1alpha1.AutomatedClusterDiscovery{}
eventtype := "Normal"
reason := "TestReason"
message := "TestMessage"

reconciler.event(obj, eventtype, reason, message)

assert.Equal(t, mockEventRecorder.CapturedObj, obj)
assert.Equal(t, mockEventRecorder.CapturedType, eventtype)
assert.Equal(t, mockEventRecorder.CapturedReason, reason)
assert.Equal(t, mockEventRecorder.CapturedMessage, message)
}

type mockEventRecorder struct {
CapturedObj runtime.Object
CapturedType string
CapturedReason string
CapturedMessage string
}

func (m *mockEventRecorder) Event(object runtime.Object, eventtype, reason, message string) {
m.CapturedObj = object
m.CapturedType = eventtype
m.CapturedReason = reason
m.CapturedMessage = message
}

type stubProvider struct {
response []*providers.ProviderCluster
clusterID string
Expand Down

0 comments on commit 19700a6

Please sign in to comment.