-
Notifications
You must be signed in to change notification settings - Fork 458
/
kernel_param_defaults.go
151 lines (132 loc) · 3.73 KB
/
kernel_param_defaults.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
// 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"
"github.com/cosi-project/runtime/pkg/controller"
"github.com/cosi-project/runtime/pkg/resource"
"go.uber.org/zap"
v1alpha1runtime "github.com/siderolabs/talos/internal/app/machined/pkg/runtime"
"github.com/siderolabs/talos/pkg/kernel/kspp"
"github.com/siderolabs/talos/pkg/machinery/kernel"
"github.com/siderolabs/talos/pkg/machinery/resources/runtime"
)
// KernelParamDefaultsController creates default kernel params.
type KernelParamDefaultsController struct {
V1Alpha1Mode v1alpha1runtime.Mode
}
// Name implements controller.Controller interface.
func (ctrl *KernelParamDefaultsController) Name() string {
return "runtime.KernelParamDefaultsController"
}
// Inputs implements controller.Controller interface.
func (ctrl *KernelParamDefaultsController) Inputs() []controller.Input {
return nil
}
// Outputs implements controller.Controller interface.
func (ctrl *KernelParamDefaultsController) Outputs() []controller.Output {
return []controller.Output{
{
Type: runtime.KernelParamDefaultSpecType,
Kind: controller.OutputShared,
},
}
}
// Run implements controller.Controller interface.
func (ctrl *KernelParamDefaultsController) Run(ctx context.Context, r controller.Runtime, logger *zap.Logger) error {
select {
case <-ctx.Done():
return nil
case <-r.EventCh():
kernelParams := ctrl.getKernelParams()
if ctrl.V1Alpha1Mode != v1alpha1runtime.ModeContainer {
kernelParams = append(kernelParams, kspp.GetKernelParams()...)
}
for _, prop := range kernelParams {
value := prop.Value
item := runtime.NewKernelParamDefaultSpec(runtime.NamespaceName, prop.Key)
if err := r.Modify(ctx, item, func(res resource.Resource) error {
res.(*runtime.KernelParamDefaultSpec).TypedSpec().Value = value
return nil
}); err != nil {
return err
}
}
}
return nil
}
func (ctrl *KernelParamDefaultsController) getKernelParams() []*kernel.Param {
res := []*kernel.Param{
{
Key: "proc.sys.net.ipv4.ip_forward",
Value: "1",
},
}
if ctrl.V1Alpha1Mode != v1alpha1runtime.ModeContainer {
res = append(res, []*kernel.Param{
{
Key: "proc.sys.net.bridge.bridge-nf-call-iptables",
Value: "1",
},
{
Key: "proc.sys.net.bridge.bridge-nf-call-ip6tables",
Value: "1",
},
}...)
}
// Apply IPv6 defaults only if IPv6 is enabled.
// NB: we only prevent the application of these rules if the IPv6 node does not exist.
// Other errors should be ignored here so that they bubble up later, where errors can be logged and handled.
_, err := os.Stat("/proc/sys/net/ipv6/conf/default/accept_ra")
if err == nil || !errors.Is(err, os.ErrNotExist) {
res = append(res, []*kernel.Param{
{
Key: "proc.sys.net.ipv6.conf.default.forwarding",
Value: "1",
},
{
Key: "proc.sys.net.ipv6.conf.default.accept_ra",
Value: "2",
},
}...)
}
res = append(res, []*kernel.Param{
// ipvs/conntrack tcp keepalive refresh.
{
Key: "proc.sys.net.ipv4.tcp_keepalive_time",
Value: "600",
},
{
Key: "proc.sys.net.ipv4.tcp_keepalive_intvl",
Value: "60",
},
{
Key: "proc.sys.kernel.panic",
Value: "10",
},
{
Key: "proc.sys.kernel.pid_max",
Value: "262144",
},
{
Key: "proc.sys.vm.overcommit_memory",
Value: "1",
},
}...)
// kernel optimization for kubernetes workloads.
res = append(res, []*kernel.Param{
// configs inotify.
{
Key: "proc.sys.fs.inotify.max_user_instances",
Value: "8192",
},
{
Key: "proc.sys.fs.aio-max-nr",
Value: "1048576",
},
}...)
return res
}