Skip to content

Commit

Permalink
nfd-worker: Add an option to disable setting the owner references
Browse files Browse the repository at this point in the history
In some cases it's desirable to control automatic garbage collection
of NodeFeature object.

Add an option to disable setting the owner references to Pod
for NodeFeature object.

Closes: 1817

Signed-off-by: Oleg Zhurakivskyy <oleg.zhurakivskyy@intel.com>
  • Loading branch information
ozhuraki committed Nov 28, 2024
1 parent a1f6f8a commit e38101c
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 34 deletions.
4 changes: 4 additions & 0 deletions cmd/nfd-worker/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
})

Expand Down Expand Up @@ -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.")
Expand Down
2 changes: 2 additions & 0 deletions cmd/nfd-worker/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"})
})
Expand Down
17 changes: 17 additions & 0 deletions docs/reference/worker-commandline-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 18 additions & 0 deletions docs/reference/worker-configuration-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
84 changes: 50 additions & 34 deletions pkg/nfd-worker/nfd-worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ type coreConfig struct {
Klog klogutils.KlogConfigOpts
LabelWhiteList utils.RegexpVal
NoPublish bool
NoOwnerRefs bool
FeatureSources []string
Sources *[]string
LabelSources []string
Expand All @@ -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
}
Expand Down Expand Up @@ -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)
Expand All @@ -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,
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit e38101c

Please sign in to comment.