Skip to content

Commit

Permalink
update: label of Resource has same key but diff value
Browse files Browse the repository at this point in the history
  • Loading branch information
Esonhugh committed Dec 2, 2024
1 parent f591f06 commit 359c657
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 19 deletions.
2 changes: 1 addition & 1 deletion pkg/metrics/matcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -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").
Expand Down
2 changes: 1 addition & 1 deletion pkg/metrics/metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
38 changes: 21 additions & 17 deletions pkg/metrics/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
}
}
Expand Down

0 comments on commit 359c657

Please sign in to comment.