Skip to content

Commit

Permalink
metrics: add version compatibility header
Browse files Browse the repository at this point in the history
  • Loading branch information
chrischdi committed Aug 29, 2024
1 parent 1389ce3 commit 550fa2f
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
9 changes: 8 additions & 1 deletion pkg/metrics/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import (
"sigs.k8s.io/controller-tools/pkg/metrics/internal/config"
"sigs.k8s.io/controller-tools/pkg/metrics/markers"
"sigs.k8s.io/controller-tools/pkg/rbac"
"sigs.k8s.io/controller-tools/pkg/version"
)

// Generator generates kube-state-metrics custom resource configuration files.
Expand All @@ -54,6 +55,10 @@ func (g Generator) RegisterMarkers(into *ctrlmarkers.Registry) error {
return nil
}

const headerText = `# Generated by controller-gen version %s
# Compatible to kube-state-metrics %s
`

// Generate generates artifacts produced by this marker.
// It's called after RegisterMarkers has been called.
func (g Generator) Generate(ctx *genall.GenerationContext) error {
Expand Down Expand Up @@ -129,9 +134,11 @@ func (g Generator) Generate(ctx *genall.GenerationContext) error {
return a < b
})

header := fmt.Sprintf(headerText, version.Version(), config.KubeStateMetricsVersion)

// Write the rendered yaml to the context which will result in stdout.
virtualFilePath := "metrics.yaml"
if err := ctx.WriteYAML(virtualFilePath, "", []interface{}{metrics}, genall.WithTransform(addCustomResourceStateKind)); err != nil {
if err := ctx.WriteYAML(virtualFilePath, header, []interface{}{metrics}, genall.WithTransform(addCustomResourceStateKind)); err != nil {
return fmt.Errorf("WriteYAML to %s: %w", virtualFilePath, err)
}

Expand Down
5 changes: 4 additions & 1 deletion pkg/metrics/internal/config/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ limitations under the License.
*/

// config contains a copy of the types from k8s.io/kube-state-metrics/pkg/customresourcestate.
// It is currently based on kube-state-metrics v2.13.0.
// The following modifications got applied:
// For `config.go`:
// * Rename the package to `config`.
Expand All @@ -38,3 +37,7 @@ limitations under the License.
// - `MetricGauge.LabelFromkey`
// - `MetricInfo.LabelFromkey`
package config

// KubeStateMetricsVersion defines which version of kube-state-metrics these types
// are based on and the output file should be compatible to.
const KubeStateMetricsVersion = "v2.13.0"

0 comments on commit 550fa2f

Please sign in to comment.