-
Notifications
You must be signed in to change notification settings - Fork 30
Add an option to generate a reference configuration from a running cluster #267
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
dcritch
wants to merge
15
commits into
openshift:main
Choose a base branch
from
dcritch:generate-reference
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
15 commits
Select commit
Hold shift + click to select a range
40ef631
implementing reference generation
dcritch 8cf573e
Merge branch 'openshift:main' into generate-reference
dcritch 31df7b6
Merge branch 'openshift:main' into generate-reference
dcritch 07c53e2
re-use -f argument for when generating
dcritch 1d49dcc
sanitize output file names
dcritch e5e864c
adding test cases
dcritch 147d829
only remove specified annotations/labels, rather than all of them
dcritch 34fe2c0
allow a user to speciy labels and annotations to be ignored
dcritch 65dc104
Merge branch 'openshift:main' into generate-reference
dcritch 99ff3e4
coderabbit.ai suggestionsg
dcritch a4df30a
fix linter issues
dcritch c65401e
additional coderabbitai suggested fixes
dcritch 0c69fbb
resolve merge conflict
dcritch c830a42
coderabbit suggestion
dcritch 53caae3
fixing linter issue
dcritch 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| # Example generate config for cluster-compare -g | ||
| # Use: kubectl cluster-compare -g docs/example/generate-config.yaml | ||
| # Or with must-gather: kubectl cluster-compare -g docs/example/generate-config.yaml -f ./must-gather.123456 | ||
| apiVersion: refgen/v1 | ||
| outputDir: ./generated-reference | ||
|
|
||
| # Optional: extra metadata.annotations / metadata.labels keys to strip from captured | ||
| # YAML and to register in generated metadata.yaml fieldsToOmit (defaults still apply). | ||
| # omitAnnotations: | ||
| # - my.company/last-synced | ||
| # omitLabels: | ||
| # - ci-build-id | ||
|
|
||
| resources: | ||
| - kind: Namespace | ||
| apiVersion: v1 | ||
| required: false | ||
| names: | ||
| - openshift-sriov-network-operator | ||
| - openshift-ptp |
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| // SPDX-License-Identifier:Apache-2.0 | ||
|
|
||
| package generate | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "os" | ||
| "path/filepath" | ||
| "strings" | ||
|
|
||
| "sigs.k8s.io/yaml" | ||
| ) | ||
|
|
||
| // validAPIVersions lists supported RefgenConfig.apiVersion values. | ||
| var validAPIVersions = []string{ | ||
| "refgen/v1", | ||
| } | ||
|
|
||
| // RefgenConfig is the root configuration for reference generation. | ||
| type RefgenConfig struct { | ||
| APIVersion string `json:"apiVersion"` | ||
| OutputDir string `json:"outputDir"` | ||
| // OmitAnnotations lists metadata.annotation keys stripped from captured manifests | ||
| // and added to generated metadata.yaml fieldsToOmit (in addition to built-in defaults). | ||
| OmitAnnotations []string `json:"omitAnnotations,omitempty"` | ||
| // OmitLabels lists metadata.labels keys stripped from captured manifests and | ||
| // added to fieldsToOmit defaults (in addition to built-in defaults). | ||
| OmitLabels []string `json:"omitLabels,omitempty"` | ||
| Resources []ResourceSpec `json:"resources"` | ||
| } | ||
|
|
||
| // ResourceSpec specifies a Kubernetes resource type to capture. | ||
| type ResourceSpec struct { | ||
| Kind string `json:"kind"` | ||
| APIVersion string `json:"apiVersion"` | ||
| Required bool `json:"required"` | ||
| Namespace string `json:"namespace,omitempty"` | ||
| Names []string `json:"names,omitempty"` | ||
| } | ||
|
|
||
| // LoadConfig loads and validates a refgen configuration file. | ||
| func LoadConfig(configPath string) (*RefgenConfig, error) { | ||
| absPath, err := filepath.Abs(configPath) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to get absolute path for %s: %w", configPath, err) | ||
| } | ||
| data, err := os.ReadFile(absPath) | ||
| if err != nil { | ||
| if os.IsNotExist(err) { | ||
| return nil, fmt.Errorf("configuration file not found: %s", configPath) | ||
| } | ||
| return nil, fmt.Errorf("failed to read configuration: %w", err) | ||
| } | ||
| var config RefgenConfig | ||
| if err := yaml.UnmarshalStrict(data, &config); err != nil { | ||
| return nil, fmt.Errorf("invalid YAML in configuration file: %w", err) | ||
| } | ||
| if config.APIVersion == "" { | ||
| config.APIVersion = "refgen/v1" | ||
| } | ||
| allowedAPIVersion := false | ||
| for _, v := range validAPIVersions { | ||
| if config.APIVersion == v { | ||
| allowedAPIVersion = true | ||
| break | ||
| } | ||
| } | ||
| if !allowedAPIVersion { | ||
| return nil, fmt.Errorf("configuration apiVersion %q is invalid; must be one of: %s", config.APIVersion, strings.Join(validAPIVersions, ", ")) | ||
| } | ||
| if config.OutputDir == "" { | ||
| config.OutputDir = "./generated-reference" | ||
| } | ||
| if len(config.Resources) == 0 { | ||
| return nil, fmt.Errorf("configuration must specify at least one resource") | ||
| } | ||
| for i, k := range config.OmitAnnotations { | ||
| if err := validateOmitKey(k, "omitAnnotations", i); err != nil { | ||
| return nil, err | ||
| } | ||
| } | ||
| for i, k := range config.OmitLabels { | ||
| if err := validateOmitKey(k, "omitLabels", i); err != nil { | ||
| return nil, err | ||
| } | ||
| } | ||
| return &config, nil | ||
| } | ||
|
|
||
| func validateOmitKey(key, field string, index int) error { | ||
| if key == "" { | ||
| return fmt.Errorf("%s[%d]: key must not be empty", field, index) | ||
| } | ||
| if strings.Contains(key, `"`) { | ||
| return fmt.Errorf("%s[%d]: key must not contain double quotes", field, index) | ||
| } | ||
| 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.
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 suspect that users will want to extend the list of ignored labels (defaults you have below) explicitly in the config file. This will allow them to capture any changes in the config file and re-generate after changes to the cluster, new versions, etc.