Skip to content

Commit

Permalink
Ensure looping over unmarshalled YAML data is consistent
Browse files Browse the repository at this point in the history
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
  • Loading branch information
mhuin committed Aug 27, 2024
1 parent 9f6bede commit 2d51c38
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
10 changes: 7 additions & 3 deletions cli/cmd/restore.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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)
Expand Down
10 changes: 9 additions & 1 deletion controllers/libs/base/images.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ package base

import (
_ "embed"
"sort"

"golang.org/x/exp/maps"
"gopkg.in/yaml.v2"
)

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

Expand Down

0 comments on commit 2d51c38

Please sign in to comment.