Skip to content

Commit

Permalink
Add node type to message
Browse files Browse the repository at this point in the history
  • Loading branch information
bastjan committed Nov 24, 2022
1 parent e0898e1 commit 54a8f06
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 14 deletions.
14 changes: 6 additions & 8 deletions controllers/ratio_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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")
}
}
Expand All @@ -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,
Expand All @@ -88,18 +86,18 @@ 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,
}, &ns)
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
}

Expand Down
8 changes: 6 additions & 2 deletions ratio/ratio.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
8 changes: 5 additions & 3 deletions ratio/ratio_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion webhooks/ratio_validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
}

Expand Down

0 comments on commit 54a8f06

Please sign in to comment.