From 8d1494d5beb1ff80e475a37bbbfde7095b4ed4a6 Mon Sep 17 00:00:00 2001 From: Krisztian Litkey Date: Fri, 9 Feb 2024 10:17:17 +0200 Subject: [PATCH] resmgr: allow label keys with '/' in expressions. 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 --- pkg/resmgr/apis/expression.go | 9 +++++++-- pkg/resmgr/apis/expression_test.go | 22 ++++++++++++++++------ 2 files changed, 23 insertions(+), 8 deletions(-) diff --git a/pkg/resmgr/apis/expression.go b/pkg/resmgr/apis/expression.go index 1cfe0a0fd..859714489 100644 --- a/pkg/resmgr/apis/expression.go +++ b/pkg/resmgr/apis/expression.go @@ -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: @@ -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) diff --git a/pkg/resmgr/apis/expression_test.go b/pkg/resmgr/apis/expression_test.go index 52829c9c3..546906604 100644 --- a/pkg/resmgr/apis/expression_test.go +++ b/pkg/resmgr/apis/expression_test.go @@ -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 @@ -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", @@ -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, }, },