@@ -18,253 +18,13 @@ package etcd
18
18
19
19
import (
20
20
"fmt"
21
- "io/ioutil"
22
- "os"
23
- "path/filepath"
24
21
"strconv"
25
22
"testing"
26
23
27
24
kubeadmapi "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm"
28
25
"k8s.io/kubernetes/cmd/kubeadm/app/constants"
29
- testutil "k8s.io/kubernetes/cmd/kubeadm/test"
30
26
)
31
27
32
- const (
33
- secureEtcdPod = `# generated by kubeadm v1.10.0
34
- apiVersion: v1
35
- kind: Pod
36
- metadata:
37
- annotations:
38
- scheduler.alpha.kubernetes.io/critical-pod: ""
39
- creationTimestamp: null
40
- labels:
41
- component: etcd
42
- tier: control-plane
43
- name: etcd
44
- namespace: kube-system
45
- spec:
46
- containers:
47
- - command:
48
- - etcd
49
- - --advertise-client-urls=https://127.0.0.1:2379
50
- - --data-dir=/var/lib/etcd
51
- - --peer-key-file=/etc/kubernetes/pki/etcd/peer.key
52
- - --peer-trusted-ca-file=/etc/kubernetes/pki/etcd/ca.crt
53
- - --listen-client-urls=https://127.0.0.1:2379
54
- - --peer-client-cert-auth=true
55
- - --cert-file=/etc/kubernetes/pki/etcd/server.crt
56
- - --key-file=/etc/kubernetes/pki/etcd/server.key
57
- - --trusted-ca-file=/etc/kubernetes/pki/etcd/ca.crt
58
- - --peer-cert-file=/etc/kubernetes/pki/etcd/peer.crt
59
- - --client-cert-auth=true
60
- image: k8s.gcr.io/etcd-amd64:3.1.12
61
- livenessProbe:
62
- exec:
63
- command:
64
- - /bin/sh
65
- - -ec
66
- - ETCDCTL_API=3 etcdctl --endpoints=127.0.0.1:2379 --cacert=/etc/kubernetes/pki/etcd/ca.crt
67
- --cert=/etc/kubernetes/pki/etcd/healthcheck-client.crt --key=/etc/kubernetes/pki/etcd/healthcheck-client.key
68
- get foo
69
- failureThreshold: 8
70
- initialDelaySeconds: 15
71
- timeoutSeconds: 15
72
- name: etcd
73
- resources: {}
74
- volumeMounts:
75
- - mountPath: /var/lib/etcd
76
- name: etcd-data
77
- - mountPath: /etc/kubernetes/pki/etcd
78
- name: etcd-certs
79
- hostNetwork: true
80
- volumes:
81
- - hostPath:
82
- path: /var/lib/etcd
83
- type: DirectoryOrCreate
84
- name: etcd-data
85
- - hostPath:
86
- path: /etc/kubernetes/pki/etcd
87
- type: DirectoryOrCreate
88
- name: etcd-certs
89
- status: {}
90
- `
91
- secureExposedEtcdPod = `
92
- apiVersion: v1
93
- kind: Pod
94
- metadata:
95
- annotations:
96
- scheduler.alpha.kubernetes.io/critical-pod: ""
97
- creationTimestamp: null
98
- labels:
99
- component: etcd
100
- tier: control-plane
101
- name: etcd
102
- namespace: kube-system
103
- spec:
104
- containers:
105
- - command:
106
- - etcd
107
- - --advertise-client-urls=https://10.0.5.5:2379
108
- - --data-dir=/var/lib/etcd
109
- - --peer-key-file=/etc/kubernetes/pki/etcd/peer.key
110
- - --peer-trusted-ca-file=/etc/kubernetes/pki/etcd/ca.crt
111
- - --listen-client-urls=https://[::0:0]:2379
112
- - --peer-client-cert-auth=true
113
- - --cert-file=/etc/kubernetes/pki/etcd/server.crt
114
- - --key-file=/etc/kubernetes/pki/etcd/server.key
115
- - --trusted-ca-file=/etc/kubernetes/pki/etcd/ca.crt
116
- - --peer-cert-file=/etc/kubernetes/pki/etcd/peer.crt
117
- - --client-cert-auth=true
118
- image: k8s.gcr.io/etcd-amd64:3.1.12
119
- livenessProbe:
120
- exec:
121
- command:
122
- - /bin/sh
123
- - -ec
124
- - ETCDCTL_API=3 etcdctl --endpoints=https://[::1]:2379 --cacert=/etc/kubernetes/pki/etcd/ca.crt
125
- --cert=/etc/kubernetes/pki/etcd/healthcheck-client.crt --key=/etc/kubernetes/pki/etcd/healthcheck-client.key
126
- get foo
127
- failureThreshold: 8
128
- initialDelaySeconds: 15
129
- timeoutSeconds: 15
130
- name: etcd
131
- resources: {}
132
- volumeMounts:
133
- - mountPath: /var/lib/etcd
134
- name: etcd-data
135
- - mountPath: /etc/kubernetes/pki/etcd
136
- name: etcd-certs
137
- hostNetwork: true
138
- volumes:
139
- - hostPath:
140
- path: /var/lib/etcd
141
- type: DirectoryOrCreate
142
- name: etcd-data
143
- - hostPath:
144
- path: /etc/kubernetes/pki/etcd
145
- type: DirectoryOrCreate
146
- name: etcd-certs
147
- status: {}
148
- `
149
- insecureEtcdPod = `# generated by kubeadm v1.9.6
150
- apiVersion: v1
151
- kind: Pod
152
- metadata:
153
- annotations:
154
- scheduler.alpha.kubernetes.io/critical-pod: ""
155
- creationTimestamp: null
156
- labels:
157
- component: etcd
158
- tier: control-plane
159
- name: etcd
160
- namespace: kube-system
161
- spec:
162
- containers:
163
- - command:
164
- - etcd
165
- - --listen-client-urls=http://127.0.0.1:2379
166
- - --advertise-client-urls=http://127.0.0.1:2379
167
- - --data-dir=/var/lib/etcd
168
- image: gcr.io/google_containers/etcd-amd64:3.1.11
169
- livenessProbe:
170
- failureThreshold: 8
171
- httpGet:
172
- host: 127.0.0.1
173
- path: /health
174
- port: 2379
175
- scheme: HTTP
176
- initialDelaySeconds: 15
177
- timeoutSeconds: 15
178
- name: etcd
179
- resources: {}
180
- volumeMounts:
181
- - mountPath: /var/lib/etcd
182
- name: etcd
183
- hostNetwork: true
184
- volumes:
185
- - hostPath:
186
- path: /var/lib/etcd
187
- type: DirectoryOrCreate
188
- name: etcd
189
- status: {}
190
- `
191
- invalidPod = `---{ broken yaml @@@`
192
- )
193
-
194
- func TestPodManifestHasTLS (t * testing.T ) {
195
- tests := []struct {
196
- description string
197
- podYaml string
198
- hasTLS bool
199
- expectErr bool
200
- writeManifest bool
201
- }{
202
- {
203
- description : "secure etcd returns true" ,
204
- podYaml : secureEtcdPod ,
205
- hasTLS : true ,
206
- writeManifest : true ,
207
- expectErr : false ,
208
- },
209
- {
210
- description : "secure exposed etcd returns true" ,
211
- podYaml : secureExposedEtcdPod ,
212
- hasTLS : true ,
213
- writeManifest : true ,
214
- expectErr : false ,
215
- },
216
- {
217
- description : "insecure etcd returns false" ,
218
- podYaml : insecureEtcdPod ,
219
- hasTLS : false ,
220
- writeManifest : true ,
221
- expectErr : false ,
222
- },
223
- {
224
- description : "invalid pod fails to unmarshal" ,
225
- podYaml : invalidPod ,
226
- hasTLS : false ,
227
- writeManifest : true ,
228
- expectErr : true ,
229
- },
230
- {
231
- description : "non-existent file returns error" ,
232
- podYaml : `` ,
233
- hasTLS : false ,
234
- writeManifest : false ,
235
- expectErr : true ,
236
- },
237
- }
238
-
239
- for _ , rt := range tests {
240
- tmpdir := testutil .SetupTempDir (t )
241
- defer os .RemoveAll (tmpdir )
242
-
243
- manifestPath := filepath .Join (tmpdir , "etcd.yaml" )
244
- if rt .writeManifest {
245
- err := ioutil .WriteFile (manifestPath , []byte (rt .podYaml ), 0644 )
246
- if err != nil {
247
- t .Fatalf ("Failed to write pod manifest\n %s\n \t fatal error: %v" , rt .description , err )
248
- }
249
- }
250
-
251
- hasTLS , actualErr := PodManifestsHaveTLS (tmpdir )
252
- if (actualErr != nil ) != rt .expectErr {
253
- t .Errorf (
254
- "PodManifestHasTLS failed\n %s\n \t expected error: %t\n \t got: %t\n \t actual error: %v" ,
255
- rt .description ,
256
- rt .expectErr ,
257
- (actualErr != nil ),
258
- actualErr ,
259
- )
260
- }
261
-
262
- if hasTLS != rt .hasTLS {
263
- t .Errorf ("PodManifestHasTLS failed\n %s\n \t expected hasTLS: %t\n \t got: %t" , rt .description , rt .hasTLS , hasTLS )
264
- }
265
- }
266
- }
267
-
268
28
func TestCheckConfigurationIsHA (t * testing.T ) {
269
29
var tests = []struct {
270
30
name string
0 commit comments