@@ -19,7 +19,6 @@ import (
19
19
"fmt"
20
20
"strings"
21
21
22
- "github.com/awslabs/kit/operator/pkg/utils/functional"
23
22
v1 "k8s.io/api/core/v1"
24
23
"k8s.io/apimachinery/pkg/util/strategicpatch"
25
24
)
@@ -57,20 +56,42 @@ func mergePatch(defaultObj, patch, object interface{}) ([]byte, error) {
57
56
return patchedBytes , nil
58
57
}
59
58
59
+ // Keep the order of the args same, if the ordering changes when object is patched Kubernetes restarts the pod
60
60
func mergeContainerArgs (defaultSpec , patch * v1.PodSpec ) * v1.PodSpec {
61
- merged := []string {}
62
- for key , value := range functional .UnionStringMaps (parseArgsFor (defaultSpec ), parseArgsFor (patch )) {
63
- merged = append (merged , strings .Join ([]string {key , value }, "=" ))
61
+ patchedArgs := parseArgsFor (patch )
62
+ // get any additional args passed in patch
63
+ extraArgs := additionalArgs (parseArgsFor (defaultSpec ), patch )
64
+ updatedArgs := []string {}
65
+ // for all the args in defaultSpec, check if the value for an arg has been updated in patch
66
+ for _ , arg := range defaultSpec .Containers [0 ].Args {
67
+ kv := strings .Split (arg , "=" )
68
+ if update , ok := patchedArgs [kv [0 ]]; ok {
69
+ kv [1 ] = update
70
+ }
71
+ updatedArgs = append (updatedArgs , strings .Join (kv , "=" ))
64
72
}
65
- patch .Containers [0 ].Args = merged
73
+ patch .Containers [0 ].Args = append ( updatedArgs , extraArgs ... )
66
74
return patch
67
75
}
68
76
69
77
func parseArgsFor (podSpec * v1.PodSpec ) map [string ]string {
70
78
result := map [string ]string {}
71
79
for _ , arg := range podSpec .Containers [0 ].Args {
72
80
kv := strings .Split (arg , "=" )
73
- result [kv [0 ]] = result [kv [1 ]]
81
+ result [kv [0 ]] = kv [1 ]
82
+ }
83
+ return result
84
+ }
85
+
86
+ // needs to preserve the order of args passed in patch in every iteration
87
+ func additionalArgs (defaultSpec map [string ]string , patch * v1.PodSpec ) []string {
88
+ result := make ([]string , 0 )
89
+ for _ , arg := range patch .Containers [0 ].Args {
90
+ kv := strings .Split (arg , "=" )
91
+ if _ , ok := defaultSpec [kv [0 ]]; ok {
92
+ continue
93
+ }
94
+ result = append (result , arg )
74
95
}
75
96
return result
76
97
}
0 commit comments