Skip to content

Commit b276764

Browse files
committed
nfd-master: prevent crash on empty config struct
Change the handling of LabelWhiteList config option to use a pointer to detect when the option is unset. This doesn't fix any detected crash but is merely general improvement and stabilization, serving easier testing. Also, use the regexp type from the core libs for the config struct - dropping the unmasrhalling code for our custom regexp type - as the core regexp now implements unmarshaller itself.
1 parent af8a41c commit b276764

File tree

3 files changed

+5
-26
lines changed

3 files changed

+5
-26
lines changed

pkg/nfd-master/nfd-master-internal_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import (
2222
"maps"
2323
"os"
2424
"path/filepath"
25-
"regexp"
2625
"sort"
2726
"strings"
2827
"testing"
@@ -108,7 +107,7 @@ func newFakeNfdAPIController(client *fakenfdclient.Clientset) *nfdController {
108107
func newFakeMaster(cli k8sclient.Interface) *nfdMaster {
109108
return &nfdMaster{
110109
nodeName: testNodeName,
111-
config: &NFDConfig{LabelWhiteList: utils.RegexpVal{Regexp: *regexp.MustCompile("")}},
110+
config: &NFDConfig{},
112111
k8sClient: cli,
113112
}
114113
}

pkg/nfd-master/nfd-master.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ type NFDConfig struct {
7676
AutoDefaultNs bool
7777
DenyLabelNs utils.StringSetVal
7878
ExtraLabelNs utils.StringSetVal
79-
LabelWhiteList utils.RegexpVal
79+
LabelWhiteList *regexp.Regexp
8080
NoPublish bool
8181
ResourceLabels utils.StringSetVal
8282
EnableTaints bool
@@ -197,7 +197,6 @@ func NewNfdMaster(args *Args) (NfdMaster, error) {
197197

198198
func newDefaultConfig() *NFDConfig {
199199
return &NFDConfig{
200-
LabelWhiteList: utils.RegexpVal{Regexp: *regexp.MustCompile("")},
201200
DenyLabelNs: utils.StringSetVal{},
202201
ExtraLabelNs: utils.StringSetVal{},
203202
NoPublish: false,
@@ -608,8 +607,8 @@ func (m *nfdMaster) filterFeatureLabel(name, value string, features *nfdv1alpha1
608607
}
609608

610609
// Skip if label doesn't match labelWhiteList
611-
if !m.config.LabelWhiteList.Regexp.MatchString(base) {
612-
return "", fmt.Errorf("%s (%s) does not match the whitelist (%s)", base, name, m.config.LabelWhiteList.Regexp.String())
610+
if m.config.LabelWhiteList != nil && !m.config.LabelWhiteList.MatchString(base) {
611+
return "", fmt.Errorf("%s (%s) does not match the whitelist (%s)", base, name, m.config.LabelWhiteList.String())
613612
}
614613

615614
return filteredValue, nil
@@ -1210,7 +1209,7 @@ func (m *nfdMaster) configure(filepath string, overrides string) error {
12101209
c.EnableTaints = *m.args.Overrides.EnableTaints
12111210
}
12121211
if m.args.Overrides.LabelWhiteList != nil {
1213-
c.LabelWhiteList = *m.args.Overrides.LabelWhiteList
1212+
c.LabelWhiteList = &m.args.Overrides.LabelWhiteList.Regexp
12141213
}
12151214
if m.args.Overrides.ResyncPeriod != nil {
12161215
c.ResyncPeriod = *m.args.Overrides.ResyncPeriod

pkg/utils/flags.go

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -42,25 +42,6 @@ func (a *RegexpVal) Set(val string) error {
4242
return err
4343
}
4444

45-
// UnmarshalJSON implements the Unmarshaler interface from "encoding/json"
46-
func (a *RegexpVal) UnmarshalJSON(data []byte) error {
47-
var v interface{}
48-
if err := json.Unmarshal(data, &v); err != nil {
49-
return err
50-
}
51-
switch val := v.(type) {
52-
case string:
53-
if r, err := regexp.Compile(string(val)); err != nil {
54-
return err
55-
} else {
56-
*a = RegexpVal{*r}
57-
}
58-
default:
59-
return fmt.Errorf("invalid regexp %s", data)
60-
}
61-
return nil
62-
}
63-
6445
// StringSetVal is a Value encapsulating a set of comma-separated strings
6546
type StringSetVal map[string]struct{}
6647

0 commit comments

Comments
 (0)