Skip to content
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

fix(controller): agent image name parsing (backport #42) #50

Merged
merged 1 commit into from
Feb 25, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 27 additions & 10 deletions cmd/controller/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,10 @@ var rootCmd = &cobra.Command{
}
},
Run: func(cmd *cobra.Command, args []string) {
var image *config.Image
imageTokens := strings.Split(agentImage, ":")
if len(imageTokens) == 2 {
image = config.NewImage(imageTokens[0], imageTokens[1])
} else {
fmt.Fprintf(os.Stderr, "Error parse agent image name\n")
if err := cmd.Help(); err != nil {
os.Exit(1)
}
os.Exit(0)
image, err := parseImageNameAndTag(agentImage)
if err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
os.Exit(1)
}

options := &config.ControllerOptions{
Expand Down Expand Up @@ -96,3 +90,26 @@ func init() {
func execute() {
cobra.CheckErr(rootCmd.Execute())
}

func parseImageNameAndTag(image string) (*config.Image, error) {
idx := strings.LastIndex(image, ":")

if idx == -1 {
return config.NewImage(image, "latest"), nil
}

// If the last colon is immediately followed by the end of the string, it's invalid (no tag).
if idx == len(image)-1 {
return nil, fmt.Errorf("invalid image name: colon without tag")
}

if strings.Count(image, ":") > 2 {
return nil, fmt.Errorf("invalid image name: multiple colons found")
}

if idx <= strings.LastIndex(image, "/") {
return config.NewImage(image, "latest"), nil
}

return config.NewImage(image[:idx], image[idx+1:]), nil
}
78 changes: 78 additions & 0 deletions cmd/controller/root_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package main

import (
"testing"

"github.com/harvester/vm-dhcp-controller/pkg/config"
"github.com/stretchr/testify/assert"
)

func TestParseImageNameAndTag(t *testing.T) {
tests := []struct {
name string
image string
expected *config.Image
err bool
}{
{
name: "valid image with registry and tag",
image: "myregistry.local:5000/rancher/harvester-vm-dhcp-controller:v0.3.3",
expected: &config.Image{
Repository: "myregistry.local:5000/rancher/harvester-vm-dhcp-controller",
Tag: "v0.3.3",
},
err: false,
},
{
name: "valid image with only image and tag",
image: "rancher/harvester-vm-dhcp-controller:v0.3.3",
expected: &config.Image{
Repository: "rancher/harvester-vm-dhcp-controller",
Tag: "v0.3.3",
},
err: false,
},
{
name: "valid image without tag",
image: "rancher/harvester-vm-dhcp-controller",
expected: &config.Image{
Repository: "rancher/harvester-vm-dhcp-controller",
Tag: "latest",
},
err: false,
},
{
name: "valid image with port but no tag",
image: "myregistry.local:5000/rancher/harvester-vm-dhcp-controller",
expected: &config.Image{
Repository: "myregistry.local:5000/rancher/harvester-vm-dhcp-controller",
Tag: "latest",
},
err: false,
},
{
name: "invalid image with colon but no tag",
image: "myregistry.local:5000/rancher/harvester-vm-dhcp-controller:",
expected: nil,
err: true,
},
{
name: "invalid image with multiple colons",
image: "myregistry.local:5000/rancher/harvester-vm-dhcp-controller:v0.3.3:latest",
expected: nil,
err: true,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result, err := parseImageNameAndTag(tt.image)
if tt.err {
assert.Error(t, err)
} else {
assert.NoError(t, err)
assert.Equal(t, tt.expected, result)
}
})
}
}
Loading