1
0
Fork 0
mirror of https://github.com/kubernetes-sigs/node-feature-discovery.git synced 2025-03-14 20:56:42 +00:00

apis/nfd: revert the type hack

Revert the hack that was a workaround for issues with k8s deepcopy-gen.
New deepcopy-gen is able to generate code correctly without issues so
this is not needed anymore.

Also, removing this hack solves issues with object validation when
creating NodeFeatureRules programmatically with nfd go-client. This is
needed later with NodeFeatureRules e2e-tests.

Logically reverts f3cc109f99.
This commit is contained in:
Markus Lehtonen 2022-07-07 20:57:58 +03:00
parent 1cf0fa6cfa
commit 345e9bf72c
4 changed files with 26 additions and 47 deletions

View file

@ -45,16 +45,6 @@ var matchOps = map[MatchOp]struct{}{
type valueRegexpCache []*regexp.Regexp
// NewMatchExpressionSet returns a new MatchExpressionSet instance.
func NewMatchExpressionSet() *MatchExpressionSet {
return &MatchExpressionSet{Expressions: make(Expressions)}
}
// Len returns the number of expressions.
func (e *Expressions) Len() int {
return len(*e)
}
// CreateMatchExpression creates a new MatchExpression instance. Returns an
// error if validation fails.
func CreateMatchExpression(op MatchOp, values ...string) (*MatchExpression, error) {
@ -329,9 +319,9 @@ type MatchedKey struct {
// an empty MatchExpressionSet and an empty set of keys returns an empty slice
// which is not nil and is treated as a match.
func (m *MatchExpressionSet) MatchGetKeys(keys map[string]feature.Nil) (bool, []MatchedKey, error) {
ret := make([]MatchedKey, 0, m.Len())
ret := make([]MatchedKey, 0, len(*m))
for n, e := range (*m).Expressions {
for n, e := range *m {
match, err := e.MatchKeys(n, keys)
if err != nil {
return false, nil, err
@ -364,9 +354,9 @@ type MatchedValue struct {
// MatchExpressionSet and an empty set of values returns an empty non-nil map
// which is treated as a match.
func (m *MatchExpressionSet) MatchGetValues(values map[string]string) (bool, []MatchedValue, error) {
ret := make([]MatchedValue, 0, m.Len())
ret := make([]MatchedValue, 0, len(*m))
for n, e := range (*m).Expressions {
for n, e := range *m {
match, err := e.MatchValues(n, values)
if err != nil {
return false, nil, err
@ -411,7 +401,7 @@ func (m *MatchExpressionSet) MatchGetInstances(instances []feature.InstanceFeatu
// UnmarshalJSON implements the Unmarshaler interface of "encoding/json".
func (m *MatchExpressionSet) UnmarshalJSON(data []byte) error {
*m = *NewMatchExpressionSet()
*m = MatchExpressionSet{}
names := make([]string, 0)
if err := json.Unmarshal(data, &names); err == nil {
@ -419,9 +409,9 @@ func (m *MatchExpressionSet) UnmarshalJSON(data []byte) error {
for _, name := range names {
split := strings.SplitN(name, "=", 2)
if len(split) == 1 {
(*m).Expressions[split[0]] = newMatchExpression(MatchExists)
(*m)[split[0]] = newMatchExpression(MatchExists)
} else {
(*m).Expressions[split[0]] = newMatchExpression(MatchIn, split[1])
(*m)[split[0]] = newMatchExpression(MatchIn, split[1])
}
}
} else {
@ -432,9 +422,9 @@ func (m *MatchExpressionSet) UnmarshalJSON(data []byte) error {
}
for k, v := range expressions {
if v != nil {
(*m).Expressions[k] = v
(*m)[k] = v
} else {
(*m).Expressions[k] = newMatchExpression(MatchExists)
(*m)[k] = newMatchExpression(MatchExists)
}
}
}

View file

@ -33,7 +33,7 @@ func TestRule(t *testing.T) {
FeatureMatcherTerm{
Feature: "domain-1.kf-1",
MatchExpressions: MatchExpressionSet{
Expressions: Expressions{"key-1": MustCreateMatchExpression(MatchExists)},
"key-1": MustCreateMatchExpression(MatchExists),
},
},
},
@ -111,7 +111,7 @@ func TestRule(t *testing.T) {
FeatureMatcherTerm{
Feature: "domain-1.vf-1",
MatchExpressions: MatchExpressionSet{
Expressions: Expressions{"key-1": MustCreateMatchExpression(MatchIn, "val-1")},
"key-1": MustCreateMatchExpression(MatchIn, "val-1"),
},
},
},
@ -132,7 +132,7 @@ func TestRule(t *testing.T) {
FeatureMatcherTerm{
Feature: "domain-1.if-1",
MatchExpressions: MatchExpressionSet{
Expressions: Expressions{"attr-1": MustCreateMatchExpression(MatchIn, "val-1")},
"attr-1": MustCreateMatchExpression(MatchIn, "val-1"),
},
},
},
@ -153,13 +153,13 @@ func TestRule(t *testing.T) {
FeatureMatcherTerm{
Feature: "domain-1.vf-1",
MatchExpressions: MatchExpressionSet{
Expressions: Expressions{"key-1": MustCreateMatchExpression(MatchIn, "val-x")},
"key-1": MustCreateMatchExpression(MatchIn, "val-x"),
},
},
FeatureMatcherTerm{
Feature: "domain-1.if-1",
MatchExpressions: MatchExpressionSet{
Expressions: Expressions{"attr-1": MustCreateMatchExpression(MatchIn, "val-1")},
"attr-1": MustCreateMatchExpression(MatchIn, "val-1"),
},
},
},
@ -168,7 +168,7 @@ func TestRule(t *testing.T) {
assert.Nilf(t, err, "unexpected error: %v", err)
assert.Nil(t, m.Labels, "instances should not have matched")
r5.MatchFeatures[0].MatchExpressions.Expressions["key-1"] = MustCreateMatchExpression(MatchIn, "val-1")
r5.MatchFeatures[0].MatchExpressions["key-1"] = MustCreateMatchExpression(MatchIn, "val-1")
m, err = r5.Execute(f)
assert.Nilf(t, err, "unexpected error: %v", err)
assert.Equal(t, r5.Labels, m.Labels, "instances should have matched")
@ -180,7 +180,7 @@ func TestRule(t *testing.T) {
FeatureMatcherTerm{
Feature: "domain-1.kf-1",
MatchExpressions: MatchExpressionSet{
Expressions: Expressions{"key-na": MustCreateMatchExpression(MatchExists)},
"key-na": MustCreateMatchExpression(MatchExists),
},
},
},
@ -196,12 +196,12 @@ func TestRule(t *testing.T) {
FeatureMatcherTerm{
Feature: "domain-1.kf-1",
MatchExpressions: MatchExpressionSet{
Expressions: Expressions{"key-1": MustCreateMatchExpression(MatchExists)},
"key-1": MustCreateMatchExpression(MatchExists),
},
},
},
})
r5.MatchFeatures[0].MatchExpressions.Expressions["key-1"] = MustCreateMatchExpression(MatchIn, "val-1")
r5.MatchFeatures[0].MatchExpressions["key-1"] = MustCreateMatchExpression(MatchIn, "val-1")
m, err = r5.Execute(f)
assert.Nilf(t, err, "unexpected error: %v", err)
assert.Equal(t, r5.Labels, m.Labels, "instances should have matched")
@ -275,27 +275,24 @@ var-2=
MatchFeatures: FeatureMatcher{
FeatureMatcherTerm{
Feature: "domain_1.kf_1",
MatchExpressions: MatchExpressionSet{Expressions: Expressions{
MatchExpressions: MatchExpressionSet{
"key-a": MustCreateMatchExpression(MatchExists),
"key-c": MustCreateMatchExpression(MatchExists),
"foo": MustCreateMatchExpression(MatchDoesNotExist),
},
},
},
FeatureMatcherTerm{
Feature: "domain_1.vf_1",
MatchExpressions: MatchExpressionSet{Expressions: Expressions{
MatchExpressions: MatchExpressionSet{
"key-1": MustCreateMatchExpression(MatchIn, "val-1", "val-2"),
"bar": MustCreateMatchExpression(MatchDoesNotExist),
},
},
},
FeatureMatcherTerm{
Feature: "domain_1.if_1",
MatchExpressions: MatchExpressionSet{Expressions: Expressions{
MatchExpressions: MatchExpressionSet{
"attr-1": MustCreateMatchExpression(MatchLt, "100"),
},
},
},
},
}
@ -337,10 +334,9 @@ var-2=
// Use a simple empty matchexpression set to match anything.
FeatureMatcherTerm{
Feature: "domain_1.kf_1",
MatchExpressions: MatchExpressionSet{Expressions: Expressions{
MatchExpressions: MatchExpressionSet{
"key-a": MustCreateMatchExpression(MatchExists),
},
},
},
},
}

View file

@ -111,12 +111,9 @@ type FeatureMatcherTerm struct {
// MatchExpressionSet contains a set of MatchExpressions, each of which is
// evaluated against a set of input values.
type MatchExpressionSet struct {
Expressions `json:",inline"`
}
type MatchExpressionSet map[string]*MatchExpression
// Expressions is a helper type to work around issues with k8s deepcopy-gen
type Expressions map[string]*MatchExpression
// MatchExpression specifies an expression to evaluate against a set of input
// values. It contains an operator that is applied when matching the input and

View file

@ -32,9 +32,7 @@ func getStaticFeatureConfig() []CustomRule {
{
PciID: &rules.PciIDRule{
MatchExpressionSet: nfdv1alpha1.MatchExpressionSet{
Expressions: nfdv1alpha1.Expressions{
"vendor": nfdv1alpha1.MustCreateMatchExpression(nfdv1alpha1.MatchIn, "15b3"),
},
"vendor": nfdv1alpha1.MustCreateMatchExpression(nfdv1alpha1.MatchIn, "15b3"),
},
},
},
@ -48,10 +46,8 @@ func getStaticFeatureConfig() []CustomRule {
{
LoadedKMod: &rules.LoadedKModRule{
MatchExpressionSet: nfdv1alpha1.MatchExpressionSet{
Expressions: nfdv1alpha1.Expressions{
"ib_uverbs": nfdv1alpha1.MustCreateMatchExpression(nfdv1alpha1.MatchExists),
"rdma_ucm": nfdv1alpha1.MustCreateMatchExpression(nfdv1alpha1.MatchExists),
},
"ib_uverbs": nfdv1alpha1.MustCreateMatchExpression(nfdv1alpha1.MatchExists),
"rdma_ucm": nfdv1alpha1.MustCreateMatchExpression(nfdv1alpha1.MatchExists),
},
},
},