forked from kyma-project/kyma
-
Notifications
You must be signed in to change notification settings - Fork 0
/
create_service_binding_usage.go
146 lines (125 loc) · 5.02 KB
/
create_service_binding_usage.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
package testsuite
import (
"github.com/pkg/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
eventingv1alpha1clientset "knative.dev/eventing/pkg/client/clientset/versioned/typed/eventing/v1alpha1"
messagingv1alpha1clientset "knative.dev/eventing/pkg/client/clientset/versioned/typed/messaging/v1alpha1"
sbuv1alpha1 "github.com/kyma-project/kyma/components/service-binding-usage-controller/pkg/apis/servicecatalog/v1alpha1"
sbuclientset "github.com/kyma-project/kyma/components/service-binding-usage-controller/pkg/client/clientset/versioned/typed/servicecatalog/v1alpha1"
"github.com/kyma-project/kyma/tests/end-to-end/external-solution-integration/pkg/helpers"
"github.com/kyma-project/kyma/tests/end-to-end/external-solution-integration/pkg/retry"
"github.com/kyma-project/kyma/tests/end-to-end/external-solution-integration/pkg/step"
)
const (
applicationName = "application-name"
brokerNamespace = "broker-namespace"
)
// CreateLambdaServiceBindingUsage is a step which creates new ServiceBindingUsage
type CreateLambdaServiceBindingUsage struct {
serviceBindingUsages sbuclientset.ServiceBindingUsageInterface
broker eventingv1alpha1clientset.BrokerInterface
subscription messagingv1alpha1clientset.SubscriptionInterface
name string
serviceBindingName string
lambdaName string
}
var _ step.Step = &CreateLambdaServiceBindingUsage{}
// NewCreateServiceBindingUsage returns new CreateLambdaServiceBindingUsage
func NewCreateServiceBindingUsage(name, serviceBindingName, lambdaName string,
serviceBindingUsages sbuclientset.ServiceBindingUsageInterface,
knativeBroker eventingv1alpha1clientset.BrokerInterface,
knativeSubscription messagingv1alpha1clientset.SubscriptionInterface) *CreateLambdaServiceBindingUsage {
return &CreateLambdaServiceBindingUsage{
serviceBindingUsages: serviceBindingUsages,
broker: knativeBroker,
subscription: knativeSubscription,
name: name,
serviceBindingName: serviceBindingName,
lambdaName: lambdaName,
}
}
// Name returns name name of the step
func (s *CreateLambdaServiceBindingUsage) Name() string {
return "Create service binding usage"
}
// Run executes the step
func (s *CreateLambdaServiceBindingUsage) Run() error {
serviceBindingUsage := &sbuv1alpha1.ServiceBindingUsage{
TypeMeta: metav1.TypeMeta{Kind: "ServiceBindingUsage", APIVersion: sbuv1alpha1.SchemeGroupVersion.String()},
ObjectMeta: metav1.ObjectMeta{
Name: s.name,
Labels: map[string]string{"Function": s.lambdaName, "ServiceBinding": s.serviceBindingName},
},
Spec: sbuv1alpha1.ServiceBindingUsageSpec{
Parameters: &sbuv1alpha1.Parameters{
EnvPrefix: &sbuv1alpha1.EnvPrefix{
Name: "",
},
},
ServiceBindingRef: sbuv1alpha1.LocalReferenceByName{
Name: s.serviceBindingName,
},
UsedBy: sbuv1alpha1.LocalReferenceByKindAndName{
Kind: "serverless-function",
Name: s.lambdaName,
},
},
}
_, err := s.serviceBindingUsages.Create(serviceBindingUsage)
if err != nil {
return err
}
return retry.Do(s.isServiceBindingUsageReady, retry.Attempts(retryAttemptsCount), retry.Delay(retryDelay))
}
func (s *CreateLambdaServiceBindingUsage) isServiceBindingUsageReady() error {
sbu, err := s.serviceBindingUsages.Get(s.name, metav1.GetOptions{})
if err != nil {
return err
}
for _, condition := range sbu.Status.Conditions {
if condition.Type == sbuv1alpha1.ServiceBindingUsageReady {
if condition.Status != sbuv1alpha1.ConditionTrue {
return errors.New("ServiceBinding is not ready")
}
break
}
}
knativeSubscriptionLabels := make(map[string]string)
knativeSubscriptionLabels[applicationName] = s.name
knativeSubscriptionLabels[brokerNamespace] = s.name
if s.subscription != nil {
knativeSubscription, err := s.subscription.List(metav1.ListOptions{
LabelSelector: labels.Set(knativeSubscriptionLabels).String(),
})
if err != nil {
return err
}
if length := len(knativeSubscription.Items); length == 0 || length > 1 {
return errors.Errorf("unexpected number of knative subscriptions were found.\n Number of knative Subscriptions: %d", length)
}
}
return retry.Do(s.isBrokerReady, retry.Attempts(retryAttemptsCount), retry.Delay(retryDelay))
}
func (s *CreateLambdaServiceBindingUsage) isBrokerReady() error {
if s.broker != nil {
knativeBroker, err := s.broker.Get("default", metav1.GetOptions{})
if err != nil {
return err
}
if !knativeBroker.Status.IsReady() {
return errors.Errorf("default knative broker in %s namespace is not ready. Status of Knative Broker: \n %+v", knativeBroker.Namespace, knativeBroker.Status)
}
}
return nil
}
// Cleanup removes all resources that may possibly created by the step
func (s *CreateLambdaServiceBindingUsage) Cleanup() error {
err := s.serviceBindingUsages.Delete(s.name, &metav1.DeleteOptions{})
if err != nil {
return err
}
return helpers.AwaitResourceDeleted(func() (interface{}, error) {
return s.serviceBindingUsages.Get(s.serviceBindingName, metav1.GetOptions{})
})
}