From b2bfc2f7925584cc55ec635ca8865924a852c320 Mon Sep 17 00:00:00 2001 From: Tamal Saha Date: Thu, 17 Oct 2024 21:52:17 -0700 Subject: [PATCH] Keep highest version in gcp script Signed-off-by: Tamal Saha --- pkg/cmds/gcp_script.go | 1 + pkg/cmds/scripts.go | 28 ++++++++++++++++++++++++---- 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/pkg/cmds/gcp_script.go b/pkg/cmds/gcp_script.go index ee6735a..1a6249c 100644 --- a/pkg/cmds/gcp_script.go +++ b/pkg/cmds/gcp_script.go @@ -54,6 +54,7 @@ func NewCmdGenerateGCPScript() *cobra.Command { } var gcpImageMap = map[string]string{ + "defaultbackend-amd64": "ingress-nginx-defaultbackend", "fluxcd/helm-controller": "flux-helm-controller", "fluxcd/source-controller": "flux-source-controller", "ingress-nginx/controller": "ingress-nginx-controller", diff --git a/pkg/cmds/scripts.go b/pkg/cmds/scripts.go index 01d3ddf..c4a6a8f 100644 --- a/pkg/cmds/scripts.go +++ b/pkg/cmds/scripts.go @@ -24,12 +24,13 @@ import ( "net/url" "os" "path/filepath" + "sort" "strings" "kmodules.xyz/go-containerregistry/name" + "github.com/Masterminds/semver/v3" "github.com/spf13/cobra" - "k8s.io/apimachinery/pkg/util/sets" "sigs.k8s.io/yaml" ) @@ -58,16 +59,35 @@ func NewCmdGenerateScripts() *cobra.Command { } func generateImageList(files []string) ([]string, error) { - images := sets.Set[string]{} + images := map[string]string{} for _, file := range files { list, err := readImageList(file) if err != nil { return nil, fmt.Errorf("failed to read image list from %s: %w", file, err) } - images.Insert(list...) + + for _, entry := range list { + img, tag, ok := strings.Cut(entry, ":") + if !ok { + images[entry] = "" + } + if existing, ok := images[img]; !ok || semver.MustParse(tag).GreaterThan(semver.MustParse(existing)) { + images[img] = tag + } + } + } + + result := make([]string, 0, len(images)) + for image, tag := range images { + if tag == "" { + result = append(result, image) + } else { + result = append(result, image+":"+tag) + } } - return sets.List(images), nil + sort.Strings(result) + return result, nil } func readImageList(file string) ([]string, error) {