forked from kubermatic/machine-controller
/
helper.go
216 lines (186 loc) · 6.68 KB
/
helper.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
207
208
209
210
211
212
213
214
215
216
/*
Copyright 2019 The Machine Controller Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package helper
import (
"encoding/json"
"fmt"
"strings"
"github.com/magnusr/kubermatic-machine-controller/pkg/cloudprovider/util"
"k8s.io/client-go/tools/clientcmd"
clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
)
const (
DefaultDockerContainerLogMaxFiles = "5"
DefaultDockerContainerLogMaxSize = "100m"
)
func GetCACert(kubeconfig *clientcmdapi.Config) (string, error) {
if len(kubeconfig.Clusters) != 1 {
return "", fmt.Errorf("kubeconfig does not contain exactly one cluster, can not extract server address")
}
// Clusters is a map so we have to use range here.
for _, clusterConfig := range kubeconfig.Clusters {
return string(clusterConfig.CertificateAuthorityData), nil
}
return "", fmt.Errorf("no CACert found")
}
// StringifyKubeconfig marshals a kubeconfig to its text form.
func StringifyKubeconfig(kubeconfig *clientcmdapi.Config) (string, error) {
kubeconfigBytes, err := clientcmd.Write(*kubeconfig)
if err != nil {
return "", fmt.Errorf("error writing kubeconfig: %w", err)
}
return string(kubeconfigBytes), nil
}
// LoadKernelModules returns a script which is responsible for loading all required kernel modules
// The nf_conntrack_ipv4 module get removed in newer kernel versions.
func LoadKernelModulesScript() string {
return `#!/usr/bin/env bash
set -euo pipefail
modprobe ip_vs
modprobe ip_vs_rr
modprobe ip_vs_wrr
modprobe ip_vs_sh
if modinfo nf_conntrack_ipv4 &> /dev/null; then
modprobe nf_conntrack_ipv4
else
modprobe nf_conntrack
fi
`
}
// KernelSettings returns the list of kernel settings required for a kubernetes worker node
// inotify changes according to https://github.com/kubernetes/kubernetes/issues/10421 - better than letting the kubelet die.
func KernelSettings() string {
return `net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
kernel.panic_on_oops = 1
kernel.panic = 10
net.ipv4.ip_forward = 1
vm.overcommit_memory = 1
fs.inotify.max_user_watches = 1048576
fs.inotify.max_user_instances = 8192
`
}
// JournalDConfig returns the journal config preferable on every node.
func JournalDConfig() string {
// JournaldMaxUse defines the maximum space that journalD logs can occupy.
// https://www.freedesktop.org/software/systemd/man/journald.conf.html#SystemMaxUse=
return `[Journal]
SystemMaxUse=5G
`
}
type dockerConfig struct {
ExecOpts []string `json:"exec-opts,omitempty"`
StorageDriver string `json:"storage-driver,omitempty"`
StorageOpts []string `json:"storage-opts,omitempty"`
LogDriver string `json:"log-driver,omitempty"`
LogOpts map[string]string `json:"log-opts,omitempty"`
InsecureRegistries []string `json:"insecure-registries,omitempty"`
RegistryMirrors []string `json:"registry-mirrors,omitempty"`
}
// DockerConfig returns the docker daemon.json.
func DockerConfig(insecureRegistries, registryMirrors []string, logMaxFiles string, logMaxSize string) (string, error) {
if len(logMaxSize) > 0 {
// Parse log max size to ensure that it has the correct units
logMaxSize = strings.ToLower(logMaxSize)
logMaxSize = strings.ReplaceAll(logMaxSize, "ki", "k")
logMaxSize = strings.ReplaceAll(logMaxSize, "mi", "m")
logMaxSize = strings.ReplaceAll(logMaxSize, "gi", "g")
} else {
logMaxSize = DefaultDockerContainerLogMaxSize
}
// Default if value is not provided
if len(logMaxFiles) == 0 {
logMaxFiles = DefaultDockerContainerLogMaxFiles
}
cfg := dockerConfig{
ExecOpts: []string{"native.cgroupdriver=systemd"},
StorageDriver: "overlay2",
LogDriver: "json-file",
LogOpts: map[string]string{
"max-size": logMaxSize,
"max-file": logMaxFiles,
},
InsecureRegistries: insecureRegistries,
RegistryMirrors: registryMirrors,
}
b, err := json.Marshal(cfg)
return string(b), err
}
func ProxyEnvironment(proxy, noProxy string) string {
return fmt.Sprintf(`HTTP_PROXY=%s
http_proxy=%s
HTTPS_PROXY=%s
https_proxy=%s
NO_PROXY=%s
no_proxy=%s`, proxy, proxy, proxy, proxy, noProxy, noProxy)
}
func SetupNodeIPEnvScript(ipFamily util.IPFamily) string {
const defaultIfcIPv4 = `DEFAULT_IFC_IP=$(ip -o route get 1 | grep -oP "src \K\S+")`
var defaultIfcIP string
switch ipFamily {
case util.IPFamilyIPv4:
defaultIfcIP = defaultIfcIPv4
case util.IPFamilyIPv6:
defaultIfcIP = `DEFAULT_IFC_IP=$(ip -o -6 route get 1:: | grep -oP "src \K\S+")`
case util.IPFamilyIPv4IPv6:
defaultIfcIP = `DEFAULT_IFC_IP=$(ip -o route get 1 | grep -oP "src \K\S+")
DEFAULT_IFC_IP6=$(ip -o -6 route get 1:: | grep -oP "src \K\S+")
if [ -z "${DEFAULT_IFC_IP6}" ]
then
echodate "Failed to get IPv6 address for the default route interface"
exit 1
fi
DEFAULT_IFC_IP=$DEFAULT_IFC_IP,$DEFAULT_IFC_IP6`
case util.IPFamilyIPv6IPv4:
defaultIfcIP = `DEFAULT_IFC_IP=$(ip -o route get 1 | grep -oP "src \K\S+")
DEFAULT_IFC_IP6=$(ip -o -6 route get 1:: | grep -oP "src \K\S+")
if [ -z "${DEFAULT_IFC_IP6}" ]
then
echodate "Failed to get IPv6 address for the default route interface"
exit 1
fi
DEFAULT_IFC_IP=$DEFAULT_IFC_IP6,$DEFAULT_IFC_IP`
default:
defaultIfcIP = defaultIfcIPv4
}
return `#!/usr/bin/env bash
echodate() {
echo "[$(date -Is)]" "$@"
}
# get the default interface IP address
` + defaultIfcIP + `
# get the full hostname
FULL_HOSTNAME=$(hostname -f)
if [ -z "${DEFAULT_IFC_IP}" ]
then
echodate "Failed to get IP address for the default route interface"
exit 1
fi
# write the nodeip_env file
# we need the line below because flatcar has the same string "coreos" in that file
if grep -q coreos /etc/os-release
then
echo -e "KUBELET_NODE_IP=${DEFAULT_IFC_IP}\nKUBELET_HOSTNAME=${FULL_HOSTNAME}" > /etc/kubernetes/nodeip.conf
elif [ ! -d /etc/systemd/system/kubelet.service.d ]
then
echodate "Can't find kubelet service extras directory"
exit 1
else
echo -e "[Service]\nEnvironment=\"KUBELET_NODE_IP=${DEFAULT_IFC_IP}\"\nEnvironment=\"KUBELET_HOSTNAME=${FULL_HOSTNAME}\"" > /etc/systemd/system/kubelet.service.d/nodeip.conf
fi
`
}
func SSHConfigAddendum() string {
return `TrustedUserCAKeys /etc/ssh/trusted-user-ca-keys.pem
CASignatureAlgorithms ecdsa-sha2-nistp256,ecdsa-sha2-nistp384,ecdsa-sha2-nistp521,ssh-ed25519,rsa-sha2-512,rsa-sha2-256,ssh-rsa`
}