forked from knative/eventing
/
kube.go
451 lines (413 loc) · 12.2 KB
/
kube.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
447
448
449
450
451
/*
Copyright 2019 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 resources
// This file contains functions that construct common Kubernetes resources.
import (
"encoding/json"
"fmt"
corev1 "k8s.io/api/core/v1"
rbacv1 "k8s.io/api/rbac/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/intstr"
"k8s.io/apimachinery/pkg/util/uuid"
pkgTest "knative.dev/pkg/test"
)
// PodOption enables further configuration of a Pod.
type PodOption func(*corev1.Pod)
// Option enables further configuration of a Role.
type RoleOption func(*rbacv1.Role)
// EventSenderPod creates a Pod that sends a single event to the given address.
func EventSenderPod(name string, sink string, event *CloudEvent) (*corev1.Pod, error) {
return eventSenderPodImage("sendevents", name, sink, event, false)
}
// EventSenderTracingPod creates a Pod that sends a single event to the given address.
func EventSenderTracingPod(name string, sink string, event *CloudEvent) (*corev1.Pod, error) {
return eventSenderPodImage("sendevents", name, sink, event, true)
}
func eventSenderPodImage(imageName string, name string, sink string, event *CloudEvent, addTracing bool) (*corev1.Pod, error) {
if event.Encoding == "" {
event.Encoding = CloudEventEncodingBinary
}
eventExtensionsBytes, error := json.Marshal(event.Extensions)
eventExtensions := string(eventExtensionsBytes)
if error != nil {
return nil, fmt.Errorf("encountered error when we marshall cloud event extensions %v", error)
}
args := []string{
"-event-id",
event.ID,
"-event-type",
event.Type,
"-event-source",
event.Source,
"-event-extensions",
eventExtensions,
"-event-data",
event.Data,
"-event-encoding",
event.Encoding,
"-sink",
sink,
}
if addTracing {
args = append(args, "-add-tracing")
}
return &corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: name,
},
Spec: corev1.PodSpec{
Containers: []corev1.Container{{
Name: imageName,
Image: pkgTest.ImagePath(imageName),
ImagePullPolicy: corev1.PullAlways,
Args: args,
}},
//TODO restart on failure?
RestartPolicy: corev1.RestartPolicyNever,
},
}, nil
}
// EventLoggerPod creates a Pod that logs events received.
func EventLoggerPod(name string) *corev1.Pod {
return eventLoggerPod("logevents", name)
}
// EventDetailsPod creates a Pod that validates events received and log details about events.
func EventDetailsPod(name string) *corev1.Pod {
return eventLoggerPod("eventdetails", name)
}
func eventLoggerPod(imageName string, name string) *corev1.Pod {
return &corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Labels: map[string]string{"e2etest": string(uuid.NewUUID())},
},
Spec: corev1.PodSpec{
Containers: []corev1.Container{{
Name: imageName,
Image: pkgTest.ImagePath(imageName),
ImagePullPolicy: corev1.PullAlways,
}},
RestartPolicy: corev1.RestartPolicyAlways,
},
}
}
// EventTransformationPod creates a Pod that transforms events received.
func EventTransformationPod(name string, event *CloudEvent) *corev1.Pod {
const imageName = "transformevents"
return &corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Labels: map[string]string{"e2etest": string(uuid.NewUUID())},
},
Spec: corev1.PodSpec{
Containers: []corev1.Container{{
Name: imageName,
Image: pkgTest.ImagePath(imageName),
ImagePullPolicy: corev1.PullAlways,
Args: []string{
"-event-type",
event.Type,
"-event-source",
event.Source,
"-event-data",
event.Data,
},
}},
RestartPolicy: corev1.RestartPolicyAlways,
},
}
}
// HelloWorldPod creates a Pod that logs "Hello, World!".
func HelloWorldPod(name string, options ...PodOption) *corev1.Pod {
const imageName = "print"
pod := &corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: name,
},
Spec: corev1.PodSpec{
Containers: []corev1.Container{{
Name: imageName,
Image: pkgTest.ImagePath(imageName),
ImagePullPolicy: corev1.PullAlways,
}},
RestartPolicy: corev1.RestartPolicyNever,
},
}
for _, option := range options {
option(pod)
}
return pod
}
// WithLabelsForPod returns an option setting the pod labels
func WithLabelsForPod(labels map[string]string) PodOption {
return func(p *corev1.Pod) {
p.Labels = labels
}
}
// SequenceStepperPod creates a Pod that can be used as a step in testing Sequence.
// Note event data used in the test must be CloudEventBaseData, and this Pod as a Subscriber will receive the event,
// and return a new event with eventMsgAppender added to data.Message.
func SequenceStepperPod(name, eventMsgAppender string) *corev1.Pod {
const imageName = "sequencestepper"
return &corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Labels: map[string]string{"e2etest": string(uuid.NewUUID())},
},
Spec: corev1.PodSpec{
Containers: []corev1.Container{{
Name: imageName,
Image: pkgTest.ImagePath(imageName),
ImagePullPolicy: corev1.PullAlways,
Args: []string{
"-msg-appender",
eventMsgAppender,
},
}},
RestartPolicy: corev1.RestartPolicyAlways,
},
}
}
// EventFilteringPod creates a Pod that either filter or send the received CloudEvent
func EventFilteringPod(name string, filter bool) *corev1.Pod {
const imageName = "filterevents"
pod := &corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Labels: map[string]string{"e2etest": string(uuid.NewUUID())},
},
Spec: corev1.PodSpec{
Containers: []corev1.Container{{
Name: imageName,
Image: pkgTest.ImagePath(imageName),
ImagePullPolicy: corev1.PullAlways,
}},
RestartPolicy: corev1.RestartPolicyAlways,
},
}
if filter {
pod.Spec.Containers[0].Args = []string{"-filter"}
}
return pod
}
const (
PerfConsumerService = "perf-consumer"
PerfAggregatorService = "perf-aggregator"
PerfServiceAccount = "perf-eventing"
)
func PerformanceConsumerService() *corev1.Service {
return Service(
PerfConsumerService,
map[string]string{"role": "perf-consumer"},
[]corev1.ServicePort{{
Protocol: corev1.ProtocolTCP,
Port: 80,
TargetPort: intstr.FromString("cloudevents"),
Name: "http",
}},
)
}
func PerformanceAggregatorService() *corev1.Service {
return Service(
PerfAggregatorService,
map[string]string{"role": "perf-aggregator"},
[]corev1.ServicePort{{
Protocol: corev1.ProtocolTCP,
Port: 10000,
TargetPort: intstr.FromString("grpc"),
Name: "grpc",
}},
)
}
func PerformanceImageReceiverPod(imageName string, pace string, warmup string, aggregatorHostname string, additionalArgs ...string) *corev1.Pod {
const podName = "perf-receiver"
args := append([]string{
"--roles=receiver",
fmt.Sprintf("--pace=%s", pace),
fmt.Sprintf("--warmup=%s", warmup),
fmt.Sprintf("--aggregator=%s:10000", aggregatorHostname),
}, additionalArgs...)
return &corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: podName,
Labels: map[string]string{
"role": "perf-consumer",
},
},
Spec: corev1.PodSpec{
ServiceAccountName: PerfServiceAccount,
RestartPolicy: corev1.RestartPolicyNever,
Containers: []corev1.Container{{
Name: "receiver",
Image: pkgTest.ImagePath(imageName),
Args: args,
Env: []corev1.EnvVar{{
Name: "POD_NAME",
ValueFrom: &corev1.EnvVarSource{
FieldRef: &corev1.ObjectFieldSelector{
FieldPath: "metadata.name",
},
},
}, {
Name: "POD_NAMESPACE",
ValueFrom: &corev1.EnvVarSource{
FieldRef: &corev1.ObjectFieldSelector{
FieldPath: "metadata.namespace",
},
},
}},
Ports: []corev1.ContainerPort{{
Name: "cloudevents",
ContainerPort: 8080,
}},
}},
},
}
}
func PerformanceImageAggregatorPod(expectedRecords int, publish bool, additionalArgs ...string) *corev1.Pod {
const podName = "perf-aggregator"
const imageName = "performance"
args := append([]string{
"--roles=aggregator",
fmt.Sprintf("--publish=%v", publish),
fmt.Sprintf("--expect-records=%d", expectedRecords),
}, additionalArgs...)
return &corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: podName,
Labels: map[string]string{
"role": "perf-aggregator",
},
},
Spec: corev1.PodSpec{
ServiceAccountName: PerfServiceAccount,
RestartPolicy: corev1.RestartPolicyNever,
Containers: []corev1.Container{{
Name: "aggregator",
Image: pkgTest.ImagePath(imageName),
Args: args,
Env: []corev1.EnvVar{{
Name: "POD_NAMESPACE",
ValueFrom: &corev1.EnvVarSource{
FieldRef: &corev1.ObjectFieldSelector{
FieldPath: "metadata.namespace",
},
},
}},
TerminationMessagePolicy: corev1.TerminationMessageFallbackToLogsOnError,
Ports: []corev1.ContainerPort{{
Name: "grpc",
ContainerPort: 10000,
}},
}},
},
}
}
// Service creates a Kubernetes Service with the given name, selector and ports
func Service(name string, selector map[string]string, ports []corev1.ServicePort) *corev1.Service {
return &corev1.Service{
ObjectMeta: metav1.ObjectMeta{
Name: name,
},
Spec: corev1.ServiceSpec{
Selector: selector,
Ports: ports,
},
}
}
// Service creates a Kubernetes Service with the given name, namespace, and
// selector. Port 8080 is set as the target port.
func ServiceDefaultHTTP(name string, selector map[string]string) *corev1.Service {
return Service(name, selector, []corev1.ServicePort{{
Name: "http",
Port: 80,
Protocol: corev1.ProtocolTCP,
TargetPort: intstr.FromInt(8080),
}})
}
// ServiceRef returns a Service ObjectReference for a given Service name.
func ServiceRef(name string) *corev1.ObjectReference {
return pkgTest.CoreV1ObjectReference(ServiceKind, CoreAPIVersion, name)
}
// ServiceAccount creates a Kubernetes ServiceAccount with the given name and namespace.
func ServiceAccount(name, namespace string) *corev1.ServiceAccount {
return &corev1.ServiceAccount{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: namespace,
},
}
}
// RoleBinding creates a Kubernetes RoleBinding with the given ServiceAccount name and
// namespace, Role or ClusterRole Kind, name, RoleBinding name and namespace.
func RoleBinding(saName, saNamespace, rKind, rName, rbName, rbNamespace string) *rbacv1.RoleBinding {
return &rbacv1.RoleBinding{
ObjectMeta: metav1.ObjectMeta{
Name: rbName,
Namespace: rbNamespace,
},
Subjects: []rbacv1.Subject{
{
Kind: "ServiceAccount",
Name: saName,
Namespace: saNamespace,
},
},
RoleRef: rbacv1.RoleRef{
Kind: rKind,
Name: rName,
APIGroup: rbacv1.SchemeGroupVersion.Group,
},
}
}
// ClusterRoleBinding creates a Kubernetes ClusterRoleBinding with the given ServiceAccount name and
// namespace, ClusterRole name, ClusterRoleBinding name.
func ClusterRoleBinding(saName, saNamespace, crName, crbName string) *rbacv1.ClusterRoleBinding {
return &rbacv1.ClusterRoleBinding{
ObjectMeta: metav1.ObjectMeta{
Name: crbName,
},
Subjects: []rbacv1.Subject{
{
Kind: "ServiceAccount",
Name: saName,
Namespace: saNamespace,
},
},
RoleRef: rbacv1.RoleRef{
Kind: "ClusterRole",
Name: crName,
APIGroup: rbacv1.SchemeGroupVersion.Group,
},
}
}
// EventWatcherRole creates a Kubernetes Role
func Role(rName string, options ...RoleOption) *rbacv1.Role {
role := &rbacv1.Role{
ObjectMeta: metav1.ObjectMeta{
Name: rName,
},
Rules: []rbacv1.PolicyRule{},
}
for _, option := range options {
option(role)
}
return role
}
// WithRuleForRole is a Role Option for adding a rule
func WithRuleForRole(rule *rbacv1.PolicyRule) RoleOption {
return func(r *rbacv1.Role) {
r.Rules = append(r.Rules, *rule)
}
}