diff --git a/Makefile b/Makefile index 9a7e568d6..82b59da65 100644 --- a/Makefile +++ b/Makefile @@ -446,4 +446,4 @@ fix-imports: sort-imports ## Sort imports $(SORT_IMPORTS) -w . .PHONY: full-gen -full-gen: generate manifests vendor tidy bundle fix-imports bundle-reset ## generates all automatically generated content +full-gen: tidy vendor generate manifests bundle fix-imports bundle-reset ## generates all automatically generated content diff --git a/controllers/selfnoderemediation_controller.go b/controllers/selfnoderemediation_controller.go index 6a938a3a5..8705f2f33 100644 --- a/controllers/selfnoderemediation_controller.go +++ b/controllers/selfnoderemediation_controller.go @@ -22,6 +22,7 @@ import ( "time" "github.com/go-logr/logr" + "github.com/medik8s/common/pkg/events" "github.com/medik8s/common/pkg/resources" "github.com/pkg/errors" @@ -49,7 +50,6 @@ const ( nhcTimeOutAnnotation = "remediation.medik8s.io/nhc-timed-out" excludeRemediationLabel = "remediation.medik8s.io/exclude-from-remediation" - eventReasonRemediationCreated = "RemediationCreated" eventReasonRemediationStopped = "RemediationStopped" eventReasonRemediationSkipped = "RemediationSkipped" @@ -175,7 +175,10 @@ func (r *SelfNodeRemediationReconciler) Reconcile(ctx context.Context, req ctrl. return ctrl.Result{}, err } - r.Recorder.Event(snr, eventTypeNormal, eventReasonRemediationCreated, "Remediation started") + //used as an indication not to spam the event + if isFinalizerAlreadyAdded := controllerutil.ContainsFinalizer(snr, SNRFinalizer); !isFinalizerAlreadyAdded { + events.RemediationStarted(r.Recorder, snr) + } defer func() { if updateErr := r.updateSnrStatus(ctx, snr); updateErr != nil { @@ -195,9 +198,8 @@ func (r *SelfNodeRemediationReconciler) Reconcile(ctx context.Context, req ctrl. }() if r.isStoppedByNHC(snr) { - msg := "SNR remediation was stopped by Node Healthcheck" - r.logger.Info(msg) - r.Recorder.Event(snr, eventTypeNormal, eventReasonRemediationStopped, msg) + r.logger.Info("NHC added the timed-out annotation, remediation will be stopped") + events.RemediationStoppedByNHC(r.Recorder, snr) return ctrl.Result{}, r.updateConditions(remediationTimeoutByNHC, snr) } @@ -230,7 +232,7 @@ func (r *SelfNodeRemediationReconciler) Reconcile(ctx context.Context, req ctrl. return ctrl.Result{}, nil } - strategy := r.getRuntimeStrategy(snr.Spec.RemediationStrategy) + strategy := r.getRuntimeStrategy(snr) switch strategy { case v1alpha1.ResourceDeletionRemediationStrategy: result, err = r.remediateWithResourceDeletion(snr, node) @@ -483,6 +485,7 @@ func (r *SelfNodeRemediationReconciler) recoverNode(node *v1.Node, snr *v1alpha1 return ctrl.Result{}, err } r.Recorder.Event(snr, eventTypeNormal, eventReasonRemoveFinalizer, "Remediation process - remove finalizer from snr") + events.RemediationFinished(r.Recorder, snr) } return ctrl.Result{}, nil @@ -897,18 +900,23 @@ func (r *SelfNodeRemediationReconciler) isResourceDeletionExpired(snr *v1alpha1. return true, 0 } -func (r *SelfNodeRemediationReconciler) getRuntimeStrategy(strategy v1alpha1.RemediationStrategyType) v1alpha1.RemediationStrategyType { +func (r *SelfNodeRemediationReconciler) getRuntimeStrategy(snr *v1alpha1.SelfNodeRemediation) v1alpha1.RemediationStrategyType { + strategy := snr.Spec.RemediationStrategy if strategy != v1alpha1.AutomaticRemediationStrategy { return strategy } + remediationStrategy := v1alpha1.ResourceDeletionRemediationStrategy if utils.IsOutOfServiceTaintGA { - r.logger.Info("Automatically selected OutOfServiceTaint Remediation strategy") - return v1alpha1.OutOfServiceTaintRemediationStrategy + remediationStrategy = v1alpha1.OutOfServiceTaintRemediationStrategy + } + + //used as an indication not to spam the log + if isFinalizerAlreadyAdded := controllerutil.ContainsFinalizer(snr, SNRFinalizer); !isFinalizerAlreadyAdded { + r.logger.Info(fmt.Sprintf("Automatically selected %s Remediation strategy", remediationStrategy)) } - r.logger.Info("Automatically selected ResourceDeletion Remediation strategy") - return v1alpha1.ResourceDeletionRemediationStrategy + return remediationStrategy } diff --git a/controllers/tests/controller/selfnoderemediation_controller_test.go b/controllers/tests/controller/selfnoderemediation_controller_test.go index c64b7c75e..dbd5d30da 100644 --- a/controllers/tests/controller/selfnoderemediation_controller_test.go +++ b/controllers/tests/controller/selfnoderemediation_controller_test.go @@ -173,7 +173,7 @@ var _ = Describe("SNR Controller", func() { It("Remediation flow", func() { node := verifyNodeIsUnschedulable() - verifyEvent("Normal", "RemediationCreated", "Remediation started") + verifyEvent("Normal", "RemediationStarted", "[remediation] Remediation started") verifyEvent("Normal", "MarkUnschedulable", "Remediation process - unhealthy node marked as unschedulable") diff --git a/go.mod b/go.mod index cb427d80d..3f1b68555 100644 --- a/go.mod +++ b/go.mod @@ -6,7 +6,7 @@ require ( github.com/Masterminds/sprig v2.22.0+incompatible github.com/go-logr/logr v1.2.3 github.com/go-ping/ping v1.1.0 - github.com/medik8s/common v1.10.0 + github.com/medik8s/common v1.12.0 github.com/onsi/ginkgo/v2 v2.9.1 github.com/onsi/gomega v1.27.4 github.com/openshift/api v0.0.0-20230414143018-3367bc7e6ac7 // release-4.13 diff --git a/go.sum b/go.sum index 7320563ad..6bbf0558c 100644 --- a/go.sum +++ b/go.sum @@ -215,8 +215,8 @@ github.com/mailru/easyjson v0.7.6/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJ github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0= github.com/matttproud/golang_protobuf_extensions v1.0.2 h1:hAHbPm5IJGijwng3PWk09JkG9WeqChjprR5s9bBZ+OM= github.com/matttproud/golang_protobuf_extensions v1.0.2/go.mod h1:BSXmuO+STAnVfrANrmjBb36TMTDstsz7MSK+HVaYKv4= -github.com/medik8s/common v1.10.0 h1:ghvW/Kiv9xdkJbFhzeh38dSmx0ltoZNT7cHrCh3WpX4= -github.com/medik8s/common v1.10.0/go.mod h1:ZT/3hfMXJLmZEcqmxRWB5LGC8Wl+qKGGQ4zM8hOE7PY= +github.com/medik8s/common v1.12.0 h1:UJ5VS4rbo/K0snfuqRiYam1NhXTwo4Q7fTng34YSlmA= +github.com/medik8s/common v1.12.0/go.mod h1:Q6YR2rgyiLl6ob4Mx2uDBmybAB3d94fImwoHPbeiE54= github.com/mitchellh/copystructure v1.1.2 h1:Th2TIvG1+6ma3e/0/bopBKohOTY7s4dA8V2q4EUcBJ0= github.com/mitchellh/copystructure v1.1.2/go.mod h1:EBArHfARyrSWO/+Wyr9zwEkc6XMFB9XyNgFNmRkZZU4= github.com/mitchellh/reflectwalk v1.0.1 h1:FVzMWA5RllMAKIdUSC8mdWo3XtwoecrH79BY70sEEpE= diff --git a/vendor/github.com/medik8s/common/pkg/events/events.go b/vendor/github.com/medik8s/common/pkg/events/events.go new file mode 100644 index 000000000..7550d3c89 --- /dev/null +++ b/vendor/github.com/medik8s/common/pkg/events/events.go @@ -0,0 +1,51 @@ +package events + +import ( + "fmt" + + corev1 "k8s.io/api/core/v1" + "k8s.io/apimachinery/pkg/runtime" + "k8s.io/client-go/tools/record" +) + +// Event message format "medik8s " +const customFmt = "[remediation] %s" + +// NormalEvent will record an event with type Normal and fixed message. +func NormalEvent(recorder record.EventRecorder, object runtime.Object, reason, message string) { + recorder.Event(object, corev1.EventTypeNormal, reason, fmt.Sprintf(customFmt, message)) +} + +// NormalEventf will record an event with type Normal and formatted message. +func NormalEventf(recorder record.EventRecorder, object runtime.Object, reason, messageFmt string, a ...interface{}) { + message := fmt.Sprintf(messageFmt, a...) + recorder.Event(object, corev1.EventTypeNormal, reason, fmt.Sprintf(customFmt, message)) +} + +// WarningEvent will record an event with type Warning and fixed message. +func WarningEvent(recorder record.EventRecorder, object runtime.Object, reason, message string) { + recorder.Event(object, corev1.EventTypeWarning, reason, fmt.Sprintf(customFmt, message)) +} + +// WarningEventf will record an event with type Warning and formatted message. +func WarningEventf(recorder record.EventRecorder, object runtime.Object, reason, messageFmt string, a ...interface{}) { + message := fmt.Sprintf(messageFmt, a...) + recorder.Event(object, corev1.EventTypeWarning, reason, fmt.Sprintf(customFmt, message)) +} + +// Special case events + +// RemediationStarted will record a Normal event with reason RemediationStarted and message Remediation started. +func RemediationStarted(recorder record.EventRecorder, object runtime.Object) { + NormalEvent(recorder, object, "RemediationStarted", "Remediation started") +} + +// RemediationStoppedByNHC will record a Normal event with reason RemediationStopped. +func RemediationStoppedByNHC(recorder record.EventRecorder, object runtime.Object) { + NormalEvent(recorder, object, "RemediationStopped", "NHC added the timed-out annotation, remediation will be stopped") +} + +// RemediationFinished will record a Normal event with reason RemediationFinished and message Remediation finished. +func RemediationFinished(recorder record.EventRecorder, object runtime.Object) { + NormalEvent(recorder, object, "RemediationFinished", "Remediation finished") +} diff --git a/vendor/github.com/medik8s/common/pkg/labels/labels.go b/vendor/github.com/medik8s/common/pkg/labels/labels.go index 001cafa8b..87409558e 100644 --- a/vendor/github.com/medik8s/common/pkg/labels/labels.go +++ b/vendor/github.com/medik8s/common/pkg/labels/labels.go @@ -9,4 +9,6 @@ const ( ControlPlaneRole = "node-role.kubernetes.io/control-plane" // DefaultTemplate label indicates to third party tools (e.g. UI) the default remediation template in case several exists. DefaultTemplate = "remediation.medik8s.io/default-template" + // ExcludeFromRemediation label would be put on a node with value "true" in order to indicate this node should not be remediated. + ExcludeFromRemediation = "remediation.medik8s.io/exclude-from-remediation" ) diff --git a/vendor/modules.txt b/vendor/modules.txt index f708c858f..e8d71b12b 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -107,8 +107,9 @@ github.com/mailru/easyjson/jwriter # github.com/matttproud/golang_protobuf_extensions v1.0.2 ## explicit; go 1.9 github.com/matttproud/golang_protobuf_extensions/pbutil -# github.com/medik8s/common v1.10.0 +# github.com/medik8s/common v1.12.0 ## explicit; go 1.20 +github.com/medik8s/common/pkg/events github.com/medik8s/common/pkg/labels github.com/medik8s/common/pkg/nodes github.com/medik8s/common/pkg/resources