Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Use dlclark/regexp2 over standard library's package #2616

Merged
merged 1 commit into from
Mar 19, 2025

Conversation

rexagod
Copy link
Member

@rexagod rexagod commented Feb 23, 2025

Fixes #2594.

Please refer to the aforementioned issue for more details.


┌[rexagod@nebuchadnezzar] [/dev/ttys002] [2594] [1]
└[~/repositories/oss/kube-state-metrics]> go run . --kubeconfig=$KUBECONFIG --metric-allowlist='^kube_(?=namespace).*_created$'
I0310 18:27:57.420618   22613 wrapper.go:120] "Starting kube-state-metrics"
I0310 18:27:57.425000   22613 server.go:194] "Used default resources"
I0310 18:27:57.425050   22613 types.go:195] "Using all namespaces"
I0310 18:27:57.425159   22613 server.go:233] "Metric allow-denylisting" allowDenyStatus="Including the following lists that were on allowlist: ^kube_(?=namespace).*_created$"
I0310 18:27:57.429918   22613 utils.go:70] "Tested communication with server"
I0310 18:27:57.442175   22613 utils.go:75] "Run with Kubernetes cluster version" major="1" minor="31" gitVersion="v1.31.0" gitTreeState="clean" gitCommit="9edcffcde5595e8a5b1a35f88c421764e575afce" platform="linux/arm64"
I0310 18:27:57.442195   22613 utils.go:76] "Communication with server successful"
I0310 18:27:57.442674   22613 server.go:350] "Started kube-state-metrics self metrics server" telemetryAddress="[::]:8081"
I0310 18:27:57.442669   22613 server.go:361] "Started metrics server" metricsServerAddress="[::]:8080"
I0310 18:27:57.442711   22613 metrics_handler.go:110] "Autosharding disabled"
I0310 18:27:57.442958   22613 tls_config.go:347] "Listening on" address="[::]:8080"
I0310 18:27:57.443027   22613 tls_config.go:350] "TLS is disabled." http2=false address="[::]:8080"
I0310 18:27:57.442920   22613 tls_config.go:347] "Listening on" address="[::]:8081"
I0310 18:27:57.443087   22613 tls_config.go:350] "TLS is disabled." http2=false address="[::]:8081"
I0310 18:27:57.445301   22613 builder.go:283] "Active resources" activeStoreNames="certificatesigningrequests,configmaps,cronjobs,daemonsets,deployments,endpoints,horizontalpodautoscalers,ingresses,jobs,leases,limitranges,mutatingwebhookconfigurations,namespaces,networkpolicies,nodes,persistentvolumeclaims,persistentvolumes,poddisruptionbudgets,pods,replicasets,replicationcontrollers,resourcequotas,secrets,services,statefulsets,storageclasses,validatingwebhookconfigurations,volumeattachments"
┌[rexagod@nebuchadnezzar] [/dev/ttys003]
└[~]> curl -s localhost:8080/metrics | grep 'kube_.*_created'
# HELP kube_namespace_created [STABLE] Unix creation timestamp
# TYPE kube_namespace_created gauge
kube_namespace_created{namespace="default"} 1.741605321e+09
kube_namespace_created{namespace="kube-node-lease"} 1.741605321e+09
kube_namespace_created{namespace="kube-public"} 1.741605321e+09
kube_namespace_created{namespace="kube-system"} 1.741605321e+09
kube_namespace_created{namespace="local-path-storage"} 1.741605326e+09

@k8s-ci-robot k8s-ci-robot added do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. labels Feb 23, 2025
@k8s-ci-robot k8s-ci-robot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Feb 23, 2025
@k8s-ci-robot
Copy link
Contributor

This issue is currently awaiting triage.

If kube-state-metrics contributors determine this is a relevant issue, they will accept it by applying the triage/accepted label and provide further guidance.

The triage/accepted label can be added by org members by writing /triage accepted in a comment.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.

@k8s-ci-robot k8s-ci-robot added needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one. size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels Feb 23, 2025
@rexagod rexagod force-pushed the 2594 branch 2 times, most recently from 2c20c68 to 76d3788 Compare March 10, 2025 11:10
@rexagod rexagod marked this pull request as ready for review March 10, 2025 11:16
@k8s-ci-robot k8s-ci-robot removed the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. label Mar 10, 2025
// Use ECMAScript as the default regexp spec to support lookarounds (#2594).
var regexpDefaultSpec regexp.RegexOptions = regexp.ECMAScript

func init() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we not make use of an init() function here and instead wrap the regexp.Compile into a function that sets this?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd prefer to initialize upstream package variables at downstream package init()-time, is this an anti-pattern? This should also help avoid missing out on setting such values in any new functions in the future that may need to initialize the same.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think in general init() is a bad pattern that we should avoid introducing because of side effects, import order etc.
Can we add this to the New() func instead?
https://github.com/kubernetes/kube-state-metrics/pull/2616/files#diff-b14b1102ef04d884e648b90166b5d35ee63edac2a544ef311770f5f289df48f4L36

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, PTAL.

@rexagod rexagod force-pushed the 2594 branch 2 times, most recently from 772822d to e8b298b Compare March 10, 2025 12:52
@@ -124,22 +140,38 @@ func TestInclude(t *testing.T) {
t.Fatal("expected New() to not fail")
}

denylist.Exclude([]string{"kube_node_.*_cores", "kube_pod_.*_bytes"})
denylist.Exclude([]string{"kube_(?=node.*cores|pod.*bytes)"})
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it compatible with "denylist.Exclude([]string{"kube_node_.cores", "kube_pod._bytes"})"?

Lookaround feature is more complicated. I will prefer using the legacy one unless performance is much better.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is. The library being introduced here is a superset of go's regexp one, and will only incur expensive perf operations for exponential matches, that go's library doesn't support since it has to guarantee linear-time operations.

Copy link
Member Author

@rexagod rexagod Mar 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also these operations are capped at a minute, so no leaks.

@k8s-ci-robot k8s-ci-robot added the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Mar 18, 2025
@rexagod rexagod requested a review from mrueg March 18, 2025 08:19
@mrueg
Copy link
Member

mrueg commented Mar 18, 2025

Changes look good to me, thanks for the work. One small ask: Can you add some more documentation to the README on how to use this? Thanks!

@@ -58,7 +58,7 @@ Flags:
--logtostderr log to standard error instead of files (default true)
--metric-allowlist string Comma-separated list of metrics to be exposed. This list comprises of exact metric names and/or regex patterns. The allowlist and denylist are mutually exclusive.
--metric-annotations-allowlist string Comma-separated list of Kubernetes annotations keys that will be used in the resource' labels metric. By default the annotations metrics are not exposed. To include them, provide a list of resource names in their plural form and Kubernetes annotation keys you would like to allow for them (Example: '=namespaces=[kubernetes.io/team,...],pods=[kubernetes.io/team],...)'. A single '*' can be provided per resource instead to allow any annotations, but that has severe performance implications (Example: '=pods=[*]').
--metric-denylist string Comma-separated list of metrics not to be enabled. This list comprises of exact metric names and/or regex patterns. The allowlist and denylist are mutually exclusive.
--metric-denylist string Comma-separated list of metrics not to be enabled. This list comprises of exact metric names and/or *ECMAScript-based* regex patterns. The allowlist and denylist are mutually exclusive.
--metric-labels-allowlist string Comma-separated list of additional Kubernetes label keys that will be used in the resource' labels metric. By default the labels metrics are not exposed. To include them, provide a list of resource names in their plural form and Kubernetes label keys you would like to allow for them (Example: '=namespaces=[k8s-label-1,k8s-label-n,...],pods=[app],...)'. A single '*' can be provided per resource instead to allow any labels, but that has severe performance implications (Example: '=pods=[*]'). Additionally, an asterisk (*) can be provided as a key, which will resolve to all resources, i.e., assuming '--resources=deployments,pods', '=*=[*]' will resolve to '=deployments=[*],pods=[*]'.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you want to extend this to the labels and annotations allowlists in a later PR?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd like to keep this exclusive to the allowdeny lists for now, and expand once we start to see positive results in the near future.

@rexagod rexagod force-pushed the 2594 branch 2 times, most recently from 7219efe to aab4eb5 Compare March 18, 2025 10:22
@k8s-ci-robot k8s-ci-robot removed the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Mar 18, 2025
@mrueg
Copy link
Member

mrueg commented Mar 19, 2025

/lgtm

Thanks for your work on this!

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Mar 19, 2025
@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: mrueg, rexagod

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@k8s-ci-robot k8s-ci-robot merged commit 5f11691 into kubernetes:main Mar 19, 2025
12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. lgtm "Looks good to me", indicates that a PR is ready to be merged. needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one. size/L Denotes a PR that changes 100-499 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support lookarounds for allow and deny lists?
4 participants