diff --git a/cmd/flux/version.go b/cmd/flux/version.go index 41f2114861..f02a161fb6 100644 --- a/cmd/flux/version.go +++ b/cmd/flux/version.go @@ -22,6 +22,7 @@ import ( "fmt" "strings" + "github.com/google/go-containerregistry/pkg/name" "github.com/spf13/cobra" v1 "k8s.io/api/apps/v1" "sigs.k8s.io/controller-runtime/pkg/client" @@ -119,12 +120,19 @@ func versionCmdRun(cmd *cobra.Command, args []string) error { } func splitImageStr(image string) (string, string, error) { - imageArr := strings.Split(image, ":") - if len(imageArr) < 2 { + ref, err := name.ParseReference(image) + if err != nil { + return "", "", fmt.Errorf("parsing image '%s' failed: %w", image, err) + } + + reg := ref.Context().RegistryStr() + repo := strings.TrimPrefix(image, reg) + parts := strings.Split(repo, ":") + if len(parts) < 2 { return "", "", fmt.Errorf("missing image tag in image %s", image) } - name, tag := imageArr[0], imageArr[1] - nameArr := strings.Split(name, "/") - return nameArr[len(nameArr)-1], tag, nil + n, t := parts[0], strings.TrimPrefix(repo, parts[0]+":") + nameArr := strings.Split(n, "/") + return nameArr[len(nameArr)-1], t, nil } diff --git a/cmd/flux/version_test.go b/cmd/flux/version_test.go new file mode 100644 index 0000000000..c209a32b20 --- /dev/null +++ b/cmd/flux/version_test.go @@ -0,0 +1,60 @@ +/* +Copyright 2021 The Flux 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 main + +import ( + "testing" + + . "github.com/onsi/gomega" +) + +func TestSplitImageStr(t *testing.T) { + tests := []struct { + url string + expectedName string + expectedTag string + }{ + { + url: "fluxcd/notification-controller:v1.0.0", + expectedName: "notification-controller", + expectedTag: "v1.0.0", + }, + { + url: "ghcr.io/fluxcd/kustomize-controller:v1.0.0", + expectedName: "kustomize-controller", + expectedTag: "v1.0.0", + }, + { + url: "reg.internal:8080/fluxcd/source-controller:v1.0.0", + expectedName: "source-controller", + expectedTag: "v1.0.0", + }, + { + url: "fluxcd/source-controller:v1.0.1@sha256:49921d1c7b100650dd654a32df1f6e626b54dfe9707d7bb7bdf43fb7c81f1baf", + expectedName: "source-controller", + expectedTag: "v1.0.1@sha256:49921d1c7b100650dd654a32df1f6e626b54dfe9707d7bb7bdf43fb7c81f1baf", + }, + } + + for _, tt := range tests { + g := NewWithT(t) + n, t, err := splitImageStr(tt.url) + g.Expect(err).To(Not(HaveOccurred())) + g.Expect(n).To(BeEquivalentTo(tt.expectedName)) + g.Expect(t).To(BeEquivalentTo(tt.expectedTag)) + } +}