1
0
Fork 0
mirror of https://github.com/kubernetes-sigs/node-feature-discovery.git synced 2025-03-15 04:57:56 +00:00
node-feature-discovery/source/custom/rules/nodename_rule.go
Marc Sluiter 7038e49d02
source/custom: Add nodename rule
There are cases when the only available metadata for discovering
features is the node's name. The "nodename" rule extends the custom
source and matches when the node's name matches one of the given
nodename regexp patterns.
It is also possible now to set an optional "value" on custom rules,
which overrides the default "true" label value in case the rule matches.
In order to allow more dynamic configurations without having to modify
the complete worker configuration, custom rules are additionally read
from a "custom.d" directory now. Typically that directory will be filled
by mounting one or more ConfigMaps.

Signed-off-by: Marc Sluiter <msluiter@redhat.com>
2021-02-24 16:26:35 +01:00

50 lines
1.4 KiB
Go

/*
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 rules
import (
"log"
"os"
"regexp"
)
var (
nodeName = os.Getenv("NODE_NAME")
)
// Rule that matches on nodenames configured in a ConfigMap
type NodenameRule []string
// Force implementation of Rule
var _ Rule = NodenameRule{}
func (n NodenameRule) Match() (bool, error) {
for _, nodenamePattern := range n {
log.Printf("DEBUG: matchNodename %s", nodenamePattern)
match, err := regexp.MatchString(nodenamePattern, nodeName)
if err != nil {
log.Printf("ERROR: nodename rule: invalid nodename regexp %q: %v", nodenamePattern, err)
continue
}
if !match {
//log.Printf("DEBUG: nodename rule: No match for pattern %q with node %q", nodenamePattern, nodeName)
continue
}
//log.Printf("DEBUG: nodename rule: Match for pattern %q with node %q", nodenamePattern, nodeName)
return true, nil
}
return false, nil
}