diff --git a/cmd/nfd-worker/main.go b/cmd/nfd-worker/main.go index 8a86ba929..d19d3624b 100644 --- a/cmd/nfd-worker/main.go +++ b/cmd/nfd-worker/main.go @@ -94,6 +94,8 @@ func parseArgs(flags *flag.FlagSet, osArgs ...string) *worker.Args { args.Overrides.FeatureSources = overrides.FeatureSources case "label-sources": args.Overrides.LabelSources = overrides.LabelSources + case "no-owner-refs": + args.Overrides.NoOwnerRefs = overrides.NoOwnerRefs } }) @@ -126,6 +128,8 @@ func initFlags(flagset *flag.FlagSet) (*worker.Args, *worker.ConfigOverrideArgs) } overrides.NoPublish = flagset.Bool("no-publish", false, "Do not publish discovered features, disable connection to nfd-master and don't create NodeFeature object.") + overrides.NoOwnerRefs = flagset.Bool("no-owner-refs", false, + "Do not set owner references for NodeFeature object.") flagset.Var(overrides.FeatureSources, "feature-sources", "Comma separated list of feature sources. Special value 'all' enables all sources. "+ "Prefix the source name with '-' to disable it.") diff --git a/cmd/nfd-worker/main_test.go b/cmd/nfd-worker/main_test.go index 3bca6d128..e5f5b24b3 100644 --- a/cmd/nfd-worker/main_test.go +++ b/cmd/nfd-worker/main_test.go @@ -43,12 +43,14 @@ func TestParseArgs(t *testing.T) { Convey("When all override args are specified", func() { args := parseArgs(flags, "-no-publish", + "-no-owner-refs", "-feature-sources=cpu", "-label-sources=fake1,fake2,fake3") Convey("args.sources is set to appropriate values", func() { So(args.Oneshot, ShouldBeFalse) So(*args.Overrides.NoPublish, ShouldBeTrue) + So(*args.Overrides.NoOwnerRefs, ShouldBeTrue) So(*args.Overrides.FeatureSources, ShouldResemble, utils.StringSliceVal{"cpu"}) So(*args.Overrides.LabelSources, ShouldResemble, utils.StringSliceVal{"fake1", "fake2", "fake3"}) }) diff --git a/deployment/components/worker-config/nfd-worker.conf.example b/deployment/components/worker-config/nfd-worker.conf.example index c608a72da..ff9125153 100644 --- a/deployment/components/worker-config/nfd-worker.conf.example +++ b/deployment/components/worker-config/nfd-worker.conf.example @@ -1,6 +1,7 @@ #core: # labelWhiteList: # noPublish: false +# noOwnerRefs: false # sleepInterval: 60s # featureSources: [all] # labelSources: [all] diff --git a/deployment/helm/node-feature-discovery/values.yaml b/deployment/helm/node-feature-discovery/values.yaml index 9ed5e25a6..feebd52b3 100644 --- a/deployment/helm/node-feature-discovery/values.yaml +++ b/deployment/helm/node-feature-discovery/values.yaml @@ -182,6 +182,7 @@ worker: #core: # labelWhiteList: # noPublish: false + # noOwnerRefs: false # sleepInterval: 60s # featureSources: [all] # labelSources: [all] diff --git a/docs/reference/worker-commandline-reference.md b/docs/reference/worker-commandline-reference.md index 95ed1a4c6..6d281e154 100644 --- a/docs/reference/worker-commandline-reference.md +++ b/docs/reference/worker-commandline-reference.md @@ -162,6 +162,23 @@ Example: nfd-worker -no-publish ``` +### -no-owner-refs + +The `-no-owner-refs` flag disables setting the owner references to Pod +of the NodeFeature object. + +> **NOTE:** This flag takes precedence over the +> [`core.noOwnerRefs`](worker-configuration-reference.md#corenoownerrefs) +> configuration file option. + +Default: *false* + +Example: + +```bash +nfd-worker -no-owner-refs +``` + ### -oneshot The `-oneshot` flag causes nfd-worker to exit after one pass of feature diff --git a/docs/reference/worker-configuration-reference.md b/docs/reference/worker-configuration-reference.md index c8a900799..e51353ee0 100644 --- a/docs/reference/worker-configuration-reference.md +++ b/docs/reference/worker-configuration-reference.md @@ -149,6 +149,24 @@ core: noPublish: true ``` +### core.noOwnerRefs + +Setting `core.noOwnerRefs` to `true` disables setting the owner references +of the NodeFeature object created by the nfd-worker. + +> **NOTE:** Overridden by the +> [`-no-owner-refs`](worker-commandline-reference.md#-no-owner-refs) +> command line flag (if specified). + +Default: `false` + +Example: + +```yaml +core: + noOwnerRefs: true +``` + ### core.klog The following options specify the logger configuration. diff --git a/pkg/nfd-worker/nfd-worker.go b/pkg/nfd-worker/nfd-worker.go index 7b5ce4d16..21ececf15 100644 --- a/pkg/nfd-worker/nfd-worker.go +++ b/pkg/nfd-worker/nfd-worker.go @@ -78,6 +78,7 @@ type coreConfig struct { Klog klogutils.KlogConfigOpts LabelWhiteList utils.RegexpVal NoPublish bool + NoOwnerRefs bool FeatureSources []string Sources *[]string LabelSources []string @@ -98,14 +99,15 @@ type Args struct { Options string MetricsPort int GrpcHealthPort int + NoOwnerRefs bool Overrides ConfigOverrideArgs } // ConfigOverrideArgs are args that override config file options type ConfigOverrideArgs struct { - NoPublish *bool - + NoPublish *bool + NoOwnerRefs *bool FeatureSources *utils.StringSliceVal LabelSources *utils.StringSliceVal } @@ -265,7 +267,44 @@ func (w *nfdWorker) runFeatureDiscovery() error { return nil } -// Run NfdWorker client. Returns if a fatal error is encountered, or, after +// Set owner ref +func (w *nfdWorker) setOwnerReference() error { + ownerReference := []metav1.OwnerReference{} + + if !w.config.Core.NoOwnerRefs { + // Get pod owner reference + podName := os.Getenv("POD_NAME") + // Add pod owner reference if it exists + if podName != "" { + if selfPod, err := w.k8sClient.CoreV1().Pods(w.kubernetesNamespace).Get(context.TODO(), podName, metav1.GetOptions{}); err != nil { + klog.ErrorS(err, "failed to get self pod, cannot inherit ownerReference for NodeFeature") + return err + } else { + ownerReference = append(ownerReference, selfPod.OwnerReferences...) + } + + podUID := os.Getenv("POD_UID") + if podUID != "" { + ownerReference = append(ownerReference, metav1.OwnerReference{ + APIVersion: "v1", + Kind: "Pod", + Name: podName, + UID: types.UID(podUID), + }) + } else { + klog.InfoS("Cannot append POD ownerReference to NodeFeature, POD_UID not specified") + } + } else { + klog.InfoS("Cannot set NodeFeature owner references, POD_NAME not specified") + } + } + + w.ownerReference = ownerReference + + return nil +} + +// Run NfdWorker client. Returns an error if a fatal error is encountered, or, after // one request if OneShot is set to 'true' in the worker args. func (w *nfdWorker) Run() error { klog.InfoS("Node Feature Discovery Worker", "version", version.Get(), "nodeName", utils.NodeName(), "namespace", w.kubernetesNamespace) @@ -281,37 +320,6 @@ func (w *nfdWorker) Run() error { labelTrigger.Reset(w.config.Core.SleepInterval.Duration) defer labelTrigger.Stop() - // Create owner ref - ownerReference := []metav1.OwnerReference{} - // Get pod owner reference - podName := os.Getenv("POD_NAME") - - // Add pod owner reference if it exists - if podName != "" { - if selfPod, err := w.k8sClient.CoreV1().Pods(w.kubernetesNamespace).Get(context.TODO(), podName, metav1.GetOptions{}); err != nil { - klog.ErrorS(err, "failed to get self pod, cannot inherit ownerReference for NodeFeature") - return err - } else { - ownerReference = append(ownerReference, selfPod.OwnerReferences...) - } - - podUID := os.Getenv("POD_UID") - if podUID != "" { - ownerReference = append(ownerReference, metav1.OwnerReference{ - APIVersion: "v1", - Kind: "Pod", - Name: podName, - UID: types.UID(podUID), - }) - } else { - klog.InfoS("Cannot append POD ownerReference to NodeFeature, POD_UID not specified") - } - } else { - klog.InfoS("Cannot set NodeFeature owner references, POD_NAME not specified") - } - - w.ownerReference = ownerReference - // Register to metrics server if w.args.MetricsPort > 0 { m := utils.CreateMetricsServer(w.args.MetricsPort, @@ -466,6 +474,11 @@ func (w *nfdWorker) configureCore(c coreConfig) error { klogV.InfoS("enabled label sources", "labelSources", n) } + err = w.setOwnerReference() + if err != nil { + return err + } + return nil } @@ -511,6 +524,9 @@ func (w *nfdWorker) configure(filepath string, overrides string) error { if w.args.Overrides.NoPublish != nil { c.Core.NoPublish = *w.args.Overrides.NoPublish } + if w.args.Overrides.NoOwnerRefs != nil { + c.Core.NoOwnerRefs = *w.args.Overrides.NoOwnerRefs + } if w.args.Overrides.FeatureSources != nil { c.Core.FeatureSources = *w.args.Overrides.FeatureSources }