forked from kyma-project/kyma
/
app_broker.go
413 lines (371 loc) · 12.3 KB
/
app_broker.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
package servicecatalog
import (
"time"
"github.com/kubernetes-incubator/service-catalog/pkg/apis/servicecatalog/v1beta1"
"github.com/kubernetes-incubator/service-catalog/pkg/client/clientset_generated/clientset"
appConnectorApi "github.com/kyma-project/kyma/components/application-broker/pkg/apis/applicationconnector/v1alpha1"
appBroker "github.com/kyma-project/kyma/components/application-broker/pkg/client/clientset/versioned"
"github.com/kyma-project/kyma/components/application-operator/pkg/apis/applicationconnector/v1alpha1"
appConnector "github.com/kyma-project/kyma/components/application-operator/pkg/client/clientset/versioned"
bu "github.com/kyma-project/kyma/components/service-binding-usage-controller/pkg/client/clientset/versioned"
"github.com/sirupsen/logrus"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
)
const (
appEnvTester = "app-env-tester"
applicationName = "application-for-testing"
apiServiceID = "api-service-id"
eventsServiceID = "events-service-id"
apiBindingName = "app-binding"
apiBindingUsageName = "app-binding"
gatewayURL = "https://gateway.local"
)
// AppBrokerUpgradeTest tests the Helm Broker business logic after Kyma upgrade phase
type AppBrokerUpgradeTest struct {
ServiceCatalogInterface clientset.Interface
K8sInterface kubernetes.Interface
BUInterface bu.Interface
AppBrokerInterface appBroker.Interface
AppConnectorInterface appConnector.Interface
}
// NewAppBrokerUpgradeTest returns new instance of the AppBrokerUpgradeTest
func NewAppBrokerUpgradeTest(scCli clientset.Interface,
k8sCli kubernetes.Interface,
buCli bu.Interface,
abCli appBroker.Interface,
acCli appConnector.Interface) *AppBrokerUpgradeTest {
return &AppBrokerUpgradeTest{
ServiceCatalogInterface: scCli,
K8sInterface: k8sCli,
BUInterface: buCli,
AppBrokerInterface: abCli,
AppConnectorInterface: acCli,
}
}
type appBrokerFlow struct {
baseFlow
scInterface clientset.Interface
k8sInterface kubernetes.Interface
buInterface bu.Interface
appBrokerInterface appBroker.Interface
appConnectorInterface appConnector.Interface
}
// CreateResources creates resources needed for e2e upgrade test
func (ut *AppBrokerUpgradeTest) CreateResources(stop <-chan struct{}, log logrus.FieldLogger, namespace string) error {
return ut.newFlow(stop, log, namespace).CreateResources()
}
// TestResources tests resources after backup phase
func (ut *AppBrokerUpgradeTest) TestResources(stop <-chan struct{}, log logrus.FieldLogger, namespace string) error {
return ut.newFlow(stop, log, namespace).TestResources()
}
func (ut *AppBrokerUpgradeTest) newFlow(stop <-chan struct{}, log logrus.FieldLogger, namespace string) *appBrokerFlow {
return &appBrokerFlow{
baseFlow: baseFlow{
log: log,
stop: stop,
namespace: namespace,
buInterface: ut.BUInterface,
scInterface: ut.ServiceCatalogInterface,
k8sInterface: ut.K8sInterface,
},
appBrokerInterface: ut.AppBrokerInterface,
appConnectorInterface: ut.AppConnectorInterface,
scInterface: ut.ServiceCatalogInterface,
}
}
func (f *appBrokerFlow) CreateResources() error {
// iterate over steps
for _, fn := range []func() error{
f.createApplication,
f.createApplicationMapping,
f.deployEnvTester,
f.waitForEnvTester,
f.waitForClassAndPlans,
f.createAPIServiceInstance,
f.createEventsServiceInstance,
f.waitForInstances,
f.createAPIServiceBinding,
f.createAPIServiceBindingUsage,
} {
err := fn()
if err != nil {
f.logReport()
return err
}
}
return nil
}
func (f *appBrokerFlow) TestResources() error {
// iterate over steps
for _, fn := range []func() error{
f.verifyEventActivation,
f.verifyEnvTesterHasGatewayInjected,
f.deleteAPIServiceBindingUsage,
f.verifyDeploymentDoesNotContainGatewayEnvs,
f.deleteAPIServiceBinding,
f.deleteAPIServiceInstance,
f.deleteEventsServiceInstance,
f.waitForInstancesDeleted,
f.deleteApplicationMapping,
f.deleteApplication,
f.waitForClassesRemoved,
} {
err := fn()
if err != nil {
f.logReport()
return err
}
}
return nil
}
func (f *appBrokerFlow) logReport() {
f.logK8SReport()
f.logServiceCatalogAndBindingUsageReport()
f.logApplicationReport()
}
func (f *appBrokerFlow) deleteApplication() error {
return f.appConnectorInterface.ApplicationconnectorV1alpha1().Applications().Delete(applicationName, &metav1.DeleteOptions{})
}
func (f *appBrokerFlow) createApplication() error {
f.log.Info("Creating Application")
_, err := f.appConnectorInterface.ApplicationconnectorV1alpha1().Applications().Create(&v1alpha1.Application{
TypeMeta: metav1.TypeMeta{
Kind: "Application",
APIVersion: "applicationconnector.kyma-project.io/v1alpha1",
},
ObjectMeta: metav1.ObjectMeta{
Name: applicationName,
},
Spec: v1alpha1.ApplicationSpec{
AccessLabel: "app-access-label",
Description: "Application used by application acceptance test",
SkipInstallation: true,
Services: []v1alpha1.Service{
{
ID: apiServiceID,
Name: apiServiceID,
Labels: map[string]string{
"connected-app": "app-name",
},
ProviderDisplayName: "provider",
DisplayName: "Some API",
Description: "Application Service Class with API",
Tags: []string{},
Entries: []v1alpha1.Entry{
{
Type: "API",
AccessLabel: "accessLabel",
GatewayUrl: gatewayURL,
},
},
},
{
ID: eventsServiceID,
Name: eventsServiceID,
Labels: map[string]string{
"connected-app": "app-name",
},
ProviderDisplayName: "provider",
DisplayName: "Some Events",
Description: "Application Service Class with Events",
Tags: []string{},
Entries: []v1alpha1.Entry{
{
Type: "Events",
},
},
},
},
},
})
return err
}
func (f *appBrokerFlow) deployEnvTester() error {
return f.createEnvTester(appEnvTester, "GATEWAY_URL")
}
func (f *appBrokerFlow) createApplicationMapping() error {
f.log.Info("Creating ApplicationMapping")
_, err := f.appBrokerInterface.ApplicationconnectorV1alpha1().ApplicationMappings(f.namespace).Create(&appConnectorApi.ApplicationMapping{
TypeMeta: metav1.TypeMeta{
Kind: "ApplicationMapping",
APIVersion: "applicationconnector.kyma-project.io/v1alpha1",
},
ObjectMeta: metav1.ObjectMeta{
Name: applicationName,
},
})
return err
}
func (f *appBrokerFlow) createAPIServiceInstance() error {
f.log.Infof("Creating API service instance")
_, err := f.scInterface.ServicecatalogV1beta1().ServiceInstances(f.namespace).Create(&v1beta1.ServiceInstance{
TypeMeta: metav1.TypeMeta{
Kind: "ServiceInstance",
APIVersion: "servicecatalog.k8s.io/v1beta1",
},
ObjectMeta: metav1.ObjectMeta{
Name: apiServiceID,
},
Spec: v1beta1.ServiceInstanceSpec{
PlanReference: v1beta1.PlanReference{
ServiceClassExternalName: apiServiceID,
ServicePlanExternalName: "default",
},
},
})
return err
}
func (f *appBrokerFlow) createEventsServiceInstance() error {
f.log.Infof("Creating Events service instance")
_, err := f.scInterface.ServicecatalogV1beta1().ServiceInstances(f.namespace).Create(&v1beta1.ServiceInstance{
TypeMeta: metav1.TypeMeta{
Kind: "ServiceInstance",
APIVersion: "servicecatalog.k8s.io/v1beta1",
},
ObjectMeta: metav1.ObjectMeta{
Name: eventsServiceID,
},
Spec: v1beta1.ServiceInstanceSpec{
PlanReference: v1beta1.PlanReference{
ServiceClassExternalName: eventsServiceID,
ServicePlanExternalName: "default",
},
},
})
return err
}
func (f *appBrokerFlow) createAPIServiceBinding() error {
return f.createBindingAndWaitForReadiness(apiBindingName, apiServiceID)
}
func (f *appBrokerFlow) createAPIServiceBindingUsage() error {
return f.createBindingUsageAndWaitForReadiness(apiBindingUsageName, apiBindingName, appEnvTester)
}
func (f *appBrokerFlow) verifyEventActivation() error {
f.log.Infof("Waiting for the event activation")
_, err := f.appBrokerInterface.ApplicationconnectorV1alpha1().EventActivations(f.namespace).Get(eventsServiceID, metav1.GetOptions{})
return err
}
func (f *appBrokerFlow) verifyEnvTesterHasGatewayInjected() error {
f.log.Info("Waiting for GATEWAY_URL env injected")
return f.waitForEnvInjected(appEnvTester, "GATEWAY_URL", gatewayURL)
}
func (f *appBrokerFlow) verifyDeploymentDoesNotContainGatewayEnvs() error {
f.log.Info("Verify deployment does not contain GATEWAY_URL environment variable")
return f.waitForEnvNotInjected(appEnvTester, "GATEWAY_URL")
}
func (f *appBrokerFlow) deleteAPIServiceBindingUsage() error {
return f.deleteBindingUsage(apiBindingUsageName)
}
func (f *appBrokerFlow) deleteAPIServiceBinding() error {
return f.deleteServiceBinding(apiBindingName)
}
func (f *appBrokerFlow) deleteAPIServiceInstance() error {
return f.deleteServiceInstance(apiServiceID)
}
func (f *appBrokerFlow) deleteEventsServiceInstance() error {
return f.deleteServiceInstance(eventsServiceID)
}
func (f *appBrokerFlow) deleteApplicationMapping() error {
return f.appBrokerInterface.ApplicationconnectorV1alpha1().ApplicationMappings(f.namespace).Delete(applicationName, &metav1.DeleteOptions{})
}
func (f *appBrokerFlow) waitForInstances() error {
f.log.Infof("Waiting for instances to be ready")
err := f.waitForInstance(apiServiceID)
if err != nil {
return err
}
err = f.waitForInstance(eventsServiceID)
return err
}
func (f *appBrokerFlow) waitForClassesRemoved() error {
return f.wait(5*time.Second, func() (bool, error) {
classes, err := f.scInterface.ServicecatalogV1beta1().ServiceClasses(f.namespace).List(metav1.ListOptions{})
if err != nil {
return false, err
}
if len(classes.Items) == 0 {
return true, nil
}
return false, nil
})
}
func (f *appBrokerFlow) waitForClassAndPlans() error {
f.log.Infof("Waiting for classes")
err := f.wait(45*time.Second, func() (bool, error) {
expectedClasses := map[string]struct{}{
apiServiceID: {},
eventsServiceID: {},
}
classes, err := f.scInterface.ServicecatalogV1beta1().ServiceClasses(f.namespace).List(metav1.ListOptions{})
if err != nil {
return false, err
}
for _, class := range classes.Items {
delete(expectedClasses, class.Spec.ExternalName)
}
if len(expectedClasses) == 0 {
return true, nil
}
return false, nil
})
if err != nil {
return err
}
f.log.Infof("Waiting for plans")
return f.wait(10*time.Second, func() (bool, error) {
expectedPlans := map[string]struct{}{
apiServiceID: {},
eventsServiceID: {},
}
plans, err := f.scInterface.ServicecatalogV1beta1().ServicePlans(f.namespace).List(metav1.ListOptions{})
if err != nil {
return false, err
}
for _, plan := range plans.Items {
delete(expectedPlans, plan.Spec.ServiceClassRef.Name)
}
if len(expectedPlans) == 0 {
return true, nil
}
return false, nil
})
}
func (f *appBrokerFlow) waitForClasses() error {
return f.wait(2*time.Minute, func() (bool, error) {
expectedClasses := map[string]struct{}{
apiServiceID: {},
eventsServiceID: {},
}
classes, err := f.scInterface.ServicecatalogV1beta1().ServiceClasses(f.namespace).List(metav1.ListOptions{})
if err != nil {
return false, err
}
for _, class := range classes.Items {
delete(expectedClasses, class.Spec.ExternalName)
}
if len(expectedClasses) == 0 {
return true, nil
}
return false, nil
})
}
func (f *appBrokerFlow) waitForInstancesDeleted() error {
err := f.waitForInstanceRemoved(apiServiceID)
if err != nil {
return err
}
return f.waitForInstanceRemoved(eventsServiceID)
}
func (f *appBrokerFlow) waitForEnvTester() error {
f.log.Info("Waiting for environment variable tester to be ready")
return f.waitForDeployment(appEnvTester)
}
func (f *appBrokerFlow) logApplicationReport() {
appMappings, err := f.appBrokerInterface.ApplicationconnectorV1alpha1().ApplicationMappings(f.namespace).List(metav1.ListOptions{})
f.report("ApplicationMappings", appMappings, err)
eventActivations, err := f.appBrokerInterface.ApplicationconnectorV1alpha1().EventActivations(f.namespace).List(metav1.ListOptions{})
f.report("EventActivations", eventActivations, err)
applications, err := f.appConnectorInterface.ApplicationconnectorV1alpha1().Applications().List(metav1.ListOptions{})
f.report("Applications", applications, err)
}