-
Notifications
You must be signed in to change notification settings - Fork 583
/
kube.go
304 lines (274 loc) · 7.97 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
/*
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 (
"fmt"
v1 "knative.dev/pkg/apis/duck/v1"
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"
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)
// 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.PullIfNotPresent,
}},
RestartPolicy: corev1.RestartPolicyAlways,
},
}
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
}
}
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)
}
// ServiceKRef returns a Service ObjectReference for a given Service name.
func ServiceKRef(name string) *v1.KReference {
ref := pkgTest.CoreV1ObjectReference(ServiceKind, CoreAPIVersion, name)
return &v1.KReference{
Kind: ref.Kind,
Namespace: ref.Namespace,
Name: ref.Name,
APIVersion: ref.APIVersion,
}
}
// 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)
}
}