-
Notifications
You must be signed in to change notification settings - Fork 8
feat(xpkg): add --annotation flag to xpkg build and xpkg push #11
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
chaitanyapantheor
wants to merge
6
commits into
crossplane:main
Choose a base branch
from
chaitanyapantheor:issues/7282
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.
+177
−11
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
4386cea
feat(xpkg): add parseAnnotations helper for OCI manifest annotations
chaitanyapantheor 7ec8790
feat(xpkg): add --annotation flag to xpkg build
chaitanyapantheor a119d6e
feat(xpkg): add --annotation flag to xpkg push
chaitanyapantheor 73f8bf9
fix(xpkg): address golangci-lint issues in annotation support
chaitanyapantheor 29c10b2
fix(xpkg): address PR review feedback on annotation support
chaitanyapantheor f40a61f
fix(xpkg): reject empty keys in parseAnnotations
chaitanyapantheor 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,61 @@ | ||
| /* | ||
| Copyright 2026 The Crossplane 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. | ||
| */ | ||
|
|
||
| package xpkg | ||
|
|
||
| import ( | ||
| "strings" | ||
|
|
||
| v1 "github.com/google/go-containerregistry/pkg/v1" | ||
| "github.com/google/go-containerregistry/pkg/v1/mutate" | ||
|
|
||
| "github.com/crossplane/crossplane-runtime/v2/pkg/errors" | ||
| ) | ||
|
|
||
| // parseAnnotations parses a slice of "key=value" strings into a map. Returns | ||
| // an error if any entry is not in key=value format. | ||
| func parseAnnotations(kvs []string) (map[string]string, error) { | ||
| anns := make(map[string]string, len(kvs)) | ||
| for _, kv := range kvs { | ||
| k, v, ok := strings.Cut(kv, "=") | ||
| if !ok { | ||
| return nil, errors.Errorf("invalid annotation %q: must be in key=value format", kv) | ||
| } | ||
| if k == "" { | ||
| return nil, errors.Errorf("invalid annotation %q: key must not be empty", kv) | ||
| } | ||
| anns[k] = v | ||
| } | ||
| return anns, nil | ||
| } | ||
|
|
||
| // annotateImage applies annotations to an OCI image manifest. It is a no-op | ||
| // when annotations is empty or nil. | ||
| func annotateImage(img v1.Image, annotations map[string]string) v1.Image { | ||
| if len(annotations) == 0 { | ||
| return img | ||
| } | ||
| return mutate.Annotations(img, annotations).(v1.Image) //nolint:forcetypeassert // mutate.Annotations always returns v1.Image when given v1.Image input | ||
| } | ||
|
|
||
| // annotateIndex applies annotations to an OCI image index manifest. It is a | ||
| // no-op when annotations is empty or nil. | ||
| func annotateIndex(idx v1.ImageIndex, annotations map[string]string) v1.ImageIndex { | ||
| if len(annotations) == 0 { | ||
| return idx | ||
| } | ||
| return mutate.Annotations(idx, annotations).(v1.ImageIndex) //nolint:forcetypeassert // mutate.Annotations always returns v1.ImageIndex when given v1.ImageIndex input | ||
| } |
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,86 @@ | ||
| /* | ||
| Copyright 2026 The Crossplane 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. | ||
| */ | ||
|
|
||
| package xpkg | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/google/go-cmp/cmp" | ||
| "github.com/google/go-cmp/cmp/cmpopts" | ||
| ) | ||
|
|
||
| func TestParseAnnotations(t *testing.T) { | ||
| type args struct { | ||
| kvs []string | ||
| } | ||
| type want struct { | ||
| anns map[string]string | ||
| err error | ||
| } | ||
|
|
||
| cases := map[string]struct { | ||
| reason string | ||
| args args | ||
| want want | ||
| }{ | ||
| "EmptySlice": { | ||
| reason: "Empty input should return an empty map with no error.", | ||
| args: args{kvs: []string{}}, | ||
| want: want{anns: map[string]string{}}, | ||
| }, | ||
| "SingleEntry": { | ||
| reason: "A single valid key=value entry should be parsed correctly.", | ||
| args: args{kvs: []string{"org.example/key=value"}}, | ||
| want: want{anns: map[string]string{"org.example/key": "value"}}, | ||
| }, | ||
| "MultipleEntries": { | ||
| reason: "Multiple valid key=value entries should all be parsed.", | ||
| args: args{kvs: []string{ | ||
| "org.opencontainers.image.source=https://github.com/example/pkg", | ||
| "org.opencontainers.image.version=v1.0.0", | ||
| }}, | ||
| want: want{anns: map[string]string{ | ||
| "org.opencontainers.image.source": "https://github.com/example/pkg", | ||
| "org.opencontainers.image.version": "v1.0.0", | ||
| }}, | ||
| }, | ||
| "ValueContainsEquals": { | ||
| reason: "Values that contain '=' characters should be preserved intact.", | ||
| args: args{kvs: []string{"key=val=ue"}}, | ||
| want: want{anns: map[string]string{"key": "val=ue"}}, | ||
| }, | ||
| "MissingEquals": { | ||
| reason: "An entry without '=' should return an error.", | ||
| args: args{kvs: []string{"invalid-no-equals"}}, | ||
| want: want{err: cmpopts.AnyError}, | ||
| }, | ||
| } | ||
|
|
||
| for name, tc := range cases { | ||
| t.Run(name, func(t *testing.T) { | ||
| got, err := parseAnnotations(tc.args.kvs) | ||
|
|
||
| if diff := cmp.Diff(tc.want.err, err, cmpopts.EquateErrors()); diff != "" { | ||
| t.Errorf("\n%s\nparseAnnotations(...): -want error, +got error:\n%s", tc.reason, diff) | ||
| } | ||
|
|
||
| if diff := cmp.Diff(tc.want.anns, got); diff != "" { | ||
| t.Errorf("\n%s\nparseAnnotations(...): -want, +got:\n%s", tc.reason, diff) | ||
| } | ||
| }) | ||
| } | ||
| } |
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
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.
Should we apply the same annotations to the index (built below with
mutate.AppendManifests)?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.
Added
annotateIndexhelper (mirrorsannotateImage) and applied it to the index beforeremote.WriteIndexin the multi-platform path. Annotations now appear on both the individual manifests and the top-level index.