Skip to content

Commit 5531537

Browse files
authored
fix: Lower casing disruption count metric on reason label (#1862)
1 parent 8d819c1 commit 5531537

File tree

6 files changed

+57
-5
lines changed

6 files changed

+57
-5
lines changed

pkg/controllers/disruption/orchestration/queue.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ import (
4444
"sigs.k8s.io/karpenter/pkg/events"
4545
"sigs.k8s.io/karpenter/pkg/metrics"
4646
"sigs.k8s.io/karpenter/pkg/operator/injection"
47+
"sigs.k8s.io/karpenter/pkg/utils/pretty"
4748
)
4849

4950
const (
@@ -196,7 +197,7 @@ func (q *Queue) Reconcile(ctx context.Context) (reconcile.Result, error) {
196197
})
197198
DisruptionQueueFailuresTotal.Add(float64(len(failedLaunches)), map[string]string{
198199
decisionLabel: cmd.Decision(),
199-
metrics.ReasonLabel: string(cmd.reason),
200+
metrics.ReasonLabel: pretty.ToSnakeCase(string(cmd.reason)),
200201
consolidationTypeLabel: cmd.consolidationType,
201202
})
202203
multiErr := multierr.Combine(err, cmd.lastError, state.RequireNoScheduleTaint(ctx, q.kubeClient, false, cmd.candidates...))
@@ -265,7 +266,7 @@ func (q *Queue) waitOrTerminate(ctx context.Context, cmd *Command) error {
265266
multiErr = multierr.Append(multiErr, client.IgnoreNotFound(err))
266267
} else {
267268
metrics.NodeClaimsDisruptedTotal.Inc(map[string]string{
268-
metrics.ReasonLabel: string(cmd.reason),
269+
metrics.ReasonLabel: pretty.ToSnakeCase(string(cmd.reason)),
269270
metrics.NodePoolLabel: cmd.candidates[i].NodeClaim.Labels[v1.NodePoolLabelKey],
270271
metrics.CapacityTypeLabel: cmd.candidates[i].NodeClaim.Labels[v1.CapacityTypeLabelKey],
271272
})

pkg/controllers/node/health/controller.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ import (
4141
"sigs.k8s.io/karpenter/pkg/metrics"
4242
"sigs.k8s.io/karpenter/pkg/operator/injection"
4343
nodeutils "sigs.k8s.io/karpenter/pkg/utils/node"
44+
"sigs.k8s.io/karpenter/pkg/utils/pretty"
4445
)
4546

4647
var allowedUnhealthyPercent = intstr.FromString("20%")
@@ -126,7 +127,7 @@ func (c *Controller) Reconcile(ctx context.Context, node *corev1.Node) (reconcil
126127
// The deletion timestamp has successfully been set for the Node, update relevant metrics.
127128
log.FromContext(ctx).V(1).Info("deleting unhealthy node")
128129
metrics.NodeClaimsDisruptedTotal.Inc(map[string]string{
129-
metrics.ReasonLabel: string(unhealthyNodeCondition.Type),
130+
metrics.ReasonLabel: pretty.ToSnakeCase(string(unhealthyNodeCondition.Type)),
130131
metrics.NodePoolLabel: node.Labels[v1.NodePoolLabelKey],
131132
metrics.CapacityTypeLabel: node.Labels[v1.CapacityTypeLabelKey],
132133
})

pkg/controllers/node/health/suite_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ import (
3838
"sigs.k8s.io/karpenter/pkg/test"
3939
. "sigs.k8s.io/karpenter/pkg/test/expectations"
4040
"sigs.k8s.io/karpenter/pkg/test/v1alpha1"
41+
"sigs.k8s.io/karpenter/pkg/utils/pretty"
4142
. "sigs.k8s.io/karpenter/pkg/utils/testing"
4243
)
4344

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

343344
ExpectMetricCounterValue(metrics.NodeClaimsDisruptedTotal, 1, map[string]string{
344-
metrics.ReasonLabel: string(cloudProvider.RepairPolicies()[0].ConditionType),
345+
metrics.ReasonLabel: pretty.ToSnakeCase(string(cloudProvider.RepairPolicies()[0].ConditionType)),
345346
metrics.NodePoolLabel: nodePool.Name,
346347
})
347348
})

pkg/controllers/nodeclaim/expiration/controller.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package expiration
1818

1919
import (
2020
"context"
21+
"strings"
2122
"time"
2223

2324
"k8s.io/utils/clock"
@@ -75,7 +76,7 @@ func (c *Controller) Reconcile(ctx context.Context, nodeClaim *v1.NodeClaim) (re
7576
// 4. The deletion timestamp has successfully been set for the NodeClaim, update relevant metrics.
7677
log.FromContext(ctx).V(1).Info("deleting expired nodeclaim")
7778
metrics.NodeClaimsDisruptedTotal.Inc(map[string]string{
78-
metrics.ReasonLabel: metrics.ExpiredReason,
79+
metrics.ReasonLabel: strings.ToLower(metrics.ExpiredReason),
7980
metrics.NodePoolLabel: nodeClaim.Labels[v1.NodePoolLabelKey],
8081
metrics.CapacityTypeLabel: nodeClaim.Labels[v1.CapacityTypeLabelKey],
8182
})

pkg/utils/pretty/pretty.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"bytes"
2121
"encoding/json"
2222
"fmt"
23+
"regexp"
2324
"strings"
2425

2526
"golang.org/x/exp/constraints"
@@ -85,3 +86,12 @@ func Taint(t v1.Taint) string {
8586
}
8687
return fmt.Sprintf("%s=%s:%s", t.Key, t.Value, t.Effect)
8788
}
89+
90+
var matchFirstCap = regexp.MustCompile("(.)([A-Z][a-z]+)")
91+
var matchAllCap = regexp.MustCompile("([a-z0-9])([A-Z])")
92+
93+
func ToSnakeCase(str string) string {
94+
snake := matchFirstCap.ReplaceAllString(str, "${1}_${2}")
95+
snake = matchAllCap.ReplaceAllString(snake, "${1}_${2}")
96+
return strings.ToLower(snake)
97+
}

pkg/utils/pretty/suite_test.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
Copyright The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package pretty_test
18+
19+
import (
20+
"testing"
21+
22+
. "github.com/onsi/ginkgo/v2"
23+
. "github.com/onsi/gomega"
24+
25+
"sigs.k8s.io/karpenter/pkg/utils/pretty"
26+
)
27+
28+
func TestOperator(t *testing.T) {
29+
RegisterFailHandler(Fail)
30+
RunSpecs(t, "Pretty Utils")
31+
}
32+
33+
var _ = Describe("Pretty", func() {
34+
It("should convert camel case to snake case", func() {
35+
camelCase := "testKeyValue"
36+
Expect(pretty.ToSnakeCase(camelCase)).To(Equal("test_key_value"))
37+
})
38+
})

0 commit comments

Comments
 (0)