Skip to content

Commit be065ff

Browse files
Adding support for loading multiple kernel modules(softdep)
1 parent f0cb9a0 commit be065ff

File tree

2 files changed

+82
-11
lines changed

2 files changed

+82
-11
lines changed

internal/controllers/nmc_reconciler.go

+61-11
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ func (w *workerHelperImpl) ProcessModuleSpec(
266266
}
267267

268268
if readyCondition.Status == v1.ConditionTrue && status.LastTransitionTime.Before(&readyCondition.LastTransitionTime) {
269-
logger.Info("Outdated last transition time status; creating unloader Pod")
269+
logger.Info("Outdated last transition time status; creating loader Pod")
270270

271271
return w.pm.CreateLoaderPod(ctx, nmc, spec)
272272
}
@@ -496,6 +496,10 @@ func (p *podManagerImpl) CreateLoaderPod(ctx context.Context, nmc client.Object,
496496
return fmt.Errorf("could not set worker config: %v", err)
497497
}
498498

499+
if nms.Config.Modprobe.ModulesLoadingOrder != nil {
500+
setWorkerSofdepConfig(pod, nms.Config.Modprobe.ModulesLoadingOrder)
501+
}
502+
499503
setWorkerActionLabel(pod, WorkerActionLoad)
500504
pod.Spec.RestartPolicy = v1.RestartPolicyNever
501505

@@ -516,6 +520,10 @@ func (p *podManagerImpl) CreateUnloaderPod(ctx context.Context, nmc client.Objec
516520
return fmt.Errorf("could not set worker config: %v", err)
517521
}
518522

523+
if nms.Config.Modprobe.ModulesLoadingOrder != nil {
524+
setWorkerSofdepConfig(pod, nms.Config.Modprobe.ModulesLoadingOrder)
525+
}
526+
519527
setWorkerActionLabel(pod, WorkerActionUnload)
520528

521529
return p.client.Create(ctx, pod)
@@ -724,16 +732,7 @@ func setWorkerConfigAnnotation(pod *v1.Pod, cfg kmmv1beta1.ModuleConfig) error {
724732
if err != nil {
725733
return fmt.Errorf("could not marshal the ModuleConfig to YAML: %v", err)
726734
}
727-
728-
annotations := pod.GetAnnotations()
729-
730-
if annotations == nil {
731-
annotations = make(map[string]string)
732-
}
733-
734-
annotations[configAnnotationKey] = string(b)
735-
736-
pod.SetAnnotations(annotations)
735+
setWorkerPodAnnotation(pod, configAnnotationKey, string(b))
737736

738737
return nil
739738
}
@@ -749,6 +748,57 @@ func setWorkerContainerArgs(pod *v1.Pod, args []string) error {
749748
return nil
750749
}
751750

751+
func setWorkerSofdepConfig(pod *v1.Pod, modulesLoadingOrder []string) {
752+
softdepAnnotationValue := getModulesOrderAnnotationValue(modulesLoadingOrder)
753+
setWorkerPodAnnotation(pod, "modules-order", softdepAnnotationValue)
754+
755+
softdepVolume := v1.Volume{
756+
Name: "modules-order",
757+
VolumeSource: v1.VolumeSource{
758+
DownwardAPI: &v1.DownwardAPIVolumeSource{
759+
Items: []v1.DownwardAPIVolumeFile{
760+
{
761+
Path: "softdep.conf",
762+
FieldRef: &v1.ObjectFieldSelector{FieldPath: "metadata.annotations['modules-order']"},
763+
},
764+
},
765+
},
766+
},
767+
}
768+
softDepVolumeMount := v1.VolumeMount{
769+
Name: "modules-order",
770+
ReadOnly: true,
771+
MountPath: "/etc/modprobe.d",
772+
}
773+
pod.Spec.Volumes = append(pod.Spec.Volumes, softdepVolume)
774+
pod.Spec.Containers[0].VolumeMounts = append(pod.Spec.Containers[0].VolumeMounts, softDepVolumeMount)
775+
}
776+
777+
func getModulesOrderAnnotationValue(modulesNames []string) string {
778+
softDepData := ""
779+
for i := 0; i < len(modulesNames); i++ {
780+
if i == len(modulesNames)-1 {
781+
break
782+
}
783+
line := prepareSoftDepLine(modulesNames[i], modulesNames[i+1])
784+
softDepData = softDepData + line
785+
}
786+
return softDepData
787+
}
788+
789+
func prepareSoftDepLine(dependendModuleName, moduleName string) string {
790+
return fmt.Sprintf("softdep %s pre: %s\n", dependendModuleName, moduleName)
791+
}
792+
793+
func setWorkerPodAnnotation(pod *v1.Pod, key, value string) {
794+
annotations := pod.GetAnnotations()
795+
if annotations == nil {
796+
annotations = make(map[string]string)
797+
}
798+
annotations[key] = value
799+
pod.SetAnnotations(annotations)
800+
}
801+
752802
//go:generate mockgen -source=nmc_reconciler.go -package=controllers -destination=mock_nmc_reconciler.go pullSecretHelper
753803

754804
type pullSecretHelper interface {

internal/controllers/nmc_reconciler_test.go

+21
Original file line numberDiff line numberDiff line change
@@ -997,6 +997,9 @@ modprobe:
997997
parameters:
998998
- a
999999
- b
1000+
`,
1001+
"modules-order": `softdep a pre: b
1002+
softdep b pre: c
10001003
`,
10011004
},
10021005
},
@@ -1033,6 +1036,11 @@ modprobe:
10331036
Name: volNameVarLibFirmware,
10341037
MountPath: "/var/lib/firmware",
10351038
},
1039+
{
1040+
Name: "modules-order",
1041+
ReadOnly: true,
1042+
MountPath: "/etc/modprobe.d",
1043+
},
10361044
},
10371045
},
10381046
},
@@ -1082,6 +1090,19 @@ modprobe:
10821090
},
10831091
},
10841092
},
1093+
{
1094+
Name: "modules-order",
1095+
VolumeSource: v1.VolumeSource{
1096+
DownwardAPI: &v1.DownwardAPIVolumeSource{
1097+
Items: []v1.DownwardAPIVolumeFile{
1098+
{
1099+
Path: "softdep.conf",
1100+
FieldRef: &v1.ObjectFieldSelector{FieldPath: "metadata.annotations['modules-order']"},
1101+
},
1102+
},
1103+
},
1104+
},
1105+
},
10851106
},
10861107
},
10871108
}

0 commit comments

Comments
 (0)