From 728ef5c4b918d6c4f5e977b3716b823922255c40 Mon Sep 17 00:00:00 2001 From: Daishan Peng Date: Thu, 26 Jul 2018 11:41:44 -0700 Subject: [PATCH] sort env key Problems: When we created workloads in rancher api, the environment variable field is converted without a specific order.(we simplely loop in the map). This will cause K8s workload spec to change constantly if we do a update, and cause the pod getting recreated. Solutions: Sort the map and make sure they always follow the same order in k8s workload spec. --- mapper/env.go | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/mapper/env.go b/mapper/env.go index e6c0d3e3..bd80532e 100644 --- a/mapper/env.go +++ b/mapper/env.go @@ -1,6 +1,8 @@ package mapper import ( + "sort" + "github.com/rancher/norman/types" "github.com/rancher/norman/types/convert" "k8s.io/api/core/v1" @@ -98,10 +100,16 @@ func (e EnvironmentMapper) ToInternal(data map[string]interface{}) error { var envVar []map[string]interface{} var envVarFrom []map[string]interface{} - for key, value := range convert.ToMapInterface(data["environment"]) { + var orderedKeys []string + environment := convert.ToMapInterface(data["environment"]) + for k := range environment { + orderedKeys = append(orderedKeys, k) + } + sort.Strings(orderedKeys) + for _, key := range orderedKeys { envVar = append(envVar, map[string]interface{}{ "name": key, - "value": value, + "value": environment[key], }) }