-
Notifications
You must be signed in to change notification settings - Fork 257
Local tag lister for semantic version constraint matching #4458
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
mozesl-nokia
wants to merge
4
commits into
kptdev:main
Choose a base branch
from
nokia:local-tag-lister
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
4 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| package fnruntime | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "context" | ||
| "fmt" | ||
| "os/exec" | ||
| "strings" | ||
|
|
||
| "github.com/regclient/regclient" | ||
| regclientref "github.com/regclient/regclient/types/ref" | ||
| ) | ||
|
|
||
| // RegClientLister is a TagLister using the regclient module to list remote OCI tags. | ||
| type RegClientLister struct { | ||
| client *regclient.RegClient | ||
| } | ||
|
|
||
| var _ TagLister = &RegClientLister{} | ||
|
|
||
| func (l *RegClientLister) Name() string { | ||
| return "regclient" | ||
| } | ||
|
|
||
| func (l *RegClientLister) List(ctx context.Context, image string) ([]string, error) { | ||
| ref, err := regclientref.New(image) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| defer func() { _ = l.client.Close(ctx, ref) }() | ||
|
|
||
| tagList, err := l.client.TagList(ctx, ref) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| return tagList.GetTags() | ||
| } | ||
|
|
||
| // LocalLister is a TagLister using the given CLI tool to list local OCI tags | ||
| type LocalLister struct { | ||
| Binary string | ||
| } | ||
|
|
||
| var _ TagLister = &LocalLister{} | ||
|
|
||
| func (l *LocalLister) Name() string { | ||
| return "local-" + l.Binary | ||
| } | ||
|
|
||
| func (l *LocalLister) List(ctx context.Context, image string) ([]string, error) { | ||
| command := exec.CommandContext(ctx, l.Binary, "image", "ls", "--filter", "reference="+image, "--format", "{{ .Tag }}") | ||
|
|
||
| stdout := &bytes.Buffer{} | ||
| stderr := &bytes.Buffer{} | ||
| command.Stdout = stdout | ||
| command.Stderr = stderr | ||
|
|
||
| if err := command.Run(); err != nil { | ||
| return nil, fmt.Errorf("failed to list local tags using %q: %w; stderr: %s", l.Binary, err, stderr.String()) | ||
| } | ||
|
|
||
| return linesToSlice(stdout.String()), nil | ||
| } | ||
mozesl-nokia marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| func linesToSlice(in string) []string { | ||
| in = strings.TrimSpace(in) | ||
| in = strings.ReplaceAll(in, "\r\n", "\n") | ||
| var out []string | ||
| for line := range strings.Lines(in) { | ||
| line = strings.TrimSpace(line) | ||
mozesl-nokia marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if line != "" { | ||
| out = append(out, line) | ||
| } | ||
| } | ||
|
|
||
| return out | ||
| } | ||
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,100 @@ | ||
| // Copyright 2026 The kpt 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 fnruntime | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| ) | ||
|
|
||
| func TestLinesToSlice(t *testing.T) { | ||
| tests := map[string]struct { | ||
| input string | ||
| expected []string | ||
| }{ | ||
| "empty": { | ||
| input: "", | ||
| expected: nil, | ||
| }, | ||
| "whitespace only": { | ||
| input: " \t\n ", | ||
| expected: nil, | ||
| }, | ||
| "single line": { | ||
| input: "v1.0.0", | ||
| expected: []string{"v1.0.0"}, | ||
| }, | ||
| "single line with surrounding space": { | ||
| input: " v1.0.0 \n", | ||
| expected: []string{"v1.0.0"}, | ||
| }, | ||
| "multiple lines": { | ||
| input: "v1.0.0\nv1.0.1\nv1.0.2", | ||
| expected: []string{"v1.0.0", "v1.0.1", "v1.0.2"}, | ||
| }, | ||
| "windows line endings": { | ||
| input: "a\r\nb\r\nc", | ||
| expected: []string{"a", "b", "c"}, | ||
| }, | ||
| "blank line not preserved": { | ||
| input: "a\n\nb", | ||
| expected: []string{"a", "b"}, | ||
| }, | ||
| "per-line trim": { | ||
| input: " foo \n bar ", | ||
| expected: []string{"foo", "bar"}, | ||
| }, | ||
| } | ||
|
|
||
| for name, tc := range tests { | ||
| t.Run(name, func(t *testing.T) { | ||
| got := linesToSlice(tc.input) | ||
| assert.Equal(t, tc.expected, got) | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestRegClientLister_Name(t *testing.T) { | ||
| l := &RegClientLister{} | ||
| assert.Equal(t, "regclient", l.Name()) | ||
| } | ||
|
|
||
| func TestLocalLister_Name(t *testing.T) { | ||
| tests := map[string]struct { | ||
| binary string | ||
| expected string | ||
| }{ | ||
| "docker": { | ||
| binary: "docker", | ||
| expected: "local-docker", | ||
| }, | ||
| "empty binary": { | ||
| binary: "", | ||
| expected: "local-", | ||
| }, | ||
| "path-like binary": { | ||
| binary: "/usr/local/bin/nerdctl", | ||
| expected: "local-/usr/local/bin/nerdctl", | ||
| }, | ||
| } | ||
|
|
||
| for name, tc := range tests { | ||
| t.Run(name, func(t *testing.T) { | ||
| l := &LocalLister{Binary: tc.binary} | ||
| assert.Equal(t, tc.expected, l.Name()) | ||
| }) | ||
| } | ||
| } |
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
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.
The
//go:build dockerbuild tag has been removed from this test file. TheTestContainerFntest attempts to pull and run actual Docker containers (line 38-39), which will fail on systems without Docker installed. This removal may cause test suite failures on such systems. Consider restoring the build tag to ensure the test only runs in Docker-capable environments, or refactor the test to use mocks instead of real containers.