/
common.go
304 lines (251 loc) · 9.72 KB
/
common.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
// Copyright (c) 2019 SAP SE or an SAP affiliate company. All rights reserved. This file is licensed under the Apache Software License, v. 2 except as noted otherwise in the LICENSE file
//
// 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.
// Deprecated: this is the deprecated gardener testframework.
// Use gardener/test/framework instead
package framework
import (
"fmt"
"io/ioutil"
"path"
"path/filepath"
"runtime"
"strings"
gardencorev1beta1 "github.com/gardener/gardener/pkg/apis/core/v1beta1"
v1beta1constants "github.com/gardener/gardener/pkg/apis/core/v1beta1/constants"
"github.com/gardener/gardener/pkg/apis/core/v1beta1/helper"
"github.com/gardener/gardener/pkg/client/kubernetes"
"github.com/gardener/gardener/pkg/utils"
"github.com/ghodss/yaml"
apimachineryRuntime "k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/serializer"
)
var (
decoder = serializer.NewCodecFactory(kubernetes.GardenScheme).UniversalDecoder()
// TemplateDir relative path for helm templates dir
TemplateDir = filepath.Join("..", "..", "resources", "templates")
)
const (
// IntegrationTestPrefix is the default prefix that will be used for test shoots if none other is specified
IntegrationTestPrefix = "itest-"
// WorkerNamePrefix is the default prefix that will be used for Shoot workers
WorkerNamePrefix = "worker-"
)
// CreateShootTestArtifacts creates a shoot object from the given path and sets common attributes (test-individual settings like workers have to be handled by each test).
func CreateShootTestArtifacts(shootTestYamlPath string, prefix *string, projectNamespace, shootRegion, cloudProfile, secretbinding, providerType, k8sVersion, externalDomain *string, clearDNS bool, clearExtensions bool) (string, *gardencorev1beta1.Shoot, error) {
shoot := &gardencorev1beta1.Shoot{}
if err := ReadObject(shootTestYamlPath, shoot); err != nil {
return "", nil, err
}
if shootRegion != nil && len(*shootRegion) > 0 {
shoot.Spec.Region = *shootRegion
}
if externalDomain != nil && len(*externalDomain) > 0 {
shoot.Spec.DNS = &gardencorev1beta1.DNS{Domain: externalDomain}
clearDNS = false
}
if projectNamespace != nil && len(*projectNamespace) > 0 {
shoot.Namespace = *projectNamespace
}
if prefix != nil && len(*prefix) != 0 {
integrationTestName, err := generateRandomShootName(*prefix, 8)
if err != nil {
return "", nil, err
}
shoot.Name = integrationTestName
}
if cloudProfile != nil && len(*cloudProfile) > 0 {
shoot.Spec.CloudProfileName = *cloudProfile
}
if secretbinding != nil && len(*secretbinding) > 0 {
shoot.Spec.SecretBindingName = *secretbinding
}
if providerType != nil && len(*providerType) > 0 {
shoot.Spec.Provider.Type = *providerType
}
if k8sVersion != nil && len(*k8sVersion) > 0 {
shoot.Spec.Kubernetes.Version = *k8sVersion
}
if clearDNS {
shoot.Spec.DNS = &gardencorev1beta1.DNS{}
}
if clearExtensions {
shoot.Spec.Extensions = nil
}
if shoot.Annotations == nil {
shoot.Annotations = map[string]string{}
}
shoot.Annotations[v1beta1constants.AnnotationShootIgnoreAlerts] = "true"
return shoot.Name, shoot, nil
}
// SetProviderConfigsFromFilepath parses the infrastructure, controlPlane and networking provider-configs and sets them on the shoot
func SetProviderConfigsFromFilepath(shoot *gardencorev1beta1.Shoot, infrastructureConfigPath, controlPlaneConfigPath, networkingConfigPath, workersConfigPath *string) error {
// clear provider configs first
shoot.Spec.Provider.InfrastructureConfig = nil
shoot.Spec.Provider.ControlPlaneConfig = nil
shoot.Spec.Networking.ProviderConfig = nil
if infrastructureConfigPath != nil && len(*infrastructureConfigPath) != 0 {
infrastructureProviderConfig, err := ParseFileAsProviderConfig(*infrastructureConfigPath)
if err != nil {
return err
}
shoot.Spec.Provider.InfrastructureConfig = infrastructureProviderConfig
}
if len(*controlPlaneConfigPath) != 0 {
controlPlaneProviderConfig, err := ParseFileAsProviderConfig(*controlPlaneConfigPath)
if err != nil {
return err
}
shoot.Spec.Provider.ControlPlaneConfig = controlPlaneProviderConfig
}
if len(*networkingConfigPath) != 0 {
networkingProviderConfig, err := ParseFileAsProviderConfig(*networkingConfigPath)
if err != nil {
return err
}
shoot.Spec.Networking.ProviderConfig = networkingProviderConfig
}
if len(*workersConfigPath) != 0 {
workers, err := ParseFileAsWorkers(*workersConfigPath)
if err != nil {
return err
}
shoot.Spec.Provider.Workers = workers
}
return nil
}
func generateRandomShootName(prefix string, length int) (string, error) {
randomString, err := utils.GenerateRandomString(length)
if err != nil {
return "", err
}
if len(prefix) > 0 {
return prefix + strings.ToLower(randomString), nil
}
return IntegrationTestPrefix + strings.ToLower(randomString), nil
}
// CreatePlantTestArtifacts creates a plant object which is read from the resources directory
func CreatePlantTestArtifacts(plantTestYamlPath string) (*gardencorev1beta1.Plant, error) {
plant := &gardencorev1beta1.Plant{}
if err := ReadObject(plantTestYamlPath, plant); err != nil {
return nil, err
}
return plant, nil
}
// ReadObject loads the contents of file and decodes it as an object.
func ReadObject(file string, into apimachineryRuntime.Object) error {
data, err := ioutil.ReadFile(file)
if err != nil {
return err
}
_, _, err = decoder.Decode(data, nil, into)
return err
}
// ParseFileAsProviderConfig parses a file as a ProviderConfig
func ParseFileAsProviderConfig(filepath string) (*gardencorev1beta1.ProviderConfig, error) {
data, err := ioutil.ReadFile(filepath)
if err != nil {
return nil, err
}
// apiServer needs JSON for the Raw data
jsonData, err := yaml.YAMLToJSON(data)
if err != nil {
return nil, fmt.Errorf("unable to decode ProviderConfig: %v", err)
}
return &gardencorev1beta1.ProviderConfig{RawExtension: apimachineryRuntime.RawExtension{Raw: jsonData}}, nil
}
// ParseFileAsWorkers parses a file as a Worker configuration
func ParseFileAsWorkers(filepath string) ([]gardencorev1beta1.Worker, error) {
data, err := ioutil.ReadFile(filepath)
if err != nil {
return nil, err
}
workers := []gardencorev1beta1.Worker{}
if err := yaml.Unmarshal(data, &workers); err != nil {
return nil, fmt.Errorf("unable to decode workers: %v", err)
}
return workers, nil
}
// GetProjectRootPath gets the root path of the project relative to the integration test folder
func GetProjectRootPath() string {
_, filename, _, _ := runtime.Caller(0)
dir := path.Join(path.Dir(filename), "../../..")
return dir
}
// AddWorkerForName adds a valid worker to the shoot for the given machine image name. Returns an error if the machine image cannot be found in the CloudProfile.
func AddWorkerForName(shoot *gardencorev1beta1.Shoot, cloudProfile *gardencorev1beta1.CloudProfile, machineImageName *string, workerZone *string) error {
found, image, err := helper.DetermineMachineImageForName(cloudProfile, *machineImageName)
if err != nil {
return err
}
if !found {
return fmt.Errorf("could not find machine image '%s' in CloudProfile '%s'", *machineImageName, cloudProfile.Name)
}
return AddWorker(shoot, cloudProfile, image, workerZone)
}
// AddWorker adds a valid default worker to the shoot for the given machineImage and CloudProfile.
func AddWorker(shoot *gardencorev1beta1.Shoot, cloudProfile *gardencorev1beta1.CloudProfile, machineImage gardencorev1beta1.MachineImage, workerZone *string) error {
if len(cloudProfile.Spec.MachineTypes) == 0 {
return fmt.Errorf("no MachineTypes configured in the Cloudprofile '%s'", cloudProfile.Name)
}
machineType := cloudProfile.Spec.MachineTypes[0]
qualifyingVersionFound, shootMachineImage, err := helper.GetLatestQualifyingShootMachineImage(machineImage)
if err != nil {
return err
}
if !qualifyingVersionFound {
return fmt.Errorf("could not add worker. No latest qualifying Shoot machine image could be determined for machine image %q. Make sure the machine image in the CloudProfile has at least one version that is not expired and not in preview", machineImage.Name)
}
workerName, err := generateRandomWorkerName(fmt.Sprintf("%s-", shootMachineImage.Name))
if err != nil {
return err
}
shoot.Spec.Provider.Workers = append(shoot.Spec.Provider.Workers, gardencorev1beta1.Worker{
Name: workerName,
Maximum: 2,
Minimum: 2,
Machine: gardencorev1beta1.Machine{
Type: machineType.Name,
Image: shootMachineImage,
},
})
if machineType.Storage == nil {
if len(cloudProfile.Spec.VolumeTypes) == 0 {
return fmt.Errorf("no VolumeTypes configured in the Cloudprofile '%s'", cloudProfile.Name)
}
shoot.Spec.Provider.Workers[0].Volume = &gardencorev1beta1.Volume{
Type: &cloudProfile.Spec.VolumeTypes[0].Name,
VolumeSize: "35Gi",
}
}
if workerZone != nil && len(*workerZone) > 0 {
// using one zone as default
shoot.Spec.Provider.Workers[0].Zones = []string{*workerZone}
}
return nil
}
func generateRandomWorkerName(prefix string) (string, error) {
var length int
remainingCharacters := 15 - len(prefix)
if remainingCharacters > 0 {
length = remainingCharacters
} else {
prefix = WorkerNamePrefix
length = 15 - len(WorkerNamePrefix)
}
randomString, err := utils.GenerateRandomString(length)
if err != nil {
return "", err
}
return prefix + strings.ToLower(randomString), nil
}