Skip to content

Commit

Permalink
source/local: implement new "local" custom rule
Browse files Browse the repository at this point in the history
Makes it possible to write custom rules that match against labels from
feature files and hooks.
  • Loading branch information
marquiz committed Mar 4, 2021
1 parent c4c143e commit 542988c
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
3 changes: 3 additions & 0 deletions source/custom/custom.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"sigs.k8s.io/node-feature-discovery/source"
"sigs.k8s.io/node-feature-discovery/source/cpu"
"sigs.k8s.io/node-feature-discovery/source/kernel"
"sigs.k8s.io/node-feature-discovery/source/local"
"sigs.k8s.io/node-feature-discovery/source/pci"
"sigs.k8s.io/node-feature-discovery/source/system"
"sigs.k8s.io/node-feature-discovery/source/usb"
Expand All @@ -40,6 +41,7 @@ type MatchRule struct {
Nodename *system.NodenameRule `json:"nodename,omitempty"`
CPU *cpu.CustomRule `json:"cpu,omitempty"`
Kernel *kernel.CustomRule `json:"kernel,omitempty"`
Local *local.CustomRule `json:"local,omitempty"`
System *system.CustomRule `json:"system,omitempty"`
}

Expand Down Expand Up @@ -128,6 +130,7 @@ func (s *customSource) discoverFeature(feature FeatureSpec) (bool, error) {
matchRules.Nodename,
matchRules.CPU,
matchRules.Kernel,
matchRules.Local,
matchRules.System,
}

Expand Down
36 changes: 36 additions & 0 deletions source/local/custom_rule.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
Copyright 2021 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package local

import (
"k8s.io/klog/v2"

"sigs.k8s.io/node-feature-discovery/source"
)

// CustomRule implements the source.CustomRule interface
type CustomRule struct {
Labels source.MatchExpressionSet
}

func (r *CustomRule) Match() (bool, error) {
if m, err := r.Labels.MatchValues(src.features.Labels); err != nil || m == false {
klog.V(2).Infof("local CustomRule: failed to match labels")
return m, err
}
return true, nil
}

0 comments on commit 542988c

Please sign in to comment.