1
0
Fork 0
mirror of https://github.com/kubernetes-sigs/node-feature-discovery.git synced 2025-03-30 19:54:46 +00:00

Merge pull request #1481 from marquiz/devel/api-creation-funcs

apis/nfd: drop creation helper functions
This commit is contained in:
Kubernetes Prow Robot 2023-12-14 18:41:18 +01:00 committed by GitHub
commit 443ff80748
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 42 additions and 76 deletions

View file

@ -193,16 +193,11 @@ spec:
type: string type: string
matchExpressions: matchExpressions:
additionalProperties: additionalProperties:
description: "MatchExpression specifies an expression description: MatchExpression specifies an expression
to evaluate against a set of input values. It to evaluate against a set of input values. It
contains an operator that is applied when matching contains an operator that is applied when matching
the input and an array of values that the operator the input and an array of values that the operator
evaluates the input against. \n NB: CreateMatchExpression evaluates the input against.
or MustCreateMatchExpression() should be used
for creating new instances. \n NB: Validate()
must be called if Op or Value fields are modified
or if a new instance is created from scratch
without using the helper functions."
properties: properties:
op: op:
description: Op is the operator to be applied. description: Op is the operator to be applied.
@ -259,15 +254,11 @@ spec:
type: string type: string
matchExpressions: matchExpressions:
additionalProperties: additionalProperties:
description: "MatchExpression specifies an expression description: MatchExpression specifies an expression
to evaluate against a set of input values. It contains to evaluate against a set of input values. It contains
an operator that is applied when matching the input an operator that is applied when matching the input
and an array of values that the operator evaluates and an array of values that the operator evaluates
the input against. \n NB: CreateMatchExpression or the input against.
MustCreateMatchExpression() should be used for creating
new instances. \n NB: Validate() must be called if
Op or Value fields are modified or if a new instance
is created from scratch without using the helper functions."
properties: properties:
op: op:
description: Op is the operator to be applied. description: Op is the operator to be applied.

View file

@ -193,16 +193,11 @@ spec:
type: string type: string
matchExpressions: matchExpressions:
additionalProperties: additionalProperties:
description: "MatchExpression specifies an expression description: MatchExpression specifies an expression
to evaluate against a set of input values. It to evaluate against a set of input values. It
contains an operator that is applied when matching contains an operator that is applied when matching
the input and an array of values that the operator the input and an array of values that the operator
evaluates the input against. \n NB: CreateMatchExpression evaluates the input against.
or MustCreateMatchExpression() should be used
for creating new instances. \n NB: Validate()
must be called if Op or Value fields are modified
or if a new instance is created from scratch
without using the helper functions."
properties: properties:
op: op:
description: Op is the operator to be applied. description: Op is the operator to be applied.
@ -259,15 +254,11 @@ spec:
type: string type: string
matchExpressions: matchExpressions:
additionalProperties: additionalProperties:
description: "MatchExpression specifies an expression description: MatchExpression specifies an expression
to evaluate against a set of input values. It contains to evaluate against a set of input values. It contains
an operator that is applied when matching the input an operator that is applied when matching the input
and an array of values that the operator evaluates and an array of values that the operator evaluates
the input against. \n NB: CreateMatchExpression or the input against.
MustCreateMatchExpression() should be used for creating
new instances. \n NB: Validate() must be called if
Op or Value fields are modified or if a new instance
is created from scratch without using the helper functions."
properties: properties:
op: op:
description: Op is the operator to be applied. description: Op is the operator to be applied.

View file

@ -42,23 +42,6 @@ var matchOps = map[MatchOp]struct{}{
MatchIsFalse: {}, MatchIsFalse: {},
} }
// CreateMatchExpression creates a new MatchExpression instance. Returns an
// error if validation fails.
func CreateMatchExpression(op MatchOp, values ...string) (*MatchExpression, error) {
m := newMatchExpression(op, values...)
return m, m.Validate()
}
// MustCreateMatchExpression creates a new MatchExpression instance. Panics if
// validation fails.
func MustCreateMatchExpression(op MatchOp, values ...string) *MatchExpression {
m, err := CreateMatchExpression(op, values...)
if err != nil {
panic(err)
}
return m
}
// newMatchExpression returns a new MatchExpression instance. // newMatchExpression returns a new MatchExpression instance.
func newMatchExpression(op MatchOp, values ...string) *MatchExpression { func newMatchExpression(op MatchOp, values ...string) *MatchExpression {
return &MatchExpression{ return &MatchExpression{

View file

@ -29,7 +29,7 @@ type BoolAssertionFunc func(assert.TestingT, bool, ...interface{}) bool
type ValueAssertionFunc func(assert.TestingT, interface{}, ...interface{}) bool type ValueAssertionFunc func(assert.TestingT, interface{}, ...interface{}) bool
func TestCreateMatchExpression(t *testing.T) { func TestMatchExpressionValidate(t *testing.T) {
type V = api.MatchValue type V = api.MatchValue
type TC struct { type TC struct {
name string name string
@ -89,7 +89,8 @@ func TestCreateMatchExpression(t *testing.T) {
for _, tc := range tcs { for _, tc := range tcs {
t.Run(tc.name, func(t *testing.T) { t.Run(tc.name, func(t *testing.T) {
_, err := api.CreateMatchExpression(tc.op, tc.values...) me := api.MatchExpression{Op: tc.op, Value: tc.values}
err := me.Validate()
tc.err(t, err) tc.err(t, err)
}) })
} }
@ -157,7 +158,7 @@ func TestMatch(t *testing.T) {
for _, tc := range tcs { for _, tc := range tcs {
t.Run(tc.name, func(t *testing.T) { t.Run(tc.name, func(t *testing.T) {
me := api.MustCreateMatchExpression(tc.op, tc.values...) me := api.MatchExpression{Op: tc.op, Value: tc.values}
res, err := me.Match(tc.valid, tc.input) res, err := me.Match(tc.valid, tc.input)
tc.result(t, res) tc.result(t, res)
assert.Nil(t, err) assert.Nil(t, err)
@ -251,7 +252,7 @@ func TestMatchKeys(t *testing.T) {
for _, tc := range tcs { for _, tc := range tcs {
t.Run(tc.name, func(t *testing.T) { t.Run(tc.name, func(t *testing.T) {
me := api.MustCreateMatchExpression(tc.op, tc.values...) me := api.MatchExpression{Op: tc.op, Value: tc.values}
res, err := me.MatchKeys(tc.key, tc.input) res, err := me.MatchKeys(tc.key, tc.input)
tc.result(t, res) tc.result(t, res)
tc.err(t, err) tc.err(t, err)
@ -320,7 +321,7 @@ func TestMatchValues(t *testing.T) {
for _, tc := range tcs { for _, tc := range tcs {
t.Run(tc.name, func(t *testing.T) { t.Run(tc.name, func(t *testing.T) {
me := api.MustCreateMatchExpression(tc.op, tc.values...) me := api.MatchExpression{Op: tc.op, Value: tc.values}
res, err := me.MatchValues(tc.key, tc.input) res, err := me.MatchValues(tc.key, tc.input)
tc.result(t, res) tc.result(t, res)
tc.err(t, err) tc.err(t, err)

View file

@ -32,7 +32,7 @@ func TestRule(t *testing.T) {
FeatureMatcherTerm{ FeatureMatcherTerm{
Feature: "domain-1.kf-1", Feature: "domain-1.kf-1",
MatchExpressions: MatchExpressionSet{ MatchExpressions: MatchExpressionSet{
"key-1": MustCreateMatchExpression(MatchExists), "key-1": newMatchExpression(MatchExists),
}, },
}, },
}, },
@ -109,7 +109,7 @@ func TestRule(t *testing.T) {
FeatureMatcherTerm{ FeatureMatcherTerm{
Feature: "domain-1.vf-1", Feature: "domain-1.vf-1",
MatchExpressions: MatchExpressionSet{ MatchExpressions: MatchExpressionSet{
"key-1": MustCreateMatchExpression(MatchIn, "val-1"), "key-1": newMatchExpression(MatchIn, "val-1"),
}, },
}, },
}, },
@ -130,7 +130,7 @@ func TestRule(t *testing.T) {
FeatureMatcherTerm{ FeatureMatcherTerm{
Feature: "domain-1.if-1", Feature: "domain-1.if-1",
MatchExpressions: MatchExpressionSet{ MatchExpressions: MatchExpressionSet{
"attr-1": MustCreateMatchExpression(MatchIn, "val-1"), "attr-1": newMatchExpression(MatchIn, "val-1"),
}, },
}, },
}, },
@ -151,13 +151,13 @@ func TestRule(t *testing.T) {
FeatureMatcherTerm{ FeatureMatcherTerm{
Feature: "domain-1.vf-1", Feature: "domain-1.vf-1",
MatchExpressions: MatchExpressionSet{ MatchExpressions: MatchExpressionSet{
"key-1": MustCreateMatchExpression(MatchIn, "val-x"), "key-1": newMatchExpression(MatchIn, "val-x"),
}, },
}, },
FeatureMatcherTerm{ FeatureMatcherTerm{
Feature: "domain-1.if-1", Feature: "domain-1.if-1",
MatchExpressions: MatchExpressionSet{ MatchExpressions: MatchExpressionSet{
"attr-1": MustCreateMatchExpression(MatchIn, "val-1"), "attr-1": newMatchExpression(MatchIn, "val-1"),
}, },
}, },
}, },
@ -166,7 +166,7 @@ func TestRule(t *testing.T) {
assert.Nilf(t, err, "unexpected error: %v", err) assert.Nilf(t, err, "unexpected error: %v", err)
assert.Nil(t, m.Labels, "instances should not have matched") assert.Nil(t, m.Labels, "instances should not have matched")
r5.MatchFeatures[0].MatchExpressions["key-1"] = MustCreateMatchExpression(MatchIn, "val-1") r5.MatchFeatures[0].MatchExpressions["key-1"] = newMatchExpression(MatchIn, "val-1")
m, err = r5.Execute(f) m, err = r5.Execute(f)
assert.Nilf(t, err, "unexpected error: %v", err) assert.Nilf(t, err, "unexpected error: %v", err)
assert.Equal(t, r5.Labels, m.Labels, "instances should have matched") assert.Equal(t, r5.Labels, m.Labels, "instances should have matched")
@ -178,7 +178,7 @@ func TestRule(t *testing.T) {
FeatureMatcherTerm{ FeatureMatcherTerm{
Feature: "domain-1.kf-1", Feature: "domain-1.kf-1",
MatchExpressions: MatchExpressionSet{ MatchExpressions: MatchExpressionSet{
"key-na": MustCreateMatchExpression(MatchExists), "key-na": newMatchExpression(MatchExists),
}, },
}, },
}, },
@ -194,12 +194,12 @@ func TestRule(t *testing.T) {
FeatureMatcherTerm{ FeatureMatcherTerm{
Feature: "domain-1.kf-1", Feature: "domain-1.kf-1",
MatchExpressions: MatchExpressionSet{ MatchExpressions: MatchExpressionSet{
"key-1": MustCreateMatchExpression(MatchExists), "key-1": newMatchExpression(MatchExists),
}, },
}, },
}, },
}) })
r5.MatchFeatures[0].MatchExpressions["key-1"] = MustCreateMatchExpression(MatchIn, "val-1") r5.MatchFeatures[0].MatchExpressions["key-1"] = newMatchExpression(MatchIn, "val-1")
m, err = r5.Execute(f) m, err = r5.Execute(f)
assert.Nilf(t, err, "unexpected error: %v", err) assert.Nilf(t, err, "unexpected error: %v", err)
assert.Equal(t, r5.Labels, m.Labels, "instances should have matched") assert.Equal(t, r5.Labels, m.Labels, "instances should have matched")
@ -279,30 +279,30 @@ var-2=
FeatureMatcherTerm{ FeatureMatcherTerm{
Feature: "domain_1.kf_1", Feature: "domain_1.kf_1",
MatchExpressions: MatchExpressionSet{ MatchExpressions: MatchExpressionSet{
"key-a": MustCreateMatchExpression(MatchExists), "key-a": newMatchExpression(MatchExists),
"key-c": MustCreateMatchExpression(MatchExists), "key-c": newMatchExpression(MatchExists),
"foo": MustCreateMatchExpression(MatchDoesNotExist), "foo": newMatchExpression(MatchDoesNotExist),
}, },
}, },
FeatureMatcherTerm{ FeatureMatcherTerm{
Feature: "domain_1.vf_1", Feature: "domain_1.vf_1",
MatchExpressions: MatchExpressionSet{ MatchExpressions: MatchExpressionSet{
"key-1": MustCreateMatchExpression(MatchIn, "val-1", "val-2"), "key-1": newMatchExpression(MatchIn, "val-1", "val-2"),
"bar": MustCreateMatchExpression(MatchDoesNotExist), "bar": newMatchExpression(MatchDoesNotExist),
}, },
}, },
FeatureMatcherTerm{ FeatureMatcherTerm{
Feature: "domain_1.if_1", Feature: "domain_1.if_1",
MatchExpressions: MatchExpressionSet{ MatchExpressions: MatchExpressionSet{
"attr-1": MustCreateMatchExpression(MatchLt, "100"), "attr-1": newMatchExpression(MatchLt, "100"),
}, },
}, },
FeatureMatcherTerm{ FeatureMatcherTerm{
Feature: "domain_1.if_1", Feature: "domain_1.if_1",
MatchExpressions: MatchExpressionSet{ MatchExpressions: MatchExpressionSet{
"attr-1": MustCreateMatchExpression(MatchExists), "attr-1": newMatchExpression(MatchExists),
"attr-2": MustCreateMatchExpression(MatchExists), "attr-2": newMatchExpression(MatchExists),
"attr-3": MustCreateMatchExpression(MatchExists), "attr-3": newMatchExpression(MatchExists),
}, },
}, },
}, },
@ -357,7 +357,7 @@ var-2=
FeatureMatcherTerm{ FeatureMatcherTerm{
Feature: "domain_1.kf_1", Feature: "domain_1.kf_1",
MatchExpressions: MatchExpressionSet{ MatchExpressions: MatchExpressionSet{
"key-a": MustCreateMatchExpression(MatchExists), "key-a": newMatchExpression(MatchExists),
}, },
}, },
}, },

View file

@ -209,12 +209,6 @@ type MatchExpressionSet map[string]*MatchExpression
// MatchExpression specifies an expression to evaluate against a set of input // MatchExpression specifies an expression to evaluate against a set of input
// values. It contains an operator that is applied when matching the input and // values. It contains an operator that is applied when matching the input and
// an array of values that the operator evaluates the input against. // an array of values that the operator evaluates the input against.
//
// NB: CreateMatchExpression or MustCreateMatchExpression() should be used for
// creating new instances.
//
// NB: Validate() must be called if Op or Value fields are modified or if a new
// instance is created from scratch without using the helper functions.
type MatchExpression struct { type MatchExpression struct {
// Op is the operator to be applied. // Op is the operator to be applied.
Op MatchOp `json:"op"` Op MatchOp `json:"op"`

View file

@ -32,7 +32,9 @@ func getStaticFeatureConfig() []CustomRule {
nfdv1alpha1.FeatureMatcherTerm{ nfdv1alpha1.FeatureMatcherTerm{
Feature: "pci.device", Feature: "pci.device",
MatchExpressions: nfdv1alpha1.MatchExpressionSet{ MatchExpressions: nfdv1alpha1.MatchExpressionSet{
"vendor": nfdv1alpha1.MustCreateMatchExpression(nfdv1alpha1.MatchIn, "15b3"), "vendor": &nfdv1alpha1.MatchExpression{
Op: nfdv1alpha1.MatchIn,
Value: nfdv1alpha1.MatchValue{"15b3"}},
}, },
}, },
}, },
@ -46,8 +48,12 @@ func getStaticFeatureConfig() []CustomRule {
nfdv1alpha1.FeatureMatcherTerm{ nfdv1alpha1.FeatureMatcherTerm{
Feature: "kernel.loadedmodule", Feature: "kernel.loadedmodule",
MatchExpressions: nfdv1alpha1.MatchExpressionSet{ MatchExpressions: nfdv1alpha1.MatchExpressionSet{
"ib_uverbs": nfdv1alpha1.MustCreateMatchExpression(nfdv1alpha1.MatchExists), "ib_uverbs": &nfdv1alpha1.MatchExpression{
"rdma_ucm": nfdv1alpha1.MustCreateMatchExpression(nfdv1alpha1.MatchExists), Op: nfdv1alpha1.MatchExists,
},
"rdma_ucm": &nfdv1alpha1.MatchExpression{
Op: nfdv1alpha1.MatchExists,
},
}, },
}, },
}, },