-
Notifications
You must be signed in to change notification settings - Fork 2k
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
feat(metric-gen): Implement metric-gen tool to create custom resource configuration from markers #2014
Closed
Closed
feat(metric-gen): Implement metric-gen tool to create custom resource configuration from markers #2014
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
ee82097
Implement metric-gen tool
chrischdi f0c5ca9
Implement unit and integration tests
chrischdi 436f87f
review fixes
chrischdi 732f801
crd testdata: add example files and description
chrischdi 7b6eea4
fix -w to output markers without additional argument
chrischdi a523a3d
review fixes
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 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 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 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 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 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 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 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,128 @@ | ||
/* | ||
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 generate | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
|
||
"github.com/spf13/cobra" | ||
"sigs.k8s.io/controller-tools/pkg/genall" | ||
"sigs.k8s.io/controller-tools/pkg/genall/help" | ||
prettyhelp "sigs.k8s.io/controller-tools/pkg/genall/help/pretty" | ||
"sigs.k8s.io/controller-tools/pkg/loader" | ||
"sigs.k8s.io/controller-tools/pkg/markers" | ||
|
||
"k8s.io/kube-state-metrics/v2/pkg/customresourcestate/generate/generator" | ||
) | ||
|
||
const ( | ||
generatorName = "metric" | ||
) | ||
|
||
var ( | ||
// optionsRegistry contains all the marker definitions used to process command line options | ||
optionsRegistry = &markers.Registry{} | ||
|
||
generateWhichMarkersFlag bool | ||
) | ||
|
||
// GenerateCommand runs the kube-state-metrics custom resource config generator. | ||
var GenerateCommand = &cobra.Command{ | ||
Use: "generate [flags] /path/to/package [/path/to/package]", | ||
Short: "Generate custom resource metrics configuration from go-code markers (experimental).", | ||
DisableFlagsInUseLine: true, | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
if generateWhichMarkersFlag { | ||
PrintMarkerDocs() | ||
return nil | ||
} | ||
|
||
if len(args) == 0 { | ||
return fmt.Errorf("requires at least 1 package argument") | ||
} | ||
|
||
// Register the metric generator itself as marker so genall.FromOptions is able to initialize the runtime properly. | ||
// This also registers the markers inside the optionsRegistry so its available to print the marker docs. | ||
metricGenerator := generator.CustomResourceConfigGenerator{} | ||
defn := markers.Must(markers.MakeDefinition(generatorName, markers.DescribesPackage, metricGenerator)) | ||
if err := optionsRegistry.Register(defn); err != nil { | ||
return err | ||
} | ||
|
||
// Load the passed packages as roots. | ||
roots, err := loader.LoadRoots(args...) | ||
if err != nil { | ||
return fmt.Errorf("loading packages %w", err) | ||
} | ||
|
||
// Set up the generator runtime using controller-tools and passing our optionsRegistry. | ||
rt, err := genall.FromOptions(optionsRegistry, []string{generatorName}) | ||
if err != nil { | ||
return fmt.Errorf("%v", err) | ||
} | ||
|
||
// Setup the generation context with the loaded roots. | ||
rt.GenerationContext.Roots = roots | ||
// Setup the runtime to output to stdout. | ||
rt.OutputRules = genall.OutputRules{Default: genall.OutputToStdout} | ||
|
||
// Run the generator using the runtime. | ||
if hadErrs := rt.Run(); hadErrs { | ||
return fmt.Errorf("generator did not run successfully") | ||
} | ||
|
||
return nil | ||
}, | ||
Example: "kube-state-metrics generate ./apis/... > custom-resource-config.yaml", | ||
} | ||
|
||
func init() { | ||
GenerateCommand.Flags().BoolVarP(&generateWhichMarkersFlag, "which-markers", "w", false, "Print out all markers available with the requested generators.") | ||
} | ||
|
||
// PrintMarkerDocs prints out marker help for the given generators specified in | ||
// the rawOptions | ||
func PrintMarkerDocs() error { | ||
// Register the metric generator itself as marker so genall.FromOptions is able to initialize the runtime properly. | ||
// This also registers the markers inside the optionsRegistry so its available to print the marker docs. | ||
metricGenerator := generator.CustomResourceConfigGenerator{} | ||
defn := markers.Must(markers.MakeDefinition(generatorName, markers.DescribesPackage, metricGenerator)) | ||
if err := optionsRegistry.Register(defn); err != nil { | ||
return err | ||
} | ||
|
||
// just grab a registry so we don't lag while trying to load roots | ||
// (like we'd do if we just constructed the full runtime). | ||
reg, err := genall.RegistryFromOptions(optionsRegistry, []string{generatorName}) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
helpInfo := help.ByCategory(reg, help.SortByCategory) | ||
|
||
for _, cat := range helpInfo { | ||
if cat.Category == "" { | ||
continue | ||
} | ||
contents := prettyhelp.MarkersDetails(false, cat.Category, cat.Markers) | ||
if err := contents.WriteTo(os.Stderr); err != nil { | ||
return err | ||
} | ||
} | ||
return nil | ||
} |
Oops, something went wrong.
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.
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.
Note: I have to add the subcommand here. Doing it inside the options package would result in cyclic imports.