Skip to content

Commit

Permalink
fix: Reduce excessive logging/events
Browse files Browse the repository at this point in the history
  • Loading branch information
hrak committed Aug 28, 2024
1 parent 8a0b6b3 commit 5835fce
Showing 1 changed file with 32 additions and 15 deletions.
47 changes: 32 additions & 15 deletions controllers/cloudstackmachine_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,14 @@ import (
"math/rand"
"reflect"
"regexp"
"strings"
"time"

"k8s.io/utils/pointer"

"github.com/pkg/errors"
corev1 "k8s.io/api/core/v1"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/cluster-api/util"
Expand Down Expand Up @@ -226,7 +228,6 @@ func (r *CloudStackMachineReconciliationRunner) GetOrCreateVMInstance() (retRes
r.Recorder.Event(r.ReconciliationSubject, "Normal", "Creating", BootstrapDataNotReady)
return r.RequeueWithMessage(BootstrapDataNotReady + ".")
}
r.Log.Info("Got Bootstrap DataSecretName.")

// Get the kubeadm bootstrap secret for this machine.
secret := &corev1.Secret{}
Expand Down Expand Up @@ -268,8 +269,11 @@ func processCustomMetadata(data []byte, r *CloudStackMachineReconciliationRunner
// RequeueIfInstanceNotRunning checks the Instance's status for running state and requeues otherwise.
func (r *CloudStackMachineReconciliationRunner) RequeueIfInstanceNotRunning() (retRes ctrl.Result, reterr error) {
if r.ReconciliationSubject.Status.InstanceState == "Running" {
r.Recorder.Event(r.ReconciliationSubject, "Normal", "Running", MachineInstanceRunning)
r.Log.Info(MachineInstanceRunning)
// Only emit this event when relevant.
if !r.ReconciliationSubject.Status.Ready {
r.Recorder.Event(r.ReconciliationSubject, "Normal", "Running", MachineInstanceRunning)
r.Log.Info(MachineInstanceRunning)
}
r.ReconciliationSubject.Status.Ready = true
} else if r.ReconciliationSubject.Status.InstanceState == "Error" {
r.Recorder.Event(r.ReconciliationSubject, "Warning", "Error", MachineInErrorMessage)
Expand All @@ -291,7 +295,7 @@ func (r *CloudStackMachineReconciliationRunner) AddToLBIfNeeded() (retRes ctrl.R
if util.IsControlPlaneMachine(r.CAPIMachine) &&
r.FailureDomain.Spec.Zone.Network.Type == cloud.NetworkTypeIsolated &&
r.CSCluster.Spec.APIServerLoadBalancer.IsEnabled() {
r.Log.Info("Assigning VM to load balancer rule.")
r.Log.V(4).Info("Assigning VM to load balancer rule.")
if r.IsoNet.Spec.Name == "" {
return r.RequeueWithMessage("Could not get required Isolated Network for VM, requeueing.")
}
Expand All @@ -300,27 +304,40 @@ func (r *CloudStackMachineReconciliationRunner) AddToLBIfNeeded() (retRes ctrl.R
return ctrl.Result{}, err
}
}

return ctrl.Result{}, nil
}

// GetOrCreateMachineStateChecker creates or gets CloudStackMachineStateChecker object.
// GetOrCreateMachineStateChecker gets or creates a CloudStackMachineStateChecker object.
func (r *CloudStackMachineReconciliationRunner) GetOrCreateMachineStateChecker() (retRes ctrl.Result, reterr error) {
checkerName := r.ReconciliationSubject.Spec.InstanceID
if checkerName == nil {
return ctrl.Result{}, errors.New(CSMachineStateCheckerCreationFailed)
}
csMachineStateChecker := &infrav1.CloudStackMachineStateChecker{
ObjectMeta: r.NewChildObjectMeta(*checkerName),
Spec: infrav1.CloudStackMachineStateCheckerSpec{InstanceID: *checkerName},
Status: infrav1.CloudStackMachineStateCheckerStatus{Ready: false},
}

if err := r.K8sClient.Create(r.RequestCtx, csMachineStateChecker); err != nil && !utils.ContainsAlreadyExistsSubstring(err) {
r.Recorder.Eventf(r.ReconciliationSubject, "Warning", "Machine State Checker", CSMachineStateCheckerCreationFailed)
return r.ReturnWrappedError(err, CSMachineStateCheckerCreationFailed)
csMachineStateChecker := &infrav1.CloudStackMachineStateChecker{}
objectKey := client.ObjectKey{Name: strings.ToLower(*checkerName), Namespace: r.Request.Namespace}
err := r.K8sClient.Get(r.RequestCtx, objectKey, csMachineStateChecker)
if err != nil {
if k8serrors.IsNotFound(err) {
csMachineStateChecker.ObjectMeta = r.NewChildObjectMeta(*checkerName)
csMachineStateChecker.Spec = infrav1.CloudStackMachineStateCheckerSpec{InstanceID: *checkerName}
csMachineStateChecker.Status = infrav1.CloudStackMachineStateCheckerStatus{Ready: false}

if err := r.K8sClient.Create(r.RequestCtx, csMachineStateChecker); err != nil {
r.Recorder.Eventf(r.ReconciliationSubject, "Warning", "Machine State Checker", CSMachineStateCheckerCreationFailed)
return r.ReturnWrappedError(err, CSMachineStateCheckerCreationFailed)
}
r.Recorder.Eventf(r.ReconciliationSubject, "Normal", "Machine State Checker", CSMachineStateCheckerCreationSuccess)
} else {
r.Recorder.Eventf(r.ReconciliationSubject, "Warning", "Machine State Checker", CSMachineStateCheckerCreationFailed)

return ctrl.Result{}, errors.Wrap(err, CSMachineStateCheckerCreationFailed)
}
}
r.Recorder.Eventf(r.ReconciliationSubject, "Normal", "Machine State Checker", CSMachineStateCheckerCreationSuccess)
return r.GetObjectByName(*checkerName, r.StateChecker)()
r.StateChecker = csMachineStateChecker

return ctrl.Result{}, nil
}

func (r *CloudStackMachineReconciliationRunner) ReconcileDelete() (retRes ctrl.Result, reterr error) {
Expand Down

0 comments on commit 5835fce

Please sign in to comment.