Fix controller version info

Signed-off-by: Stefan Prodan <stefan.prodan@gmail.com>
This commit is contained in:
Stefan Prodan
2023-08-22 12:36:58 +03:00
parent eac82585ad
commit 4cdb75b74d
2 changed files with 73 additions and 5 deletions

View File

@ -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
}

60
cmd/flux/version_test.go Normal file
View File

@ -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))
}
}