Skip to content

Commit

Permalink
Make generating NotScale events configurable; use logs when disabled (#…
Browse files Browse the repository at this point in the history
…204)

* Make generating NotScale events configurable; use logs when disabled

* Adding status update along w/ log line

* removed info level log line
  • Loading branch information
levipe01 committed Mar 22, 2024
1 parent ee5c8ef commit 9adcbcf
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 6 deletions.
4 changes: 2 additions & 2 deletions api/v1alpha1/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ const (
ConditionReasonFailedGetResourceMetric = "FailedGetResourceMetric"
// ConditionValidMetricFound Condition when a valid metric is retrieved
ConditionValidMetricFound = "ValidMetricFound"
// CondistionReasonNotScaling Condition reason when not scaling
ConditionReasonNotScaling = "NotScaling"
// ReasonFailedSpecCheck Reason when the spec of the WPA is incorrect
ReasonFailedSpecCheck = "FailedSpecCheck"
// ReasonNotScaling Reason when not scaling
ReasonNotScaling = "NotScaling"
// ReasonScaling Reason when scaling
ReasonScaling = "Scaling"
// ReasonFailedScale Reason when unable to scale
Expand Down
12 changes: 11 additions & 1 deletion controllers/watermarkpodautoscaler_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ const (
desiredCountAcceptable = "the desired count is within the acceptable range"
)

type Options struct {
SkipNotScalingEvents bool
}

// WatermarkPodAutoscalerReconciler reconciles a WatermarkPodAutoscaler object
type WatermarkPodAutoscalerReconciler struct {
// This client, initialized using mgr.Client() above, is a split client
Expand All @@ -82,6 +86,7 @@ type WatermarkPodAutoscalerReconciler struct {
syncPeriod time.Duration
eventRecorder record.EventRecorder
replicaCalc ReplicaCalculatorItf
Options Options
}

// +kubebuilder:rbac:groups=apps;extensions,resources=deployments/finalizers,resourceNames=watermarkpodautoscalers,verbs=update
Expand Down Expand Up @@ -373,7 +378,12 @@ func (r *WatermarkPodAutoscalerReconciler) reconcileWPA(ctx context.Context, log

logger.Info("Successful rescale", "currentReplicas", currentReplicas, "desiredReplicas", desiredReplicas, "rescaleReason", rescaleReason)
} else {
r.eventRecorder.Eventf(wpa, corev1.EventTypeNormal, datadoghqv1alpha1.ReasonNotScaling, fmt.Sprintf("Decided not to scale %s to %d (last scale time was %v )", reference, desiredReplicas, wpa.Status.LastScaleTime))
if r.Options.SkipNotScalingEvents {
setCondition(wpa, autoscalingv2.ScalingActive, corev1.ConditionTrue, datadoghqv1alpha1.ConditionReasonNotScaling, "the WPA was able to successfully calculate a replica count and decided not to scale %s to %d (last scale time was %v )", reference, desiredReplicas, wpa.Status.LastScaleTime)
} else {
r.eventRecorder.Eventf(wpa, corev1.EventTypeNormal, datadoghqv1alpha1.ConditionReasonNotScaling, fmt.Sprintf("Decided not to scale %s to %d (last scale time was %v )", reference, desiredReplicas, wpa.Status.LastScaleTime))
}

desiredReplicas = currentReplicas
}

Expand Down
9 changes: 6 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ func main() {
var leaderElectionResourceLock string
var ddProfilingEnabled bool
var workers int
var skipNotScalingEvents bool
flag.BoolVar(&printVersionArg, "version", false, "print version and exit")
flag.StringVar(&metricsAddr, "metrics-addr", ":8080", "The address the metric endpoint binds to.")
flag.BoolVar(&enableLeaderElection, "enable-leader-election", true,
Expand All @@ -69,6 +70,7 @@ func main() {
flag.StringVar(&leaderElectionResourceLock, "leader-election-resource", "configmaps", "determines which resource lock to use for leader election. option:[configmapsleases|endpointsleases|configmaps]")
flag.BoolVar(&ddProfilingEnabled, "ddProfilingEnabled", false, "Enable the datadog profiler")
flag.IntVar(&workers, "workers", 1, "Maximum number of concurrent Reconciles which can be run")
flag.BoolVar(&skipNotScalingEvents, "skipNotScalingEvents", false, "Log NotScaling decisions instead of creating Kubernetes events")

logLevel := zap.LevelFlag("loglevel", zapcore.InfoLevel, "Set log level")

Expand Down Expand Up @@ -124,9 +126,10 @@ func main() {
klog.SetLogger(managerLogger) // Redirect klog to the controller logger (zap)

if err = (&controllers.WatermarkPodAutoscalerReconciler{
Client: mgr.GetClient(),
Log: managerLogger,
Scheme: mgr.GetScheme(),
Client: mgr.GetClient(),
Log: managerLogger,
Scheme: mgr.GetScheme(),
Options: controllers.Options{SkipNotScalingEvents: skipNotScalingEvents},
}).SetupWithManager(mgr, workers); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "WatermarkPodAutoscaler")
exitCode = 1
Expand Down

0 comments on commit 9adcbcf

Please sign in to comment.