@@ -13,7 +13,7 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
13
See the License for the specific language governing permissions and
14
14
limitations under the License.
15
15
*/
16
- package metric
16
+ package generator
17
17
18
18
import (
19
19
"fmt"
@@ -26,27 +26,28 @@ import (
26
26
"k8s.io/klog/v2"
27
27
"sigs.k8s.io/controller-tools/pkg/crd"
28
28
"sigs.k8s.io/controller-tools/pkg/loader"
29
- "sigs.k8s.io/controller-tools/pkg/markers"
29
+ ctrlmarkers "sigs.k8s.io/controller-tools/pkg/markers"
30
30
31
+ "k8s.io/kube-state-metrics/v2/exp/metric-gen/markers"
31
32
"k8s.io/kube-state-metrics/v2/pkg/customresourcestate"
32
33
)
33
34
34
- type Parser struct {
35
+ type parser struct {
35
36
* crd.Parser
36
37
37
38
CustomResourceStates map [schema.GroupKind ]customresourcestate.Resource
38
39
FlattenedMetrics map [crd.TypeIdent ][]customresourcestate.Metric
39
40
}
40
41
41
- func newParser (parser * crd.Parser ) * Parser {
42
- return & Parser {
43
- Parser : parser ,
42
+ func newParser (p * crd.Parser ) * parser {
43
+ return & parser {
44
+ Parser : p ,
44
45
CustomResourceStates : make (map [schema.GroupKind ]customresourcestate.Resource ),
45
46
FlattenedMetrics : make (map [crd.TypeIdent ][]customresourcestate.Metric ),
46
47
}
47
48
}
48
49
49
- func (p * Parser ) NeedResourceFor (groupKind schema.GroupKind ) {
50
+ func (p * parser ) NeedResourceFor (groupKind schema.GroupKind ) {
50
51
if _ , exists := p .CustomResourceStates [groupKind ]; exists {
51
52
return
52
53
}
@@ -61,9 +62,8 @@ func (p *Parser) NeedResourceFor(groupKind schema.GroupKind) {
61
62
62
63
resource := customresourcestate.Resource {
63
64
GroupVersionKind : customresourcestate.GroupVersionKind {
64
- Group : groupKind .Group ,
65
- Kind : groupKind .Kind ,
66
- Version : "" , // TODO
65
+ Group : groupKind .Group ,
66
+ Kind : groupKind .Kind ,
67
67
},
68
68
}
69
69
@@ -74,9 +74,9 @@ func (p *Parser) NeedResourceFor(groupKind schema.GroupKind) {
74
74
continue
75
75
}
76
76
77
- // Skip if namePrefix marker is not set to not create configuration for CRs used in other CRs.
78
- // e .g. to not create configuration for KubeadmControlPlaneTemplate.
79
- if m := typeInfo .Markers .Get (NameMarkerName ); m == nil {
77
+ // Skip if gvk marker is not set to not create configuration for CRs used in other CRs.
78
+ // E .g. to not create configuration for KubeadmControlPlaneTemplate.
79
+ if m := typeInfo .Markers .Get (markers . GVKMarkerName ); m == nil {
80
80
continue
81
81
}
82
82
@@ -101,7 +101,7 @@ func (p *Parser) NeedResourceFor(groupKind schema.GroupKind) {
101
101
102
102
for _ , markerVals := range typeInfo .Markers {
103
103
for _ , val := range markerVals {
104
- if resourceMarker , isResourceMarker := val .(ResourceMarker ); isResourceMarker {
104
+ if resourceMarker , isResourceMarker := val .(markers. ResourceMarker ); isResourceMarker {
105
105
if err := resourceMarker .ApplyToResource (& resource ); err != nil {
106
106
pkg .AddError (loader .ErrFromNode (err /* an okay guess */ , typeInfo .RawSpec ))
107
107
}
@@ -120,10 +120,10 @@ type generatorRequester interface {
120
120
// generatorContext stores and provides information across a hierarchy of metric generators generation.
121
121
type generatorContext struct {
122
122
pkg * loader.Package
123
- info * markers .TypeInfo
123
+ info * ctrlmarkers .TypeInfo
124
124
generatorRequester generatorRequester
125
125
126
- PackageMarkers markers .MarkerValues
126
+ PackageMarkers ctrlmarkers .MarkerValues
127
127
}
128
128
129
129
func newGeneratorContext (pkg * loader.Package , req generatorRequester ) * generatorContext {
@@ -147,12 +147,12 @@ func (c *generatorContext) requestGenerator(pkgPath, typeName string) []customre
147
147
})
148
148
}
149
149
150
- func generatorsFromMarkers (m markers .MarkerValues , basePath ... string ) []customresourcestate.Generator {
150
+ func generatorsFromMarkers (m ctrlmarkers .MarkerValues , basePath ... string ) []customresourcestate.Generator {
151
151
generators := []customresourcestate.Generator {}
152
152
153
153
for _ , markerVals := range m {
154
154
for _ , val := range markerVals {
155
- if generatorMarker , isGeneratorMarker := val .(GeneratorMarker ); isGeneratorMarker {
155
+ if generatorMarker , isGeneratorMarker := val .(markers. LocalGeneratorMarker ); isGeneratorMarker {
156
156
if g := generatorMarker .ToGenerator (basePath ... ); g != nil {
157
157
generators = append (generators , * g )
158
158
}
@@ -163,7 +163,7 @@ func generatorsFromMarkers(m markers.MarkerValues, basePath ...string) []customr
163
163
return generators
164
164
}
165
165
166
- func (p * Parser ) NeedMetricsGeneratorFor (typ crd.TypeIdent ) []customresourcestate.Generator {
166
+ func (p * parser ) NeedMetricsGeneratorFor (typ crd.TypeIdent ) []customresourcestate.Generator {
167
167
if _ , knownMetrics := p .FlattenedMetrics [typ ]; knownMetrics {
168
168
return nil
169
169
}
@@ -239,9 +239,9 @@ func generatorsFor(ctx *generatorContext, rawType ast.Expr) []customresourcestat
239
239
func localNamedToGenerators (ctx * generatorContext , ident * ast.Ident ) []customresourcestate.Generator {
240
240
typeInfo := ctx .pkg .TypesInfo .TypeOf (ident )
241
241
if typeInfo == types .Typ [types .Invalid ] {
242
- // Expected to hit this error for types from not loaded packages
243
- // TODO(chrischdi): verify
244
- // klog.Warningf("Skipping unknown type: %v", loader.ErrFromNode(fmt.Errorf("unknown type %s", ident.Name), ident))
242
+ // It is expected to hit this error for types from not loaded transitive package dependencies.
243
+ // This leads to ignoring markers defined on the transitive types. Otherwise
244
+ // markers on transitive types would lead to additional metrics.
245
245
return nil
246
246
}
247
247
0 commit comments