Skip to content

Commit 5dc84dd

Browse files
committed
improve
1 parent d7d5ef2 commit 5dc84dd

File tree

5 files changed

+44
-67
lines changed

5 files changed

+44
-67
lines changed

exp/metric-gen/markers/helper_types.go renamed to exp/metric-gen/markers/helper.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,10 @@ import (
1919
"fmt"
2020

2121
"k8s.io/client-go/util/jsonpath"
22+
"k8s.io/klog/v2"
2223
ctrlmarkers "sigs.k8s.io/controller-tools/pkg/markers"
24+
25+
"k8s.io/kube-state-metrics/v2/pkg/customresourcestate"
2326
)
2427

2528
type markerDefinitionWithHelp struct {
@@ -85,3 +88,34 @@ func (j jsonPath) Parse() ([]string, error) {
8588

8689
return ret, nil
8790
}
91+
92+
func newMetricMeta(basePath []string, j jsonPath, jsonLabelsFromPath map[string]jsonPath) customresourcestate.MetricMeta {
93+
path := basePath
94+
if j != "" {
95+
valueFrom, err := j.Parse()
96+
if err != nil {
97+
klog.Fatal(err)
98+
}
99+
if len(valueFrom) > 0 {
100+
path = append(path, valueFrom...)
101+
}
102+
}
103+
104+
labelsFromPath := map[string][]string{}
105+
for k, v := range jsonLabelsFromPath {
106+
path := []string{}
107+
var err error
108+
if v != "." {
109+
path, err = v.Parse()
110+
if err != nil {
111+
klog.Fatal(err)
112+
}
113+
}
114+
labelsFromPath[k] = path
115+
}
116+
117+
return customresourcestate.MetricMeta{
118+
Path: path,
119+
LabelsFromPath: labelsFromPath,
120+
}
121+
}

exp/metric-gen/markers/markers.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,14 @@ var (
3434

3535
// +controllertools:marker:generateHelp:category=CRD
3636

37-
// ResourceMarker is a marker that knows how to apply itself to a particular
38-
// version in a CRD Spec.
37+
// ResourceMarker is a marker that configures a custom resource.
3938
type ResourceMarker interface {
4039
// ApplyToCRD applies this marker to the given CRD, in the given version
4140
// within that CRD. It's called after everything else in the CRD is populated.
4241
ApplyToResource(resource *customresourcestate.Resource) error
4342
}
4443

45-
// LocalGeneratorMarker is a marker that knows how to create a generator from itself.
44+
// LocalGeneratorMarker is a marker that creates a custom resource metric generator.
4645
type LocalGeneratorMarker interface {
4746
// ApplyToCRD applies this marker to the given CRD, in the given version
4847
// within that CRD. It's called after everything else in the CRD is populated.

exp/metric-gen/markers/metric_gauge.go

Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,7 @@ func (gaugeMarker) help() *markers.DefinitionHelp {
6262
}
6363

6464
func (g gaugeMarker) ToGenerator(basePath ...string) *customresourcestate.Generator {
65-
additionalPath, err := g.JSONPath.Parse()
66-
if err != nil {
67-
klog.Fatal(err)
68-
}
65+
var err error
6966
var valueFrom []string
7067
if g.ValueFrom != nil {
7168
valueFrom, err = g.ValueFrom.Parse()
@@ -74,33 +71,14 @@ func (g gaugeMarker) ToGenerator(basePath ...string) *customresourcestate.Genera
7471
}
7572
}
7673

77-
labelsFromPath := map[string][]string{}
78-
for k, v := range g.LabelsFromPath {
79-
path := []string{}
80-
var err error
81-
if v != "." {
82-
path, err = v.Parse()
83-
if err != nil {
84-
klog.Fatal(err)
85-
}
86-
}
87-
labelsFromPath[k] = path
88-
}
89-
90-
// nolint:gocritic
91-
path := append(basePath, additionalPath...)
92-
9374
return &customresourcestate.Generator{
9475
Name: g.Name,
9576
Help: g.Help,
9677
Each: customresourcestate.Metric{
9778
Type: customresourcestate.MetricTypeGauge,
9879
Gauge: &customresourcestate.MetricGauge{
99-
NilIsZero: g.NilIsZero,
100-
MetricMeta: customresourcestate.MetricMeta{
101-
Path: path,
102-
LabelsFromPath: labelsFromPath,
103-
},
80+
NilIsZero: g.NilIsZero,
81+
MetricMeta: newMetricMeta(basePath, g.JSONPath, g.LabelsFromPath),
10482
LabelFromKey: g.LabelFromKey,
10583
ValueFrom: valueFrom,
10684
},

exp/metric-gen/markers/metric_info.go

Lines changed: 1 addition & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@ package markers
1818
import (
1919
"sigs.k8s.io/controller-tools/pkg/markers"
2020

21-
"k8s.io/klog/v2"
22-
2321
"k8s.io/kube-state-metrics/v2/pkg/customresourcestate"
2422
)
2523

@@ -60,40 +58,13 @@ func (infoMarker) help() *markers.DefinitionHelp {
6058
}
6159

6260
func (i infoMarker) ToGenerator(basePath ...string) *customresourcestate.Generator {
63-
path := basePath
64-
if i.JSONPath != "" {
65-
valueFrom, err := i.JSONPath.Parse()
66-
if err != nil {
67-
klog.Fatal(err)
68-
}
69-
if len(valueFrom) > 0 {
70-
path = append(path, valueFrom...)
71-
}
72-
}
73-
74-
labelsFromPath := map[string][]string{}
75-
for k, v := range i.LabelsFromPath {
76-
path := []string{}
77-
var err error
78-
if v != "." {
79-
path, err = v.Parse()
80-
if err != nil {
81-
klog.Fatal(err)
82-
}
83-
}
84-
labelsFromPath[k] = path
85-
}
86-
8761
return &customresourcestate.Generator{
8862
Name: i.Name,
8963
Help: i.Help,
9064
Each: customresourcestate.Metric{
9165
Type: customresourcestate.MetricTypeInfo,
9266
Info: &customresourcestate.MetricInfo{
93-
MetricMeta: customresourcestate.MetricMeta{
94-
Path: path,
95-
LabelsFromPath: labelsFromPath,
96-
},
67+
MetricMeta: newMetricMeta(basePath, i.JSONPath, i.LabelsFromPath),
9768
LabelFromKey: i.LabelFromKey,
9869
},
9970
},

exp/metric-gen/markers/metric_stateset.go

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,6 @@ func (stateSetMarker) help() *markers.DefinitionHelp {
6161
}
6262

6363
func (s stateSetMarker) ToGenerator(basePath ...string) *customresourcestate.Generator {
64-
path := basePath
65-
6664
var valueFrom []string
6765
var err error
6866
if s.JSONPath != nil {
@@ -87,13 +85,10 @@ func (s stateSetMarker) ToGenerator(basePath ...string) *customresourcestate.Gen
8785
Each: customresourcestate.Metric{
8886
Type: customresourcestate.MetricTypeStateSet,
8987
StateSet: &customresourcestate.MetricStateSet{
90-
MetricMeta: customresourcestate.MetricMeta{
91-
Path: path,
92-
LabelsFromPath: labelsFromPath,
93-
},
94-
List: s.List,
95-
LabelName: s.LabelName,
96-
ValueFrom: valueFrom,
88+
MetricMeta: newMetricMeta(basePath, "", s.LabelsFromPath),
89+
List: s.List,
90+
LabelName: s.LabelName,
91+
ValueFrom: valueFrom,
9792
},
9893
},
9994
}

0 commit comments

Comments
 (0)