From 2d51c38f574ccf6f96512d99fa220d865d6cb158 Mon Sep 17 00:00:00 2001 From: Matthieu Huin Date: Wed, 29 May 2024 10:46:54 +0200 Subject: [PATCH] Ensure looping over unmarshalled YAML data is consistent We noticed that unmarshalling data from YAML to maps may not be consistent in Golang, especially when it comes to key order. Add a step to sort keys whenever looping over data that has been unmarshalled, so that loops are consistent over time. Change-Id: Ia7549d8b6adc979b4193131e06e3ac32edef5a84 --- cli/cmd/restore.go | 10 +++++++--- controllers/libs/base/images.go | 10 +++++++++- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/cli/cmd/restore.go b/cli/cmd/restore.go index a07aac12..39b25a43 100644 --- a/cli/cmd/restore.go +++ b/cli/cmd/restore.go @@ -25,11 +25,13 @@ import ( "fmt" "os" "path/filepath" + "sort" cliutils "github.com/softwarefactory-project/sf-operator/cli/cmd/utils" controllers "github.com/softwarefactory-project/sf-operator/controllers" "github.com/spf13/cobra" + "golang.org/x/exp/maps" appsv1 "k8s.io/api/apps/v1" apiv1 "k8s.io/api/core/v1" @@ -47,11 +49,13 @@ func restoreSecret(backupDir string, env cliutils.ENV) { secret := apiv1.Secret{} if cliutils.GetMOrDie(&env, sec, &secret) { secretMap := secretContent["data"].(map[string]interface{}) - for key, value := range secretMap { - stringValue, ok := value.(string) + secretMapKeys := maps.Keys(secretMap) + sort.Strings(secretMapKeys) + for _, key := range secretMapKeys { + stringValue, ok := secretMap[key].(string) if !ok { ctrl.Log.Error(errors.New("can not convert secret data value to string"), - "Can not restore secret"+sec) + "Can not restore secret "+sec) os.Exit(1) } secret.Data[key] = []byte(stringValue) diff --git a/controllers/libs/base/images.go b/controllers/libs/base/images.go index 7a343b4a..c0be4a03 100644 --- a/controllers/libs/base/images.go +++ b/controllers/libs/base/images.go @@ -6,7 +6,9 @@ package base import ( _ "embed" + "sort" + "golang.org/x/exp/maps" "gopkg.in/yaml.v2" ) @@ -44,13 +46,19 @@ func getImage(name string) string { } func GetSelfManagedImages() []Image { + imagesByName := make(map[string]Image) ret := []Image{} images := loadImages() for _, image := range images.Images { if image.Source != "" { - ret = append(ret, image) + imagesByName[image.Name] = image } } + imageNames := maps.Keys(imagesByName) + sort.Strings(imageNames) + for _, imageName := range imageNames { + ret = append(ret, imagesByName[imageName]) + } return ret }