-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathkubeadm.go
214 lines (183 loc) · 7.04 KB
/
kubeadm.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
// Copyright 2021 Kinvolk GmbH
//
// 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 kubeadm
import (
"bytes"
"encoding/base64"
"fmt"
"strings"
"text/template"
"time"
"github.com/coreos/pkg/capnslog"
"github.com/coreos/mantle/kola"
"github.com/coreos/mantle/kola/cluster"
"github.com/coreos/mantle/kola/register"
"github.com/coreos/mantle/kola/tests/etcd"
"github.com/coreos/mantle/platform"
"github.com/coreos/mantle/platform/conf"
"github.com/coreos/mantle/util"
)
var (
// params are used to render script templates
// Release is the kubernetes release version we want to use
// ReleaseVersion is the version of the kubelet service and kubeadm dropin
// TODO: when a new version of kubernetes will be tested, it would be nice
// to have a map[string]Release with Release struct holding the parameter below
params = map[string]interface{}{
"CNIVersion": "v0.8.2",
"CRIctlVersion": "v1.17.0",
"ReleaseVersion": "v0.4.0",
"Release": "v1.21.0",
"DownloadDir": "/opt/bin",
"PodSubnet": "192.168.0.0/17",
"KubeadmSum": "0673408403a3474c868ae86109f11f9114bca7ddce204be0d169316fb3ce0edefa4b2a472ba9b8308e423e6b927d4098ac36296405570f444f39551fb1c4bbb4",
"KubeletSum": "530689c0cc32ef1830f7ae26ac10995f815043d48a905141e23a34a5e61522c4ee2ff46953648c47c5592d7c2ffa40ce90469a697f36f68475b8da5abd73f9f5",
"CRIctlSum": "e258f4607a89b8d44c700036e636dd42cc3e2ed27a3bb13beef736f80f64f10b7974c01259a66131d3f7b44ed0c61b1ca0ea91597c416a9c095c432de5112d44",
"CNISum": "a8fd7ffce7f4f1fb00ac2e4840b7961efc3ec844c5e99035c72baa95f038b316552ce71f8a17f82c04d614da490689953a7a80008d7b9be563187673813934b5",
"KubectlSum": "9557d298146ef62ffbcf05b3591bf1ce74f345628370447a4f614b5f64e367b5bfa8e397cc4755da9ea38f1ba04c95c65c313e735550ffc3b03c197e936c3e11",
}
plog = capnslog.NewPackageLogger("github.com/coreos/mantle", "kola/tests/kubeadm")
etcdConfig = conf.ContainerLinuxConfig(`
etcd:
advertise_client_urls: http://{PRIVATE_IPV4}:2379
listen_client_urls: http://0.0.0.0:2379
systemd:
units:
- name: etcd-member.service
enabled: true
`)
)
func init() {
register.Register(®ister.Test{
Name: "kubeadm.base",
Distros: []string{"cl"},
ExcludePlatforms: []string{"esx", "azure"},
Run: kubeadmBaseTest,
})
}
// kubeadmBaseTest asserts that the cluster is up and running
func kubeadmBaseTest(c cluster.TestCluster) {
board := kola.QEMUOptions.Board
params["Arch"] = strings.SplitN(board, "-", 2)[0]
kubectl, err := setup(c)
if err != nil {
c.Fatalf("unable to setup cluster: %v", err)
}
c.Run("node readiness", func(c cluster.TestCluster) {
// we let some times to the cluster to be fully booted
if err := util.Retry(10, 10*time.Second, func() error {
// notice the extra space before "Ready", it's to not catch
// "NotReady" nodes
out := c.MustSSH(kubectl, "/opt/bin/kubectl get nodes | grep \" Ready\"| wc -l")
readyNodesCnt := string(out)
if readyNodesCnt != "2" {
return fmt.Errorf("ready nodes should be equal to 2: %s", readyNodesCnt)
}
return nil
}); err != nil {
c.Fatalf("nodes are not ready: %v", err)
}
})
c.Run("nginx deployment", func(c cluster.TestCluster) {
// nginx manifest has been deployed through ignition
if _, err := c.SSH(kubectl, "/opt/bin/kubectl apply -f nginx.yaml"); err != nil {
c.Fatalf("unable to deploy nginx: %v", err)
}
if err := util.Retry(10, 10*time.Second, func() error {
out := c.MustSSH(kubectl, "/opt/bin/kubectl get deployments -o json | jq '.items | .[] | .status.readyReplicas'")
readyCnt := string(out)
if readyCnt != "1" {
return fmt.Errorf("ready replicas should be equal to 1: %s", readyCnt)
}
return nil
}); err != nil {
c.Fatalf("nginx is not deployed: %v", err)
}
})
}
// render takes care of template rendering
// using `b` parameter, we can render in a base64 encoded format
func render(s string, p map[string]interface{}, b bool) (*bytes.Buffer, error) {
tmpl, err := template.New("install").Parse(s)
if err != nil {
return nil, fmt.Errorf("unable to parse script: %w", err)
}
var buf bytes.Buffer
if err := tmpl.Execute(&buf, p); err != nil {
return nil, fmt.Errorf("unable to execute template: %w", err)
}
if b {
b64 := base64.StdEncoding.EncodeToString(buf.Bytes())
buf.Reset()
if _, err := buf.WriteString(b64); err != nil {
return nil, fmt.Errorf("unable to write bas64 content to buffer: %w", err)
}
}
return &buf, nil
}
// setup creates a cluster with kubeadm
// cluster is composed by etcd node, worker and master node
// it returns master node in order to have direct access on node
// with kubectl installed and setup
func setup(c cluster.TestCluster) (platform.Machine, error) {
plog.Infof("creating etcd node")
etcdNode, err := c.NewMachine(etcdConfig)
if err != nil {
return nil, fmt.Errorf("unable to create etcd node: %w", err)
}
if err := etcd.GetClusterHealth(c, etcdNode, 1); err != nil {
return nil, fmt.Errorf("unable to get etcd node health: %w", err)
}
params["Endpoints"] = []string{fmt.Sprintf("http://%s:2379", etcdNode.PrivateIP())}
plog.Infof("creating master node")
mScript, err := render(masterScript, params, true)
if err != nil {
return nil, fmt.Errorf("unable to render master script: %w", err)
}
params["MasterScript"] = mScript.String()
masterCfg, err := render(masterConfig, params, false)
if err != nil {
return nil, fmt.Errorf("unable to render container linux config for master: %w", err)
}
master, err := c.NewMachine(conf.ContainerLinuxConfig(masterCfg.String()))
if err != nil {
return nil, fmt.Errorf("unable to create master node: %w", err)
}
out, err := c.SSH(master, "sudo /home/core/install.sh")
if err != nil {
return nil, fmt.Errorf("unable to run master script: %w", err)
}
// "out" holds the worker config generated
// by the master script install
params["WorkerConfig"] = string(out)
plog.Infof("creating worker node")
wScript, err := render(workerScript, params, true)
if err != nil {
return nil, fmt.Errorf("unable to render worker script: %w", err)
}
params["WorkerScript"] = wScript.String()
workerCfg, err := render(workerConfig, params, false)
if err != nil {
return nil, fmt.Errorf("unable to render container linux config for master: %w", err)
}
worker, err := c.NewMachine(conf.ContainerLinuxConfig(workerCfg.String()))
if err != nil {
return nil, fmt.Errorf("unable to create worker node: %w", err)
}
out, err = c.SSH(worker, "sudo /home/core/install.sh")
if err != nil {
return nil, fmt.Errorf("unable to run worker script: %w", err)
}
return master, nil
}