Skip to content

Commit

Permalink
nfd-master: parse kubeconfig even with NoPublish set
Browse files Browse the repository at this point in the history
Don't try to be too smart when kubeconfig is needed. In practice, the
nfd-master really doesn't work anymore (with the NodeFeature API
enabled) without a kubeconfig set. This patch fixes crashes happening
when NoPublish is enabled, e.g. in listing all nodes in the nfd api
handler and in getting single node objects in the node updater pool.

This patch changes the kubeconfig parsing to happen at the creation of
the nfd-master instance. We don't need to do that at reconfigure time as
none of the dynamic config options affect it. Unit tests are adjusted,
accordingly.
  • Loading branch information
marquiz committed Apr 5, 2024
1 parent fcb8d3c commit 0a4da25
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 33 deletions.
33 changes: 17 additions & 16 deletions pkg/nfd-master/nfd-master-internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ import (
fakecorev1client "k8s.io/client-go/kubernetes/typed/core/v1/fake"
clienttesting "k8s.io/client-go/testing"
"k8s.io/client-go/tools/cache"
"k8s.io/klog/v2"

nfdv1alpha1 "sigs.k8s.io/node-feature-discovery/pkg/apis/nfd/v1alpha1"
"sigs.k8s.io/node-feature-discovery/pkg/features"
Expand Down Expand Up @@ -115,6 +114,7 @@ func newFakeMaster(opts ...NfdMasterOption) *nfdMaster {
defaultOpts := []NfdMasterOption{
withNodeName(testNodeName),
withConfig(&NFDConfig{}),
WithKubernetesClient(fakeclient.NewSimpleClientset()),
}
m, err := NewNfdMaster(append(defaultOpts, opts...)...)
if err != nil {
Expand Down Expand Up @@ -664,6 +664,10 @@ leaderElection:

func TestDynamicConfig(t *testing.T) {
Convey("When running nfd-master", t, func() {
// Add feature gates as running nfd-master depends on that
err := features.NFDMutableFeatureGate.Add(features.DefaultNFDFeatureGates)
So(err, ShouldBeNil)

tmpDir, err := os.MkdirTemp("", "*.nfd-test")
So(err, ShouldBeNil)
defer os.RemoveAll(tmpDir)
Expand All @@ -674,7 +678,7 @@ func TestDynamicConfig(t *testing.T) {
So(err, ShouldBeNil)

// Create config file
configFile := filepath.Join(configDir, "master.conf")
configFile := filepath.Clean(filepath.Join(configDir, "master.conf"))

writeConfig := func(data string) {
f, err := os.Create(configFile)
Expand All @@ -685,25 +689,22 @@ func TestDynamicConfig(t *testing.T) {
So(err, ShouldBeNil)
}
writeConfig(`
klog:
v: "4"
extraLabelNs: ["added.ns.io"]
`)

noPublish := true
// Add FeatureGates flag
if err := features.NFDMutableFeatureGate.Add(features.DefaultNFDFeatureGates); err != nil {
klog.ErrorS(err, "failed to add default feature gates")
os.Exit(1)
}
_ = features.NFDMutableFeatureGate.OverrideDefault(features.NodeFeatureAPI, false)
master := newFakeMaster(WithArgs(&Args{
ConfigFile: configFile,
Overrides: ConfigOverrideArgs{
NoPublish: &noPublish,
},
}))
master := newFakeMaster(
WithArgs(&Args{ConfigFile: configFile}),
WithKubernetesClient(fakeclient.NewSimpleClientset(newTestNode())))

Convey("config file updates should take effect", func() {
go func() { _ = master.Run() }()
go func() {
Convey("nfd-master should exit gracefully", t, func() {
err = master.Run()
So(err, ShouldBeNil)
})
}()
defer master.Stop()
// Check initial config
time.Sleep(10 * time.Second)
Expand Down
34 changes: 18 additions & 16 deletions pkg/nfd-master/nfd-master.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,19 @@ func NewNfdMaster(opts ...NfdMasterOption) (NfdMaster, error) {
nfd.configFilePath = filepath.Clean(nfd.args.ConfigFile)
}

// k8sClient might've been set via opts by tests
if nfd.k8sClient == nil {
kubeconfig, err := utils.GetKubeconfig(nfd.args.Kubeconfig)
if err != nil {
return nfd, err
}
cli, err := k8sclient.NewForConfig(kubeconfig)
if err != nil {
return nfd, err
}
nfd.k8sClient = cli
}

nfd.nodeUpdaterPool = newNodeUpdaterPool(nfd)

return nfd, nil
Expand Down Expand Up @@ -770,10 +783,6 @@ func (m *nfdMaster) nfdAPIUpdateOneNode(node *corev1.Node) error {
return objs[i].Namespace < objs[j].Namespace
})

if m.config.NoPublish {
return nil
}

klog.V(1).InfoS("processing of node initiated by NodeFeature API", "nodeName", node.Name)

features := nfdv1alpha1.NewNodeFeatureSpec()
Expand Down Expand Up @@ -875,6 +884,11 @@ func (m *nfdMaster) refreshNodeFeatures(node *corev1.Node, labels map[string]str
taints = filterTaints(crTaints)
}

if m.config.NoPublish {
klog.V(1).InfoS("node update skipped, NoPublish=true", "nodeName", node.Name)
return nil
}

err := m.updateNodeObject(node, labels, annotations, extendedResources, taints)
if err != nil {
klog.ErrorS(err, "failed to update node", "nodeName", node.Name)
Expand Down Expand Up @@ -1250,18 +1264,6 @@ func (m *nfdMaster) configure(filepath string, overrides string) error {
return err
}

if !c.NoPublish {
kubeconfig, err := utils.GetKubeconfig(m.args.Kubeconfig)
if err != nil {
return err
}
cli, err := k8sclient.NewForConfig(kubeconfig)
if err != nil {
return err
}
m.k8sClient = cli
}

// Pre-process DenyLabelNS into 2 lists: one for normal ns, and the other for wildcard ns
normalDeniedNs, wildcardDeniedNs := preProcessDeniedNamespaces(c.DenyLabelNs)
m.deniedNs.normal = normalDeniedNs
Expand Down
9 changes: 8 additions & 1 deletion pkg/nfd-master/nfd-master_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"testing"

. "github.com/smartystreets/goconvey/convey"
fakeclient "k8s.io/client-go/kubernetes/fake"
m "sigs.k8s.io/node-feature-discovery/pkg/nfd-master"
)

Expand All @@ -36,7 +37,13 @@ func TestNewNfdMaster(t *testing.T) {
})
})
Convey("When -config is supplied", func() {
_, err := m.NewNfdMaster(m.WithArgs(&m.Args{CertFile: "crt", KeyFile: "key", CaFile: "ca", ConfigFile: "master-config.yaml"}))
_, err := m.NewNfdMaster(
m.WithArgs(&m.Args{
CertFile: "crt",
KeyFile: "key",
CaFile: "ca",
ConfigFile: "master-config.yaml"}),
m.WithKubernetesClient(fakeclient.NewSimpleClientset()))
Convey("An error should not be returned", func() {
So(err, ShouldBeNil)
})
Expand Down

0 comments on commit 0a4da25

Please sign in to comment.