1
0
Fork 0
mirror of https://github.com/kubernetes-sigs/node-feature-discovery.git synced 2024-12-14 11:57:51 +00:00

nfd-worker: Add an option to disable setting the owner references

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>
This commit is contained in:
Oleg Zhurakivskyy 2024-08-28 21:00:23 +03:00
parent a1f6f8a1b2
commit 20ef877ab1
7 changed files with 93 additions and 34 deletions

View file

@ -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.")

View file

@ -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"})
})

View file

@ -1,6 +1,7 @@
#core:
# labelWhiteList:
# noPublish: false
# noOwnerRefs: false
# sleepInterval: 60s
# featureSources: [all]
# labelSources: [all]

View file

@ -177,6 +177,7 @@ worker:
#core:
# labelWhiteList:
# noPublish: false
# noOwnerRefs: false
# sleepInterval: 60s
# featureSources: [all]
# labelSources: [all]

View file

@ -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

View file

@ -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.

View file

@ -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
}