Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ Emit Kubernetes Events when Cluster Phase, ControlPlaneReady, or InfrastructureReady change #7786

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions internal/controllers/cluster/cluster_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"time"

"github.com/pkg/errors"
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/meta"
"k8s.io/apimachinery/pkg/runtime"
Expand Down Expand Up @@ -331,6 +332,7 @@ func (r *Reconciler) reconcileDelete(ctx context.Context, cluster *clusterv1.Clu
}

controllerutil.RemoveFinalizer(cluster, clusterv1.ClusterFinalizer)
r.recorder.Eventf(cluster, corev1.EventTypeNormal, "Deleted", "Cluster %s has been deleted", cluster.Name)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this a common pattern? Wondering if pods do this too, and/or if there isn't some kind of general Object deleted event that gets emitted

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For a pod, there's the Killing Event that's emitted when it is deleted. Here's the lifecycle events of creating a statefulset and then running a kubectl delete against the pod.

LAST SEEN   TYPE      REASON                            OBJECT                                            MESSAGE
60s         Normal    Scheduled                         pod/touge-debug-0                                 Successfully assigned default/touge-debug-0 to ip-10-158-59-46.us-west-2.compute.internal
4s          Normal    SuccessfulCreate                  statefulset/touge-debug                           create Pod touge-debug-0 in StatefulSet touge-debug successful
59s         Normal    Pulling                           pod/touge-debug-0                                 Pulling image "mtougeron/touge-debug"
46s         Normal    Pulled                            pod/touge-debug-0                                 Successfully pulled image "mtougeron/touge-debug" in 13.117780542s
46s         Normal    Created                           pod/touge-debug-0                                 Created container ubuntu
46s         Normal    Started                           pod/touge-debug-0                                 Started container ubuntu
3s          Normal    Killing                           pod/touge-debug-0                                 Stopping container ubuntu
4s          Normal    Pulled                            pod/touge-debug-0                                 Container image "mtougeron/touge-debug" already present on machine
4s          Normal    Scheduled                         pod/touge-debug-0                                 Successfully assigned default/touge-debug-0 to ip-10-158-59-46.us-west-2.compute.internal
3s          Normal    Created                           pod/touge-debug-0                                 Created container ubuntu
3s          Normal    Started                           pod/touge-debug-0                                 Started container ubuntu

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this event could be emitted multiple times as it depends on how quickly the garbage collector in kube-controller-manager actually removes the object.

But I assume we don't care about how often this event is emitted?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Personally I think that's acceptable. From the CAPI perspective the action happened so it's appropriate for us to emit the event.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is acceptable too

return ctrl.Result{}, nil
}

Expand Down
22 changes: 22 additions & 0 deletions internal/controllers/cluster/cluster_controller_phases.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ import (
)

func (r *Reconciler) reconcilePhase(_ context.Context, cluster *clusterv1.Cluster) {
preReconcilePhase := cluster.Status.GetTypedPhase()

if cluster.Status.Phase == "" {
cluster.Status.SetTypedPhase(clusterv1.ClusterPhasePending)
}
Expand All @@ -61,6 +63,16 @@ func (r *Reconciler) reconcilePhase(_ context.Context, cluster *clusterv1.Cluste
if !cluster.DeletionTimestamp.IsZero() {
cluster.Status.SetTypedPhase(clusterv1.ClusterPhaseDeleting)
}

// Only record the event if the status has changed
if preReconcilePhase != cluster.Status.GetTypedPhase() {
// Failed clusters should get a Warning event
if cluster.Status.GetTypedPhase() == clusterv1.ClusterPhaseFailed {
r.recorder.Eventf(cluster, corev1.EventTypeWarning, string(cluster.Status.GetTypedPhase()), "Cluster %s is %s: %s", cluster.Name, string(cluster.Status.GetTypedPhase()), pointer.StringDeref(cluster.Status.FailureMessage, "unknown"))
} else {
r.recorder.Eventf(cluster, corev1.EventTypeNormal, string(cluster.Status.GetTypedPhase()), "Cluster %s is %s", cluster.Name, string(cluster.Status.GetTypedPhase()))
}
}
}

// reconcileExternal handles generic unstructured objects referenced by a Cluster.
Expand Down Expand Up @@ -163,11 +175,16 @@ func (r *Reconciler) reconcileInfrastructure(ctx context.Context, cluster *clust
}

// Determine if the infrastructure provider is ready.
preReconcileInfrastructureReady := cluster.Status.InfrastructureReady
ready, err := external.IsReady(infraConfig)
if err != nil {
return ctrl.Result{}, err
}
cluster.Status.InfrastructureReady = ready
// Only record the event if the status has changed
if preReconcileInfrastructureReady != cluster.Status.InfrastructureReady {
r.recorder.Eventf(cluster, corev1.EventTypeNormal, "InfrastructureReady", "Cluster %s InfrastructureReady is now %t", cluster.Name, cluster.Status.InfrastructureReady)
}

// Report a summary of current status of the infrastructure object defined for this cluster.
conditions.SetMirror(cluster, clusterv1.InfrastructureReadyCondition,
Expand Down Expand Up @@ -225,12 +242,17 @@ func (r *Reconciler) reconcileControlPlane(ctx context.Context, cluster *cluster
return ctrl.Result{}, nil
}

preReconcileControlPlaneReady := cluster.Status.ControlPlaneReady
// Determine if the control plane provider is ready.
ready, err := external.IsReady(controlPlaneConfig)
if err != nil {
return ctrl.Result{}, err
}
cluster.Status.ControlPlaneReady = ready
// Only record the event if the status has changed
if preReconcileControlPlaneReady != cluster.Status.ControlPlaneReady {
r.recorder.Eventf(cluster, corev1.EventTypeNormal, "ControlPlaneReady", "Cluster %s ControlPlaneReady is now %t", cluster.Name, cluster.Status.ControlPlaneReady)
}

// Report a summary of current status of the control plane object defined for this cluster.
conditions.SetMirror(cluster, clusterv1.ControlPlaneReadyCondition,
Expand Down
13 changes: 9 additions & 4 deletions internal/controllers/cluster/cluster_controller_phases_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/client-go/tools/record"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/fake"
Expand Down Expand Up @@ -136,7 +137,8 @@ func TestClusterReconcilePhases(t *testing.T) {
Build()
}
r := &Reconciler{
Client: c,
Client: c,
recorder: record.NewFakeRecorder(32),
}

res, err := r.reconcileInfrastructure(ctx, tt.cluster)
Expand Down Expand Up @@ -215,7 +217,8 @@ func TestClusterReconcilePhases(t *testing.T) {
Build()
}
r := &Reconciler{
Client: c,
Client: c,
recorder: record.NewFakeRecorder(32),
}
res, err := r.reconcileKubeconfig(ctx, tt.cluster)
if tt.wantErr {
Expand Down Expand Up @@ -364,7 +367,8 @@ func TestClusterReconciler_reconcilePhase(t *testing.T) {
Build()

r := &Reconciler{
Client: c,
Client: c,
recorder: record.NewFakeRecorder(32),
}
r.reconcilePhase(ctx, tt.cluster)
g.Expect(tt.cluster.Status.GetTypedPhase()).To(Equal(tt.wantPhase))
Expand Down Expand Up @@ -479,7 +483,8 @@ func TestClusterReconcilePhases_reconcileFailureDomains(t *testing.T) {
}

r := &Reconciler{
Client: fake.NewClientBuilder().WithObjects(objs...).Build(),
Client: fake.NewClientBuilder().WithObjects(objs...).Build(),
recorder: record.NewFakeRecorder(32),
}

_, err := r.reconcileInfrastructure(ctx, tt.cluster)
Expand Down