-
Notifications
You must be signed in to change notification settings - Fork 1.4k
🌱 feat: add check version against metadata utility #12529
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
richardcase
wants to merge
1
commit into
kubernetes-sigs:main
Choose a base branch
from
richardcase:metadata_file_checker
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.
+108
−0
Open
Changes from all commits
Commits
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,24 @@ | ||
# Metadata Version Validator | ||
|
||
## Purpose | ||
|
||
A small utility that given a version number it will check that a release series is defined in the passed in metadata.yaml file. | ||
|
||
This is primarily for use in release pipelines. | ||
|
||
## Usage | ||
|
||
First create the binary by: | ||
|
||
```bash | ||
cd hack/tools | ||
go build -tags=tools -o bin/metadata-version-validator sigs.k8s.io/cluster-api/hack/tools/metadata-version-validator | ||
``` | ||
|
||
Then run the binary to check a version: | ||
|
||
```bash | ||
bin/metadata-version-validator --file ../../cluster-api-provider-aws/metadata.yaml --version v2.8.0 | ||
``` | ||
|
||
If there is no release series defined there will be an error message and also a non-zero exit code. |
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,84 @@ | ||
/* | ||
Copyright 2025 The Kubernetes Authors. | ||
|
||
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. | ||
*/ | ||
|
||
// main is the main package for metadata-version-validator. | ||
package main | ||
|
||
import ( | ||
"flag" | ||
"fmt" | ||
"os" | ||
"strings" | ||
|
||
"github.com/blang/semver/v4" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
"k8s.io/apimachinery/pkg/runtime/serializer" | ||
"k8s.io/klog/v2" | ||
|
||
clusterctlv1 "sigs.k8s.io/cluster-api/cmd/clusterctl/api/v1alpha3" | ||
) | ||
|
||
func main() { | ||
file := flag.String("file", "", "Path to metadata.yaml file to check") | ||
version := flag.String("version", "metadata.yaml", "The version number to check against the metadata file") | ||
|
||
flag.Parse() | ||
|
||
if *file == "" { | ||
klog.Exit("--file must be specified") | ||
} | ||
if *version == "" { | ||
klog.Exit("--version must be specified") | ||
} | ||
|
||
if err := runCheckMetadata(*file, *version); err != nil { | ||
klog.Exitf("failed checking metadata: %v", err) | ||
} | ||
} | ||
|
||
func runCheckMetadata(file, version string) error { | ||
versionToCheck := strings.TrimPrefix(version, "v") | ||
|
||
ver, err := semver.Parse(versionToCheck) | ||
if err != nil { | ||
return fmt.Errorf("failed parsing %s as semver version: %w", version, err) | ||
} | ||
|
||
fileData, err := os.ReadFile(file) //nolint:gosec | ||
if err != nil { | ||
return fmt.Errorf("failed reading file %s: %w", file, err) | ||
} | ||
|
||
scheme := runtime.NewScheme() | ||
if err := clusterctlv1.AddToScheme(scheme); err != nil { | ||
return fmt.Errorf("failed to add metadata type to scheme: %w", err) | ||
} | ||
metadata := &clusterctlv1.Metadata{} | ||
codecFactory := serializer.NewCodecFactory(scheme) | ||
|
||
if err := runtime.DecodeInto(codecFactory.UniversalDecoder(), fileData, metadata); err != nil { | ||
return fmt.Errorf("failed decoding metadata from file: %w", err) | ||
} | ||
|
||
for _, series := range metadata.ReleaseSeries { | ||
if series.Major == int32(ver.Major) && series.Minor == int32(ver.Minor) { | ||
klog.Info("Success, there is a release series defined in the metadata file") | ||
return nil | ||
} | ||
} | ||
|
||
return fmt.Errorf("failed to find release series for version %s in metadata files %s", version, file) | ||
} |
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.
Maybe it's better to reference a file in CAPI so the docs example is self-contained? That is, I got an error trying to follow this doc naively.