Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

nfd-master: prevent crash on empty config struct #1657

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions pkg/nfd-master/nfd-master-internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"maps"
"os"
"path/filepath"
"regexp"
"sort"
"strings"
"testing"
Expand Down Expand Up @@ -108,7 +107,7 @@ func newFakeNfdAPIController(client *fakenfdclient.Clientset) *nfdController {
func newFakeMaster(cli k8sclient.Interface) *nfdMaster {
return &nfdMaster{
nodeName: testNodeName,
config: &NFDConfig{LabelWhiteList: utils.RegexpVal{Regexp: *regexp.MustCompile("")}},
config: &NFDConfig{},
k8sClient: cli,
}
}
Expand Down
9 changes: 4 additions & 5 deletions pkg/nfd-master/nfd-master.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@
AutoDefaultNs bool
DenyLabelNs utils.StringSetVal
ExtraLabelNs utils.StringSetVal
LabelWhiteList utils.RegexpVal
LabelWhiteList *regexp.Regexp
NoPublish bool
ResourceLabels utils.StringSetVal
EnableTaints bool
Expand Down Expand Up @@ -197,7 +197,6 @@

func newDefaultConfig() *NFDConfig {
return &NFDConfig{
LabelWhiteList: utils.RegexpVal{Regexp: *regexp.MustCompile("")},
DenyLabelNs: utils.StringSetVal{},
ExtraLabelNs: utils.StringSetVal{},
NoPublish: false,
Expand Down Expand Up @@ -608,8 +607,8 @@
}

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

Check warning on line 611 in pkg/nfd-master/nfd-master.go

View check run for this annotation

Codecov / codecov/patch

pkg/nfd-master/nfd-master.go#L611

Added line #L611 was not covered by tests
}

return filteredValue, nil
Expand Down Expand Up @@ -1210,7 +1209,7 @@
c.EnableTaints = *m.args.Overrides.EnableTaints
}
if m.args.Overrides.LabelWhiteList != nil {
c.LabelWhiteList = *m.args.Overrides.LabelWhiteList
c.LabelWhiteList = &m.args.Overrides.LabelWhiteList.Regexp

Check warning on line 1212 in pkg/nfd-master/nfd-master.go

View check run for this annotation

Codecov / codecov/patch

pkg/nfd-master/nfd-master.go#L1212

Added line #L1212 was not covered by tests
}
if m.args.Overrides.ResyncPeriod != nil {
c.ResyncPeriod = *m.args.Overrides.ResyncPeriod
Expand Down
19 changes: 0 additions & 19 deletions pkg/utils/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,25 +42,6 @@ func (a *RegexpVal) Set(val string) error {
return err
}

// UnmarshalJSON implements the Unmarshaler interface from "encoding/json"
func (a *RegexpVal) UnmarshalJSON(data []byte) error {
var v interface{}
if err := json.Unmarshal(data, &v); err != nil {
return err
}
switch val := v.(type) {
case string:
if r, err := regexp.Compile(string(val)); err != nil {
return err
} else {
*a = RegexpVal{*r}
}
default:
return fmt.Errorf("invalid regexp %s", data)
}
return nil
}

// StringSetVal is a Value encapsulating a set of comma-separated strings
type StringSetVal map[string]struct{}

Expand Down