Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions cmd/controller-gen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import (
"sigs.k8s.io/controller-tools/pkg/genall/help"
Copy link
Member

@sbueringer sbueringer Oct 23, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧵 Marker prefix

Follow-up to the discussion in the meeting.

I think we should go with a k8s:controller-gen prefix.

I think it clearly identifies that these markers belong to conroller-gen. The name of this binary will remain the same, independent of in which repository it is hosted.

I thought a bit about if it's worth renaming the repo to make controller-gen easier to find.
I would not do it:

  • we host some other tools in this repo
  • we might want to host additional tools in this repo going forward
  • it's a breaking change for setup-envtest as it uses release artifacts from this repo
  • it's disruptive in general and requires a bunch of effort

I think overall it's not worth it just to make it slightly easier for users to find controller-gen to open issues. It's in general not hard to find controller-gen. A similar situation exists for e.g. kubeadm.

I see how kuberbuilder was misleading as a marker prefix. I think the same problem does not exist in the same way for controller-gen

@chrischdi @JoelSpeed WDYT?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree. I think mostly controller-gen get's either installed via go install or downloaded from the release page. So should be good enough to find the repo with that prefix.

prettyhelp "sigs.k8s.io/controller-tools/pkg/genall/help/pretty"
"sigs.k8s.io/controller-tools/pkg/markers"
"sigs.k8s.io/controller-tools/pkg/metrics"
"sigs.k8s.io/controller-tools/pkg/rbac"
"sigs.k8s.io/controller-tools/pkg/schemapatcher"
"sigs.k8s.io/controller-tools/pkg/version"
Expand All @@ -57,6 +58,7 @@ var (
"applyconfiguration": applyconfiguration.Generator{},
"webhook": webhook.Generator{},
"schemapatch": schemapatcher.Generator{},
"metrics": metrics.Generator{},
}

// allOutputRules defines the list of all known output rules, giving
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ require (
k8s.io/apiextensions-apiserver v0.34.1
k8s.io/apimachinery v0.34.1
k8s.io/apiserver v0.34.1
k8s.io/client-go v0.34.1
k8s.io/code-generator v0.34.1
k8s.io/gengo/v2 v2.0.0-20250604051438-85fd79dbfd9f
k8s.io/utils v0.0.0-20250604170112-4c0f3b243397
Expand Down Expand Up @@ -88,7 +89,6 @@ require (
google.golang.org/protobuf v1.36.7 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 // indirect
k8s.io/client-go v0.34.1 // indirect
k8s.io/component-base v0.34.1 // indirect
k8s.io/klog/v2 v2.130.1 // indirect
k8s.io/kube-openapi v0.0.0-20250710124328-f3f2b991d03b // indirect
Expand Down
19 changes: 15 additions & 4 deletions pkg/crd/markers/zz_generated.markerhelp.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

130 changes: 130 additions & 0 deletions pkg/metrics/generate_integration_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
/*
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If wanted, I can volunteer to maintain this part of controller-gen (and/or also help on other parts of controller-tools) :-)

Copyright 2023 The Kubernetes Authors All rights reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package metrics

import (
"bytes"
"fmt"
"io"
"os"
"path"
"strings"
"testing"

"github.com/google/go-cmp/cmp"
"sigs.k8s.io/controller-tools/pkg/genall"
"sigs.k8s.io/controller-tools/pkg/loader"
"sigs.k8s.io/controller-tools/pkg/markers"
"sigs.k8s.io/controller-tools/pkg/metrics/internal/config"
)

func Test_Generate(t *testing.T) {
cwd, err := os.Getwd()
if err != nil {
t.Error(err)
}

optionsRegistry := &markers.Registry{}

metricGenerator := Generator{}
if err := metricGenerator.RegisterMarkers(optionsRegistry); err != nil {
t.Error(err)
}

out := &outputRule{
buf: &bytes.Buffer{},
}

// Load the passed packages as roots.
roots, err := loader.LoadRoots(path.Join(cwd, "testdata", "..."))
if err != nil {
t.Errorf("loading packages %v", err)
}

gen := Generator{}

generationContext := &genall.GenerationContext{
Collector: &markers.Collector{Registry: optionsRegistry},
Roots: roots,
Checker: &loader.TypeChecker{},
OutputRule: out,
}

t.Log("Trying to generate a custom resource configuration from the loaded packages")

if err := gen.Generate(generationContext); err != nil {
t.Error(err)
}

output := strings.Split(out.buf.String(), "\n---\n")

header := fmt.Sprintf(headerText, "(devel)", config.KubeStateMetricsVersion)

if len(output) != 3 {
t.Error("Expected two output files, metrics configuration followed by rbac.")
return
}

generatedData := map[string]string{
"metrics.yaml": header + "---\n" + output[1],
"rbac.yaml": "---\n" + output[2],
}

t.Log("Comparing output to testdata to check for regressions")

for _, golden := range []string{"metrics.yaml", "rbac.yaml"} {
// generatedRaw := strings.TrimSpace(output[i])

expectedRaw, err := os.ReadFile(path.Clean(path.Join(cwd, "testdata", golden)))
if err != nil {
t.Error(err)
return
}

// Remove leading `---` and trim newlines
generated := strings.TrimSpace(strings.TrimPrefix(generatedData[golden], "---"))
expected := strings.TrimSpace(strings.TrimPrefix(string(expectedRaw), "---"))

diff := cmp.Diff(expected, generated)
if diff != "" {
t.Log("generated:")
t.Log(generated)
t.Log("diff:")
t.Log(diff)
t.Logf("Expected output to match file `testdata/%s` but it does not.", golden)
t.Logf("If the change is intended, use `go generate ./pkg/metrics/testdata` to regenerate the `testdata/%s` file.", golden)
t.Errorf("Detected a diff between the output of the integration test and the file `testdata/%s`.", golden)
return
}
}
}

type outputRule struct {
buf *bytes.Buffer
}

func (o *outputRule) Open(_ *loader.Package, _ string) (io.WriteCloser, error) {
return nopCloser{o.buf}, nil
}

type nopCloser struct {
io.Writer
}

func (n nopCloser) Close() error {
return nil
}
Loading