Repository navigation
fix(release): prevent release if tag major/minor isnt in metadata.yaml #90
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
Merged
Merged
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
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,94 @@ | ||
| /* | ||
| Copyright 2026 cloudscale.ch. | ||
|
|
||
| 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 main | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "errors" | ||
| "flag" | ||
| "fmt" | ||
| "os" | ||
| "regexp" | ||
| "strconv" | ||
|
|
||
| "k8s.io/apimachinery/pkg/util/yaml" | ||
| ) | ||
|
|
||
| var releaseTagPattern = regexp.MustCompile(`^v(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)(?:-[0-9A-Za-z.-]+)?(?:\+[0-9A-Za-z.-]+)?$`) | ||
|
|
||
| type providerMetadata struct { | ||
| ReleaseSeries []releaseSeries `json:"releaseSeries"` | ||
| } | ||
|
|
||
| type releaseSeries struct { | ||
| Major int `json:"major"` | ||
| Minor int `json:"minor"` | ||
| } | ||
|
|
||
| func main() { | ||
| tag := flag.String("tag", "", "release tag to verify") | ||
| metadataPath := flag.String("metadata", "metadata.yaml", "path to the clusterctl provider metadata") | ||
| flag.Parse() | ||
|
|
||
| metadata, err := os.ReadFile(*metadataPath) | ||
| if err != nil { | ||
| fmt.Fprintf(os.Stderr, "Error reading metadata: %v\n", err) | ||
| os.Exit(1) | ||
| } | ||
| if err := verifyReleaseSeries(*tag, metadata); err != nil { | ||
| fmt.Fprintf(os.Stderr, "Release metadata verification failed: %v\n", err) | ||
| os.Exit(1) | ||
| } | ||
|
|
||
| fmt.Printf("Verified that %s contains the release series for %s\n", *metadataPath, *tag) | ||
| } | ||
|
|
||
| func verifyReleaseSeries(tag string, metadataYAML []byte) error { | ||
| matches := releaseTagPattern.FindStringSubmatch(tag) | ||
| if matches == nil { | ||
| return fmt.Errorf("tag %q is not a valid semantic version prefixed with v", tag) | ||
| } | ||
|
|
||
| major, err := strconv.Atoi(matches[1]) | ||
| if err != nil { | ||
| return fmt.Errorf("parse tag major version: %w", err) | ||
| } | ||
| minor, err := strconv.Atoi(matches[2]) | ||
| if err != nil { | ||
| return fmt.Errorf("parse tag minor version: %w", err) | ||
| } | ||
|
|
||
| metadataJSON, err := yaml.ToJSON(metadataYAML) | ||
| if err != nil { | ||
| return fmt.Errorf("parse metadata: %w", err) | ||
| } | ||
| metadata := providerMetadata{} | ||
| if err := json.Unmarshal(metadataJSON, &metadata); err != nil { | ||
| return fmt.Errorf("parse metadata: %w", err) | ||
| } | ||
| if len(metadata.ReleaseSeries) == 0 { | ||
| return errors.New("metadata contains no release series") | ||
| } | ||
|
|
||
| for _, series := range metadata.ReleaseSeries { | ||
| if series.Major == major && series.Minor == minor { | ||
| return nil | ||
| } | ||
| } | ||
|
|
||
| return fmt.Errorf("metadata does not contain release series %d.%d required by tag %s", major, minor, tag) | ||
| } |
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,95 @@ | ||
| /* | ||
| Copyright 2026 cloudscale.ch. | ||
|
|
||
| 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 main | ||
|
|
||
| import ( | ||
| "strings" | ||
| "testing" | ||
| ) | ||
|
|
||
| func TestVerifyReleaseSeries(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| tag string | ||
| metadata string | ||
| wantError string | ||
| }{ | ||
| { | ||
| name: "stable release has a matching series", | ||
| tag: "v1.1.0", | ||
| metadata: `apiVersion: clusterctl.cluster.x-k8s.io/v1alpha3 | ||
| kind: Metadata | ||
| releaseSeries: | ||
| - major: 1 | ||
| minor: 1 | ||
| contract: v1beta2 | ||
| `, | ||
| }, | ||
| { | ||
| name: "prerelease has a matching series", | ||
| tag: "v1.2.0-beta.1", | ||
| metadata: `releaseSeries: | ||
| - major: 1 | ||
| minor: 2 | ||
| contract: v1beta2 | ||
| `, | ||
| }, | ||
| { | ||
| name: "matching major with wrong minor fails", | ||
| tag: "v1.1.0", | ||
| metadata: `releaseSeries: | ||
| - major: 1 | ||
| minor: 0 | ||
| contract: v1beta2 | ||
| `, | ||
| wantError: "release series 1.1", | ||
| }, | ||
| { | ||
| name: "invalid tag fails", | ||
| tag: "dev", | ||
| metadata: "releaseSeries: []\n", | ||
| wantError: "valid semantic version", | ||
| }, | ||
| { | ||
| name: "invalid metadata fails", | ||
| tag: "v1.1.0", | ||
| metadata: "releaseSeries: [\n", | ||
| wantError: "parse metadata", | ||
| }, | ||
| { | ||
| name: "empty release series fails", | ||
| tag: "v1.0.0", | ||
| metadata: "releaseSeries: []\n", | ||
| wantError: "no release series", | ||
| }, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| err := verifyReleaseSeries(tt.tag, []byte(tt.metadata)) | ||
| if tt.wantError == "" { | ||
| if err != nil { | ||
| t.Fatalf("verifyReleaseSeries() error = %v", err) | ||
| } | ||
| return | ||
| } | ||
| if err == nil || !strings.Contains(err.Error(), tt.wantError) { | ||
| t.Fatalf("verifyReleaseSeries() error = %v, want an error containing %q", err, tt.wantError) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
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.
Another error case we are handling but there is no test yet:
{ name: "empty release series fails", tag: "v1.0.0", metadata: "releaseSeries: []\n", wantError: "no release series", },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.
fair enough, added that test.