From 359c65789385496661c537df94c3f600ce2af8e2 Mon Sep 17 00:00:00 2001 From: Esonhugh Date: Mon, 2 Dec 2024 23:28:46 +0800 Subject: [PATCH] update: label of Resource has same key but diff value --- pkg/metrics/matcher.go | 2 +- pkg/metrics/metrics_test.go | 2 +- pkg/metrics/resource.go | 38 ++++++++++++++++++++----------------- 3 files changed, 23 insertions(+), 19 deletions(-) diff --git a/pkg/metrics/matcher.go b/pkg/metrics/matcher.go index ff5a9ba..edc55a5 100644 --- a/pkg/metrics/matcher.go +++ b/pkg/metrics/matcher.go @@ -28,7 +28,7 @@ func DefaultMatchRules() MatchRules { AddLabel("namespace").AddLabel("pod").AddLabel("service_account"), NewMetricMatcher("service").AddLabel("namespace").AddLabel("service"). - AddLabel("cluster_ip"), + AddLabel("cluster_ip").AddLabel("external_name").AddLabel("load_balancer_ip"), NewMetricMatcher("endpoint_address").SetHeader("kube_endpoint_address"). AddLabel("namespace").AddLabel("endpoint").AddLabel("ip"), NewMetricMatcher("endpoint_port").SetHeader("kube_endpoint_ports"). diff --git a/pkg/metrics/metrics_test.go b/pkg/metrics/metrics_test.go index e201c68..f248c46 100644 --- a/pkg/metrics/metrics_test.go +++ b/pkg/metrics/metrics_test.go @@ -8,7 +8,7 @@ import ( ) func TestMetrics(t *testing.T) { - f, err := os.Open("./metrics_output.txt") + f, err := os.Open("./metrics") if err != nil { t.Fatalf("open file failed: %v", err) t.Fail() diff --git a/pkg/metrics/resource.go b/pkg/metrics/resource.go index f9aed08..528ff61 100644 --- a/pkg/metrics/resource.go +++ b/pkg/metrics/resource.go @@ -3,25 +3,31 @@ package metrics import "encoding/json" type Resource struct { - Namespace string `json:"namespace"` - Type string `json:"type"` - Name string `json:"name"` - Spec map[string]string `json:"spec"` + Namespace string `json:"namespace"` + Type string `json:"type"` + Name string `json:"name"` + Spec map[string][]string `json:"spec"` } func NewResource(t string) *Resource { return &Resource{ Type: t, - Spec: make(map[string]string, 4), + Spec: make(map[string][]string, 4), } } func (r *Resource) AddLabelSpec(l Label) { - r.Spec[l.Key] = l.Value + if l.Value == "" { + return + } + if _, ok := r.Spec[l.Key]; !ok { + r.Spec[l.Key] = make([]string, 0) + } + r.Spec[l.Key] = append(r.Spec[l.Key], l.Value) } func (r *Resource) AddSpec(key string, value string) { - r.Spec[key] = value + r.AddLabelSpec(Label{Key: key, Value: value}) } type ResourceList []*Resource @@ -44,30 +50,28 @@ func ConvertToResource(r []*MetricMatcher) []*Resource { for _, m := range r { var resource *Resource var addFlag = true + + resourceType := m.Name if m.Name == "endpoint_address" || m.Name == "endpoint_port" { for i, c := range res { if m.FindLabel("namespace") == c.Namespace && m.FindLabel("endpoint") == c.Name { resource = res[i] addFlag = false - } else { - resource = NewResource("endpoint") } } - } else { - resource = NewResource(m.Name) + resourceType = "endpoint" } - resource.Namespace = m.FindLabel("namespace") - if m.Name == "endpoint_address" || m.Name == "endpoint_port" { - resource.Name = m.FindLabel("endpoint") - } else { - resource.Name = m.FindLabel(m.Name) + if addFlag { + resource = NewResource(resourceType) } + resource.Namespace = m.FindLabel("namespace") + resource.Name = m.FindLabel(resourceType) // merge endpoint_address and endpoint_port for _, l := range m.Labels { - if l.Key != "namespace" && l.Key != resource.Type { + if l.Key != "namespace" && l.Key != resourceType { resource.AddLabelSpec(l) } }