Skip to content

Commit a87e94b

Browse files
alexecsarabala1979
authored andcommitted
fix: Fix expression template random errors. Fixes argoproj#6673 (argoproj#6786)
Signed-off-by: Alex Collins <[email protected]>
1 parent 254c730 commit a87e94b

File tree

3 files changed

+65
-3
lines changed

3 files changed

+65
-3
lines changed

util/expand/expand.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package expand
2+
3+
import (
4+
"sort"
5+
"strings"
6+
7+
"github.com/doublerebel/bellows"
8+
)
9+
10+
func Expand(m map[string]interface{}) map[string]interface{} {
11+
return bellows.Expand(removeConflicts(m))
12+
}
13+
14+
// It is possible for the map to contain conflicts:
15+
// {"a.b": 1, "a": 2}
16+
// What should the result be? We remove the less-specific key.
17+
// {"a.b": 1, "a": 2} -> {"a.b": 1, "a": 2}
18+
func removeConflicts(m map[string]interface{}) map[string]interface{} {
19+
var keys []string
20+
n := map[string]interface{}{}
21+
for k, v := range m {
22+
keys = append(keys, k)
23+
n[k] = v
24+
}
25+
sort.Strings(keys)
26+
for i := 0; i < len(keys)-1; i++ {
27+
k := keys[i]
28+
// remove any parent that has a child
29+
if strings.HasPrefix(keys[i+1]+".", k) {
30+
delete(n, k)
31+
}
32+
}
33+
return n
34+
}

util/expand/expand_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package expand
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/stretchr/testify/assert"
8+
)
9+
10+
func TestExpand(t *testing.T) {
11+
for i := 0; i < 100; i++ { // loop 100 times, because map ordering is not determisitic
12+
t.Run(fmt.Sprint(i), func(t *testing.T) {
13+
before := map[string]interface{}{
14+
"a.b": 1,
15+
"a": 2,
16+
"ab": 3,
17+
}
18+
after := Expand(before)
19+
assert.Len(t, before, 3, "original map unchanged")
20+
assert.Equal(t, map[string]interface{}{
21+
"a": map[string]interface{}{
22+
"b": 1,
23+
},
24+
"ab": 3,
25+
}, after)
26+
})
27+
}
28+
}

util/expr/env/env.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ import (
44
"encoding/json"
55

66
"github.com/Masterminds/sprig"
7-
87
exprpkg "github.com/argoproj/pkg/expr"
9-
"github.com/doublerebel/bellows"
8+
9+
"github.com/argoproj/argo-workflows/v3/util/expand"
1010
)
1111

1212
var sprigFuncMap = sprig.GenericFuncMap() // a singleton for better performance
@@ -17,7 +17,7 @@ func init() {
1717
}
1818

1919
func GetFuncMap(m map[string]interface{}) map[string]interface{} {
20-
env := bellows.Expand(m)
20+
env := expand.Expand(m)
2121
for k, v := range exprpkg.GetExprEnvFunctionMap() {
2222
env[k] = v
2323
}

0 commit comments

Comments
 (0)