forked from apache/camel-k
/
collection.go
495 lines (433 loc) · 14.2 KB
/
collection.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
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
/*
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You 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 kubernetes
import (
appsv1 "k8s.io/api/apps/v1"
"k8s.io/api/batch/v1beta1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
ctrl "sigs.k8s.io/controller-runtime/pkg/client"
eventing "knative.dev/eventing/pkg/apis/eventing/v1beta1"
serving "knative.dev/serving/pkg/apis/serving/v1"
monitoringv1 "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1"
routev1 "github.com/openshift/api/route/v1"
v1 "github.com/aanogueira/camel-k/pkg/apis/camel/v1"
)
// A Collection is a container of Kubernetes resources
type Collection struct {
items []ctrl.Object
}
// NewCollection creates a new empty collection
func NewCollection(objects ...ctrl.Object) *Collection {
collection := Collection{
items: make([]ctrl.Object, 0, len(objects)),
}
collection.items = append(collection.items, objects...)
return &collection
}
// Size returns the number of resources belonging to the collection
func (c *Collection) Size() int {
return len(c.items)
}
// Items returns all resources belonging to the collection
func (c *Collection) Items() []ctrl.Object {
return c.items
}
// AsKubernetesList returns all resources wrapped in a Kubernetes list
func (c *Collection) AsKubernetesList() *corev1.List {
lst := corev1.List{
TypeMeta: metav1.TypeMeta{
Kind: "List",
APIVersion: "v1",
},
Items: make([]runtime.RawExtension, 0, len(c.items)),
}
for _, res := range c.items {
raw := runtime.RawExtension{
Object: res,
}
lst.Items = append(lst.Items, raw)
}
return &lst
}
// Add adds a resource to the collection
func (c *Collection) Add(resource ctrl.Object) {
if resource != nil {
c.items = append(c.items, resource)
}
}
// AddFirst adds a resource to the head of the collection
func (c *Collection) AddFirst(resource ctrl.Object) {
if resource != nil {
c.items = append([]ctrl.Object{resource}, c.items...)
}
}
// AddAll adds all resources to the collection
func (c *Collection) AddAll(resource []ctrl.Object) {
c.items = append(c.items, resource...)
}
// VisitDeployment executes the visitor function on all Deployment resources
func (c *Collection) VisitDeployment(visitor func(*appsv1.Deployment)) {
c.Visit(func(res runtime.Object) {
if conv, ok := res.(*appsv1.Deployment); ok {
visitor(conv)
}
})
}
// VisitDeploymentE executes the visitor function on all Deployment resources
func (c *Collection) VisitDeploymentE(visitor func(*appsv1.Deployment) error) error {
return c.VisitE(func(res runtime.Object) error {
if conv, ok := res.(*appsv1.Deployment); ok {
return visitor(conv)
}
return nil
})
}
// GetDeployment returns a Deployment that matches the given function
func (c *Collection) GetDeployment(filter func(*appsv1.Deployment) bool) *appsv1.Deployment {
var retValue *appsv1.Deployment
c.VisitDeployment(func(re *appsv1.Deployment) {
if filter(re) {
retValue = re
}
})
return retValue
}
// GetDeploymentForIntegration returns a Deployment for the given integration
func (c *Collection) GetDeploymentForIntegration(integration *v1.Integration) *appsv1.Deployment {
if integration == nil {
return nil
}
return c.GetDeployment(func(d *appsv1.Deployment) bool {
return d.ObjectMeta.Labels[v1.IntegrationLabel] == integration.Name
})
}
// HasDeployment returns true if a deployment matching the given condition is present
func (c *Collection) HasDeployment(filter func(*appsv1.Deployment) bool) bool {
return c.GetDeployment(filter) != nil
}
// RemoveDeployment removes and returns a Deployment that matches the given function
func (c *Collection) RemoveDeployment(filter func(*appsv1.Deployment) bool) *appsv1.Deployment {
res := c.Remove(func(res runtime.Object) bool {
if conv, ok := res.(*appsv1.Deployment); ok {
return filter(conv)
}
return false
})
if res == nil {
return nil
}
return res.(*appsv1.Deployment)
}
// VisitConfigMap executes the visitor function on all ConfigMap resources
func (c *Collection) VisitConfigMap(visitor func(*corev1.ConfigMap)) {
c.Visit(func(res runtime.Object) {
if conv, ok := res.(*corev1.ConfigMap); ok {
visitor(conv)
}
})
}
// GetConfigMap returns a ConfigMap that matches the given function
func (c *Collection) GetConfigMap(filter func(*corev1.ConfigMap) bool) *corev1.ConfigMap {
var retValue *corev1.ConfigMap
c.VisitConfigMap(func(re *corev1.ConfigMap) {
if filter(re) {
retValue = re
}
})
return retValue
}
// RemoveConfigMap removes and returns a ConfigMap that matches the given function
func (c *Collection) RemoveConfigMap(filter func(*corev1.ConfigMap) bool) *corev1.ConfigMap {
res := c.Remove(func(res runtime.Object) bool {
if conv, ok := res.(*corev1.ConfigMap); ok {
return filter(conv)
}
return false
})
if res == nil {
return nil
}
return res.(*corev1.ConfigMap)
}
// VisitService executes the visitor function on all Service resources
func (c *Collection) VisitService(visitor func(*corev1.Service)) {
c.Visit(func(res runtime.Object) {
if conv, ok := res.(*corev1.Service); ok {
visitor(conv)
}
})
}
// GetService returns a Service that matches the given function
func (c *Collection) GetService(filter func(*corev1.Service) bool) *corev1.Service {
var retValue *corev1.Service
c.VisitService(func(re *corev1.Service) {
if filter(re) {
retValue = re
}
})
return retValue
}
// GetUserServiceForIntegration returns a user Service for the given integration
func (c *Collection) GetUserServiceForIntegration(integration *v1.Integration) *corev1.Service {
if integration == nil {
return nil
}
return c.GetService(func(s *corev1.Service) bool {
return s.ObjectMeta.Labels != nil &&
s.ObjectMeta.Labels[v1.IntegrationLabel] == integration.Name &&
s.ObjectMeta.Labels["camel.apache.org/service.type"] == v1.ServiceTypeUser
})
}
// GetServiceForIntegration returns a user Service for the given integration
func (c *Collection) GetServiceForIntegration(integration *v1.Integration) *corev1.Service {
if integration == nil {
return nil
}
return c.GetService(func(s *corev1.Service) bool {
return s.ObjectMeta.Labels != nil && s.ObjectMeta.Labels[v1.IntegrationLabel] == integration.Name
})
}
// GetKnativeService returns a knative Service that matches the given function
func (c *Collection) GetKnativeService(filter func(*serving.Service) bool) *serving.Service {
var retValue *serving.Service
c.VisitKnativeService(func(re *serving.Service) {
if filter(re) {
retValue = re
}
})
return retValue
}
// VisitRoute executes the visitor function on all Route resources
func (c *Collection) VisitRoute(visitor func(*routev1.Route)) {
c.Visit(func(res runtime.Object) {
if conv, ok := res.(*routev1.Route); ok {
visitor(conv)
}
})
}
// GetRoute returns a Route that matches the given function
func (c *Collection) GetRoute(filter func(*routev1.Route) bool) *routev1.Route {
var retValue *routev1.Route
c.VisitRoute(func(re *routev1.Route) {
if filter(re) {
retValue = re
}
})
return retValue
}
// GetCronJob returns a CronJob that matches the given function
func (c *Collection) GetCronJob(filter func(job *v1beta1.CronJob) bool) *v1beta1.CronJob {
var retValue *v1beta1.CronJob
c.VisitCronJob(func(re *v1beta1.CronJob) {
if filter(re) {
retValue = re
}
})
return retValue
}
// VisitCronJob executes the visitor function on all CronJob resources
func (c *Collection) VisitCronJob(visitor func(*v1beta1.CronJob)) {
c.Visit(func(res runtime.Object) {
if conv, ok := res.(*v1beta1.CronJob); ok {
visitor(conv)
}
})
}
// VisitCronJobE executes the visitor function on all CronJob resources
func (c *Collection) VisitCronJobE(visitor func(*v1beta1.CronJob) error) error {
return c.VisitE(func(res runtime.Object) error {
if conv, ok := res.(*v1beta1.CronJob); ok {
return visitor(conv)
}
return nil
})
}
// VisitKnativeService executes the visitor function on all Knative serving Service resources
func (c *Collection) VisitKnativeService(visitor func(*serving.Service)) {
c.Visit(func(res runtime.Object) {
if conv, ok := res.(*serving.Service); ok {
visitor(conv)
}
})
}
// VisitKnativeServiceE executes the visitor function on all Knative serving Service resources
func (c *Collection) VisitKnativeServiceE(visitor func(*serving.Service) error) error {
return c.VisitE(func(res runtime.Object) error {
if conv, ok := res.(*serving.Service); ok {
return visitor(conv)
}
return nil
})
}
// VisitKnativeTrigger executes the visitor function on all Knative eventing Trigger resources
func (c *Collection) VisitKnativeTrigger(visitor func(trigger *eventing.Trigger)) {
c.Visit(func(res runtime.Object) {
if conv, ok := res.(*eventing.Trigger); ok {
visitor(conv)
}
})
}
// HasKnativeTrigger returns true if a Knative trigger respecting filter is found
func (c *Collection) HasKnativeTrigger(filter func(trigger *eventing.Trigger) bool) bool {
var retValue *bool
c.VisitKnativeTrigger(func(re *eventing.Trigger) {
if filter(re) {
found := true
retValue = &found
}
})
return retValue != nil && *retValue
}
// GetContainer --
func (c *Collection) GetContainer(filter func(container *corev1.Container) bool) *corev1.Container {
var retValue *corev1.Container
c.VisitContainer(func(container *corev1.Container) {
if filter(container) {
retValue = container
}
})
return retValue
}
// GetContainerByName --
func (c *Collection) GetContainerByName(name string) *corev1.Container {
return c.GetContainer(func(c *corev1.Container) bool {
return c.Name == name
})
}
// VisitContainer executes the visitor function on all Containers inside deployments or other resources
func (c *Collection) VisitContainer(visitor func(container *corev1.Container)) {
c.VisitDeployment(func(d *appsv1.Deployment) {
for idx := range d.Spec.Template.Spec.Containers {
cntref := &d.Spec.Template.Spec.Containers[idx]
visitor(cntref)
}
})
c.VisitKnativeConfigurationSpec(func(cs *serving.ConfigurationSpec) {
for id := range cs.Template.Spec.Containers {
cntref := &cs.Template.Spec.Containers[id]
visitor(cntref)
}
})
c.VisitCronJob(func(c *v1beta1.CronJob) {
for idx := range c.Spec.JobTemplate.Spec.Template.Spec.Containers {
cntref := &c.Spec.JobTemplate.Spec.Template.Spec.Containers[idx]
visitor(cntref)
}
})
}
// GetController returns the controller associated with the integration (e.g. Deployment, Knative Service or CronJob)
func (c *Collection) GetController(filter func(object ctrl.Object) bool) ctrl.Object {
d := c.GetDeployment(func(deployment *appsv1.Deployment) bool {
return filter(deployment)
})
if d != nil {
return d
}
svc := c.GetKnativeService(func(service *serving.Service) bool {
return filter(service)
})
if svc != nil {
return svc
}
cj := c.GetCronJob(func(job *v1beta1.CronJob) bool {
return filter(job)
})
if cj != nil {
return cj
}
return nil
}
// VisitPodSpec executes the visitor function on all PodSpec inside deployments or other resources
func (c *Collection) VisitPodSpec(visitor func(container *corev1.PodSpec)) {
c.VisitDeployment(func(d *appsv1.Deployment) {
visitor(&d.Spec.Template.Spec)
})
c.VisitKnativeConfigurationSpec(func(cs *serving.ConfigurationSpec) {
visitor(&cs.Template.Spec.PodSpec)
})
c.VisitCronJob(func(d *v1beta1.CronJob) {
visitor(&d.Spec.JobTemplate.Spec.Template.Spec)
})
}
// VisitPodTemplateMeta executes the visitor function on all PodTemplate metadata inside deployments or other resources
func (c *Collection) VisitPodTemplateMeta(visitor func(meta *metav1.ObjectMeta)) {
c.VisitDeployment(func(d *appsv1.Deployment) {
visitor(&d.Spec.Template.ObjectMeta)
})
c.VisitKnativeConfigurationSpec(func(cs *serving.ConfigurationSpec) {
visitor(&cs.Template.ObjectMeta)
})
c.VisitCronJob(func(d *v1beta1.CronJob) {
visitor(&d.Spec.JobTemplate.Spec.Template.ObjectMeta)
})
}
// VisitKnativeConfigurationSpec executes the visitor function on all knative ConfigurationSpec inside serving Services
func (c *Collection) VisitKnativeConfigurationSpec(visitor func(container *serving.ConfigurationSpec)) {
c.VisitKnativeService(func(s *serving.Service) {
visitor(&s.Spec.ConfigurationSpec)
})
}
// VisitMetaObject executes the visitor function on all meta.Object resources
func (c *Collection) VisitMetaObject(visitor func(metav1.Object)) {
c.Visit(func(res runtime.Object) {
if conv, ok := res.(metav1.Object); ok {
visitor(conv)
}
})
}
// Visit executes the visitor function on all resources
func (c *Collection) Visit(visitor func(runtime.Object)) {
for _, res := range c.items {
visitor(res)
}
}
// VisitE executes the visitor function on all resources breaking if the visitor function
// returns an error
func (c *Collection) VisitE(visitor func(runtime.Object) error) error {
for _, res := range c.items {
if err := visitor(res); err != nil {
return err
}
}
return nil
}
// Remove removes the given element from the collection and returns it
func (c *Collection) Remove(selector func(runtime.Object) bool) runtime.Object {
for idx, res := range c.items {
if selector(res) {
c.items = append(c.items[0:idx], c.items[idx+1:]...)
return res
}
}
return nil
}
func (c *Collection) VisitPodMonitor(visitor func(*monitoringv1.PodMonitor)) {
c.Visit(func(res runtime.Object) {
if conv, ok := res.(*monitoringv1.PodMonitor); ok {
visitor(conv)
}
})
}
func (c *Collection) GetPodMonitor(filter func(*monitoringv1.PodMonitor) bool) *monitoringv1.PodMonitor {
var retValue *monitoringv1.PodMonitor
c.VisitPodMonitor(func(podMonitor *monitoringv1.PodMonitor) {
if filter(podMonitor) {
retValue = podMonitor
}
})
return retValue
}