Remove duplicated ksm metrics when self-deployed ksm clusterpodmonitor has duplicated entries as the gke managed one#1955
Conversation
There was a problem hiding this comment.
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.
| 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, | ||
| }) | ||
| } |
There was a problem hiding this comment.
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{ |
There was a problem hiding this comment.
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", |
There was a problem hiding this comment.
I guess the assumption is that user would somehow set this label?
Remove duplicated ksm metrics when self-deployed ksm clusterpodmonitor has duplicated entries as the gke managed one