Skip to content

Commit faac375

Browse files
committed
MON-4115: expose label metrics for jobs and cronjobs
Adds `AdditionalLabelsAllowList` to KSM config. Signed-off-by: Pranshu Srivastava <[email protected]>
1 parent e7abc6a commit faac375

File tree

4 files changed

+80
-0
lines changed

4 files changed

+80
-0
lines changed

Documentation/api.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@ The `KubeStateMetricsConfig` resource defines settings for the `kube-state-metri
174174
| resources | *[v1.ResourceRequirements](https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.33/#resourcerequirements-v1-core) | Defines resource requests and limits for the KubeStateMetrics container. |
175175
| tolerations | [][v1.Toleration](https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.33/#toleration-v1-core) | Defines tolerations for the pods. |
176176
| topologySpreadConstraints | []v1.TopologySpreadConstraint | Defines a pod's topology spread constraints. |
177+
| additionalLabelsAllowList | *string | Defines any additional resource-based label-metrics' allow listing in addition to the default one. Refer: https://pkg.go.dev/k8s.io/kube-state-metrics/[email protected]/pkg/options#LabelsAllowList |
177178

178179
[Back to TOC](#table-of-contents)
179180

Documentation/openshiftdocs/modules/kubestatemetricsconfig.adoc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ Appears in: link:clustermonitoringconfiguration.adoc[ClusterMonitoringConfigurat
2626

2727
|topologySpreadConstraints|[]v1.TopologySpreadConstraint|Defines a pod's topology spread constraints.
2828

29+
|additionalLabelsAllowList|*string|Defines any additional resource-based label-metrics' allow listing in addition to the default one. Refer: https://pkg.go.dev/k8s.io/kube-state-metrics/[email protected]/pkg/options#LabelsAllowList
30+
2931
|===
3032

3133
link:../index.adoc[Back to TOC]

pkg/manifests/manifests.go

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -760,6 +760,18 @@ func (f *Factory) KubeStateMetricsDeployment() (*appsv1.Deployment, error) {
760760
if f.config.ClusterMonitoringConfiguration.KubeStateMetricsConfig.Resources != nil {
761761
d.Spec.Template.Spec.Containers[i].Resources = *f.config.ClusterMonitoringConfiguration.KubeStateMetricsConfig.Resources
762762
}
763+
additionalAllowList := f.config.ClusterMonitoringConfiguration.KubeStateMetricsConfig.AdditionalLabelsAllowList
764+
if additionalAllowList != nil && *additionalAllowList != "" {
765+
err = setLabelsAllowList(*additionalAllowList)
766+
if err != nil {
767+
return nil, fmt.Errorf("error parsing allowlist: %w", err)
768+
}
769+
for i = range container.Args {
770+
if strings.HasPrefix(container.Args[i], "--metric-labels-allowlist=") {
771+
container.Args[i] += "," + *additionalAllowList
772+
}
773+
}
774+
}
763775
}
764776
}
765777

@@ -3614,3 +3626,65 @@ func hashStringMap(m map[string]string) string {
36143626
}
36153627
return hashByteMap(byteMap)
36163628
}
3629+
3630+
func setLabelsAllowList(value string) error {
3631+
var errLabelsAllowListFormat = errors.New("invalid format, should be: metric1=[label1,label2,labeln...],...,metricN=[...]")
3632+
3633+
// Taken from text/scanner EOF constant.
3634+
const EOF = -1
3635+
var (
3636+
m = map[string][]string{}
3637+
previous rune
3638+
next rune
3639+
firstWordPos int
3640+
name string
3641+
)
3642+
firstWordPos = 0
3643+
3644+
for i, v := range value {
3645+
if i+1 == len(value) {
3646+
next = EOF
3647+
} else {
3648+
next = []rune(value)[i+1]
3649+
}
3650+
if i-1 >= 0 {
3651+
previous = []rune(value)[i-1]
3652+
} else {
3653+
previous = v
3654+
}
3655+
3656+
switch v {
3657+
case '=':
3658+
if previous == ',' || next != '[' {
3659+
return errLabelsAllowListFormat
3660+
}
3661+
name = strings.TrimSpace(string([]rune(value)[firstWordPos:i]))
3662+
m[name] = []string{}
3663+
firstWordPos = i + 1
3664+
case '[':
3665+
if previous != '=' {
3666+
return errLabelsAllowListFormat
3667+
}
3668+
firstWordPos = i + 1
3669+
case ']':
3670+
// if after metric group, has char not comma or end.
3671+
if next != EOF && next != ',' {
3672+
return errLabelsAllowListFormat
3673+
}
3674+
if previous != '[' {
3675+
m[name] = append(m[name], strings.TrimSpace(string(([]rune(value)[firstWordPos:i]))))
3676+
}
3677+
firstWordPos = i + 1
3678+
case ',':
3679+
// if starts or ends with comma
3680+
if previous == v || next == EOF || next == ']' {
3681+
return errLabelsAllowListFormat
3682+
}
3683+
if previous != ']' {
3684+
m[name] = append(m[name], strings.TrimSpace(string(([]rune(value)[firstWordPos:i]))))
3685+
}
3686+
firstWordPos = i + 1
3687+
}
3688+
}
3689+
return nil
3690+
}

pkg/manifests/types.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,9 @@ type KubeStateMetricsConfig struct {
175175
Tolerations []v1.Toleration `json:"tolerations,omitempty"`
176176
// Defines a pod's topology spread constraints.
177177
TopologySpreadConstraints []v1.TopologySpreadConstraint `json:"topologySpreadConstraints,omitempty"`
178+
// Defines any additional resource-based label-metrics' allow listing in addition to the default one.
179+
// Refer: https://pkg.go.dev/k8s.io/kube-state-metrics/[email protected]/pkg/options#LabelsAllowList
180+
AdditionalLabelsAllowList *string `json:"additionalLabelsAllowList,omitempty"`
178181
}
179182

180183
// The `PrometheusK8sConfig` resource defines settings for the Prometheus

0 commit comments

Comments
 (0)