Skip to content

Commit 08dabd7

Browse files
Adding support for loading multiple kernel modules(softdep) (#549)
1 parent b99968a commit 08dabd7

File tree

2 files changed

+83
-10
lines changed

2 files changed

+83
-10
lines changed

Diff for: internal/controllers/nmc_reconciler.go

+62-10
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"fmt"
77
"path/filepath"
88
"reflect"
9+
"strings"
910

1011
kmmv1beta1 "github.com/kubernetes-sigs/kernel-module-management/api/v1beta1"
1112
"github.com/kubernetes-sigs/kernel-module-management/internal/config"
@@ -267,7 +268,7 @@ func (w *workerHelperImpl) ProcessModuleSpec(
267268
}
268269

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

272273
return w.pm.CreateLoaderPod(ctx, nmc, spec)
273274
}
@@ -497,6 +498,12 @@ func (p *podManagerImpl) CreateLoaderPod(ctx context.Context, nmc client.Object,
497498
return fmt.Errorf("could not set worker config: %v", err)
498499
}
499500

501+
if nms.Config.Modprobe.ModulesLoadingOrder != nil {
502+
if err = setWorkerSofdepConfig(pod, nms.Config.Modprobe.ModulesLoadingOrder); err != nil {
503+
return fmt.Errorf("could not set software dependency for mulitple modules: %v", err)
504+
}
505+
}
506+
500507
setWorkerActionLabel(pod, WorkerActionLoad)
501508
pod.Spec.RestartPolicy = v1.RestartPolicyNever
502509

@@ -517,6 +524,12 @@ func (p *podManagerImpl) CreateUnloaderPod(ctx context.Context, nmc client.Objec
517524
return fmt.Errorf("could not set worker config: %v", err)
518525
}
519526

527+
if nms.Config.Modprobe.ModulesLoadingOrder != nil {
528+
if err = setWorkerSofdepConfig(pod, nms.Config.Modprobe.ModulesLoadingOrder); err != nil {
529+
return fmt.Errorf("could not set software dependency for mulitple modules: %v", err)
530+
}
531+
}
532+
520533
setWorkerActionLabel(pod, WorkerActionUnload)
521534

522535
return p.client.Create(ctx, pod)
@@ -725,29 +738,68 @@ func setWorkerConfigAnnotation(pod *v1.Pod, cfg kmmv1beta1.ModuleConfig) error {
725738
if err != nil {
726739
return fmt.Errorf("could not marshal the ModuleConfig to YAML: %v", err)
727740
}
741+
setWorkerPodAnnotation(pod, configAnnotationKey, string(b))
728742

729-
annotations := pod.GetAnnotations()
743+
return nil
744+
}
730745

731-
if annotations == nil {
732-
annotations = make(map[string]string)
746+
func setWorkerContainerArgs(pod *v1.Pod, args []string) error {
747+
container, _ := podcmd.FindContainerByName(pod, workerContainerName)
748+
if container == nil {
749+
return errors.New("could not find the worker container")
733750
}
734751

735-
annotations[configAnnotationKey] = string(b)
736-
737-
pod.SetAnnotations(annotations)
752+
container.Args = args
738753

739754
return nil
740755
}
741756

742-
func setWorkerContainerArgs(pod *v1.Pod, args []string) error {
757+
func setWorkerSofdepConfig(pod *v1.Pod, modulesLoadingOrder []string) error {
758+
softdepAnnotationValue := getModulesOrderAnnotationValue(modulesLoadingOrder)
759+
setWorkerPodAnnotation(pod, "modules-order", softdepAnnotationValue)
760+
761+
softdepVolume := v1.Volume{
762+
Name: "modules-order",
763+
VolumeSource: v1.VolumeSource{
764+
DownwardAPI: &v1.DownwardAPIVolumeSource{
765+
Items: []v1.DownwardAPIVolumeFile{
766+
{
767+
Path: "softdep.conf",
768+
FieldRef: &v1.ObjectFieldSelector{FieldPath: "metadata.annotations['modules-order']"},
769+
},
770+
},
771+
},
772+
},
773+
}
774+
softDepVolumeMount := v1.VolumeMount{
775+
Name: "modules-order",
776+
ReadOnly: true,
777+
MountPath: "/etc/modprobe.d",
778+
}
779+
pod.Spec.Volumes = append(pod.Spec.Volumes, softdepVolume)
743780
container, _ := podcmd.FindContainerByName(pod, workerContainerName)
744781
if container == nil {
745782
return errors.New("could not find the worker container")
746783
}
784+
container.VolumeMounts = append(pod.Spec.Containers[0].VolumeMounts, softDepVolumeMount)
785+
return nil
786+
}
747787

748-
container.Args = args
788+
func getModulesOrderAnnotationValue(modulesNames []string) string {
789+
var softDepData strings.Builder
790+
for i := 0; i < len(modulesNames)-1; i++ {
791+
fmt.Fprintf(&softDepData, "softdep %s pre: %s\n", modulesNames[i], modulesNames[i+1])
792+
}
793+
return softDepData.String()
794+
}
749795

750-
return nil
796+
func setWorkerPodAnnotation(pod *v1.Pod, key, value string) {
797+
annotations := pod.GetAnnotations()
798+
if annotations == nil {
799+
annotations = make(map[string]string)
800+
}
801+
annotations[key] = value
802+
pod.SetAnnotations(annotations)
751803
}
752804

753805
//go:generate mockgen -source=nmc_reconciler.go -package=controllers -destination=mock_nmc_reconciler.go pullSecretHelper

Diff for: 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)