/
csi.go
276 lines (239 loc) · 6.4 KB
/
csi.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
// Package csi implements csi test operations.
package csi
import (
"bytes"
"fmt"
"os"
"reflect"
"strings"
"text/template"
"time"
"github.com/aws/aws-k8s-tester/ec2config"
"github.com/aws/aws-k8s-tester/internal/ec2"
"github.com/aws/aws-k8s-tester/internal/ssh"
"github.com/aws/aws-k8s-tester/pkg/fileutil"
"go.uber.org/zap"
)
const policyDocument = `{
"Version": "2012-10-17",
"Statement": [
{
"Action": "ec2:*",
"Effect": "Allow",
"Resource": "*"
},
{
"Effect": "Allow",
"Action": "elasticloadbalancing:*",
"Resource": "*"
},
{
"Effect": "Allow",
"Action": "cloudwatch:*",
"Resource": "*"
},
{
"Effect": "Allow",
"Action": "autoscaling:*",
"Resource": "*"
},
{
"Effect": "Allow",
"Action": "iam:CreateServiceLinkedRole",
"Resource": "*",
"Condition": {
"StringEquals": {
"iam:AWSServiceName": [
"autoscaling.amazonaws.com",
"ec2scheduled.amazonaws.com",
"elasticloadbalancing.amazonaws.com",
"spot.amazonaws.com",
"spotfleet.amazonaws.com"
]
}
}
}
]
}
`
type csiTester struct {
cfg *ec2config.Config
ec ec2.Deployer
terminateOnExit bool
journalctlLogs bool
}
// Tester defines CSI test interface.
type Tester interface {
RunIntegration() error
Down() error
}
// NewTester creates a new tester.
func NewTester(cfg *ec2config.Config, terminateOnExit, journalctlLogs bool) (ct *csiTester, err error) {
// TODO: some sort of validations check
ct = &csiTester{
terminateOnExit: terminateOnExit,
journalctlLogs: journalctlLogs,
cfg: cfg,
}
if ct.ec, err = ec2.NewDeployer(ct.cfg); err != nil {
return nil, fmt.Errorf("failed to create EC2 deployer (%v)", err)
}
if err = ct.ec.Create(); err != nil {
return nil, fmt.Errorf("failed to create EC2 instance (%v)", err)
}
return ct, nil
}
// CreateConfig creates a new configuration.
func CreateConfig(vpcID, prNum, githubAccount, githubBranch string) (cfg *ec2config.Config, err error) {
cfg = ec2config.NewDefault()
cfg.UploadTesterLogs = false
cfg.VPCID = vpcID
cfg.IngressRulesTCP = map[string]string{"22": "0.0.0.0/0"}
cfg.Plugins = []string{
"update-amazon-linux-2",
"install-go-1.12.7",
}
cfg.InstanceProfileFilePath, err = fileutil.WriteTempFile([]byte(policyDocument))
if err != nil {
return nil, err
}
// If prNum is set to an non-empty string, the master branch of kubernetes-sigs/aws-ebs-csi-driver is used,
// regardless of whether or not githubAccount and githubBranch have different values.
if prNum != "" {
if githubAccount != "kubernetes-sigs" || githubBranch != "master" {
fmt.Printf("WARNING: PR number %s takes precedence over non-default GitHub account and/or branch.\n", prNum)
}
cfg.Plugins = append(cfg.Plugins, "install-csi-"+prNum)
} else {
cfg.CustomScript, err = createCustomScript(gitAccountAndBranch{
Account: githubAccount,
Branch: githubBranch,
})
if err != nil {
return nil, err
}
}
cfg.Wait = true
return cfg, nil
}
func (ct *csiTester) RunIntegration() error {
lg := ct.ec.Logger()
downOrPrintCommands := func() {
if ct.terminateOnExit {
err := ct.Down()
if err != nil {
lg.Warn("failed to take down all resources", zap.Error(err))
}
} else {
fmt.Println(ct.cfg.SSHCommands())
}
}
fmt.Println(ct.cfg.SSHCommands())
var iv ec2config.Instance
for _, v := range ct.cfg.Instances {
iv = v
break
}
sh, serr := ssh.New(ssh.Config{
Logger: lg,
KeyPath: ct.cfg.KeyPath,
PublicIP: iv.PublicIP,
PublicDNSName: iv.PublicDNSName,
UserName: ct.cfg.UserName})
if serr != nil {
downOrPrintCommands()
return fmt.Errorf("failed to create SSH (%v)", serr)
}
defer sh.Close()
if err := sh.Connect(); err != nil {
downOrPrintCommands()
return fmt.Errorf("failed to connect SSH (%v)", err)
}
testCmd := fmt.Sprintf(`cd /home/%s/go/src/github.com/kubernetes-sigs/aws-ebs-csi-driver && sudo sh -c 'source /home/%s/.bashrc && ./hack/test-integration.sh'`, ct.cfg.UserName, ct.cfg.UserName)
out, err := sh.Run(
testCmd,
ssh.WithTimeout(10*time.Minute),
)
if err != nil {
// handle "Process exited with status 2" error
downOrPrintCommands()
return fmt.Errorf("CSI integration test FAILED (%v, %v)", err, reflect.TypeOf(err))
}
testOutput := string(out)
fmt.Printf("CSI test output:\n\n%s\n\n", testOutput)
/*
expects
Ran 1 of 1 Specs in 25.028 seconds
SUCCESS! -- 1 Passed | 0 Failed | 0 Pending | 0 Skipped
*/
if !strings.Contains(testOutput, "1 Passed") {
downOrPrintCommands()
return fmt.Errorf("CSI integration test FAILED")
}
if ct.journalctlLogs {
// full journal logs (e.g. disk mounts)
lg.Info("fetching journal logs")
journalCmd := "sudo journalctl --no-pager --output=short-precise"
out, err = sh.Run(journalCmd)
if err != nil {
lg.Warn(
"failed to run journalctl",
zap.String("cmd", journalCmd),
zap.Error(err),
)
} else {
fmt.Printf("journalctl logs:\n\n%s\n\n", string(out))
}
}
if ct.terminateOnExit {
err = ct.Down()
if err != nil {
lg.Warn("failed to take down all resources", zap.Error(err))
}
os.RemoveAll(ct.cfg.InstanceProfileFilePath)
}
return nil
}
func (ct *csiTester) Down() error {
return ct.ec.Terminate()
}
type gitAccountAndBranch struct {
Account string
Branch string
}
func createCustomScript(g gitAccountAndBranch) (string, error) {
tpl := template.Must(template.New("installGitAccountAndBranchTemplate").Parse(installGitAccountAndBranchTemplate))
buf := bytes.NewBuffer(nil)
if err := tpl.Execute(buf, g); err != nil {
return "", err
}
return buf.String(), nil
}
const installGitAccountAndBranchTemplate = `
################################## install kubernetes-sigs from account {{ .Account }}, branch {{ .Branch }}
mkdir -p ${GOPATH}/src/github.com/kubernetes-sigs/
cd ${GOPATH}/src/github.com/kubernetes-sigs/
RETRIES=10
DELAY=10
COUNT=1
while [[ ${COUNT} -lt ${RETRIES} ]]; do
rm -rf ./aws-ebs-csi-driver
git clone "https://github.com/{{ .Account }}/aws-ebs-csi-driver.git"
if [[ $? -eq 0 ]]; then
RETRIES=0
echo "Successfully git cloned!"
break
fi
let COUNT=${COUNT}+1
sleep ${DELAY}
done
cd ${GOPATH}/src/github.com/kubernetes-sigs/aws-ebs-csi-driver
git checkout origin/{{ .Branch }}
git checkout -B {{ .Branch }}
git remote -v
git branch
git log --pretty=oneline -5
pwd
make aws-ebs-csi-driver && sudo cp ./bin/aws-ebs-csi-driver /usr/local/bin/aws-ebs-csi-driver
##################################
`