Skip to content

Commit

Permalink
Keep highest version in gcp script
Browse files Browse the repository at this point in the history
Signed-off-by: Tamal Saha <[email protected]>
  • Loading branch information
tamalsaha committed Oct 18, 2024
1 parent 64e45b1 commit b2bfc2f
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
1 change: 1 addition & 0 deletions pkg/cmds/gcp_script.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
28 changes: 24 additions & 4 deletions pkg/cmds/scripts.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit b2bfc2f

Please sign in to comment.