-
Notifications
You must be signed in to change notification settings - Fork 458
/
kernel_module_spec.go
90 lines (73 loc) · 2.32 KB
/
kernel_module_spec.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
package runtime
import (
"context"
"errors"
"fmt"
"strings"
"github.com/cosi-project/runtime/pkg/controller"
"github.com/cosi-project/runtime/pkg/safe"
"github.com/pmorjan/kmod"
"go.uber.org/zap"
v1alpha1runtime "github.com/siderolabs/talos/internal/app/machined/pkg/runtime"
"github.com/siderolabs/talos/pkg/machinery/resources/runtime"
)
// KernelModuleSpecController watches KernelModuleSpecs, sets/resets kernel params.
type KernelModuleSpecController struct {
V1Alpha1Mode v1alpha1runtime.Mode
}
// Name implements controller.Controller interface.
func (ctrl *KernelModuleSpecController) Name() string {
return "runtime.KernelModuleSpecController"
}
// Inputs implements controller.Controller interface.
func (ctrl *KernelModuleSpecController) Inputs() []controller.Input {
return []controller.Input{
{
Namespace: runtime.NamespaceName,
Type: runtime.KernelModuleSpecType,
Kind: controller.InputStrong,
},
}
}
// Outputs implements controller.Controller interface.
func (ctrl *KernelModuleSpecController) Outputs() []controller.Output {
return nil
}
// Run implements controller.Controller interface.
func (ctrl *KernelModuleSpecController) Run(ctx context.Context, r controller.Runtime, logger *zap.Logger) error {
if ctrl.V1Alpha1Mode == v1alpha1runtime.ModeContainer {
// not supported in container mode
return nil
}
manager, err := kmod.New()
if err != nil {
return fmt.Errorf("error initializing kmod manager: %w", err)
}
for {
select {
case <-ctx.Done():
return nil
case <-r.EventCh():
}
modules, err := safe.ReaderListAll[*runtime.KernelModuleSpec](ctx, r)
if err != nil {
return err
}
var multiErr error
// note: this code doesn't support module unloading in any way for now
for iter := modules.Iterator(); iter.Next(); {
module := iter.Value().TypedSpec()
parameters := strings.Join(module.Parameters, " ")
if err = manager.Load(module.Name, parameters, 0); err != nil {
multiErr = errors.Join(multiErr, fmt.Errorf("error loading module %q: %w", module.Name, err))
}
}
if multiErr != nil {
return multiErr
}
r.ResetRestartBackoff()
}
}