Skip to content

softdep: use a template #387

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 22 additions & 16 deletions internal/daemonset/daemonset.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package daemonset

import (
"bytes"
"context"
"errors"
"fmt"
"strings"
"text/template"

"github.com/go-openapi/swag"
kmmv1beta1 "github.com/kubernetes-sigs/kernel-module-management/api/v1beta1"
Expand Down Expand Up @@ -179,9 +181,14 @@ func (dc *daemonSetGenerator) SetDriverContainerAsDesired(
}

var modulesOrderAnnotations map[string]string
if mld.Modprobe.ModulesLoadingOrder != nil {
if len(mld.Modprobe.ModulesLoadingOrder) >= 2 {
modulesOrderConf, err := getModulesOrderAnnotationValue(mld)
if err != nil {
return fmt.Errorf("could not generate the softdep configuration: %v", err)
}

modulesOrderAnnotations = map[string]string{
"modules-order": getModulesOrderAnnotationValue(mld),
"modules-order": modulesOrderConf,
}
softdepVolume := v1.Volume{
Name: "modules-order",
Expand Down Expand Up @@ -494,19 +501,18 @@ func makeUnloadCommand(spec kmmv1beta1.ModprobeSpec, modName string) []string {
return append(unloadCommandShell, unloadCommand.String())
}

func getModulesOrderAnnotationValue(mld *api.ModuleLoaderData) string {
modulesNames := mld.Modprobe.ModulesLoadingOrder
softDepData := ""
for i := 0; i < len(modulesNames); i++ {
if i == len(modulesNames)-1 {
break
}
line := prepareSoftDepLine(modulesNames[i], modulesNames[i+1])
softDepData = softDepData + line
}
return softDepData
}
var softdepTemplate = template.Must(
template.New("softdep").Parse(
`{{ range $index, $mod := slice $ 1 -}}
softdep {{ index $ $index }} pre: {{ $mod }}
{{ end -}}
`),
)

func getModulesOrderAnnotationValue(mld *api.ModuleLoaderData) (string, error) {
var buf bytes.Buffer

err := softdepTemplate.Execute(&buf, mld.Modprobe.ModulesLoadingOrder)

func prepareSoftDepLine(dependendModuleName, moduleName string) string {
return fmt.Sprintf("softdep %s pre: %s\n", dependendModuleName, moduleName)
return buf.String(), err
}