Skip to content

Remove duplicated ksm metrics when self-deployed ksm clusterpodmonitor has duplicated entries as the gke managed one#1955

Open
CatherineF-dev wants to merge 1 commit into
GoogleCloudPlatform:mainfrom
CatherineF-dev:main2
Open

Remove duplicated ksm metrics when self-deployed ksm clusterpodmonitor has duplicated entries as the gke managed one#1955
CatherineF-dev wants to merge 1 commit into
GoogleCloudPlatform:mainfrom
CatherineF-dev:main2

Conversation

@CatherineF-dev

@CatherineF-dev CatherineF-dev commented Jun 9, 2026

Copy link
Copy Markdown

Remove duplicated ksm metrics when self-deployed ksm clusterpodmonitor has duplicated entries as the gke managed one

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Code Review

This pull request introduces a deduplication mechanism (deduplicateKSM) to resolve duplicate metric collection between GKE's managed kube-state-metrics and self-deployed customer kube-state-metrics, along with comprehensive unit tests. Feedback is provided regarding an issue where deduplication drop rules are only applied to the first endpoint of the customer's KSM; looping over all endpoints is recommended to ensure robust deduplication.

Comment thread pkg/operator/collection.go Outdated
Comment on lines +554 to +564
newEp := &custCopy.Spec.Endpoints[0]

if len(gkeKeepRules) > 0 {
// GKE has allowlist. Drop these metrics from Customer KSM.
for _, kr := range gkeKeepRules {
newEp.MetricRelabeling = append(newEp.MetricRelabeling, monitoringv1.RelabelingRule{
Action: "drop",
SourceLabels: []string{"__name__"},
Regex: kr.Regex,
})
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

The current implementation only applies the deduplication drop rules to the first endpoint (Endpoints[0]) of the customer's self-deployed KSM. If the customer's ClusterPodMonitoring resource defines multiple endpoints, any subsequent endpoints will not receive these drop rules, potentially leading to duplicate metric collection.

We should loop over all endpoints in custCopy.Spec.Endpoints and apply the drop rules to each of them to ensure robust deduplication.

		if len(gkeKeepRules) > 0 {
			// GKE has allowlist. Drop these metrics from Customer KSM.
			for i := range custCopy.Spec.Endpoints {
				ep := &custCopy.Spec.Endpoints[i]
				for _, kr := range gkeKeepRules {
					ep.MetricRelabeling = append(ep.MetricRelabeling, monitoringv1.RelabelingRule{
						Action:       "drop",
						SourceLabels: []string{"__name__"},
						Regex:        kr.Regex,
					})
				}
			}
		}

…r has duplicated entries as the gke managed one
// GKE has allowlist. Drop these metrics from Customer KSM.
epCopy := custEp.DeepCopy()
for _, kr := range gkeKeepRules {
epCopy.MetricRelabeling = append(epCopy.MetricRelabeling, monitoringv1.RelabelingRule{

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

We can't modify user configs at the moment

"app.kubernetes.io/name": "gke-managed-kube-state-metrics",
}
customerLabels := map[string]string{
"app.kubernetes.io/name": "self-deployed-kube-state-metrics",

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I guess the assumption is that user would somehow set this label?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants