/
scenarios_aws.go
236 lines (214 loc) · 6.6 KB
/
scenarios_aws.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
/*
Copyright 2020 The Kubermatic Kubernetes Platform contributors.
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 (
"errors"
"fmt"
"time"
"github.com/kubermatic/kubermatic/api/pkg/semver"
awsapiclient "github.com/kubermatic/kubermatic/api/pkg/test/e2e/api/utils/apiclient/client/aws"
apimodels "github.com/kubermatic/kubermatic/api/pkg/test/e2e/api/utils/apiclient/models"
"k8s.io/apimachinery/pkg/util/sets"
utilpointer "k8s.io/utils/pointer"
)
const awsDC = "aws-eu-central-1a"
// Returns a matrix of (version x operating system)
func getAWSScenarios(versions []*semver.Semver) []testScenario {
var scenarios []testScenario
for _, v := range versions {
// Ubuntu
scenarios = append(scenarios, &awsScenario{
version: v,
nodeOsSpec: apimodels.OperatingSystemSpec{
Ubuntu: &apimodels.UbuntuSpec{},
},
})
// CoreOS
scenarios = append(scenarios, &awsScenario{
version: v,
nodeOsSpec: apimodels.OperatingSystemSpec{
ContainerLinux: &apimodels.ContainerLinuxSpec{
// Otherwise the nodes restart directly after creation - bad for tests
DisableAutoUpdate: true,
},
},
})
// Flatcar
scenarios = append(scenarios, &awsScenario{
version: v,
nodeOsSpec: apimodels.OperatingSystemSpec{
Flatcar: &apimodels.FlatcarSpec{
// Otherwise the nodes restart directly after creation - bad for tests
DisableAutoUpdate: true,
},
},
})
scenarios = append(scenarios, &awsScenario{
version: v,
nodeOsSpec: apimodels.OperatingSystemSpec{
Centos: &apimodels.CentOSSpec{},
},
})
}
return scenarios
}
type awsScenario struct {
version *semver.Semver
nodeOsSpec apimodels.OperatingSystemSpec
}
func (s *awsScenario) Name() string {
return fmt.Sprintf("aws-%s-%s", getOSNameFromSpec(s.nodeOsSpec), s.version.String())
}
func (s *awsScenario) Cluster(secrets secrets) *apimodels.CreateClusterSpec {
return &apimodels.CreateClusterSpec{
Cluster: &apimodels.Cluster{
Type: "kubernetes",
Spec: &apimodels.ClusterSpec{
Cloud: &apimodels.CloudSpec{
DatacenterName: awsDC,
Aws: &apimodels.AWSCloudSpec{
SecretAccessKey: secrets.AWS.SecretAccessKey,
AccessKeyID: secrets.AWS.AccessKeyID,
},
},
Version: s.version,
},
},
}
}
func (s *awsScenario) NodeDeployments(num int, secrets secrets) ([]apimodels.NodeDeployment, error) {
instanceType := "t2.medium"
volumeType := "gp2"
volumeSize := int64(100)
listVPCParams := &awsapiclient.ListAWSVPCSParams{
AccessKeyID: utilpointer.StringPtr(secrets.AWS.AccessKeyID),
SecretAccessKey: utilpointer.StringPtr(secrets.AWS.SecretAccessKey),
DC: awsDC,
}
listVPCParams.SetTimeout(15 * time.Second)
vpcResponse, err := secrets.kubermaticClient.Aws.ListAWSVPCS(listVPCParams, secrets.kubermaticAuthenticator)
if err != nil {
return nil, fmt.Errorf("failed to get vpcs: %s", fmtSwaggerError(err))
}
if len(vpcResponse.Payload) < 1 {
return nil, errors.New("got zero vpcs back")
}
vpcID := vpcResponse.Payload[0].VpcID
for _, vpc := range vpcResponse.Payload {
if vpc.IsDefault {
vpcID = vpc.VpcID
break
}
}
listSubnetParams := &awsapiclient.ListAWSSubnetsParams{
AccessKeyID: utilpointer.StringPtr(secrets.AWS.AccessKeyID),
SecretAccessKey: utilpointer.StringPtr(secrets.AWS.SecretAccessKey),
DC: awsDC,
VPC: utilpointer.StringPtr(vpcID),
}
listSubnetParams.SetTimeout(15 * time.Second)
subnetResponse, err := secrets.kubermaticClient.Aws.ListAWSSubnets(listSubnetParams, secrets.kubermaticAuthenticator)
if err != nil {
return nil, fmt.Errorf("failed to get subnets: %s", fmtSwaggerError(err))
}
if n := len(subnetResponse.Payload); n < 3 {
return nil, fmt.Errorf("expected to get at least three subnets, got %d", n)
}
// Find three subnets that are in different AZs to preserve the multi az testcase
allAZs := sets.String{}
var subnets []*apimodels.AWSSubnet
for _, subnet := range subnetResponse.Payload {
if !allAZs.Has(subnet.AvailabilityZone) {
allAZs.Insert(subnet.AvailabilityZone)
subnets = append(subnets, subnet)
}
}
if n := len(subnets); n < 3 {
return nil, fmt.Errorf("wanted three subnets in different AZs, got only %d", n)
}
nds := []apimodels.NodeDeployment{
{
Spec: &apimodels.NodeDeploymentSpec{
Template: &apimodels.NodeSpec{
Cloud: &apimodels.NodeCloudSpec{
Aws: &apimodels.AWSNodeSpec{
InstanceType: &instanceType,
VolumeType: &volumeType,
VolumeSize: &volumeSize,
AvailabilityZone: subnets[0].AvailabilityZone,
SubnetID: subnets[0].ID,
},
},
Versions: &apimodels.NodeVersionInfo{
Kubelet: s.version.String(),
},
OperatingSystem: &s.nodeOsSpec,
},
},
},
{
Spec: &apimodels.NodeDeploymentSpec{
Template: &apimodels.NodeSpec{
Cloud: &apimodels.NodeCloudSpec{
Aws: &apimodels.AWSNodeSpec{
InstanceType: &instanceType,
VolumeType: &volumeType,
VolumeSize: &volumeSize,
AvailabilityZone: subnets[1].AvailabilityZone,
SubnetID: subnets[1].ID,
},
},
Versions: &apimodels.NodeVersionInfo{
Kubelet: s.version.String(),
},
OperatingSystem: &s.nodeOsSpec,
},
},
},
{
Spec: &apimodels.NodeDeploymentSpec{
Template: &apimodels.NodeSpec{
Cloud: &apimodels.NodeCloudSpec{
Aws: &apimodels.AWSNodeSpec{
InstanceType: &instanceType,
VolumeType: &volumeType,
VolumeSize: &volumeSize,
AvailabilityZone: subnets[2].AvailabilityZone,
SubnetID: subnets[2].ID,
},
},
Versions: &apimodels.NodeVersionInfo{
Kubelet: s.version.String(),
},
OperatingSystem: &s.nodeOsSpec,
},
},
},
}
// evenly distribute the nodes among deployments
nodesInEachAZ := num / 3
azsWithExtraNode := num % 3
for i := range nds {
var replicas int32
if i < azsWithExtraNode {
replicas = int32(nodesInEachAZ + 1)
} else {
replicas = int32(nodesInEachAZ)
}
nds[i].Spec.Replicas = &replicas
}
return nds, nil
}
func (s *awsScenario) OS() apimodels.OperatingSystemSpec {
return s.nodeOsSpec
}