forked from kubernetes/test-infra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
eks.go
332 lines (308 loc) · 9.32 KB
/
eks.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
/*
Copyright 2018 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 / eks.go implements kubetest deployer interface for EKS.
// It uses 'aws-k8s-tester' and 'kubectl' binaries, rather than importing internal packages.
// All underlying implementation and external dependencies are compiled into one binary.
package main
import (
"flag"
"fmt"
"io/ioutil"
"net/http"
"os"
osexec "os/exec"
"path/filepath"
"syscall"
"time"
"github.com/aws/aws-k8s-tester/eksconfig"
"github.com/aws/aws-k8s-tester/ekstester"
"k8s.io/client-go/kubernetes"
"k8s.io/test-infra/kubetest/process"
"k8s.io/test-infra/kubetest/util"
)
var (
eksKubectlPath = flag.String("eks-kubectl-path", "/tmp/aws-k8s-tester/kubectl", "(eks only) Path to the kubectl binary to use.")
eksKubecfgPath = flag.String("eks-kubeconfig-path", "/tmp/aws-k8s-tester/kubeconfig", "(eks only) Path to the kubeconfig file to use.")
eksNodes = flag.String("eks-nodes", "1", "(eks only) Number of nodes in the EKS cluster.")
eksNodeInstanceType = flag.String("eks-node-instance-type", "m3.xlarge", "(eks only) Instance type to use for nodes.")
)
func migrateEKSOptions() error {
// Prevent ginkgo-e2e.sh from using the cluster/eks functions.
if err := os.Setenv("KUBERNETES_CONFORMANCE_TEST", "yes"); err != nil {
return err
}
if err := os.Setenv("KUBERNETES_CONFORMANCE_PROVIDER", "eks"); err != nil {
return err
}
return util.MigrateOptions([]util.MigratedOption{
// Env vars required by upstream ginkgo-e2e.sh.
{
Env: "KUBECTL",
Option: eksKubectlPath,
Name: "--eks-kubectl-path",
},
{
Env: "KUBECONFIG",
Option: eksKubecfgPath,
Name: "--eks-kubeconfig-path",
},
// Env vars specific to aws-k8s-tester.
{
Env: "AWS_K8S_TESTER_EKS_WORKER_NODE_ASG_MIN",
Option: eksNodes,
Name: "--eks-nodes",
},
{
Env: "AWS_K8S_TESTER_EKS_WORKER_NODE_ASG_MAX",
Option: eksNodes,
Name: "--eks-nodes",
},
{
Env: "AWS_K8S_TESTER_EKS_WORKER_NODE_INSTANCE_TYPE",
Option: eksNodeInstanceType,
Name: "--eks-node-instance-type",
},
})
}
// eksDeployer implements EKS deployer interface using "aws-k8s-tester" binary.
// Satisfies "k8s.io/test-infra/kubetest/main.go" 'deployer' and 'publisher" interfaces.
// Reference https://github.com/kubernetes/test-infra/blob/master/kubetest/main.go.
type eksDeployer struct {
stopc chan struct{}
cfg *eksconfig.Config
ctrl *process.Control
}
// newEKS creates a new EKS deployer.
func newEKS(timeout time.Duration, verbose bool) (ekstester.Deployer, error) {
err := migrateEKSOptions()
if err != nil {
return nil, err
}
cfg := eksconfig.NewDefault()
err = cfg.UpdateFromEnvs()
if err != nil {
return nil, err
}
var f *os.File
f, err = ioutil.TempFile(os.TempDir(), "aws-k8s-tester-config")
if err != nil {
return nil, err
}
cfg.ConfigPath = f.Name()
if err = f.Close(); err != nil {
return nil, fmt.Errorf("failed to close aws-k8s-tester-config file %v", err)
}
if err = cfg.Sync(); err != nil {
return nil, err
}
dp := &eksDeployer{
stopc: make(chan struct{}),
cfg: cfg,
ctrl: process.NewControl(
timeout,
time.NewTimer(timeout),
time.NewTimer(timeout),
verbose,
),
}
if err := dp.fetchAWSK8sTester(); err != nil {
return nil, fmt.Errorf("failed to fetch aws-k8s-tester: %v", err)
}
return dp, nil
}
// Up creates a new EKS cluster.
func (dp *eksDeployer) Up() (err error) {
// "create cluster" command outputs cluster information
// in the configuration file (e.g. VPC ID, ALB DNS names, etc.)
// this needs be reloaded for other deployer method calls
createCmd := osexec.Command(
dp.cfg.AWSK8sTesterPath,
"eks",
"--path="+dp.cfg.ConfigPath,
"create",
"cluster",
)
errc := make(chan error)
go func() {
_, oerr := dp.ctrl.Output(createCmd)
errc <- oerr
}()
select {
case <-dp.stopc:
fmt.Fprintln(os.Stderr, "received stop signal, interrupting 'create cluster' command...")
ierr := createCmd.Process.Signal(syscall.SIGINT)
err = fmt.Errorf("'create cluster' command interrupted (interrupt error %v)", ierr)
case err = <-errc:
}
return err
}
// Down tears down the existing EKS cluster.
func (dp *eksDeployer) Down() (err error) {
// reload configuration from disk to read the latest configuration
if _, err = dp.LoadConfig(); err != nil {
return err
}
_, err = dp.ctrl.Output(osexec.Command(
dp.cfg.AWSK8sTesterPath,
"eks",
"--path="+dp.cfg.ConfigPath,
"delete",
"cluster",
))
return err
}
// IsUp returns an error if the cluster is not up and running.
func (dp *eksDeployer) IsUp() (err error) {
// reload configuration from disk to read the latest configuration
if _, err = dp.LoadConfig(); err != nil {
return err
}
_, err = dp.ctrl.Output(osexec.Command(
dp.cfg.AWSK8sTesterPath,
"eks",
"--path="+dp.cfg.ConfigPath,
"check",
"cluster",
))
if err != nil {
return err
}
if _, err = dp.LoadConfig(); err != nil {
return err
}
if dp.cfg.ClusterState.Status != "ACTIVE" {
return fmt.Errorf("cluster %q status is %q",
dp.cfg.ClusterName,
dp.cfg.ClusterState.Status,
)
}
return nil
}
// TestSetup checks if EKS testing cluster has been set up or not.
func (dp *eksDeployer) TestSetup() error {
return dp.IsUp()
}
// GetClusterCreated returns EKS cluster creation time and error (if any).
func (dp *eksDeployer) GetClusterCreated(v string) (time.Time, error) {
err := dp.IsUp()
if err != nil {
return time.Time{}, err
}
return dp.cfg.ClusterState.Created, nil
}
func (dp *eksDeployer) GetWorkerNodeLogs() (err error) {
// reload configuration from disk to read the latest configuration
if _, err = dp.LoadConfig(); err != nil {
return err
}
_, err = dp.ctrl.Output(osexec.Command(
dp.cfg.AWSK8sTesterPath,
"eks",
"--path="+dp.cfg.ConfigPath,
"test", "get-worker-node-logs",
))
return err
}
// DumpClusterLogs dumps all logs to artifact directory.
// Let default kubetest log dumper handle all artifact uploads.
// See https://github.com/kubernetes/test-infra/pull/9811/files#r225776067.
func (dp *eksDeployer) DumpClusterLogs(artifactDir, _ string) (err error) {
// reload configuration from disk to read the latest configuration
if _, err = dp.LoadConfig(); err != nil {
return err
}
_, err = dp.ctrl.Output(osexec.Command(
dp.cfg.AWSK8sTesterPath,
"eks",
"--path="+dp.cfg.ConfigPath,
"test", "get-worker-node-logs",
))
if err != nil {
return err
}
_, err = dp.ctrl.Output(osexec.Command(
dp.cfg.AWSK8sTesterPath,
"eks",
"--path="+dp.cfg.ConfigPath,
"test", "dump-cluster-logs",
artifactDir,
))
return err
}
// KubectlCommand returns "kubectl" command object for API reachability tests.
func (dp *eksDeployer) KubectlCommand() (*osexec.Cmd, error) {
// reload configuration from disk to read the latest configuration
if _, err := dp.LoadConfig(); err != nil {
return nil, err
}
return osexec.Command(dp.cfg.KubectlPath, "--kubeconfig="+dp.cfg.KubeConfigPath), nil
}
// Stop stops ongoing operations.
// This is useful for local development.
// For example, one may run "Up" but have to cancel ongoing "Up"
// operation. Then, it can just send syscall.SIGINT to trigger "Stop".
func (dp *eksDeployer) Stop() {
close(dp.stopc)
}
// LoadConfig reloads configuration from disk to read the latest
// cluster configuration and its states.
func (dp *eksDeployer) LoadConfig() (eksconfig.Config, error) {
var err error
dp.cfg, err = eksconfig.Load(dp.cfg.ConfigPath)
return *dp.cfg, err
}
func getLatestAWSK8sTesterURL() (string, error) {
resp, err := http.Get("https://github.com/aws/aws-k8s-tester/releases/latest")
if err != nil {
return "", err
}
redirectURL := resp.Request.URL.String()
basepath, version := filepath.Split(redirectURL)
if basepath == "" {
return "", fmt.Errorf("Couldn't extract version from redirect URL")
}
return fmt.Sprintf("https://github.com/aws/aws-k8s-tester/releases/download/%s/aws-k8s-tester-%s-linux-amd64", version, version), nil
}
func (dp *eksDeployer) fetchAWSK8sTester() error {
if err := os.RemoveAll(dp.cfg.AWSK8sTesterPath); err != nil {
return err
}
if err := os.MkdirAll(filepath.Dir(dp.cfg.AWSK8sTesterPath), 0700); err != nil {
return err
}
f, err := os.Create(dp.cfg.AWSK8sTesterPath)
if err != nil {
return fmt.Errorf("failed to create %q (%v)", dp.cfg.AWSK8sTesterPath, err)
}
dp.cfg.AWSK8sTesterPath = f.Name()
var awsK8sTesterDownloadURL string
awsK8sTesterDownloadURL, err = getLatestAWSK8sTesterURL()
if err != nil {
return err
}
if err = httpRead(awsK8sTesterDownloadURL, f); err != nil {
return err
}
if err = f.Close(); err != nil {
return fmt.Errorf("failed to close aws-k8s-tester file %v", err)
}
if err = util.EnsureExecutable(dp.cfg.AWSK8sTesterPath); err != nil {
return err
}
return nil
}
// KubernetesClientSet is an empty implement to make eksDeployer meet
// the definition of interface ekstester.Deployer
func (dp *eksDeployer) KubernetesClientSet() *kubernetes.Clientset {
return nil
}