mirror of
https://github.com/kubernetes-sigs/node-feature-discovery.git
synced 2025-03-28 02:37:11 +00:00
Add validation for feature label values
Prevents NFD errors e.g. in the case custom hooks happen to output invalid label values.
This commit is contained in:
parent
ab5b286bd7
commit
7009fa2b5b
1 changed files with 13 additions and 4 deletions
17
main.go
17
main.go
|
@ -50,6 +50,7 @@ const (
|
|||
var (
|
||||
version = "" // Must not be const, set using ldflags at build time
|
||||
validFeatureNameRe = regexp.MustCompile(`^([-.\w]*)?[A-Za-z0-9]$`)
|
||||
validFeatureValRe = regexp.MustCompile(`^(([A-Za-z0-9][-.\w]*)?[A-Za-z0-9])?$`)
|
||||
)
|
||||
|
||||
// package loggers
|
||||
|
@ -359,20 +360,28 @@ func getFeatureLabels(source source.FeatureSource) (labels Labels, err error) {
|
|||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for k := range features {
|
||||
// Validate label
|
||||
for k, v := range features {
|
||||
// Validate label name
|
||||
if !validFeatureNameRe.MatchString(k) {
|
||||
stderrLogger.Printf("Invalid feature name '%s', ignoring...", k)
|
||||
continue
|
||||
}
|
||||
|
||||
prefix := source.Name() + "-"
|
||||
switch source.(type) {
|
||||
case local.Source:
|
||||
// Do not prefix labels from the hooks
|
||||
prefix = ""
|
||||
}
|
||||
labels[fmt.Sprintf("%s%s", prefix, k)] = fmt.Sprintf("%v", features[k])
|
||||
label := prefix + k
|
||||
|
||||
// Validate label value
|
||||
value := fmt.Sprintf("%v", v)
|
||||
if !validFeatureValRe.MatchString(value) {
|
||||
stderrLogger.Printf("Invalid value '%s' for label '%s', ignoring...", value, label)
|
||||
continue
|
||||
}
|
||||
|
||||
labels[label] = value
|
||||
}
|
||||
return labels, nil
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue