Skip to content

Commit

Permalink
fix: Lower casing disruption count metric on reason label (#1862)
Browse files Browse the repository at this point in the history
  • Loading branch information
engedaam authored Dec 7, 2024
1 parent 8d819c1 commit 5531537
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 5 deletions.
5 changes: 3 additions & 2 deletions pkg/controllers/disruption/orchestration/queue.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import (
"sigs.k8s.io/karpenter/pkg/events"
"sigs.k8s.io/karpenter/pkg/metrics"
"sigs.k8s.io/karpenter/pkg/operator/injection"
"sigs.k8s.io/karpenter/pkg/utils/pretty"
)

const (
Expand Down Expand Up @@ -196,7 +197,7 @@ func (q *Queue) Reconcile(ctx context.Context) (reconcile.Result, error) {
})
DisruptionQueueFailuresTotal.Add(float64(len(failedLaunches)), map[string]string{
decisionLabel: cmd.Decision(),
metrics.ReasonLabel: string(cmd.reason),
metrics.ReasonLabel: pretty.ToSnakeCase(string(cmd.reason)),
consolidationTypeLabel: cmd.consolidationType,
})
multiErr := multierr.Combine(err, cmd.lastError, state.RequireNoScheduleTaint(ctx, q.kubeClient, false, cmd.candidates...))
Expand Down Expand Up @@ -265,7 +266,7 @@ func (q *Queue) waitOrTerminate(ctx context.Context, cmd *Command) error {
multiErr = multierr.Append(multiErr, client.IgnoreNotFound(err))
} else {
metrics.NodeClaimsDisruptedTotal.Inc(map[string]string{
metrics.ReasonLabel: string(cmd.reason),
metrics.ReasonLabel: pretty.ToSnakeCase(string(cmd.reason)),
metrics.NodePoolLabel: cmd.candidates[i].NodeClaim.Labels[v1.NodePoolLabelKey],
metrics.CapacityTypeLabel: cmd.candidates[i].NodeClaim.Labels[v1.CapacityTypeLabelKey],
})
Expand Down
3 changes: 2 additions & 1 deletion pkg/controllers/node/health/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import (
"sigs.k8s.io/karpenter/pkg/metrics"
"sigs.k8s.io/karpenter/pkg/operator/injection"
nodeutils "sigs.k8s.io/karpenter/pkg/utils/node"
"sigs.k8s.io/karpenter/pkg/utils/pretty"
)

var allowedUnhealthyPercent = intstr.FromString("20%")
Expand Down Expand Up @@ -126,7 +127,7 @@ func (c *Controller) Reconcile(ctx context.Context, node *corev1.Node) (reconcil
// The deletion timestamp has successfully been set for the Node, update relevant metrics.
log.FromContext(ctx).V(1).Info("deleting unhealthy node")
metrics.NodeClaimsDisruptedTotal.Inc(map[string]string{
metrics.ReasonLabel: string(unhealthyNodeCondition.Type),
metrics.ReasonLabel: pretty.ToSnakeCase(string(unhealthyNodeCondition.Type)),
metrics.NodePoolLabel: node.Labels[v1.NodePoolLabelKey],
metrics.CapacityTypeLabel: node.Labels[v1.CapacityTypeLabelKey],
})
Expand Down
3 changes: 2 additions & 1 deletion pkg/controllers/node/health/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import (
"sigs.k8s.io/karpenter/pkg/test"
. "sigs.k8s.io/karpenter/pkg/test/expectations"
"sigs.k8s.io/karpenter/pkg/test/v1alpha1"
"sigs.k8s.io/karpenter/pkg/utils/pretty"
. "sigs.k8s.io/karpenter/pkg/utils/testing"
)

Expand Down Expand Up @@ -341,7 +342,7 @@ var _ = Describe("Node Health", func() {
Expect(nodeClaim.DeletionTimestamp).ToNot(BeNil())

ExpectMetricCounterValue(metrics.NodeClaimsDisruptedTotal, 1, map[string]string{
metrics.ReasonLabel: string(cloudProvider.RepairPolicies()[0].ConditionType),
metrics.ReasonLabel: pretty.ToSnakeCase(string(cloudProvider.RepairPolicies()[0].ConditionType)),
metrics.NodePoolLabel: nodePool.Name,
})
})
Expand Down
3 changes: 2 additions & 1 deletion pkg/controllers/nodeclaim/expiration/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package expiration

import (
"context"
"strings"
"time"

"k8s.io/utils/clock"
Expand Down Expand Up @@ -75,7 +76,7 @@ func (c *Controller) Reconcile(ctx context.Context, nodeClaim *v1.NodeClaim) (re
// 4. The deletion timestamp has successfully been set for the NodeClaim, update relevant metrics.
log.FromContext(ctx).V(1).Info("deleting expired nodeclaim")
metrics.NodeClaimsDisruptedTotal.Inc(map[string]string{
metrics.ReasonLabel: metrics.ExpiredReason,
metrics.ReasonLabel: strings.ToLower(metrics.ExpiredReason),
metrics.NodePoolLabel: nodeClaim.Labels[v1.NodePoolLabelKey],
metrics.CapacityTypeLabel: nodeClaim.Labels[v1.CapacityTypeLabelKey],
})
Expand Down
10 changes: 10 additions & 0 deletions pkg/utils/pretty/pretty.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"bytes"
"encoding/json"
"fmt"
"regexp"
"strings"

"golang.org/x/exp/constraints"
Expand Down Expand Up @@ -85,3 +86,12 @@ func Taint(t v1.Taint) string {
}
return fmt.Sprintf("%s=%s:%s", t.Key, t.Value, t.Effect)
}

var matchFirstCap = regexp.MustCompile("(.)([A-Z][a-z]+)")
var matchAllCap = regexp.MustCompile("([a-z0-9])([A-Z])")

func ToSnakeCase(str string) string {
snake := matchFirstCap.ReplaceAllString(str, "${1}_${2}")
snake = matchAllCap.ReplaceAllString(snake, "${1}_${2}")
return strings.ToLower(snake)
}
38 changes: 38 additions & 0 deletions pkg/utils/pretty/suite_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
Copyright The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package pretty_test

import (
"testing"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"

"sigs.k8s.io/karpenter/pkg/utils/pretty"
)

func TestOperator(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Pretty Utils")
}

var _ = Describe("Pretty", func() {
It("should convert camel case to snake case", func() {
camelCase := "testKeyValue"
Expect(pretty.ToSnakeCase(camelCase)).To(Equal("test_key_value"))
})
})

0 comments on commit 5531537

Please sign in to comment.