1
0
Fork 0
mirror of https://github.com/kubernetes-sigs/node-feature-discovery.git synced 2025-03-05 08:17:04 +00:00

kubeletnotifier: introduce kubeletnotifier package

Enabling reactive update for nfd-topology-updater
by detecting changes in Kubelet state/checkpoint files,
and signaling to the main loop to update the NodeResourceTopology
objects.

This has high value when scaling is an issue.
Having multiple pods deployed in between single update instance
might reflect incorrect resource accounting in the NRT CRs.
Example:
Time Interval = 5s
t0 - New update sent to NRT CRs
t1 - Schedule guaranteed podA
t2 - Schedule guaranteed podB
time elapsed between t0-t2 < 5 seconds,
IOW the update on t0 is the recent update.

In t2 the resource accounting reflected by NRT
is not aligned with the actual accounting because
NRT CRs doesn't reflect the change happened in t1.

With this reactive update feature we expect an update to be trigger
between t1 and t2 so the NRT objects will reflect more accurate
picture.

There still might be a scenario when the updates
aren't fast enough, but this is an additional
future planned optimization.

The notifier has two event types:
1. Time based - keeping the old behavior, trigger
an update per interval.
2. FS event - trigger an update when Kubelet state/checkpoint files modified.

Signed-off-by: Talor Itzhak <titzhak@redhat.com>
This commit is contained in:
Talor Itzhak 2023-01-05 14:55:25 +02:00
parent adea670ded
commit 0f65b87329

View file

@ -0,0 +1,96 @@
/*
Copyright 2023 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package kubeletnotifier
import (
"fmt"
"time"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/klog/v2"
"github.com/fsnotify/fsnotify"
)
type EventType string
const (
IntervalBased EventType = "intervalBased"
FSUpdate EventType = "fsUpdate"
)
// TODO make path configurable
var stateFiles = sets.NewString(
"cpu_manager_state",
"memory_manager_state",
"kubelet_internal_checkpoint",
)
type Notifier struct {
sleepInterval time.Duration
// destination where notifications are sent
dest chan<- Info
fsEvent <-chan fsnotify.Event
}
type Info struct {
Event EventType
}
func New(sleepInterval time.Duration, dest chan<- Info, kubeletDirPath string) (*Notifier, error) {
ch, err := createFSWatcherEvent([]string{kubeletDirPath})
if err != nil {
return nil, err
}
return &Notifier{
sleepInterval: sleepInterval,
dest: dest,
fsEvent: ch,
}, nil
}
func (n *Notifier) Run() {
t := time.Tick(n.sleepInterval)
for {
select {
case <-t:
klog.V(5).Infof("timer update received")
i := Info{Event: IntervalBased}
n.dest <- i
case e := <-n.fsEvent:
klog.V(5).Infof("fsnotify event from file %q: %q received", e.Name, e.Op)
if stateFiles.Has(e.Name) {
i := Info{Event: FSUpdate}
n.dest <- i
}
}
}
}
func createFSWatcherEvent(fsWatchPaths []string) (chan fsnotify.Event, error) {
fsWatcher, err := fsnotify.NewWatcher()
if err != nil {
return nil, err
}
for _, path := range fsWatchPaths {
if err = fsWatcher.Add(path); err != nil {
return nil, fmt.Errorf("failed to watch: %q; %w", path, err)
}
}
return fsWatcher.Events, nil
}