/
inmemorychannel.go
192 lines (161 loc) · 5.71 KB
/
inmemorychannel.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
/*
Copyright 2020 The Knative Authors
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 testing
import (
"context"
"time"
"k8s.io/apimachinery/pkg/types"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
duckv1beta1 "knative.dev/eventing/pkg/apis/duck/v1beta1"
"knative.dev/eventing/pkg/apis/eventing"
"knative.dev/eventing/pkg/apis/messaging"
"knative.dev/eventing/pkg/apis/messaging/v1beta1"
"knative.dev/pkg/apis"
)
// InMemoryChannelOption enables further configuration of a v1beta1.InMemoryChannel.
type InMemoryChannelOption func(*v1beta1.InMemoryChannel)
// NewInMemoryChannel creates a v1beta1.InMemoryChannel with InMemoryChannelOption .
func NewInMemoryChannel(name, namespace string, imcopt ...InMemoryChannelOption) *v1beta1.InMemoryChannel {
imc := &v1beta1.InMemoryChannel{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: namespace,
Annotations: map[string]string{messaging.SubscribableDuckVersionAnnotation: "v1beta1"},
},
Spec: v1beta1.InMemoryChannelSpec{},
}
for _, opt := range imcopt {
opt(imc)
}
imc.SetDefaults(context.Background())
return imc
}
func WithInitInMemoryChannelConditions(imc *v1beta1.InMemoryChannel) {
imc.Status.InitializeConditions()
}
func WithInMemoryChannelGeneration(gen int64) InMemoryChannelOption {
return func(s *v1beta1.InMemoryChannel) {
s.Generation = gen
}
}
func WithInMemoryChannelStatusObservedGeneration(gen int64) InMemoryChannelOption {
return func(s *v1beta1.InMemoryChannel) {
s.Status.ObservedGeneration = gen
}
}
func WithInMemoryChannelDeleted(imc *v1beta1.InMemoryChannel) {
deleteTime := metav1.NewTime(time.Unix(1e9, 0))
imc.ObjectMeta.SetDeletionTimestamp(&deleteTime)
}
func WithInMemoryChannelSubscribers(subscribers []duckv1beta1.SubscriberSpec) InMemoryChannelOption {
return func(imc *v1beta1.InMemoryChannel) {
imc.Spec.Subscribers = subscribers
}
}
func WithInMemoryChannelDeploymentFailed(reason, message string) InMemoryChannelOption {
return func(imc *v1beta1.InMemoryChannel) {
imc.Status.MarkDispatcherFailed(reason, message)
}
}
func WithInMemoryChannelDeploymentUnknown(reason, message string) InMemoryChannelOption {
return func(imc *v1beta1.InMemoryChannel) {
imc.Status.MarkDispatcherUnknown(reason, message)
}
}
func WithInMemoryChannelDeploymentReady() InMemoryChannelOption {
return func(imc *v1beta1.InMemoryChannel) {
imc.Status.PropagateDispatcherStatus(&appsv1.DeploymentStatus{Conditions: []appsv1.DeploymentCondition{{Type: appsv1.DeploymentAvailable, Status: corev1.ConditionTrue}}})
}
}
func WithInMemoryChannelServicetNotReady(reason, message string) InMemoryChannelOption {
return func(imc *v1beta1.InMemoryChannel) {
imc.Status.MarkServiceFailed(reason, message)
}
}
func WithInMemoryChannelServiceReady() InMemoryChannelOption {
return func(imc *v1beta1.InMemoryChannel) {
imc.Status.MarkServiceTrue()
}
}
func WithInMemoryChannelChannelServiceNotReady(reason, message string) InMemoryChannelOption {
return func(imc *v1beta1.InMemoryChannel) {
imc.Status.MarkChannelServiceFailed(reason, message)
}
}
func WithInMemoryChannelChannelServiceReady() InMemoryChannelOption {
return func(imc *v1beta1.InMemoryChannel) {
imc.Status.MarkChannelServiceTrue()
}
}
func WithInMemoryChannelEndpointsNotReady(reason, message string) InMemoryChannelOption {
return func(imc *v1beta1.InMemoryChannel) {
imc.Status.MarkEndpointsFailed(reason, message)
}
}
func WithInMemoryChannelEndpointsReady() InMemoryChannelOption {
return func(imc *v1beta1.InMemoryChannel) {
imc.Status.MarkEndpointsTrue()
}
}
func WithInMemoryChannelAddress(a string) InMemoryChannelOption {
return func(imc *v1beta1.InMemoryChannel) {
imc.Status.SetAddress(&apis.URL{
Scheme: "http",
Host: a,
})
}
}
func WithInMemoryChannelReady(host string) InMemoryChannelOption {
return func(imc *v1beta1.InMemoryChannel) {
imc.Status.SetAddress(&apis.URL{
Scheme: "http",
Host: host,
})
imc.Status.MarkChannelServiceTrue()
imc.Status.MarkEndpointsTrue()
imc.Status.MarkServiceTrue()
}
}
func WithInMemoryChannelReadySubscriber(uid string) InMemoryChannelOption {
return WithInMemoryChannelReadySubscriberAndGeneration(uid, 0)
}
func WithInMemoryChannelReadySubscriberAndGeneration(uid string, observedGeneration int64) InMemoryChannelOption {
return func(c *v1beta1.InMemoryChannel) {
c.Status.Subscribers = append(c.Status.Subscribers, duckv1beta1.SubscriberStatus{
UID: types.UID(uid),
ObservedGeneration: observedGeneration,
Ready: corev1.ConditionTrue,
})
}
}
func WithInMemoryChannelStatusSubscribers(subscriberStatuses []duckv1beta1.SubscriberStatus) InMemoryChannelOption {
return func(imc *v1beta1.InMemoryChannel) {
imc.Status.Subscribers = subscriberStatuses
}
}
func WithInMemoryChannelDuckAnnotationV1Beta1(imc *v1beta1.InMemoryChannel) {
annotations := map[string]string{
messaging.SubscribableDuckVersionAnnotation: "v1beta1",
}
imc.ObjectMeta.SetAnnotations(annotations)
}
func WithInMemoryScopeAnnotation(value string) InMemoryChannelOption {
return func(imc *v1beta1.InMemoryChannel) {
if imc.Annotations == nil {
imc.Annotations = make(map[string]string)
}
imc.Annotations[eventing.ScopeAnnotationKey] = value
}
}