-
Notifications
You must be signed in to change notification settings - Fork 245
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: PiotrProkop <pprokop@nvidia.com>
- Loading branch information
1 parent
0159ab0
commit 59d50c1
Showing
11 changed files
with
511 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
apiVersion: kustomize.config.k8s.io/v1beta1 | ||
kind: Kustomization | ||
|
||
namespace: node-feature-discovery | ||
|
||
resources: | ||
- topologyupdater-gc-clusterrole.yaml | ||
- topologyupdater-gc-clusterrolebinding.yaml | ||
- topologyupdater-gc-serviceaccount.yaml |
25 changes: 25 additions & 0 deletions
25
deployment/base/rbac-topologyupdater-gc/topologyupdater-gc-clusterrole.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
apiVersion: rbac.authorization.k8s.io/v1 | ||
kind: ClusterRole | ||
metadata: | ||
name: nfd-topology-gc | ||
rules: | ||
- apiGroups: | ||
- "" | ||
resources: | ||
- nodes | ||
verbs: | ||
- list | ||
- watch | ||
- apiGroups: | ||
- "" | ||
resources: | ||
- nodes/proxy | ||
verbs: | ||
- get | ||
- apiGroups: | ||
- topology.node.k8s.io | ||
resources: | ||
- noderesourcetopologies | ||
verbs: | ||
- delete | ||
- list |
12 changes: 12 additions & 0 deletions
12
deployment/base/rbac-topologyupdater-gc/topologyupdater-gc-clusterrolebinding.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
apiVersion: rbac.authorization.k8s.io/v1 | ||
kind: ClusterRoleBinding | ||
metadata: | ||
name: nfd-topology-gc | ||
roleRef: | ||
apiGroup: rbac.authorization.k8s.io | ||
kind: ClusterRole | ||
name: nfd-topology-gc | ||
subjects: | ||
- kind: ServiceAccount | ||
name: nfd-topology-gc | ||
namespace: default |
4 changes: 4 additions & 0 deletions
4
deployment/base/rbac-topologyupdater-gc/topologyupdater-gc-serviceaccount.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
apiVersion: v1 | ||
kind: ServiceAccount | ||
metadata: | ||
name: nfd-topology-gc |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
apiVersion: kustomize.config.k8s.io/v1beta1 | ||
kind: Kustomization | ||
|
||
namespace: node-feature-discovery | ||
|
||
resources: | ||
- topologyupdater-gc.yaml |
25 changes: 25 additions & 0 deletions
25
deployment/base/topologyupdater-gc/topologyupdater-gc.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
apiVersion: apps/v1 | ||
kind: Deployment | ||
metadata: | ||
labels: | ||
app: nfd | ||
name: nfd-topology-gc | ||
spec: | ||
selector: | ||
matchLabels: | ||
app: nfd-topology-gc | ||
template: | ||
metadata: | ||
labels: | ||
app: nfd-topology-gc | ||
spec: | ||
dnsPolicy: ClusterFirstWithHostNet | ||
serviceAccount: nfd-topology-gc | ||
containers: | ||
- name: nfd-topology-gc | ||
image: gcr.io/k8s-staging-nfd/node-feature-discovery:master | ||
imagePullPolicy: Always | ||
command: | ||
- "nfd-topology-updater" | ||
- "-gc-enabled" | ||
- "-gc-interval=10s" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,175 @@ | ||
/* | ||
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 nfdtopologyupdater | ||
|
||
import ( | ||
"context" | ||
"time" | ||
|
||
topologyclientset "github.com/k8stopologyawareschedwg/noderesourcetopology-api/pkg/generated/clientset/versioned" | ||
corev1 "k8s.io/api/core/v1" | ||
"k8s.io/apimachinery/pkg/api/errors" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/client-go/informers" | ||
"k8s.io/client-go/kubernetes" | ||
restclient "k8s.io/client-go/rest" | ||
"k8s.io/client-go/tools/cache" | ||
"k8s.io/klog/v2" | ||
|
||
"sigs.k8s.io/node-feature-discovery/pkg/apihelper" | ||
) | ||
|
||
type nrtGarbageCollector struct { | ||
stopChan chan struct{} | ||
k8sClient kubernetes.Interface | ||
topoClient topologyclientset.Interface | ||
gcPeriod time.Duration | ||
} | ||
|
||
func newNRTGarbageCollector(config *restclient.Config, stop chan struct{}, gcPeriod time.Duration) (*nrtGarbageCollector, error) { | ||
helper := apihelper.K8sHelpers{Kubeconfig: config} | ||
cli, err := helper.GetTopologyClient() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
clientset := kubernetes.NewForConfigOrDie(config) | ||
|
||
return &nrtGarbageCollector{ | ||
k8sClient: clientset, | ||
topoClient: cli, | ||
stopChan: stop, | ||
gcPeriod: gcPeriod, | ||
}, nil | ||
} | ||
|
||
func (n *nrtGarbageCollector) deleteNodeHandler(object interface{}) { | ||
// handle a case when we are starting up and need to clear stale NRT resources | ||
obj := object | ||
if deletedFinalStateUnknown, ok := object.(cache.DeletedFinalStateUnknown); ok { | ||
klog.V(2).Infof("found stale NodeResourceTopology for node: %s ", object) | ||
obj = deletedFinalStateUnknown.Obj | ||
} | ||
|
||
node, ok := obj.(*corev1.Node) | ||
if !ok { | ||
klog.Errorf("cannot convert %v to v1.Node", object) | ||
return | ||
} | ||
if err := n.topoClient.TopologyV1alpha1().NodeResourceTopologies().Delete(context.TODO(), node.GetName(), metav1.DeleteOptions{}); err != nil { | ||
if errors.IsNotFound(err) { | ||
klog.V(2).Infof("NodeResourceTopology for node %s not found, omitting deletion", node.GetName()) | ||
return | ||
} else { | ||
klog.Warningf("failed to delete NodeResourceTopology for node %s: %s", node.GetName(), err.Error()) | ||
return | ||
} | ||
} | ||
klog.Infof("NodeResourceTopology for node %s has been deleted", node.GetName()) | ||
} | ||
|
||
// populateNodeIndexer populates cache with NRTs so we know which one to delete on first synchronization | ||
func (n *nrtGarbageCollector) populateNodeIndexer(indexer cache.Indexer) error { | ||
nrts, err := n.topoClient.TopologyV1alpha1().NodeResourceTopologies().List(context.TODO(), metav1.ListOptions{}) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
for _, nrt := range nrts.Items { | ||
node := &corev1.Node{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: nrt.GetName(), | ||
}, | ||
} | ||
klog.V(2).Infof("adding node %s to indexer", node.GetName()) | ||
err := indexer.Add(node) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
} | ||
|
||
return nil | ||
} | ||
|
||
// periodicGC runs garbage collector at every gcPeriod to make sure we haven't missed any node | ||
func (n *nrtGarbageCollector) periodicGC(gcPeriod time.Duration, indexer cache.Indexer) { | ||
gcTrigger := time.NewTicker(gcPeriod) | ||
for { | ||
select { | ||
case <-gcTrigger.C: | ||
klog.Infof("Running periodic GC") | ||
objects := indexer.List() | ||
nodes := map[string]struct{}{} | ||
for _, object := range objects { | ||
key, err := cache.MetaNamespaceKeyFunc(object) | ||
if err != nil { | ||
klog.Warningf("cannot create key for %v: %s", object, err.Error()) | ||
continue | ||
} | ||
nodes[key] = struct{}{} | ||
} | ||
nrts, err := n.topoClient.TopologyV1alpha1().NodeResourceTopologies().List(context.TODO(), metav1.ListOptions{}) | ||
if err != nil { | ||
klog.Warningf("cannot list NRTs %s", err.Error()) | ||
break | ||
} | ||
for _, nrt := range nrts.Items { | ||
key, err := cache.MetaNamespaceKeyFunc(&nrt) | ||
if err != nil { | ||
klog.Warningf("cannot create key for %v: %s", nrt, err.Error()) | ||
continue | ||
} | ||
if _, ok := nodes[key]; !ok { | ||
if err := n.topoClient.TopologyV1alpha1().NodeResourceTopologies().Delete(context.TODO(), nrt.GetName(), metav1.DeleteOptions{}); err != nil { | ||
if errors.IsNotFound(err) { | ||
klog.V(2).Infof("NodeResourceTopology %s not found, omitting deletion", nrt.GetName()) | ||
continue | ||
} else { | ||
klog.Warningf("failed to delete NodeResourceTopology for node %s: %s", nrt.GetName(), err.Error()) | ||
continue | ||
} | ||
} | ||
} | ||
} | ||
case <-n.stopChan: | ||
klog.Infof("shutting down periodic Garbage Collector") | ||
return | ||
} | ||
} | ||
} | ||
|
||
func (n *nrtGarbageCollector) start() error { | ||
factory := informers.NewSharedInformerFactory(n.k8sClient, 5*time.Minute) | ||
nodeInformer := factory.Core().V1().Nodes().Informer() | ||
|
||
nodeInformer.AddEventHandler(cache.ResourceEventHandlerFuncs{ | ||
DeleteFunc: n.deleteNodeHandler, | ||
}) | ||
|
||
if err := n.populateNodeIndexer(nodeInformer.GetIndexer()); err != nil { | ||
return err | ||
} | ||
|
||
factory.Start(n.stopChan) | ||
// do initial cleanup | ||
factory.WaitForCacheSync(n.stopChan) | ||
|
||
go n.periodicGC(n.gcPeriod, nodeInformer.GetIndexer()) | ||
|
||
return nil | ||
} |
Oops, something went wrong.