-
Couldn't load subscription status.
- Fork 459
✨ metrics: add generator for kube-state-metrics customresource configuration #1043
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
679e6bb
8bd0c13
cefa525
00578ce
d5b53a1
9c508b3
623b301
0fcf94e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,130 @@ | ||
| /* | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
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-genprefix.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:
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?
There was a problem hiding this comment.
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 installor downloaded from the release page. So should be good enough to find the repo with that prefix.