/
event_listener_types.go
343 lines (302 loc) · 11.8 KB
/
event_listener_types.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
/*
Copyright 2021 The Tekton 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 v1beta1
import (
"fmt"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"knative.dev/pkg/apis"
duckv1 "knative.dev/pkg/apis/duck/v1"
"knative.dev/pkg/apis/duck/v1beta1"
"knative.dev/pkg/kmeta"
)
// Check that EventListener may be validated and defaulted.
var _ apis.Validatable = (*EventListener)(nil)
var _ apis.Defaultable = (*EventListener)(nil)
// +genclient
// +genreconciler:krshapedlogic=false
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
// EventListener exposes a service to accept HTTP event payloads.
//
// +k8s:openapi-gen=true
type EventListener struct {
metav1.TypeMeta `json:",inline"`
// +optional
metav1.ObjectMeta `json:"metadata,omitempty"`
// Spec holds the desired state of the EventListener from the client
// +optional
Spec EventListenerSpec `json:"spec"`
// +optional
Status EventListenerStatus `json:"status,omitempty"`
}
var _ kmeta.OwnerRefable = (*EventListener)(nil)
// EventListenerSpec defines the desired state of the EventListener, represented
// by a list of Triggers.
type EventListenerSpec struct {
ServiceAccountName string `json:"serviceAccountName,omitempty"`
// +listType=atomic
Triggers []EventListenerTrigger `json:"triggers,omitempty"`
// Trigger groups allow for centralized processing of an interceptor chain
// +listType=atomic
TriggerGroups []EventListenerTriggerGroup `json:"triggerGroups,omitempty"`
NamespaceSelector NamespaceSelector `json:"namespaceSelector,omitempty"`
LabelSelector *metav1.LabelSelector `json:"labelSelector,omitempty"`
Resources Resources `json:"resources,omitempty"`
CloudEventURI string `json:"cloudEventURI,omitempty"`
}
type Resources struct {
KubernetesResource *KubernetesResource `json:"kubernetesResource,omitempty"`
CustomResource *CustomResource `json:"customResource,omitempty"`
}
type CustomResource struct {
runtime.RawExtension `json:",inline"`
}
type KubernetesResource struct {
Replicas *int32 `json:"replicas,omitempty"`
ServiceType corev1.ServiceType `json:"serviceType,omitempty"`
ServicePort *int32 `json:"servicePort,omitempty"`
ServiceLoadBalancerClass *string `json:"serviceLoadBalancerClass,omitempty"`
duckv1.WithPodSpec `json:"spec,omitempty"`
}
// EventListenerTrigger represents a connection between TriggerBinding, Params,
// and TriggerTemplate; TriggerBinding provides extracted values for
// TriggerTemplate to then create resources from. TriggerRef can also be
// provided instead of TriggerBinding, Interceptors and TriggerTemplate
type EventListenerTrigger struct {
// +listType=atomic
Bindings []*EventListenerBinding `json:"bindings,omitempty"`
Template *EventListenerTemplate `json:"template,omitempty"`
TriggerRef string `json:"triggerRef,omitempty"`
// +optional
Name string `json:"name,omitempty"`
// +listType=atomic
Interceptors []*EventInterceptor `json:"interceptors,omitempty"`
// ServiceAccountName optionally associates credentials with each trigger;
// more granular authorization for
// who is allowed to utilize the associated pipeline
// vs. defaulting to whatever permissions are associated
// with the entire EventListener and associated sink facilitates
// multi-tenant model based scenarios
// +optional
ServiceAccountName string `json:"serviceAccountName,omitempty"`
}
// EventListenerTriggerGroup defines a group of Triggers that share a common set of interceptors
type EventListenerTriggerGroup struct {
Name string `json:"name"`
// +listType=atomic
Interceptors []*TriggerInterceptor `json:"interceptors"`
TriggerSelector EventListenerTriggerSelector `json:"triggerSelector"`
}
// EventListenerTriggerSelector defines ways to select a group of triggers using their metadata
type EventListenerTriggerSelector struct {
NamespaceSelector NamespaceSelector `json:"namespaceSelector,omitempty"`
LabelSelector *metav1.LabelSelector `json:"labelSelector,omitempty"`
}
// EventInterceptor provides a hook to intercept and pre-process events
type EventInterceptor = TriggerInterceptor
// SecretRef contains the information required to reference a single secret string
// This is needed because the other secretRef types are not cross-namespace and do not
// actually contain the "SecretName" field, which allows us to access a single secret value.
type SecretRef struct {
SecretKey string `json:"secretKey,omitempty"`
SecretName string `json:"secretName,omitempty"`
}
// EventListenerBinding refers to a particular TriggerBinding or ClusterTriggerBinding resource.
type EventListenerBinding = TriggerSpecBinding
// EventListenerTemplate refers to a particular TriggerTemplate resource.
type EventListenerTemplate = TriggerSpecTemplate
// EventListenerList contains a list of TriggerBinding
//
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
type EventListenerList struct {
metav1.TypeMeta `json:",inline"`
// +optional
metav1.ListMeta `json:"metadata,omitempty"`
Items []EventListener `json:"items"`
}
// EventListenerStatus holds the status of the EventListener
// +k8s:deepcopy-gen=true
type EventListenerStatus struct {
duckv1.Status `json:",inline"`
// EventListener is Addressable. It currently exposes the service DNS
// address of the the EventListener sink
v1beta1.AddressStatus `json:",inline"`
// Configuration stores configuration for the EventListener service
Configuration EventListenerConfig `json:"configuration"`
}
// EventListenerConfig stores configuration for resources generated by the
// EventListener
type EventListenerConfig struct {
// GeneratedResourceName is the name given to all resources reconciled by
// the EventListener
GeneratedResourceName string `json:"generatedName"`
}
// NamespaceSelector is a selector for selecting either all namespaces or a
// list of namespaces.
// +k8s:openapi-gen=true
type NamespaceSelector struct {
// List of namespace names.
// +listType=atomic
MatchNames []string `json:"matchNames,omitempty"`
}
// The conditions that are internally resolved by the EventListener reconciler
const (
// ServiceExists is the ConditionType set on the EventListener, which
// specifies Service existence.
ServiceExists apis.ConditionType = "Service"
// DeploymentExists is the ConditionType set on the EventListener, which
// specifies Deployment existence.
DeploymentExists apis.ConditionType = "Deployment"
)
// Check that EventListener may be validated and defaulted.
// TriggerBindingKind defines the type of TriggerBinding used by the EventListener.
type TriggerBindingKind string
const (
// NamespacedTriggerBindingKind indicates that triggerbinding type has a namespace scope.
NamespacedTriggerBindingKind TriggerBindingKind = "TriggerBinding"
// ClusterTriggerBindingKind indicates that triggerbinding type has a cluster scope.
ClusterTriggerBindingKind TriggerBindingKind = "ClusterTriggerBinding"
)
var eventListenerCondSet = apis.NewLivingConditionSet(
ServiceExists,
DeploymentExists,
)
// GetGroupVersionKind implements kmeta.OwnerRefable
func (el *EventListener) GetGroupVersionKind() schema.GroupVersionKind {
return SchemeGroupVersion.WithKind("EventListener")
}
// GetCondition returns the Condition matching the given type.
func (els *EventListenerStatus) GetCondition(t apis.ConditionType) *apis.Condition {
return eventListenerCondSet.Manage(els).GetCondition(t)
}
// SetCondition sets the condition, unsetting previous conditions with the same
// type as necessary. This is a local change and needs to be persisted to the
// K8s API elsewhere.
func (els *EventListenerStatus) SetCondition(newCond *apis.Condition) {
if newCond != nil {
// TODO: Should the ConditionManager be set somewhere?
eventListenerCondSet.Manage(els).SetCondition(*newCond)
}
}
func (els *EventListenerStatus) SetReadyCondition() {
for _, ct := range []apis.ConditionType{
ServiceExists,
DeploymentExists,
apis.ConditionType(appsv1.DeploymentProgressing),
apis.ConditionType(appsv1.DeploymentAvailable)} {
if sc := els.GetCondition(ct); sc != nil {
if sc.Status != corev1.ConditionTrue {
els.SetCondition(&apis.Condition{
Type: apis.ConditionReady,
Status: corev1.ConditionFalse,
Message: fmt.Sprintf("Condition %s has status: %s with message: %s", sc.Type, sc.Status, sc.Message),
})
return
}
}
}
els.SetCondition(&apis.Condition{
Type: apis.ConditionReady,
Status: corev1.ConditionTrue,
Message: "EventListener is ready",
})
}
// SetDeploymentConditions sets the Deployment conditions on the EventListener,
// which is a reflection of the actual Deployment status.
func (els *EventListenerStatus) SetDeploymentConditions(deploymentConditions []appsv1.DeploymentCondition) {
// Manually remove the DeploymentReplicaFailure condition since it does
// not always exist and would stay around otherwise
replicaFailureIndex := -1
for i := range els.Conditions {
if els.Conditions[i].Type == apis.ConditionType(appsv1.DeploymentReplicaFailure) {
replicaFailureIndex = i
break
}
}
if replicaFailureIndex != -1 {
els.Conditions = append(els.Conditions[:replicaFailureIndex], els.Conditions[replicaFailureIndex+1:]...)
}
for _, cond := range deploymentConditions {
els.SetCondition(&apis.Condition{
Type: apis.ConditionType(cond.Type),
Status: cond.Status,
Reason: cond.Reason,
Message: cond.Message,
})
}
}
func (els *EventListenerStatus) SetConditionsForDynamicObjects(conditions v1beta1.Conditions) {
for _, cond := range conditions {
els.SetCondition(&apis.Condition{
Type: cond.Type,
Status: cond.Status,
Reason: cond.Reason,
Message: cond.Message,
})
}
els.SetCondition(&apis.Condition{
Type: apis.ConditionReady,
Status: corev1.ConditionTrue,
Message: "EventListener is ready",
})
}
// SetExistsCondition simplifies setting the exists conditions on the
// EventListenerStatus.
func (els *EventListenerStatus) SetExistsCondition(cond apis.ConditionType, err error) {
if err != nil {
els.SetCondition(&apis.Condition{
Type: cond,
Status: corev1.ConditionFalse,
Message: err.Error(),
})
} else {
els.SetCondition(&apis.Condition{
Type: cond,
Status: corev1.ConditionTrue,
Message: fmt.Sprintf("%s exists", cond),
})
}
}
// InitializeConditions will set all conditions in eventListenerCondSet to false
// for the EventListener. This does not use the InitializeCondition() provided
// by the conditionsImpl to avoid setting the happy condition. This is a local
// change and needs to be persisted to the K8s API elsewhere.
func (els *EventListenerStatus) InitializeConditions() {
for _, condition := range []apis.ConditionType{
ServiceExists,
DeploymentExists,
apis.ConditionReady,
} {
els.SetCondition(&apis.Condition{
Type: condition,
Status: corev1.ConditionFalse,
})
}
}
// SetAddress sets the address (as part of Addressable contract) and marks the correct condition.
func (els *EventListenerStatus) SetAddress(hostname string) {
if els.Address == nil {
els.Address = &v1beta1.Addressable{}
}
if hostname != "" {
els.Address.URL = &apis.URL{
Scheme: "http",
Host: hostname,
}
} else {
els.Address.URL = nil
}
}