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: fix a crash when processing NodeFeatureRules #1173

Merged
merged 1 commit into from
Apr 21, 2023
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
19 changes: 19 additions & 0 deletions pkg/apis/nfd/v1alpha1/feature.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,15 @@ limitations under the License.

package v1alpha1

// NewNodeFeatureSpec creates a new emprty instance of NodeFeatureSpec type,
// initializing all fields to proper empty values.
func NewNodeFeatureSpec() *NodeFeatureSpec {
return &NodeFeatureSpec{
Features: *NewFeatures(),
Labels: make(map[string]string),
}
}

// NewFeatures creates a new instance of Features, initializing all feature
// types (flags, attributes and instances) to empty values.
func NewFeatures() *Features {
Expand Down Expand Up @@ -56,7 +65,11 @@ func NewInstanceFeature(attrs map[string]string) *InstanceFeature {
}

// InsertAttributeFeatures inserts new values into a specific feature.
// TODO: add unit tests
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😄

func (f *Features) InsertAttributeFeatures(domain, feature string, values map[string]string) {
if f.Attributes == nil {
f.Attributes = make(map[string]AttributeFeatureSet)
}
key := domain + "." + feature
if _, ok := f.Attributes[key]; !ok {
f.Attributes[key] = NewAttributeFeatures(values)
Expand All @@ -70,6 +83,7 @@ func (f *Features) InsertAttributeFeatures(domain, feature string, values map[st

// Exists returns a non-empty string if a feature exists. The return value is
// the type of the feautre, i.e. "flag", "attribute" or "instance".
// TODO: add unit tests
func (f *Features) Exists(name string) string {
if _, ok := f.Flags[name]; ok {
return "flag"
Expand All @@ -85,6 +99,7 @@ func (f *Features) Exists(name string) string {

// MergeInto merges two FeatureSpecs into one. Data in the input object takes
// precedence (overwrite) over data of the existing object we're merging into.
// TODO: add unit tests
func (in *NodeFeatureSpec) MergeInto(out *NodeFeatureSpec) {
in.Features.MergeInto(&out.Features)
if in.Labels != nil {
Expand All @@ -100,6 +115,7 @@ func (in *NodeFeatureSpec) MergeInto(out *NodeFeatureSpec) {
// MergeInto merges two sets of features into one. Features from the input set
// take precedence (overwrite) features from the existing features of the set
// we're merging into.
// TODO: add unit tests
func (in *Features) MergeInto(out *Features) {
if in.Flags != nil {
if out.Flags == nil {
Expand Down Expand Up @@ -134,6 +150,7 @@ func (in *Features) MergeInto(out *Features) {
}

// MergeInto merges two sets of flag featues.
// TODO: add unit tests
func (in *FlagFeatureSet) MergeInto(out *FlagFeatureSet) {
if in.Elements != nil {
if out.Elements == nil {
Expand All @@ -146,6 +163,7 @@ func (in *FlagFeatureSet) MergeInto(out *FlagFeatureSet) {
}

// MergeInto merges two sets of attribute featues.
// TODO: add unit tests
func (in *AttributeFeatureSet) MergeInto(out *AttributeFeatureSet) {
if in.Elements != nil {
if out.Elements == nil {
Expand All @@ -158,6 +176,7 @@ func (in *AttributeFeatureSet) MergeInto(out *AttributeFeatureSet) {
}

// MergeInto merges two sets of instance featues.
// TODO: add unit tests
func (in *InstanceFeatureSet) MergeInto(out *InstanceFeatureSet) {
if in.Elements != nil {
if out.Elements == nil {
Expand Down
3 changes: 2 additions & 1 deletion pkg/nfd-master/nfd-master.go
Original file line number Diff line number Diff line change
Expand Up @@ -649,7 +649,8 @@ func (m *nfdMaster) nfdAPIUpdateOneNode(nodeName string) error {

klog.V(1).Infof("processing node %q, initiated by NodeFeature API", nodeName)

features := &nfdv1alpha1.NodeFeatureSpec{}
features := nfdv1alpha1.NewNodeFeatureSpec()

annotations := Annotations{}

if len(objs) > 0 {
Expand Down