Skip to content

Commit 5da8635

Browse files
pkg/collector/corechecks/cluster/ksm: optimize ownerTags
* optimize allocations * check ReplicaSet name pattern used by modern Kubernetes first * remove redundant Job index pattern check (name can not contain '+', earlier dash index ensures suffix does not start with '-' and strconv.Atoi only allows '[0-9]') ``` goos: darwin goarch: arm64 pkg: github.com/DataDog/datadog-agent/pkg/collector/corechecks/cluster/ksm cpu: Apple M4 Max │ HEAD~1 │ HEAD │ │ sec/op │ sec/op vs base │ OwnerTags/ReplicaSet-16 106.70n ± 1% 80.68n ± 1% -24.38% (p=0.000 n=10) OwnerTags/Job-16 96.45n ± 1% 59.68n ± 1% -38.13% (p=0.000 n=10) geomean 101.4n 69.39n -31.60% │ HEAD~1 │ HEAD │ │ B/op │ B/op vs base │ OwnerTags/ReplicaSet-16 104.00 ± 0% 88.00 ± 0% -15.38% (p=0.000 n=10) OwnerTags/Job-16 88.00 ± 0% 72.00 ± 0% -18.18% (p=0.000 n=10) geomean 95.67 79.60 -16.79% │ HEAD~1 │ HEAD │ │ allocs/op │ allocs/op vs base │ OwnerTags/ReplicaSet-16 4.000 ± 0% 3.000 ± 0% -25.00% (p=0.000 n=10) OwnerTags/Job-16 4.000 ± 0% 3.000 ± 0% -25.00% (p=0.000 n=10) geomean 4.000 3.000 -25.00% ``` Follow up on #43407
1 parent 366b402 commit 5da8635

File tree

2 files changed

+6
-12
lines changed

2 files changed

+6
-12
lines changed

pkg/collector/corechecks/cluster/ksm/kubernetes_state.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1309,19 +1309,18 @@ func ownerTags(kind, name string) []string {
13091309
return nil
13101310
}
13111311

1312-
tagList := []string{tagKey + ":" + name}
13131312
switch kind {
13141313
case kubernetes.JobKind:
13151314
if cronjob, _ := kubernetes.ParseCronJobForJob(name); cronjob != "" {
1316-
return append(tagList, tags.KubeCronjob+":"+cronjob)
1315+
return []string{tagKey + ":" + name, tags.KubeCronjob + ":" + cronjob}
13171316
}
13181317
case kubernetes.ReplicaSetKind:
13191318
if deployment := kubernetes.ParseDeploymentForReplicaSet(name); deployment != "" {
1320-
return append(tagList, tags.KubeDeployment+":"+deployment)
1319+
return []string{tagKey + ":" + name, tags.KubeDeployment + ":" + deployment}
13211320
}
13221321
}
13231322

1324-
return tagList
1323+
return []string{tagKey + ":" + name}
13251324
}
13261325

13271326
// labelsMapperOverride allows overriding the default label mapping for

pkg/util/kubernetes/helpers.go

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import (
1414
// Taken from https://github.com/kow3ns/kubernetes/blob/96067e6d7b24a05a6a68a0d94db622957448b5ab/staging/src/k8s.io/apimachinery/pkg/util/rand/rand.go#L76
1515
const KubeAllowedEncodeStringAlphaNums = "bcdfghjklmnpqrstvwxz2456789"
1616

17-
// Digits holds the digits used for naming replicasets in kubenetes < 1.8
17+
// Digits holds the digits used for naming replicasets in kubernetes < 1.8
1818
const Digits = "1234567890"
1919

2020
// ParseDeploymentForReplicaSet gets the deployment name from a replicaset,
@@ -54,14 +54,9 @@ func ParseCronJobForJob(name string) (string, int) {
5454
return "", 0
5555
}
5656

57-
if !stringInRuneset(suffix, Digits) {
58-
// Invalid suffix
59-
return "", 0
60-
}
61-
6257
id, err := strconv.Atoi(suffix)
6358
if err != nil {
64-
// Cannot happen because of the test just above
59+
// Invalid suffix
6560
return "", 0
6661
}
6762

@@ -94,7 +89,7 @@ func removeKubernetesNameSuffix(name string) string {
9489
return ""
9590
}
9691

97-
if !stringInRuneset(suffix, Digits) && !stringInRuneset(suffix, KubeAllowedEncodeStringAlphaNums) {
92+
if !stringInRuneset(suffix, KubeAllowedEncodeStringAlphaNums) && !stringInRuneset(suffix, Digits) {
9893
// Invalid suffix
9994
return ""
10095
}

0 commit comments

Comments
 (0)