forked from kubernetes/test-infra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkops.go
476 lines (417 loc) · 13.9 KB
/
kops.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
/*
Copyright 2017 The Kubernetes 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 main
import (
"bytes"
"context"
"encoding/json"
"errors"
"flag"
"fmt"
"golang.org/x/crypto/ssh"
"io/ioutil"
"log"
"os"
"os/exec"
"os/user"
"path/filepath"
"strconv"
"strings"
"time"
)
var (
// kops specific flags.
kopsPath = flag.String("kops", "", "(kops only) Path to the kops binary. kops will be downloaded from kops-base-url if not set.")
kopsCluster = flag.String("kops-cluster", "", "(kops only) Deprecated. Cluster name for kops; if not set defaults to --cluster.")
kopsState = flag.String("kops-state", "", "(kops only) s3:// path to kops state store. Must be set.")
kopsSSHUser = flag.String("kops-ssh-user", os.Getenv("USER"), "(kops only) Username for SSH connections to nodes.)")
kopsSSHKey = flag.String("kops-ssh-key", "", "(kops only) Path to ssh key-pair for each node (defaults '~/.ssh/kube_aws_rsa' if unset.)")
kopsKubeVersion = flag.String("kops-kubernetes-version", "", "(kops only) If set, the version of Kubernetes to deploy (can be a URL to a GCS path where the release is stored) (Defaults to kops default, latest stable release.).")
kopsZones = flag.String("kops-zones", "us-west-2a", "(kops only) zones for kops deployment, comma delimited.")
kopsNodes = flag.Int("kops-nodes", 2, "(kops only) Number of nodes to create.")
kopsUpTimeout = flag.Duration("kops-up-timeout", 20*time.Minute, "(kops only) Time limit between 'kops config / kops update' and a response from the Kubernetes API.")
kopsAdminAccess = flag.String("kops-admin-access", "", "(kops only) If set, restrict apiserver access to this CIDR range.")
kopsImage = flag.String("kops-image", "", "(kops only) Image (AMI) for nodes to use. (Defaults to kops default, a Debian image with a custom kubernetes kernel.)")
kopsArgs = flag.String("kops-args", "", "(kops only) Additional space-separated args to pass unvalidated to 'kops create cluster', e.g. '--kops-args=\"--dns private --node-size t2.micro\"'")
kopsPriorityPath = flag.String("kops-priority-path", "", "Insert into PATH if set")
kopsBaseURL = flag.String("kops-base-url", "", "Base URL for a prebuilt version of kops")
kopsVersion = flag.String("kops-version", "", "URL to a file containing a valid kops-base-url")
)
type kops struct {
path string
kubeVersion string
zones []string
nodes int
adminAccess string
cluster string
image string
args string
kubecfg string
// sshUser is the username to use when SSHing to nodes (for example for log capture)
sshUser string
// sshPublicKey is the path to the SSH public key matching sshPrivateKey
sshPublicKey string
// sshPrivateKey is the path to the SSH private key matching sshPublicKey
sshPrivateKey string
// GCP project we should use
gcpProject string
// Cloud provider in use (gce, aws)
provider string
}
var _ deployer = kops{}
func migrateKopsEnv() error {
return migrateOptions([]migratedOption{
{
env: "KOPS_STATE_STORE",
option: kopsState,
name: "--kops-state",
skipPush: true,
},
{
env: "AWS_SSH_KEY",
option: kopsSSHKey,
name: "--kops-ssh-key",
skipPush: true,
},
{
env: "PRIORITY_PATH",
option: kopsPriorityPath,
name: "--kops-priority-path",
skipPush: true,
},
})
}
func newKops(provider, gcpProject, cluster string) (*kops, error) {
tmpdir, err := ioutil.TempDir("", "kops")
if err != nil {
return nil, err
}
if err := migrateKopsEnv(); err != nil {
return nil, err
}
if *kopsCluster != "" {
cluster = *kopsCluster
}
if cluster == "" {
return nil, fmt.Errorf("--cluster or --kops-cluster must be set to a valid cluster name for kops deployment")
}
if *kopsState == "" {
return nil, fmt.Errorf("--kops-state must be set to a valid S3 path for kops deployment")
}
if *kopsPriorityPath != "" {
if err := insertPath(*kopsPriorityPath); err != nil {
return nil, err
}
}
// TODO(fejta): consider explicitly passing these env items where needed.
sshKey := *kopsSSHKey
if sshKey == "" {
usr, err := user.Current()
if err != nil {
return nil, err
}
sshKey = filepath.Join(usr.HomeDir, ".ssh/kube_aws_rsa")
}
if err := os.Setenv("KOPS_STATE_STORE", *kopsState); err != nil {
return nil, err
}
// Repoint KUBECONFIG to an isolated kubeconfig in our temp directory
kubecfg := filepath.Join(tmpdir, "kubeconfig")
f, err := os.Create(kubecfg)
if err != nil {
return nil, err
}
defer f.Close()
if err := f.Chmod(0600); err != nil {
return nil, err
}
if err := os.Setenv("KUBECONFIG", kubecfg); err != nil {
return nil, err
}
// Set KUBERNETES_CONFORMANCE_TEST so the auth info is picked up
// from kubectl instead of bash inference.
if err := os.Setenv("KUBERNETES_CONFORMANCE_TEST", "yes"); err != nil {
return nil, err
}
// Set KUBERNETES_CONFORMANCE_PROVIDER to override the
// cloudprovider for KUBERNETES_CONFORMANCE_TEST.
if err := os.Setenv("KUBERNETES_CONFORMANCE_PROVIDER", "aws"); err != nil {
return nil, err
}
// AWS_SSH_KEY is required by the AWS e2e tests.
if err := os.Setenv("AWS_SSH_KEY", sshKey); err != nil {
return nil, err
}
// ZONE is required by the AWS e2e tests.
zones := strings.Split(*kopsZones, ",")
if err := os.Setenv("ZONE", zones[0]); err != nil {
return nil, err
}
// Set kops-base-url from kops-version
if *kopsVersion != "" {
if *kopsBaseURL != "" {
return nil, fmt.Errorf("cannot set --kops-version and --kops-base-url")
}
var b bytes.Buffer
if err := httpRead(*kopsVersion, &b); err != nil {
return nil, err
}
latest := strings.TrimSpace(b.String())
log.Printf("Got latest kops version from %v: %v", *kopsVersion, latest)
if latest == "" {
return nil, fmt.Errorf("version URL %v was empty", *kopsVersion)
}
*kopsBaseURL = latest
}
// kops looks at KOPS_BASE_URL env var, so export it here
if *kopsBaseURL != "" {
if err := os.Setenv("KOPS_BASE_URL", *kopsBaseURL); err != nil {
return nil, err
}
}
// Download kops from kopsBaseURL if kopsPath is not set
if *kopsPath == "" {
if *kopsBaseURL == "" {
return nil, fmt.Errorf("--kops or --kops-base-url must be set")
}
kopsBinURL := *kopsBaseURL + "/linux/amd64/kops"
log.Printf("Download kops binary from %s", kopsBinURL)
kopsBin := filepath.Join(tmpdir, "kops")
f, err := os.Create(kopsBin)
if err != nil {
return nil, fmt.Errorf("error creating file %q: %v", kopsBin, err)
}
defer f.Close()
if err := httpRead(kopsBinURL, f); err != nil {
return nil, err
}
if err := ensureExecutable(kopsBin); err != nil {
return nil, err
}
*kopsPath = kopsBin
}
return &kops{
path: *kopsPath,
kubeVersion: *kopsKubeVersion,
sshPrivateKey: sshKey,
sshPublicKey: sshKey + ".pub",
sshUser: *kopsSSHUser,
zones: zones,
nodes: *kopsNodes,
adminAccess: *kopsAdminAccess,
cluster: cluster,
image: *kopsImage,
args: *kopsArgs,
kubecfg: kubecfg,
provider: provider,
gcpProject: gcpProject,
}, nil
}
func (k kops) isGoogleCloud() bool {
return k.provider == "gce"
}
func (k kops) Up() error {
// If we downloaded kubernetes, pass that version to kops
if k.kubeVersion == "" {
// TODO(justinsb): figure out a refactor that allows us to get this from acquireKubernetes cleanly
kubeReleaseUrl := os.Getenv("KUBERNETES_RELEASE_URL")
kubeRelease := os.Getenv("KUBERNETES_RELEASE")
if kubeReleaseUrl != "" && kubeRelease != "" {
if !strings.HasSuffix(kubeReleaseUrl, "/") {
kubeReleaseUrl += "/"
}
k.kubeVersion = kubeReleaseUrl + kubeRelease
}
}
var featureFlags []string
createArgs := []string{
"create", "cluster",
"--name", k.cluster,
"--ssh-public-key", k.sshPublicKey,
"--node-count", strconv.Itoa(k.nodes),
"--zones", strings.Join(k.zones, ","),
}
if k.kubeVersion != "" {
createArgs = append(createArgs, "--kubernetes-version", k.kubeVersion)
}
if k.adminAccess != "" {
createArgs = append(createArgs, "--admin-access", k.adminAccess)
// Enable nodeport access from the same IP (we expect it to be the test IPs)
featureFlags = append(featureFlags, "SpecOverrideFlag")
createArgs = append(createArgs, "--override", "cluster.spec.nodePortAccess="+k.adminAccess)
}
if k.image != "" {
createArgs = append(createArgs, "--image", k.image)
}
if k.gcpProject != "" {
createArgs = append(createArgs, "--project", k.gcpProject)
}
if k.isGoogleCloud() {
featureFlags = append(featureFlags, "AlphaAllowGCE")
}
if k.args != "" {
createArgs = append(createArgs, strings.Split(k.args, " ")...)
}
if len(featureFlags) != 0 {
os.Setenv("KOPS_FEATURE_FLAGS", strings.Join(featureFlags, ","))
}
if err := finishRunning(exec.Command(k.path, createArgs...)); err != nil {
return fmt.Errorf("kops configuration failed: %v", err)
}
if err := finishRunning(exec.Command(k.path, "update", "cluster", k.cluster, "--yes")); err != nil {
return fmt.Errorf("kops bringup failed: %v", err)
}
// TODO(zmerlynn): More cluster validation. This should perhaps be
// added to kops and not here, but this is a fine place to loop
// for now.
return waitForReadyNodes(k.nodes+1, *kopsUpTimeout)
}
func (k kops) IsUp() error {
return isUp(k)
}
func (k kops) DumpClusterLogs(localPath, gcsPath string) error {
privateKeyPath := k.sshPrivateKey
if strings.HasPrefix(privateKeyPath, "~/") {
privateKeyPath = filepath.Join(os.Getenv("HOME"), privateKeyPath[2:])
}
key, err := ioutil.ReadFile(privateKeyPath)
if err != nil {
return fmt.Errorf("error reading private key %q: %v", k.sshPrivateKey, err)
}
signer, err := ssh.ParsePrivateKey(key)
if err != nil {
return fmt.Errorf("error parsing private key %q: %v", k.sshPrivateKey, err)
}
sshConfig := &ssh.ClientConfig{
User: k.sshUser,
Auth: []ssh.AuthMethod{
ssh.PublicKeys(signer),
},
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
}
sshClientFactory := &sshClientFactoryImplementation{
sshConfig: sshConfig,
}
logDumper, err := newLogDumper(sshClientFactory, localPath)
if err != nil {
return err
}
ctx, cancel := context.WithCancel(context.TODO())
defer cancel()
finished := make(chan error)
go func() {
finished <- k.dumpAllNodes(ctx, logDumper)
}()
for {
select {
case <-interrupt.C:
cancel()
case err := <-finished:
return err
}
}
}
// dumpAllNodes connects to every node and dumps the logs
func (k *kops) dumpAllNodes(ctx context.Context, d *logDumper) error {
// Make sure kubeconfig is set, in particular before calling DumpAllNodes, which calls kubectlGetNodes
if err := k.TestSetup(); err != nil {
return fmt.Errorf("error setting up kubeconfig: %v", err)
}
var additionalIPs []string
dump, err := k.runKopsDump()
if err != nil {
log.Printf("unable to get cluster status from kops: %v", err)
} else {
for _, instance := range dump.Instances {
name := instance.Name
if len(instance.PublicAddresses) == 0 {
log.Printf("ignoring instance in kops status with no public address: %v", name)
continue
}
additionalIPs = append(additionalIPs, instance.PublicAddresses[0])
}
}
if err := d.DumpAllNodes(ctx, additionalIPs); err != nil {
return err
}
return nil
}
func (k kops) TestSetup() error {
info, err := os.Stat(k.kubecfg)
if err != nil {
if os.IsNotExist(err) {
log.Printf("kubeconfig file %s not found", k.kubecfg)
} else {
return err
}
} else if info.Size() > 0 {
// Assume that if we already have it, it's good.
return nil
}
if err := finishRunning(exec.Command(k.path, "export", "kubecfg", k.cluster)); err != nil {
return fmt.Errorf("failure from 'kops export kubecfg %s': %v", k.cluster, err)
}
// Double-check that the file was exported
info, err = os.Stat(k.kubecfg)
if err != nil {
return fmt.Errorf("kubeconfig file %s was not exported", k.kubecfg)
}
if info.Size() == 0 {
return fmt.Errorf("exported kubeconfig file %s was empty", k.kubecfg)
}
return nil
}
func (k kops) Down() error {
// We do a "kops get" first so the exit status of "kops delete" is
// more sensical in the case of a non-existent cluster. ("kops
// delete" will exit with status 1 on a non-existent cluster)
err := finishRunning(exec.Command(k.path, "get", "clusters", k.cluster))
if err != nil {
// This is expected if the cluster doesn't exist.
return nil
}
return finishRunning(exec.Command(k.path, "delete", "cluster", k.cluster, "--yes"))
}
func (k kops) GetClusterCreated(gcpProject string) (time.Time, error) {
return time.Time{}, errors.New("not implemented")
}
// kopsDump is the format of data as dumped by `kops toolbox dump -ojson`
type kopsDump struct {
Instances []*kopsDumpInstance `json:"instances"`
}
// String implements fmt.Stringer
func (o *kopsDump) String() string {
return jsonForDebug(o)
}
// kopsDumpInstance is the format of an instance (machine) in a kops dump
type kopsDumpInstance struct {
Name string `json:"name"`
PublicAddresses []string `json:"publicAddresses"`
}
// String implements fmt.Stringer
func (o *kopsDumpInstance) String() string {
return jsonForDebug(o)
}
// runKopsDump runs a kops toolbox dump to dump the status of the cluster
func (k *kops) runKopsDump() (*kopsDump, error) {
o, err := output(exec.Command(k.path, "toolbox", "dump", "--name", k.cluster, "-ojson"))
if err != nil {
log.Printf("error running kops toolbox dump: %s\n%s", wrapError(err).Error(), string(o))
return nil, err
}
dump := &kopsDump{}
if err := json.Unmarshal(o, dump); err != nil {
return nil, fmt.Errorf("error parsing kops toolbox dump output: %v", err)
}
return dump, nil
}