-
Notifications
You must be signed in to change notification settings - Fork 458
✨ 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
Open
chrischdi
wants to merge
8
commits into
kubernetes-sigs:main
Choose a base branch
from
chrischdi:pr-metrics-generator
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
679e6bb
rbac: expose NormalizeRules function to use in other generators too
chrischdi 8bd0c13
metrics: add internal copy of kube-state-metrics config types
chrischdi cefa525
metrics: implement markers for metrics
chrischdi 00578ce
metrics: implement generator and parser for custom resource configura…
chrischdi d5b53a1
go mod tidy
chrischdi 9c508b3
metrics: implement test for the metrics generator
chrischdi 623b301
linter fixes
chrischdi 0fcf94e
Fix linter issues and regenerate
chrischdi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.