|
| 1 | +/* |
| 2 | +Copyright 2023 The Kubernetes Authors All rights reserved. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | +package metric |
| 17 | + |
| 18 | +import ( |
| 19 | + "fmt" |
| 20 | + "sort" |
| 21 | + |
| 22 | + "k8s.io/klog/v2" |
| 23 | + "sigs.k8s.io/controller-tools/pkg/crd" |
| 24 | + "sigs.k8s.io/controller-tools/pkg/genall" |
| 25 | + "sigs.k8s.io/controller-tools/pkg/loader" |
| 26 | + "sigs.k8s.io/controller-tools/pkg/markers" |
| 27 | + |
| 28 | + "k8s.io/kube-state-metrics/v2/pkg/customresourcestate" |
| 29 | +) |
| 30 | + |
| 31 | +type Generator struct{} |
| 32 | + |
| 33 | +func (Generator) CheckFilter() loader.NodeFilter { |
| 34 | + // Re-use controller-tools filter to filter out unrelated nodes that aren't used |
| 35 | + // in CRD generation, like interfaces and struct fields without JSON tag. |
| 36 | + return crd.Generator{}.CheckFilter() |
| 37 | +} |
| 38 | + |
| 39 | +func (g Generator) Generate(ctx *genall.GenerationContext) error { |
| 40 | + // Create the parser which is specific to the metric generator. |
| 41 | + parser := newParser( |
| 42 | + &crd.Parser{ |
| 43 | + Collector: ctx.Collector, |
| 44 | + Checker: ctx.Checker, |
| 45 | + }, |
| 46 | + ) |
| 47 | + |
| 48 | + // Loop over all passed packages. |
| 49 | + for _, root := range ctx.Roots { |
| 50 | + // skip packages which don't import metav1 because they can't define a CRD without meta v1. |
| 51 | + metav1 := root.Imports()["k8s.io/apimachinery/pkg/apis/meta/v1"] |
| 52 | + if metav1 == nil { |
| 53 | + continue |
| 54 | + } |
| 55 | + |
| 56 | + // parse the given package to feed crd.FindKubeKinds to find CRD objects. |
| 57 | + parser.NeedPackage(root) |
| 58 | + kubeKinds := crd.FindKubeKinds(parser.Parser, metav1) |
| 59 | + if len(kubeKinds) == 0 { |
| 60 | + klog.Fatalf("no objects in the roots") |
| 61 | + } |
| 62 | + |
| 63 | + for _, gv := range kubeKinds { |
| 64 | + // Create customresourcestate.Resource for each CRD which contains all metric |
| 65 | + // definitions for the CRD. |
| 66 | + parser.NeedResourceFor(gv) |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + // Build customresourcestate configuration file from generated data. |
| 71 | + metrics := customresourcestate.Metrics{ |
| 72 | + Spec: customresourcestate.MetricsSpec{ |
| 73 | + Resources: []customresourcestate.Resource{}, |
| 74 | + }, |
| 75 | + } |
| 76 | + |
| 77 | + // Sort the resources to get a deterministic output. |
| 78 | + |
| 79 | + for _, resource := range parser.CustomResourceStates { |
| 80 | + if len(resource.Metrics) > 0 { |
| 81 | + // sort the metrics |
| 82 | + sort.Slice(resource.Metrics, func(i, j int) bool { |
| 83 | + return resource.Metrics[i].Name < resource.Metrics[j].Name |
| 84 | + }) |
| 85 | + |
| 86 | + metrics.Spec.Resources = append(metrics.Spec.Resources, resource) |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + sort.Slice(metrics.Spec.Resources, func(i, j int) bool { |
| 91 | + if metrics.Spec.Resources[i].MetricNamePrefix == nil && metrics.Spec.Resources[j].MetricNamePrefix == nil { |
| 92 | + a := metrics.Spec.Resources[i].GroupVersionKind.Group + "/" + metrics.Spec.Resources[i].GroupVersionKind.Version + "/" + metrics.Spec.Resources[i].GroupVersionKind.Kind |
| 93 | + b := metrics.Spec.Resources[j].GroupVersionKind.Group + "/" + metrics.Spec.Resources[j].GroupVersionKind.Version + "/" + metrics.Spec.Resources[j].GroupVersionKind.Kind |
| 94 | + return a < b |
| 95 | + } |
| 96 | + |
| 97 | + // Either a or b will not be the empty string, so we can compare them. |
| 98 | + var a, b string |
| 99 | + if metrics.Spec.Resources[i].MetricNamePrefix == nil { |
| 100 | + a = *metrics.Spec.Resources[i].MetricNamePrefix |
| 101 | + } |
| 102 | + if metrics.Spec.Resources[j].MetricNamePrefix != nil { |
| 103 | + b = *metrics.Spec.Resources[j].MetricNamePrefix |
| 104 | + } |
| 105 | + return a < b |
| 106 | + }) |
| 107 | + |
| 108 | + // Write the rendered yaml to the context which will result in stdout. |
| 109 | + filePath := "metrics.yaml" |
| 110 | + if err := ctx.WriteYAML(filePath, "", []interface{}{metrics}, genall.WithTransform(addCustomResourceStateKind)); err != nil { |
| 111 | + return fmt.Errorf("WriteYAML to %s: %w", filePath, err) |
| 112 | + } |
| 113 | + |
| 114 | + return nil |
| 115 | +} |
| 116 | + |
| 117 | +// addCustomResourceStateKind adds the correct kind because we don't have a correct |
| 118 | +// kubernetes-style object as configuration definition. |
| 119 | +func addCustomResourceStateKind(obj map[string]interface{}) error { |
| 120 | + obj["kind"] = "CustomResourceStateMetrics" |
| 121 | + return nil |
| 122 | +} |
| 123 | + |
| 124 | +func (g Generator) RegisterMarkers(into *markers.Registry) error { |
| 125 | + for _, m := range markerDefinitions { |
| 126 | + if err := m.Register(into); err != nil { |
| 127 | + return err |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + return nil |
| 132 | +} |
0 commit comments