Skip to content

Commit

Permalink
Fix controller version info
Browse files Browse the repository at this point in the history
Signed-off-by: Stefan Prodan <[email protected]>
  • Loading branch information
stefanprodan committed Aug 22, 2023
1 parent eac8258 commit 4cdb75b
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 5 deletions.
18 changes: 13 additions & 5 deletions cmd/flux/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 changes: 60 additions & 0 deletions cmd/flux/version_test.go
Original file line number Diff line number Diff line change
@@ -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))
}
}

0 comments on commit 4cdb75b

Please sign in to comment.