This repository has been archived by the owner on Apr 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
software.go
208 lines (185 loc) · 5.43 KB
/
software.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
package kubernetes
import (
"errors"
"strconv"
"strings"
"github.com/caos/orbos/internal/operator/nodeagent/dep/sysctl"
"github.com/caos/orbos/internal/operator/common"
"github.com/caos/orbos/mntr"
)
type KubernetesVersion int
const (
Unknown KubernetesVersion = iota
V1x15x0
V1x15x1
V1x15x2
V1x15x3
V1x15x4
V1x15x5
V1x15x6
V1x15x7
V1x15x8
V1x15x9
V1x15x10
V1x15x11
V1x15x12
V1x16x0
V1x16x1
V1x16x2
V1x16x3
V1x16x4
V1x16x5
V1x16x6
V1x16x7
V1x16x8
V1x16x9
V1x16x10
V1x16x11
V1x16x12
V1x16x13
V1x16x14
V1x17x0
V1x17x1
V1x17x2
V1x17x3
V1x17x4
V1x17x5
V1x17x6
V1x17x7
V1x17x8
V1x17x9
V1x17x10
V1x17x11
V1x18x0
V1x18x1
V1x18x2
V1x18x3
V1x18x4
V1x18x5
V1x18x6
V1x18x7
V1x18x8
)
var kubernetesVersions = []string{
"unknown",
"v1.15.0", "v1.15.1", "v1.15.2", "v1.15.3", "v1.15.4", "v1.15.5", "v1.15.6", "v1.15.7", "v1.15.8", "v1.15.9", "v1.15.10", "v1.15.11", "v1.15.12",
"v1.16.0", "v1.16.1", "v1.16.2", "v1.16.3", "v1.16.4", "v1.16.5", "v1.16.6", "v1.16.7", "v1.16.8", "v1.16.9", "v1.16.10", "v1.16.11", "v1.16.12", "v1.16.13", "v1.16.14",
"v1.17.0", "v1.17.1", "v1.17.2", "v1.17.3", "v1.17.4", "v1.17.5", "v1.17.6", "v1.17.7", "v1.17.8", "v1.17.9", "v1.17.10", "v1.17.11",
"v1.18.0", "v1.18.1", "v1.18.2", "v1.18.3", "v1.18.4", "v1.18.5", "v1.18.6", "v1.18.7", "v1.18.8"}
func (k KubernetesVersion) String() string {
return kubernetesVersions[k]
}
func (k KubernetesVersion) DefineSoftware() common.Software {
dockerVersion := "docker-ce v19.03.5"
// if minor, err := k.ExtractMinor(); err != nil && minor <= 15 {
// dockerVersion = "docker-ce v18.09.6"
// }
sysctlPkg := common.Package{}
sysctl.Enable(&sysctlPkg, sysctl.IpForward)
sysctl.Enable(&sysctlPkg, sysctl.BridgeNfCallIptables)
sysctl.Enable(&sysctlPkg, sysctl.BridgeNfCallIp6tables)
return common.Software{
Swap: common.Package{Version: "disabled"},
Containerruntime: common.Package{Version: dockerVersion, Config: map[string]string{
"daemon.json": `{
"exec-opts": ["native.cgroupdriver=systemd"],
"log-driver": "json-file",
"log-opts": {
"max-size": "100m"
},
"storage-driver": "overlay2"
}`,
}},
Kubelet: common.Package{Version: k.String()},
Kubeadm: common.Package{Version: k.String()},
Kubectl: common.Package{Version: k.String()},
Sysctl: sysctlPkg,
}
}
func KubernetesSoftware(current common.Software) common.Software {
return common.Software{
Swap: current.Swap,
Containerruntime: current.Containerruntime,
Kubelet: current.Kubelet,
Kubeadm: current.Kubeadm,
Kubectl: current.Kubectl,
Sysctl: current.Sysctl,
}
}
func ParseString(version string) KubernetesVersion {
for idx, k8sVersion := range kubernetesVersions {
if k8sVersion == version {
return KubernetesVersion(idx)
}
}
return KubernetesVersion(0)
}
func (k KubernetesVersion) equals(other KubernetesVersion) bool {
return string(k) == string(other)
}
func (k KubernetesVersion) NextHighestMinor() KubernetesVersion {
switch k {
case V1x15x0, V1x15x1, V1x15x2, V1x15x3, V1x15x4, V1x15x5, V1x15x6, V1x15x7, V1x15x8, V1x15x9, V1x15x10, V1x15x11, V1x15x12:
return V1x16x14
case V1x16x0, V1x16x1, V1x16x2, V1x16x3, V1x16x4, V1x16x5, V1x16x6, V1x16x7, V1x16x8, V1x16x9, V1x16x10, V1x16x11, V1x16x12, V1x16x13, V1x16x14:
return V1x17x11
case V1x17x0, V1x17x1, V1x17x2, V1x17x3, V1x17x4, V1x17x5, V1x17x6, V1x17x7, V1x17x8, V1x17x9, V1x17x10, V1x17x11:
return V1x18x8
default:
return Unknown
}
}
func (k KubernetesVersion) ExtractMinor(monitor mntr.Monitor) (int, error) {
return k.extractNumber(monitor, 1)
}
func (k KubernetesVersion) ExtractPatch(monitor mntr.Monitor) (int, error) {
return k.extractNumber(monitor, 2)
}
func (k KubernetesVersion) extractNumber(monitor mntr.Monitor, position int) (int, error) {
if k == Unknown {
return 0, errors.New("Unknown kubernetes version")
}
parts := strings.Split(k.String(), ".")
version, err := strconv.ParseInt(parts[position], 10, 8)
if err != nil {
return 0, err
}
monitor.WithFields(map[string]interface{}{
"number": version,
"position": position,
"string": k,
}).Debug("Extracted from semantic version")
return int(version), nil
}
func softwareContains(this common.Software, that common.Software) bool {
return contains(this.Swap, that.Swap) &&
contains(this.Kubelet, that.Kubelet) &&
contains(this.Kubeadm, that.Kubeadm) &&
contains(this.Kubectl, that.Kubectl) &&
contains(this.Containerruntime, that.Containerruntime) &&
contains(this.KeepaliveD, that.KeepaliveD) &&
contains(this.Nginx, that.Nginx) &&
contains(this.Hostname, that.Hostname) &&
sysctl.Contains(this.Sysctl, that.Sysctl) &&
contains(this.Health, that.Health)
}
func contains(this, that common.Package) bool {
return that.Version == "" && that.Config == nil || common.PackageEquals(this, that)
}
func softwareDefines(this common.Software, that common.Software) bool {
return defines(this.Swap, that.Swap) &&
defines(this.Kubelet, that.Kubelet) &&
defines(this.Kubeadm, that.Kubeadm) &&
defines(this.Kubectl, that.Kubectl) &&
defines(this.Containerruntime, that.Containerruntime) &&
defines(this.KeepaliveD, that.KeepaliveD) &&
defines(this.Nginx, that.Nginx) &&
defines(this.Hostname, that.Hostname) &&
defines(this.Sysctl, that.Sysctl) &&
defines(this.Health, that.Health)
}
func defines(this, that common.Package) bool {
zeroPkg := common.Package{}
defines := common.PackageEquals(that, zeroPkg) || !common.PackageEquals(this, zeroPkg)
return defines
}