Skip to content

Commit

Permalink
resmgr: allow label keys with '/' in expressions.
Browse files Browse the repository at this point in the history
Update affinity expression reference resolution to use the
re-joint tail of the remaining reference as a key whenever
we're resolving entries in a map[string]string. This allows
using domain-prefixed label keys in affinity expressions.

Signed-off-by: Krisztian Litkey <[email protected]>
  • Loading branch information
klihub committed Feb 9, 2024
1 parent 475d293 commit 3666878
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 8 deletions.
9 changes: 7 additions & 2 deletions pkg/resmgr/apis/expression.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,12 +250,18 @@ func ResolveRef(subject Evaluable, spec string) (string, bool, error) {
switch v := obj.(type) {
case string:
obj = v
ref = ref[1:]
case map[string]string:
if len(ref) > 1 {
key = strings.Join(ref, "/")
log.Debug(" - using tailing ref %s as map key...", key)
}
value, ok := v[key]
if !ok {
return "", false, nil
}
obj = value
ref = nil
case error:
return "", false, exprError("%s: failed to resolve %q: %v", subject, spec, v)
default:
Expand All @@ -265,9 +271,8 @@ func ResolveRef(subject Evaluable, spec string) (string, bool, error) {
subject, spec, obj)
}
obj = e.Eval(key)
ref = ref[1:]
}

ref = ref[1:]
}

str, ok := obj.(string)
Expand Down
22 changes: 16 additions & 6 deletions pkg/resmgr/apis/expression_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,12 @@ func TestResolveRefAndKeyValue(t *testing.T) {
defer logger.Flush()

pod := newEvaluable("P1", "pns", "pqos",
map[string]string{"l1": "plone", "l2": "pltwo", "l5": "plfive"}, nil, nil)
map[string]string{
"l1": "plone",
"l2": "pltwo",
"l5": "plfive",
"io.test/label1": "io.test/value1",
}, nil, nil)

tcases := []struct {
name string
Expand All @@ -100,7 +105,11 @@ func TestResolveRefAndKeyValue(t *testing.T) {
{
name: "test resolving references",
subject: newEvaluable("C1", "cns", "cqos",
map[string]string{"l1": "clone", "l2": "cltwo", "l3": "clthree"},
map[string]string{
"l1": "clone",
"l2": "cltwo",
"l3": "clthree",
},
map[string]string{"t1": "ctone", "t2": "cttwo", "t3": "ctthree"}, pod),
keys: []string{
"name", "namespace", "qosclass",
Expand All @@ -111,34 +120,35 @@ func TestResolveRefAndKeyValue(t *testing.T) {
"pod/labels/l3",
"pod/labels/l4",
"pod/labels/l5",
"pod/labels/io.test/label1",
":,-pod/qosclass,pod/namespace,pod/name,name",
},
values: []string{
"C1", "cns", "cqos",
"clone", "cltwo", "clthree", "",
"ctone", "cttwo", "ctthree", "",
"plone", "pltwo", "", "", "plfive",
"plone", "pltwo", "", "", "plfive", "io.test/value1",
"",
},
keyvalues: []string{
"C1", "cns", "cqos",
"clone", "cltwo", "clthree", "",
"ctone", "cttwo", "ctthree", "",
"plone", "pltwo", "", "", "plfive",
"plone", "pltwo", "", "", "plfive", "io.test/value1",
"pqos-pns-P1-C1",
},
ok: []bool{
true, true, true,
true, true, true, false,
true, true, true, false,
true, true, false, false, true,
true, true, false, false, true, true,
false,
},
error: []bool{
false, false, false,
false, false, false, false,
false, false, false, false,
false, false, false, false, false,
false, false, false, false, false, false,
true,
},
},
Expand Down

0 comments on commit 3666878

Please sign in to comment.