-
Notifications
You must be signed in to change notification settings - Fork 2
/
utils.go
446 lines (381 loc) · 14.9 KB
/
utils.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
package test
import (
"context"
"os"
"github.com/equinor/radix-operator/pkg/apis/defaults"
"github.com/equinor/radix-operator/pkg/apis/kube"
radixv1 "github.com/equinor/radix-operator/pkg/apis/radix/v1"
"github.com/equinor/radix-operator/pkg/apis/utils"
"github.com/equinor/radix-operator/pkg/apis/utils/labels"
radixclient "github.com/equinor/radix-operator/pkg/client/clientset/versioned"
log "github.com/sirupsen/logrus"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/uuid"
"k8s.io/client-go/kubernetes"
secretProviderClient "sigs.k8s.io/secrets-store-csi-driver/pkg/client/clientset/versioned"
)
const dnsZone = "dev.radix.equinor.com"
// Utils Instance variables
type Utils struct {
client kubernetes.Interface
radixclient radixclient.Interface
kubeUtil *kube.Kube
}
// NewTestUtils Constructor
func NewTestUtils(client kubernetes.Interface, radixclient radixclient.Interface,
secretproviderclient secretProviderClient.Interface) Utils {
kubeUtil, _ := kube.New(client, radixclient, secretproviderclient)
return Utils{
client: client,
radixclient: radixclient,
kubeUtil: kubeUtil,
}
}
func (tu *Utils) GetKubeUtil() *kube.Kube {
return tu.kubeUtil
}
// ApplyRegistration Will help persist an application registration
func (tu *Utils) ApplyRegistration(registrationBuilder utils.RegistrationBuilder) (*radixv1.RadixRegistration, error) {
rr := registrationBuilder.BuildRR()
_, err := tu.radixclient.RadixV1().RadixRegistrations().Create(context.TODO(), rr, metav1.CreateOptions{})
if err != nil {
if errors.IsAlreadyExists(err) {
return tu.ApplyRegistrationUpdate(registrationBuilder)
}
return rr, err
}
return rr, nil
}
// ApplyRegistrationUpdate Will help update a registration
func (tu *Utils) ApplyRegistrationUpdate(registrationBuilder utils.RegistrationBuilder) (*radixv1.RadixRegistration, error) {
rr := registrationBuilder.BuildRR()
rrPrev, err := tu.radixclient.RadixV1().RadixRegistrations().Get(context.TODO(), rr.GetName(), metav1.GetOptions{})
if err != nil {
return nil, err
}
rr.Status = rrPrev.Status
rr, err = tu.radixclient.RadixV1().RadixRegistrations().Update(context.TODO(), rr, metav1.UpdateOptions{})
if err != nil {
return nil, err
}
return rr, nil
}
// ApplyApplication Will help persist an application
func (tu *Utils) ApplyApplication(applicationBuilder utils.ApplicationBuilder) (*radixv1.RadixApplication, error) {
regBuilder := applicationBuilder.GetRegistrationBuilder()
var rr *radixv1.RadixRegistration
if regBuilder != nil {
rr, _ = tu.ApplyRegistration(regBuilder)
}
ra := applicationBuilder.BuildRA()
appNamespace := CreateAppNamespace(tu.client, ra.GetName())
_, err := tu.radixclient.RadixV1().RadixApplications(appNamespace).Create(context.TODO(), ra, metav1.CreateOptions{})
if err != nil {
if errors.IsAlreadyExists(err) {
return tu.ApplyApplicationUpdate(applicationBuilder)
}
return ra, err
}
ra.ObjectMeta.UID = uuid.NewUUID() // imitate new UID, assigned by Kubernetes
// Note: rr may be nil if not found but that is fine
for _, env := range ra.Spec.Environments {
if _, err := tu.ApplyEnvironment(utils.NewEnvironmentBuilder().
WithAppName(ra.GetName()).
WithAppLabel().
WithEnvironmentName(env.Name).
WithRegistrationOwner(rr).
WithOrphaned(false)); err != nil {
return nil, err
}
}
return ra, nil
}
// ApplyApplicationUpdate Will help update an application
func (tu *Utils) ApplyApplicationUpdate(applicationBuilder utils.ApplicationBuilder) (*radixv1.RadixApplication, error) {
ra := applicationBuilder.BuildRA()
appNamespace := utils.GetAppNamespace(ra.GetName())
_, err := tu.radixclient.RadixV1().RadixApplications(appNamespace).Update(context.TODO(), ra, metav1.UpdateOptions{})
if err != nil {
return ra, err
}
var rr *radixv1.RadixRegistration
regBuilder := applicationBuilder.GetRegistrationBuilder()
if regBuilder != nil {
rr, err = tu.ApplyRegistration(regBuilder)
} else {
rr, err = tu.radixclient.RadixV1().RadixRegistrations().Get(context.TODO(), ra.GetName(), metav1.GetOptions{})
}
if err != nil && !errors.IsNotFound(err) {
return ra, err
}
// Note: rr may be nil if not found but that is fine
for _, env := range ra.Spec.Environments {
if _, err := tu.ApplyEnvironment(utils.NewEnvironmentBuilder().
WithAppName(ra.GetName()).
WithAppLabel().
WithEnvironmentName(env.Name).
WithRegistrationOwner(rr)); err != nil {
return nil, err
}
}
return ra, nil
}
// ApplyDeployment Will help persist a deployment
func (tu *Utils) ApplyDeployment(deploymentBuilder utils.DeploymentBuilder) (*radixv1.RadixDeployment, error) {
envs := make(map[string]struct{})
if deploymentBuilder.GetApplicationBuilder() != nil {
ra, _ := tu.ApplyApplication(deploymentBuilder.GetApplicationBuilder())
for _, env := range ra.Spec.Environments {
envs[env.Name] = struct{}{}
}
}
rd := deploymentBuilder.BuildRD()
log.Debugf("%s", rd.GetObjectMeta().GetCreationTimestamp())
envs[rd.Namespace] = struct{}{}
for env := range envs {
CreateEnvNamespace(tu.client, rd.Spec.AppName, env)
}
envNamespace := rd.Namespace
tu.applyRadixDeploymentEnvVarsConfigMaps(rd)
newRd, err := tu.radixclient.RadixV1().RadixDeployments(envNamespace).Create(context.TODO(), rd, metav1.CreateOptions{})
if err != nil {
return nil, err
}
return newRd, nil
}
// ApplyDeploymentUpdate Will help update a deployment
func (tu *Utils) ApplyDeploymentUpdate(deploymentBuilder utils.DeploymentBuilder) (*radixv1.RadixDeployment, error) {
rd := deploymentBuilder.BuildRD()
envNamespace := utils.GetEnvironmentNamespace(rd.Spec.AppName, rd.Spec.Environment)
rdPrev, err := tu.radixclient.RadixV1().RadixDeployments(envNamespace).Get(context.TODO(), rd.GetName(), metav1.GetOptions{})
if err != nil {
return nil, err
}
rd.Status = rdPrev.Status
rd, err = tu.radixclient.RadixV1().RadixDeployments(envNamespace).Update(context.TODO(), rd, metav1.UpdateOptions{})
if err != nil {
return nil, err
}
return rd, nil
}
// ApplyJob Will help persist a radixjob
func (tu *Utils) ApplyJob(jobBuilder utils.JobBuilder) (*radixv1.RadixJob, error) {
if jobBuilder.GetApplicationBuilder() != nil {
if _, err := tu.ApplyApplication(jobBuilder.GetApplicationBuilder()); err != nil {
return nil, err
}
}
rj := jobBuilder.BuildRJ()
appNamespace := CreateAppNamespace(tu.client, rj.Spec.AppName)
newRj, err := tu.radixclient.RadixV1().RadixJobs(appNamespace).Create(context.TODO(), rj, metav1.CreateOptions{})
if err != nil {
return nil, err
}
return newRj, nil
}
// ApplyJobUpdate Will help update a radixjob
func (tu *Utils) ApplyJobUpdate(jobBuilder utils.JobBuilder) (*radixv1.RadixJob, error) {
rj := jobBuilder.BuildRJ()
appNamespace := CreateAppNamespace(tu.client, rj.Spec.AppName)
rjPrev, err := tu.radixclient.RadixV1().RadixJobs(appNamespace).Get(context.TODO(), rj.GetName(), metav1.GetOptions{})
if err != nil {
return nil, err
}
rj.Status = rjPrev.Status
rj, err = tu.radixclient.RadixV1().RadixJobs(appNamespace).Update(context.TODO(), rj, metav1.UpdateOptions{})
if err != nil {
return nil, err
}
return rj, nil
}
// ApplyEnvironment Will help persist a RadixEnvironment
func (tu *Utils) ApplyEnvironment(environmentBuilder utils.EnvironmentBuilder) (*radixv1.RadixEnvironment, error) {
re := environmentBuilder.BuildRE()
log.Debugf("%s", re.GetObjectMeta().GetCreationTimestamp())
newRe, err := tu.radixclient.RadixV1().RadixEnvironments().Create(context.TODO(), re, metav1.CreateOptions{})
if err != nil {
if errors.IsAlreadyExists(err) {
return tu.ApplyEnvironmentUpdate(environmentBuilder)
}
return nil, err
}
return newRe, nil
}
// ApplyEnvironmentUpdate Will help update a RadixEnvironment
func (tu *Utils) ApplyEnvironmentUpdate(environmentBuilder utils.EnvironmentBuilder) (*radixv1.RadixEnvironment, error) {
re := environmentBuilder.BuildRE()
rePrev, err := tu.radixclient.RadixV1().RadixEnvironments().Get(context.TODO(), re.GetName(), metav1.GetOptions{})
if err != nil {
return nil, err
}
re.Status = rePrev.Status
re, err = tu.radixclient.RadixV1().RadixEnvironments().Update(context.TODO(), re, metav1.UpdateOptions{})
if err != nil {
return nil, err
}
return re, nil
}
// SetRequiredEnvironmentVariables Sets the required environment
// variables needed for the operator to run properly
func SetRequiredEnvironmentVariables() {
os.Setenv(defaults.OperatorDefaultUserGroupEnvironmentVariable, "1234-5678-91011")
os.Setenv(defaults.OperatorDNSZoneEnvironmentVariable, dnsZone)
os.Setenv(defaults.OperatorAppAliasBaseURLEnvironmentVariable, "app.dev.radix.equinor.com")
os.Setenv(defaults.OperatorEnvLimitDefaultMemoryEnvironmentVariable, "300M")
os.Setenv(defaults.OperatorRollingUpdateMaxUnavailable, "25%")
os.Setenv(defaults.OperatorRollingUpdateMaxSurge, "25%")
os.Setenv(defaults.OperatorReadinessProbeInitialDelaySeconds, "5")
os.Setenv(defaults.OperatorReadinessProbePeriodSeconds, "10")
os.Setenv(defaults.OperatorRadixJobSchedulerEnvironmentVariable, "radix-job-scheduler:main-latest")
os.Setenv(defaults.OperatorClusterTypeEnvironmentVariable, "development")
os.Setenv(defaults.OperatorTenantIdEnvironmentVariable, "01234567-8901-2345-6789-012345678901")
os.Setenv(defaults.ContainerRegistryEnvironmentVariable, "any.container.registry")
os.Setenv(defaults.AppContainerRegistryEnvironmentVariable, "any.app.container.registry")
}
// CreateClusterPrerequisites Will do the needed setup which is part of radix boot
func (tu *Utils) CreateClusterPrerequisites(clustername, egressIps, subscriptionId string) error {
SetRequiredEnvironmentVariables()
if _, err := tu.client.CoreV1().Secrets(corev1.NamespaceDefault).Create(
context.TODO(),
&corev1.Secret{
Type: "Opaque",
ObjectMeta: metav1.ObjectMeta{
Name: "radix-known-hosts-git",
Namespace: corev1.NamespaceDefault,
},
Data: map[string][]byte{
"known_hosts": []byte("abcd"),
},
},
metav1.CreateOptions{}); err != nil {
return err
}
_, err := tu.client.CoreV1().ConfigMaps(corev1.NamespaceDefault).Create(
context.TODO(),
&corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: "radix-config",
Namespace: corev1.NamespaceDefault,
},
Data: map[string]string{
"clustername": clustername,
"clusterActiveEgressIps": egressIps,
"subscriptionId": subscriptionId,
},
},
metav1.CreateOptions{})
return err
}
// CreateAppNamespace Helper method to creat app namespace
func CreateAppNamespace(kubeclient kubernetes.Interface, appName string) string {
ns := utils.GetAppNamespace(appName)
createNamespace(kubeclient, appName, "app", ns)
return ns
}
// CreateEnvNamespace Helper method to creat env namespace
func CreateEnvNamespace(kubeclient kubernetes.Interface, appName, environment string) string {
ns := utils.GetEnvironmentNamespace(appName, environment)
createNamespace(kubeclient, appName, environment, ns)
return ns
}
func createNamespace(kubeclient kubernetes.Interface, appName, envName, ns string) {
namespace := corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: ns,
Labels: map[string]string{
kube.RadixAppLabel: appName,
kube.RadixEnvLabel: envName,
},
},
}
if _, err := kubeclient.CoreV1().Namespaces().Create(context.TODO(), &namespace, metav1.CreateOptions{}); err != nil {
log.Error(err)
}
}
// IntPtr Helper function to get the pointer of an int
func IntPtr(i int) *int {
return &i
}
func (tu *Utils) applyRadixDeploymentEnvVarsConfigMaps(rd *radixv1.RadixDeployment) map[string]*corev1.ConfigMap {
envVarConfigMapsMap := map[string]*corev1.ConfigMap{}
for _, deployComponent := range rd.Spec.Components {
envVarConfigMapsMap[deployComponent.GetName()] = tu.ensurePopulatedEnvVarsConfigMaps(rd, &deployComponent)
}
for _, deployJoyComponent := range rd.Spec.Jobs {
envVarConfigMapsMap[deployJoyComponent.GetName()] = tu.ensurePopulatedEnvVarsConfigMaps(rd, &deployJoyComponent)
}
return envVarConfigMapsMap
}
func (tu *Utils) ensurePopulatedEnvVarsConfigMaps(rd *radixv1.RadixDeployment, deployComponent radixv1.RadixCommonDeployComponent) *corev1.ConfigMap {
initialEnvVarsConfigMap, _, _ := tu.kubeUtil.GetOrCreateEnvVarsConfigMapAndMetadataMap(rd.GetNamespace(), rd.Spec.AppName,
deployComponent.GetName())
desiredConfigMap := initialEnvVarsConfigMap.DeepCopy()
for envVarName, envVarValue := range deployComponent.GetEnvironmentVariables() {
if utils.IsRadixEnvVar(envVarName) {
continue
}
desiredConfigMap.Data[envVarName] = envVarValue
}
_ = tu.kubeUtil.ApplyConfigMap(rd.GetNamespace(), initialEnvVarsConfigMap, desiredConfigMap)
return desiredConfigMap
}
// GetRadixAzureKeyVaultObjectTypePtr Gets pointer to RadixAzureKeyVaultObjectType
func GetRadixAzureKeyVaultObjectTypePtr(objectType radixv1.RadixAzureKeyVaultObjectType) *radixv1.RadixAzureKeyVaultObjectType {
return &objectType
}
// GetAzureKeyVaultTypeSecrets Gets secrets with kube.RadixSecretRefTypeLabel and value v1.RadixSecretRefTypeAzureKeyVault
func GetAzureKeyVaultTypeSecrets(secrets *corev1.SecretList) *corev1.SecretList {
var azureKeyVaultSecrets []corev1.Secret
for _, secret := range secrets.Items {
if label, ok := secret.ObjectMeta.Labels[kube.RadixSecretRefTypeLabel]; ok && label == string(radixv1.RadixSecretRefTypeAzureKeyVault) {
azureKeyVaultSecrets = append(azureKeyVaultSecrets, secret)
}
}
return &corev1.SecretList{Items: azureKeyVaultSecrets}
}
// RegisterRadixDNSAliases Register RadixDNSAliases
func RegisterRadixDNSAliases(radixClient radixclient.Interface, radixDNSAliasesMap map[string]DNSAlias) error {
if radixDNSAliasesMap == nil {
return nil
}
for alias, aliasesSpec := range radixDNSAliasesMap {
err := RegisterRadixDNSAliasBySpec(radixClient, alias, aliasesSpec)
if err != nil {
return err
}
}
return nil
}
// RegisterRadixDNSAlias Register RadixDNSAlias by properties
func RegisterRadixDNSAlias(radixClient radixclient.Interface, appName, componentName, envName, alias string) error {
return RegisterRadixDNSAliasBySpec(radixClient, alias, DNSAlias{
AppName: appName,
Environment: envName,
Component: componentName,
})
}
// RegisterRadixDNSAliasBySpec Register RadixDNSAlias by its spec
func RegisterRadixDNSAliasBySpec(radixClient radixclient.Interface, alias string, aliasesSpec DNSAlias) error {
_, err := radixClient.RadixV1().RadixDNSAliases().Create(context.Background(),
&radixv1.RadixDNSAlias{
ObjectMeta: metav1.ObjectMeta{
Name: alias,
Labels: labels.Merge(labels.ForApplicationName(aliasesSpec.AppName), labels.ForComponentName(aliasesSpec.Component), labels.ForEnvironmentName(aliasesSpec.Environment)),
Finalizers: []string{kube.RadixDNSAliasFinalizer},
},
Spec: radixv1.RadixDNSAliasSpec{
AppName: aliasesSpec.AppName,
Environment: aliasesSpec.Environment,
Component: aliasesSpec.Component,
},
}, metav1.CreateOptions{})
return err
}
type DNSAlias struct {
Alias string
AppName string
Environment string
Component string
}