diff --git a/controllers/ratio_controller.go b/controllers/ratio_controller.go index 14a985e..d28576f 100644 --- a/controllers/ratio_controller.go +++ b/controllers/ratio_controller.go @@ -52,8 +52,6 @@ func (r *RatioReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl } for nodeSel, ratio := range nsRatios { - // TODO() add nodeSel to warning message - sel, err := labels.ConvertSelectorToLabelsMap(nodeSel) if err != nil { return ctrl.Result{}, fmt.Errorf("failed to convert node selector '%s' to labels map: %w", nodeSel, err) @@ -67,10 +65,10 @@ func (r *RatioReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl if ratio.Below(*limit) { l.Info("recording warn event: ratio too low") - if err := r.warnPod(ctx, req.Name, req.Namespace, ratio, limit); err != nil { + if err := r.warnPod(ctx, req.Name, req.Namespace, ratio, nodeSel, limit); err != nil { l.Error(err, "failed to record event on pod") } - if err := r.warnNamespace(ctx, req.Namespace, ratio, limit); err != nil { + if err := r.warnNamespace(ctx, req.Namespace, ratio, nodeSel, limit); err != nil { l.Error(err, "failed to record event on namespace") } } @@ -79,7 +77,7 @@ func (r *RatioReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl return ctrl.Result{}, nil } -func (r *RatioReconciler) warnPod(ctx context.Context, name, namespace string, nsRatio *ratio.Ratio, limit *resource.Quantity) error { +func (r *RatioReconciler) warnPod(ctx context.Context, name, namespace string, nsRatio *ratio.Ratio, sel string, limit *resource.Quantity) error { pod := corev1.Pod{} err := r.Get(ctx, client.ObjectKey{ Namespace: namespace, @@ -88,10 +86,10 @@ func (r *RatioReconciler) warnPod(ctx context.Context, name, namespace string, n if err != nil { return err } - r.Recorder.Event(&pod, "Warning", eventReason, nsRatio.Warn(limit)) + r.Recorder.Event(&pod, "Warning", eventReason, nsRatio.Warn(limit, sel)) return nil } -func (r *RatioReconciler) warnNamespace(ctx context.Context, name string, nsRatio *ratio.Ratio, limit *resource.Quantity) error { +func (r *RatioReconciler) warnNamespace(ctx context.Context, name string, nsRatio *ratio.Ratio, sel string, limit *resource.Quantity) error { ns := corev1.Namespace{} err := r.Get(ctx, client.ObjectKey{ Name: name, @@ -99,7 +97,7 @@ func (r *RatioReconciler) warnNamespace(ctx context.Context, name string, nsRati if err != nil { return err } - r.Recorder.Event(&ns, "Warning", eventReason, nsRatio.Warn(limit)) + r.Recorder.Event(&ns, "Warning", eventReason, nsRatio.Warn(limit, sel)) return nil } diff --git a/ratio/ratio.go b/ratio/ratio.go index 558a1a1..49bde57 100644 --- a/ratio/ratio.go +++ b/ratio/ratio.go @@ -102,9 +102,13 @@ func (r Ratio) String() string { } // Warn returns a warning string explaining that the ratio is not considered fair use -func (r Ratio) Warn(limit *resource.Quantity) string { +func (r Ratio) Warn(limit *resource.Quantity, nodeSelector string) string { // WARNING(glrf) Warnings MUST NOT contain newlines. K8s will simply drop your warning if you add newlines. - w := fmt.Sprintf("Current memory to CPU ratio of %s/core in this namespace is below the fair use ratio", r.String()) + w := fmt.Sprintf("Current memory to CPU ratio of %s/core", r.String()) + if nodeSelector != "" { + w = fmt.Sprintf("%s for node type %q", w, nodeSelector) + } + w = fmt.Sprintf("%s in this namespace is below the fair use ratio", w) if limit != nil { w = fmt.Sprintf("%s of %s/core", w, limit) } diff --git a/ratio/ratio_test.go b/ratio/ratio_test.go index 61cd422..0518355 100644 --- a/ratio/ratio_test.go +++ b/ratio/ratio_test.go @@ -288,9 +288,11 @@ func TestRatio_Warn(t *testing.T) { CPU: cpu.AsDec(), Memory: memory.AsDec(), } - assert.Contains(t, r.Warn(nil), "1Gi") + assert.Contains(t, r.Warn(nil, ""), "1Gi") lim := resource.MustParse("1Mi") - assert.Contains(t, r.Warn(&lim), "1Mi") + assert.Contains(t, r.Warn(&lim, ""), "1Mi") + + assert.Contains(t, r.Warn(&lim, "class=x"), "class=x") } func FuzzRatio(f *testing.F) { @@ -305,7 +307,7 @@ func FuzzRatio(f *testing.F) { Memory: memQuant.AsDec(), } lim := resource.MustParse(fmt.Sprintf("%dMi", limit)) - out := r.Warn(&lim) + out := r.Warn(&lim, "") assert.NotEmpty(t, out) r.Below(lim) diff --git a/webhooks/ratio_validator.go b/webhooks/ratio_validator.go index d63655b..810d16c 100644 --- a/webhooks/ratio_validator.go +++ b/webhooks/ratio_validator.go @@ -119,7 +119,7 @@ func (v *RatioValidator) Handle(ctx context.Context, req admission.Request) admi if r.Below(*limit) { l.Info("ratio too low", "node_selector", nodeSel, "ratio", r) - warnings = append(warnings, r.Warn(limit)) + warnings = append(warnings, r.Warn(limit, nodeSel)) } }