Skip to content

Commit

Permalink
topology-updater: Add owner reference to NRT object
Browse files Browse the repository at this point in the history
Signed-off-by: Oleg Zhurakivskyy <oleg.zhurakivskyy@intel.com>
  • Loading branch information
ozhuraki committed Mar 14, 2024
1 parent 52d4337 commit 9880248
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ rules:
verbs:
- get
- list
- apiGroups:
- ""
resources:
- namespaces
verbs:
- get
- apiGroups:
- ""
resources:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@ rules:
verbs:
- get
- list
- apiGroups:
- ""
resources:
- namespaces
verbs:
- get
- apiGroups:
- ""
resources:
Expand Down
28 changes: 27 additions & 1 deletion pkg/nfd-topology-updater/nfd-topology-updater.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (

"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
k8sclient "k8s.io/client-go/kubernetes"
"k8s.io/klog/v2"
kubeletconfigv1beta1 "k8s.io/kubelet/config/v1beta1"
Expand Down Expand Up @@ -80,6 +81,9 @@ type nfdTopologyUpdater struct {
eventSource <-chan kubeletnotifier.Info
configFilePath string
config *NFDConfig
kubernetesNamespace string
ownerRefs []metav1.OwnerReference
k8sClient k8sclient.Interface
kubeletConfigFunc func() (*kubeletconfigv1beta1.KubeletConfiguration, error)
}

Expand All @@ -105,6 +109,8 @@ func NewTopologyUpdater(args Args, resourcemonitorArgs resourcemonitor.Args) (Nf
nodeName: utils.NodeName(),
eventSource: eventSource,
config: &NFDConfig{},
kubernetesNamespace: utils.GetKubernetesNamespace(),
ownerRefs: []metav1.OwnerReference{},
kubeletConfigFunc: kubeletConfigFunc,
}
if args.ConfigFile != "" {
Expand Down Expand Up @@ -146,6 +152,7 @@ func (w *nfdTopologyUpdater) Run() error {
if err != nil {
return err
}
w.k8sClient = k8sClient

if err := w.configure(); err != nil {
return fmt.Errorf("faild to configure Node Feature Discovery Topology Updater: %w", err)
Expand Down Expand Up @@ -222,11 +229,29 @@ func (w *nfdTopologyUpdater) Stop() {
}

func (w *nfdTopologyUpdater) updateNodeResourceTopology(zoneInfo v1alpha2.ZoneList, scanResponse resourcemonitor.ScanResponse, readKubeletConfig bool) error {

if len(w.ownerRefs) == 0 {
ns, err := w.k8sClient.CoreV1().Namespaces().Get(context.TODO(), w.kubernetesNamespace, metav1.GetOptions{})
if err != nil {
klog.ErrorS(err, "Cannot get NodeResourceTopology owner reference")
} else {
w.ownerRefs = []metav1.OwnerReference{
{
APIVersion: "v1",
Kind: "Namespace",
Name: ns.Name,
UID: types.UID(ns.UID),
},
}
}
}

nrt, err := w.topoClient.TopologyV1alpha2().NodeResourceTopologies().Get(context.TODO(), w.nodeName, metav1.GetOptions{})
if errors.IsNotFound(err) {
nrtNew := v1alpha2.NodeResourceTopology{
ObjectMeta: metav1.ObjectMeta{
Name: w.nodeName,
Name: w.nodeName,
OwnerReferences: w.ownerRefs,
},
Zones: zoneInfo,
Attributes: v1alpha2.AttributeList{},
Expand All @@ -248,6 +273,7 @@ func (w *nfdTopologyUpdater) updateNodeResourceTopology(zoneInfo v1alpha2.ZoneLi

nrtMutated := nrt.DeepCopy()
nrtMutated.Zones = zoneInfo
nrtMutated.OwnerReferences = w.ownerRefs

attributes := scanResponse.Attributes

Expand Down
22 changes: 22 additions & 0 deletions test/e2e/topology_updater_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,28 @@ var _ = NFDDescribe(Label("nfd-topology-updater"), func() {
return true
}, time.Minute, 5*time.Second).Should(BeTrue(), "didn't get updated node topology info")
})

It("should check that that topology object is garbage colleted", func(ctx context.Context) {

By("Deleting the nfd-topology namespace")
err := f.ClientSet.CoreV1().Namespaces().Delete(ctx, f.Namespace.Name, metav1.DeleteOptions{})
Expect(err).NotTo(HaveOccurred())

By("checking that topology was garbage collected")
Eventually(func() bool {
t, err := topologyClient.TopologyV1alpha2().NodeResourceTopologies().Get(ctx, topologyUpdaterNode.Name, metav1.GetOptions{})
if err != nil {
if apierrors.IsNotFound(err) {
framework.Logf("missing node topology resource for %q", topologyUpdaterNode.Name)
return false
}
framework.Logf("failed to get the node topology resource: %v", err)
return true
}
framework.Logf("topology resource: %v", t)
return true
}).WithPolling(15 * time.Second).WithTimeout(60 * time.Second).Should(BeFalse())
})
})

When("sleep interval disabled", func() {
Expand Down
16 changes: 15 additions & 1 deletion test/e2e/utils/rbac.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
corev1 "k8s.io/api/core/v1"
rbacv1 "k8s.io/api/rbac/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
clientset "k8s.io/client-go/kubernetes"
)

Expand Down Expand Up @@ -97,7 +98,15 @@ func ConfigureRBAC(ctx context.Context, cs clientset.Interface, ns string) error

// DeconfigureRBAC removes RBAC configuration
func DeconfigureRBAC(ctx context.Context, cs clientset.Interface, ns string) error {
err := cs.RbacV1().ClusterRoleBindings().Delete(ctx, "nfd-topology-updater-e2e", metav1.DeleteOptions{})
_, err := cs.CoreV1().Namespaces().Get(ctx, ns, metav1.GetOptions{})
if err != nil {
if apierrors.IsNotFound(err) {
return nil
}
return err
}

err = cs.RbacV1().ClusterRoleBindings().Delete(ctx, "nfd-topology-updater-e2e", metav1.DeleteOptions{})
if err != nil {
return err
}
Expand Down Expand Up @@ -251,6 +260,11 @@ func createClusterRoleTopologyUpdater(ctx context.Context, cs clientset.Interfac
Resources: []string{"pods"},
Verbs: []string{"get", "list", "watch"},
},
{
APIGroups: []string{""},
Resources: []string{"namespaces"},
Verbs: []string{"get"},
},
{
APIGroups: []string{"topology.node.k8s.io"},
Resources: []string{"noderesourcetopologies"},
Expand Down

0 comments on commit 9880248

Please sign in to comment.