Skip to content

Commit b527a57

Browse files
fixing Daemonsets GC (#356)
Fixing the version labels that are checked during GC n order to find out of device-plugin/module-loader DS needs to be deleted
1 parent e5c310c commit b527a57

File tree

2 files changed

+27
-12
lines changed

2 files changed

+27
-12
lines changed

internal/daemonset/daemonset.go

+9-4
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,8 @@ func NewCreator(client client.Client, kernelLabel string, scheme *runtime.Scheme
5757
func (dc *daemonSetGenerator) GarbageCollect(ctx context.Context, mod *kmmv1beta1.Module, existingDS []appsv1.DaemonSet, validKernels sets.Set[string]) ([]string, error) {
5858
deleted := make([]string, 0)
5959

60-
versionLabel := utils.GetModuleVersionLabelName(mod.Namespace, mod.Name)
6160
for _, ds := range existingDS {
62-
if isOlderVersionUnusedDaemonset(&ds, versionLabel, mod.Spec.ModuleLoader.Container.Version) {
61+
if isOlderVersionUnusedDaemonset(&ds, mod.Spec.ModuleLoader.Container.Version) {
6362
if err := dc.client.Delete(ctx, &ds); err != nil {
6463
return nil, fmt.Errorf("could not delete DaemonSet %s: %v", ds.Name, err)
6564
}
@@ -332,8 +331,14 @@ func getDevicePluginNodeLabel(namespace, moduleName string, useDeprecatedLabel b
332331
return fmt.Sprintf("kmm.node.kubernetes.io/%s.%s.device-plugin-ready", namespace, moduleName)
333332
}
334333

335-
func isOlderVersionUnusedDaemonset(ds *appsv1.DaemonSet, moduleVersionLabel, moduleVersion string) bool {
336-
return ds.Labels[moduleVersionLabel] != moduleVersion && ds.Status.DesiredNumberScheduled == 0
334+
func isOlderVersionUnusedDaemonset(ds *appsv1.DaemonSet, moduleVersion string) bool {
335+
moduleName := ds.Labels[constants.ModuleNameLabel]
336+
moduleNamespace := ds.Namespace
337+
versionLabel := utils.GetModuleLoaderVersionLabelName(moduleNamespace, moduleName)
338+
if IsDevicePluginDS(ds) {
339+
versionLabel = utils.GetDevicePluginVersionLabelName(moduleNamespace, moduleName)
340+
}
341+
return ds.Labels[versionLabel] != moduleVersion && ds.Status.DesiredNumberScheduled == 0
337342
}
338343

339344
func isModuleLoaderDaemonsetWithInvalidKernel(ds *appsv1.DaemonSet, kernelLabel string, validKernels sets.Set[string]) bool {

internal/daemonset/daemonset_test.go

+18-8
Original file line numberDiff line numberDiff line change
@@ -550,22 +550,32 @@ var _ = Describe("GarbageCollect", func() {
550550
},
551551
},
552552
}
553-
versionLabel := utils.GetModuleVersionLabelName(mod.Namespace, mod.Name)
553+
moduleLoaderVersionLabel := utils.GetModuleLoaderVersionLabelName(mod.Namespace, mod.Name)
554+
devicePluginVersionLabel := utils.GetDevicePluginVersionLabelName(mod.Namespace, mod.Name)
554555

555-
DescribeTable("device-plugin and modue-loader GC", func(devicePluginFormerLabel, moduleLoaderFormerLabel, moduleLoaderInvalidKernel bool,
556+
DescribeTable("device-plugin and module-loader GC", func(devicePluginFormerLabel, moduleLoaderFormerLabel, moduleLoaderInvalidKernel bool,
556557
devicePluginFormerDesired, moduleLoaderFormerDesired int) {
557558
moduleLoaderDS := appsv1.DaemonSet{
558559
ObjectMeta: metav1.ObjectMeta{
559560
Name: "moduleLoader",
560561
Namespace: "namespace",
561-
Labels: map[string]string{kernelLabel: legitKernelVersion, constants.DaemonSetRole: moduleLoaderRoleLabelValue, versionLabel: currentModuleVersion},
562+
Labels: map[string]string{
563+
kernelLabel: legitKernelVersion,
564+
constants.DaemonSetRole: moduleLoaderRoleLabelValue,
565+
moduleLoaderVersionLabel: currentModuleVersion,
566+
constants.ModuleNameLabel: mod.Name,
567+
},
562568
},
563569
}
564570
devicePluginDS := appsv1.DaemonSet{
565571
ObjectMeta: metav1.ObjectMeta{
566572
Name: "devicePlugin",
567573
Namespace: "namespace",
568-
Labels: map[string]string{constants.DaemonSetRole: devicePluginRoleLabelValue, versionLabel: currentModuleVersion},
574+
Labels: map[string]string{
575+
constants.DaemonSetRole: devicePluginRoleLabelValue,
576+
devicePluginVersionLabel: currentModuleVersion,
577+
constants.ModuleNameLabel: mod.Name,
578+
},
569579
},
570580
}
571581
devicePluginFormerVersionDS := &appsv1.DaemonSet{}
@@ -577,14 +587,14 @@ var _ = Describe("GarbageCollect", func() {
577587
if devicePluginFormerLabel {
578588
devicePluginFormerVersionDS = devicePluginDS.DeepCopy()
579589
devicePluginFormerVersionDS.SetName("devicePluginFormer")
580-
devicePluginFormerVersionDS.Labels[versionLabel] = "former label"
590+
devicePluginFormerVersionDS.Labels[devicePluginVersionLabel] = "former label"
581591
devicePluginFormerVersionDS.Status.DesiredNumberScheduled = int32(devicePluginFormerDesired)
582592
existingDS = append(existingDS, *devicePluginFormerVersionDS)
583593
}
584594
if moduleLoaderFormerLabel {
585595
moduleLoaderFormerVersionDS = moduleLoaderDS.DeepCopy()
586596
moduleLoaderFormerVersionDS.SetName("moduleLoaderFormer")
587-
moduleLoaderFormerVersionDS.Labels[versionLabel] = "former label"
597+
moduleLoaderFormerVersionDS.Labels[moduleLoaderVersionLabel] = "former label"
588598
moduleLoaderFormerVersionDS.Status.DesiredNumberScheduled = int32(moduleLoaderFormerDesired)
589599
existingDS = append(existingDS, *moduleLoaderFormerVersionDS)
590600
}
@@ -627,7 +637,7 @@ var _ = Describe("GarbageCollect", func() {
627637
ObjectMeta: metav1.ObjectMeta{
628638
Name: "moduleLoader",
629639
Namespace: "namespace",
630-
Labels: map[string]string{kernelLabel: notLegitKernelVersion, constants.DaemonSetRole: moduleLoaderRoleLabelValue, versionLabel: currentModuleVersion},
640+
Labels: map[string]string{kernelLabel: notLegitKernelVersion, constants.DaemonSetRole: moduleLoaderRoleLabelValue, moduleLoaderVersionLabel: currentModuleVersion},
631641
},
632642
}
633643
clnt.EXPECT().Delete(context.Background(), &deleteDS).Return(fmt.Errorf("some error"))
@@ -637,7 +647,7 @@ var _ = Describe("GarbageCollect", func() {
637647
_, err := dc.GarbageCollect(context.Background(), mod, existingDS, sets.New[string](legitKernelVersion))
638648
Expect(err).To(HaveOccurred())
639649

640-
deleteDS.Labels[versionLabel] = "former label"
650+
deleteDS.Labels[moduleLoaderVersionLabel] = "former label"
641651
clnt.EXPECT().Delete(context.Background(), &deleteDS).Return(fmt.Errorf("some error"))
642652

643653
existingDS = []appsv1.DaemonSet{deleteDS}

0 commit comments

Comments
 (0)