Skip to content

fix: change go struct to go map #131

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 18, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 50 additions & 2 deletions modules/firehose/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/goto/entropy/pkg/errors"
"github.com/goto/entropy/pkg/helm"
"github.com/goto/entropy/pkg/kube"
"github.com/mitchellh/mapstructure"
)

const (
Expand Down Expand Up @@ -310,6 +311,9 @@ func (fd *firehoseDriver) getHelmRelease(res resource.Resource, conf Config,
imageRepository = conf.ChartValues.ImageRepository
}

requiredDuringSchedulingIgnoredDuringExecutionInterface := preferenceSliceToInterfaceSlice(requiredDuringSchedulingIgnoredDuringExecution)
preferredDuringSchedulingIgnoredDuringExecutionInterface := weightedPreferencesToInterfaceSlice(preferredDuringSchedulingIgnoredDuringExecution)

rc.Values = map[string]any{
labelsConfKey: modules.CloneAndMergeMaps(deploymentLabels, entropyLabels),
"replicaCount": conf.Replicas,
Expand All @@ -333,8 +337,8 @@ func (fd *firehoseDriver) getHelmRelease(res resource.Resource, conf Config,
},
"tolerations": tolerations,
"nodeAffinityMatchExpressions": map[string]any{
"requiredDuringSchedulingIgnoredDuringExecution": requiredDuringSchedulingIgnoredDuringExecution,
"preferredDuringSchedulingIgnoredDuringExecution": preferredDuringSchedulingIgnoredDuringExecution,
"requiredDuringSchedulingIgnoredDuringExecution": requiredDuringSchedulingIgnoredDuringExecutionInterface,
"preferredDuringSchedulingIgnoredDuringExecution": preferredDuringSchedulingIgnoredDuringExecutionInterface,
},
"init-firehose": map[string]any{
"enabled": fd.conf.InitContainer.Enabled,
Expand Down Expand Up @@ -467,3 +471,47 @@ func renderTplOfMapStringAny(labelsTpl map[string]any, labelsValues map[string]s

return labelsTpl, nil
}

func preferenceSliceToInterfaceSlice(prefs []Preference) []map[string]interface{} {
result := make([]map[string]interface{}, len(prefs))

for i, pref := range prefs {
var prefMap map[string]interface{}
if err := mapstructure.Decode(pref, &prefMap); err != nil {
continue
}

lowercaseMap := make(map[string]interface{})
for k, v := range prefMap {
lowercaseMap[strings.ToLower(k)] = v
}
result[i] = lowercaseMap
}

return result
}

func weightedPreferencesToInterfaceSlice(weightedPrefs []WeightedPreference) []map[string]interface{} {
result := make([]map[string]interface{}, len(weightedPrefs))

for i, wp := range weightedPrefs {
var wpMap map[string]interface{}
if err := mapstructure.Decode(wp, &wpMap); err != nil {
continue
}

lowercaseMap := make(map[string]interface{})
for k, v := range wpMap {
// Special handling for the preference field
if k == "Preference" && v != nil {
// Convert the nested Preference slice
lowercaseMap["preference"] = preferenceSliceToInterfaceSlice(wp.Preference)
} else {
lowercaseMap[strings.ToLower(k)] = v
}
}
result[i] = lowercaseMap
}

return result
}
Loading