Skip to content

Commit

Permalink
Merge pull request #1657 from marquiz/devel/master-label-whitelist
Browse files Browse the repository at this point in the history
nfd-master: prevent crash on empty config struct
  • Loading branch information
k8s-ci-robot committed Apr 5, 2024
2 parents ad96c30 + b276764 commit cb24f7c
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 26 deletions.
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 @@ type NFDConfig struct {
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 NewNfdMaster(args *Args) (NfdMaster, error) {

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 @@ func (m *nfdMaster) filterFeatureLabel(name, value string, features *nfdv1alpha1
}

// 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())
}

return filteredValue, nil
Expand Down Expand Up @@ -1210,7 +1209,7 @@ func (m *nfdMaster) configure(filepath string, overrides string) error {
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
}
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

0 comments on commit cb24f7c

Please sign in to comment.