-
Notifications
You must be signed in to change notification settings - Fork 458
/
kernel_param_spec.go
206 lines (163 loc) · 5.14 KB
/
kernel_param_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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
// 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"
"os"
"strings"
"github.com/cosi-project/runtime/pkg/controller"
"github.com/cosi-project/runtime/pkg/resource"
"github.com/hashicorp/go-multierror"
"go.uber.org/zap"
krnl "github.com/siderolabs/talos/pkg/kernel"
"github.com/siderolabs/talos/pkg/kernel/kspp"
"github.com/siderolabs/talos/pkg/machinery/kernel"
"github.com/siderolabs/talos/pkg/machinery/resources/runtime"
)
// KernelParamSpecController watches KernelParamSpecs, sets/resets kernel params.
type KernelParamSpecController struct {
defaults map[string]string
state map[string]string
}
// Name implements controller.Controller interface.
func (ctrl *KernelParamSpecController) Name() string {
return "runtime.KernelParamSpecController"
}
// Inputs implements controller.Controller interface.
func (ctrl *KernelParamSpecController) Inputs() []controller.Input {
return []controller.Input{
{
Namespace: runtime.NamespaceName,
Type: runtime.KernelParamDefaultSpecType,
Kind: controller.InputStrong,
},
{
Namespace: runtime.NamespaceName,
Type: runtime.KernelParamSpecType,
Kind: controller.InputStrong,
},
}
}
// Outputs implements controller.Controller interface.
func (ctrl *KernelParamSpecController) Outputs() []controller.Output {
return []controller.Output{
{
Type: runtime.KernelParamStatusType,
Kind: controller.OutputExclusive,
},
}
}
// Run implements controller.Controller interface.
//
//nolint:gocyclo,cyclop
func (ctrl *KernelParamSpecController) Run(ctx context.Context, r controller.Runtime, logger *zap.Logger) error {
if ctrl.state == nil {
ctrl.state = map[string]string{}
}
if ctrl.defaults == nil {
ctrl.defaults = map[string]string{}
}
for {
select {
case <-ctx.Done():
return nil
case <-r.EventCh():
ksppParams := map[string]struct{}{}
for _, param := range kspp.GetKernelParams() {
ksppParams[param.Key] = struct{}{}
}
defaults, err := r.List(ctx, resource.NewMetadata(runtime.NamespaceName, runtime.KernelParamDefaultSpecType, "", resource.VersionUndefined))
if err != nil {
return err
}
configs, err := r.List(ctx, resource.NewMetadata(runtime.NamespaceName, runtime.KernelParamSpecType, "", resource.VersionUndefined))
if err != nil {
return err
}
configsCounts := len(configs.Items)
list := configs.Items
list = append(list, defaults.Items...)
touchedIDs := map[string]string{}
var errs *multierror.Error
for i, item := range list {
spec := item.(runtime.KernelParam).TypedSpec()
id := item.Metadata().ID()
if value, duplicate := touchedIDs[id]; i >= configsCounts && duplicate {
if _, ok := ksppParams[id]; ok {
logger.Warn("overriding KSPP enforced parameter, this is not recommended", zap.String("key", id), zap.String("value", value))
}
continue
}
if err = ctrl.updateKernelParam(ctx, r, id, spec.Value); err != nil {
if errors.Is(err, os.ErrNotExist) && spec.IgnoreErrors {
status := runtime.NewKernelParamStatus(runtime.NamespaceName, id)
if e := r.Modify(ctx, status, func(res resource.Resource) error {
res.(*runtime.KernelParamStatus).TypedSpec().Unsupported = true
return nil
}); e != nil {
errs = multierror.Append(errs, e)
}
} else {
errs = multierror.Append(errs, err)
}
continue
}
touchedIDs[id] = spec.Value
}
for key := range ctrl.state {
if _, ok := touchedIDs[key]; ok {
continue
}
if err = ctrl.resetKernelParam(ctx, r, key); err != nil {
errs = multierror.Append(errs, err)
}
}
if errs != nil {
return errs
}
}
r.ResetRestartBackoff()
}
}
func (ctrl *KernelParamSpecController) updateKernelParam(ctx context.Context, r controller.Runtime, key, value string) error {
prop := &kernel.Param{
Key: key,
Value: value,
}
if _, ok := ctrl.defaults[key]; !ok {
if data, err := krnl.ReadParam(prop); err == nil {
ctrl.defaults[key] = string(data)
} else if !errors.Is(err, os.ErrNotExist) {
return err
}
}
if err := krnl.WriteParam(prop); err != nil {
return err
}
ctrl.state[key] = value
status := runtime.NewKernelParamStatus(runtime.NamespaceName, key)
return r.Modify(ctx, status, func(res resource.Resource) error {
res.(*runtime.KernelParamStatus).TypedSpec().Current = value
res.(*runtime.KernelParamStatus).TypedSpec().Default = strings.TrimSpace(ctrl.defaults[key])
return nil
})
}
func (ctrl *KernelParamSpecController) resetKernelParam(ctx context.Context, r controller.Runtime, key string) error {
var err error
if def, ok := ctrl.defaults[key]; ok {
err = krnl.WriteParam(&kernel.Param{
Key: key,
Value: def,
})
} else {
err = krnl.DeleteParam(&kernel.Param{Key: key})
}
if err != nil {
return err
}
delete(ctrl.defaults, key)
delete(ctrl.state, key)
return r.Destroy(ctx, resource.NewMetadata(runtime.NamespaceName, runtime.KernelParamStatusType, key, resource.VersionUndefined))
}