Skip to content

Commit c85d519

Browse files
committed
Remove non KMM related tolerations from the NMC status
Until now, the nmc status reflected all pod tolerations, including those unrelated to KMM. This commit updates the behavior to source tolerations from a dedicated annotation instead, ensuring only KMM-specific tolerations appear in the status.
1 parent 6131e7d commit c85d519

File tree

5 files changed

+72
-18
lines changed

5 files changed

+72
-18
lines changed

internal/controllers/nmc_reconciler.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -563,15 +563,21 @@ func (h *nmcReconcilerHelperImpl) SyncStatus(ctx context.Context, nmcObj *kmmv1b
563563
errs,
564564
fmt.Errorf("%s: could not unmarshal the ModuleConfig from YAML: %v", podNSN, err),
565565
)
566-
566+
continue
567+
}
568+
tolerationsAnnotation := h.podManager.GetTolerationsAnnotation(&p)
569+
if err = yaml.UnmarshalStrict([]byte(tolerationsAnnotation), &status.Tolerations); err != nil {
570+
errs = append(
571+
errs,
572+
fmt.Errorf("%s: could not unmarshal the ModuleConfig from YAML: %v", podNSN, err),
573+
)
567574
continue
568575
}
569576

570577
if p.Spec.ImagePullSecrets != nil {
571578
status.ImageRepoSecret = &p.Spec.ImagePullSecrets[0]
572579
}
573580
status.ServiceAccountName = p.Spec.ServiceAccountName
574-
status.Tolerations = p.Spec.Tolerations
575581

576582
status.BootId = node.Status.NodeInfo.BootID
577583

internal/controllers/nmc_reconciler_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1229,10 +1229,14 @@ var _ = Describe("nmcReconcilerHelperImpl_SyncStatus", func() {
12291229
b, err := yaml.Marshal(cfg)
12301230
Expect(err).NotTo(HaveOccurred())
12311231

1232+
tolerations, err := yaml.Marshal(p.Spec.Tolerations)
1233+
Expect(err).NotTo(HaveOccurred())
1234+
12321235
gomock.InOrder(
12331236
mockWorkerPodManager.EXPECT().ListWorkerPodsOnNode(ctx, nmcName).Return([]v1.Pod{p}, nil),
12341237
mockWorkerPodManager.EXPECT().IsUnloaderPod(&p).Return(false),
12351238
mockWorkerPodManager.EXPECT().GetConfigAnnotation(&p).Return(string(b)),
1239+
mockWorkerPodManager.EXPECT().GetTolerationsAnnotation(&p).Return(string(tolerations)),
12361240
kubeClient.EXPECT().Status().Return(sw),
12371241
sw.EXPECT().Patch(ctx, nmc, gomock.Any()),
12381242
mockWorkerPodManager.EXPECT().DeletePod(ctx, &p),

internal/pod/mock_workerpodmanager.go

Lines changed: 14 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/pod/workerpodmanager.go

Lines changed: 42 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -40,26 +40,28 @@ type WorkerPodManager interface {
4040
IsUnloaderPod(p *v1.Pod) bool
4141
GetConfigAnnotation(p *v1.Pod) string
4242
HashAnnotationDiffer(p1, p2 *v1.Pod) bool
43+
GetTolerationsAnnotation(p *v1.Pod) string
4344
}
4445

4546
const (
4647
NodeModulesConfigFinalizer = "kmm.node.kubernetes.io/nodemodulesconfig-reconciler"
4748
WorkerContainerName = "worker"
4849

49-
volMountPointConfig = "/etc/kmm-worker"
50-
configFileName = "config.yaml"
51-
configFullPath = volMountPointConfig + "/" + configFileName
52-
volNameConfig = "config"
53-
sharedFilesDir = "/tmp"
54-
volNameTmp = "tmp"
55-
volumeNameConfig = "config"
56-
initContainerName = "image-extractor"
57-
modulesOrderKey = "kmm.node.kubernetes.io/modules-order"
58-
workerActionLoad = "Load"
59-
workerActionUnload = "Unload"
60-
actionLabelKey = "kmm.node.kubernetes.io/worker-action"
61-
configAnnotationKey = "kmm.node.kubernetes.io/worker-config"
62-
hashAnnotationKey = "kmm.node.kubernetes.io/worker-hash"
50+
volMountPointConfig = "/etc/kmm-worker"
51+
configFileName = "config.yaml"
52+
configFullPath = volMountPointConfig + "/" + configFileName
53+
volNameConfig = "config"
54+
sharedFilesDir = "/tmp"
55+
volNameTmp = "tmp"
56+
volumeNameConfig = "config"
57+
initContainerName = "image-extractor"
58+
modulesOrderKey = "kmm.node.kubernetes.io/modules-order"
59+
workerActionLoad = "Load"
60+
workerActionUnload = "Unload"
61+
actionLabelKey = "kmm.node.kubernetes.io/worker-action"
62+
configAnnotationKey = "kmm.node.kubernetes.io/worker-config"
63+
hashAnnotationKey = "kmm.node.kubernetes.io/worker-hash"
64+
tolerationsAnnotationKey = "kmm.node.kubernetes.io/worker-tolerations"
6365
)
6466

6567
var (
@@ -195,6 +197,9 @@ func (wpmi *workerPodManagerImpl) LoaderPodTemplate(ctx context.Context, nmc cli
195197
if err = setWorkerConfigAnnotation(pod, nms.Config); err != nil {
196198
return nil, fmt.Errorf("could not set worker config: %v", err)
197199
}
200+
if err = setWorkerTolerationsAnnotation(pod, nms.Tolerations); err != nil {
201+
return nil, fmt.Errorf("could not set worker tolerations: %v", err)
202+
}
198203

199204
if err = setWorkerSecurityContext(pod, wpmi.workerCfg, privileged); err != nil {
200205
return nil, fmt.Errorf("could not set the worker Pod as privileged: %v", err)
@@ -220,6 +225,9 @@ func (wpmi *workerPodManagerImpl) UnloaderPodTemplate(ctx context.Context, nmc c
220225
if err = setWorkerConfigAnnotation(pod, nms.Config); err != nil {
221226
return nil, fmt.Errorf("could not set worker config: %v", err)
222227
}
228+
if err = setWorkerTolerationsAnnotation(pod, nms.Tolerations); err != nil {
229+
return nil, fmt.Errorf("could not set worker tolerations: %v", err)
230+
}
223231

224232
if err = setWorkerSecurityContext(pod, wpmi.workerCfg, false); err != nil {
225233
return nil, fmt.Errorf("could not set the worker Pod's security context: %v", err)
@@ -284,6 +292,14 @@ func (wpmi *workerPodManagerImpl) GetConfigAnnotation(p *v1.Pod) string {
284292

285293
return p.Annotations[configAnnotationKey]
286294
}
295+
func (wpmi *workerPodManagerImpl) GetTolerationsAnnotation(p *v1.Pod) string {
296+
297+
if p == nil {
298+
return ""
299+
}
300+
301+
return p.Annotations[tolerationsAnnotationKey]
302+
}
287303

288304
func (wpmi *workerPodManagerImpl) HashAnnotationDiffer(p1, p2 *v1.Pod) bool {
289305

@@ -455,6 +471,18 @@ func setWorkerConfigAnnotation(pod *v1.Pod, cfg kmmv1beta1.ModuleConfig) error {
455471
return nil
456472
}
457473

474+
func setWorkerTolerationsAnnotation(pod *v1.Pod, tolerations []v1.Toleration) error {
475+
if len(tolerations) != 0 {
476+
b, err := yaml.Marshal(tolerations)
477+
if err != nil {
478+
return fmt.Errorf("could not marshal the Tolerations to YAML: %v", err)
479+
}
480+
meta.SetAnnotation(pod, tolerationsAnnotationKey, string(b))
481+
}
482+
483+
return nil
484+
}
485+
458486
func setWorkerSofdepConfig(pod *v1.Pod, modulesLoadingOrder []string) error {
459487
softdepAnnotationValue := getModulesOrderAnnotationValue(modulesLoadingOrder)
460488
meta.SetAnnotation(pod, modulesOrderKey, softdepAnnotationValue)

internal/pod/workerpodmanager_test.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -485,6 +485,7 @@ cp -R /firmware-path/* /tmp/firmware-path;
485485
} else {
486486
configAnnotationValue = strings.ReplaceAll(configAnnotationValue, "firmwarePath: /firmware-path\n ", "")
487487
}
488+
tolerationAnotationValue := "- effect: NoExecute\n key: test-key\n value: test-value\n"
488489
pod := v1.Pod{
489490
ObjectMeta: metav1.ObjectMeta{
490491
Name: WorkerPodName(nmcName, moduleName),
@@ -497,8 +498,9 @@ cp -R /firmware-path/* /tmp/firmware-path;
497498
constants.ModuleNameLabel: moduleName,
498499
},
499500
Annotations: map[string]string{
500-
configAnnotationKey: configAnnotationValue,
501-
modulesOrderKey: modulesOrderValue,
501+
configAnnotationKey: configAnnotationValue,
502+
modulesOrderKey: modulesOrderValue,
503+
tolerationsAnnotationKey: tolerationAnotationValue,
502504
},
503505
},
504506
Spec: v1.PodSpec{

0 commit comments

Comments
 (0)