forked from rancher/rancher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
workload_common.go
792 lines (723 loc) · 23.6 KB
/
workload_common.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
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
package workload
import (
"strconv"
"strings"
"fmt"
"encoding/json"
"github.com/rancher/types/apis/apps/v1beta2"
batchv1 "github.com/rancher/types/apis/batch/v1"
"github.com/rancher/types/apis/batch/v1beta1"
"github.com/rancher/types/apis/core/v1"
"github.com/rancher/types/config"
corev1beta2 "k8s.io/api/apps/v1beta2"
corebatchv1 "k8s.io/api/batch/v1"
corebatchv1beta1 "k8s.io/api/batch/v1beta1"
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/intstr"
)
const (
AppVersion = "apps/v1beta2"
BatchBetaVersion = "batch/v1beta1"
BatchVersion = "batch/v1"
WorkloadAnnotation = "field.cattle.io/targetWorkloadIds"
PortsAnnotation = "field.cattle.io/ports"
ClusterIPServiceType = "ClusterIP"
DeploymentType = "deployment"
ReplicationControllerType = "replicationcontroller"
ReplicaSetType = "replicaset"
DaemonSetType = "daemonset"
StatefulSetType = "statefulset"
JobType = "job"
CronJobType = "cronjob"
WorkloadAnnotatioNoop = "workload.cattle.io/targetWorkloadIdNoop"
WorkloaAnnotationdPortBasedService = "workload.cattle.io/workloadPortBased"
)
var WorkloadKinds = map[string]bool{
"Deployment": true,
"ReplicationController": true,
"ReplicaSet": true,
"DaemonSet": true,
"StatefulSet": true,
"Job": true,
"CronJob": true,
}
type Workload struct {
Name string
Namespace string
UUID types.UID
SelectorLabels map[string]string
Annotations map[string]string
TemplateSpec *corev1.PodTemplateSpec
Kind string
APIVersion string
OwnerReferences []metav1.OwnerReference
Labels map[string]string
Key string
Status *Status
}
type Status struct {
Replicas int32
AvailableReplicas int32
}
type CommonController struct {
DeploymentLister v1beta2.DeploymentLister
ReplicationControllerLister v1.ReplicationControllerLister
ReplicaSetLister v1beta2.ReplicaSetLister
DaemonSetLister v1beta2.DaemonSetLister
StatefulSetLister v1beta2.StatefulSetLister
JobLister batchv1.JobLister
CronJobLister v1beta1.CronJobLister
Deployments v1beta2.DeploymentInterface
ReplicationControllers v1.ReplicationControllerInterface
ReplicaSets v1beta2.ReplicaSetInterface
DaemonSets v1beta2.DaemonSetInterface
StatefulSets v1beta2.StatefulSetInterface
Jobs batchv1.JobInterface
CronJobs v1beta1.CronJobInterface
Sync func(key string, w *Workload) error
}
func NewWorkloadController(workload *config.UserOnlyContext, f func(key string, w *Workload) error) CommonController {
c := CommonController{
DeploymentLister: workload.Apps.Deployments("").Controller().Lister(),
ReplicationControllerLister: workload.Core.ReplicationControllers("").Controller().Lister(),
ReplicaSetLister: workload.Apps.ReplicaSets("").Controller().Lister(),
DaemonSetLister: workload.Apps.DaemonSets("").Controller().Lister(),
StatefulSetLister: workload.Apps.StatefulSets("").Controller().Lister(),
JobLister: workload.BatchV1.Jobs("").Controller().Lister(),
CronJobLister: workload.BatchV1Beta1.CronJobs("").Controller().Lister(),
Deployments: workload.Apps.Deployments(""),
ReplicationControllers: workload.Core.ReplicationControllers(""),
ReplicaSets: workload.Apps.ReplicaSets(""),
DaemonSets: workload.Apps.DaemonSets(""),
StatefulSets: workload.Apps.StatefulSets(""),
Jobs: workload.BatchV1.Jobs(""),
CronJobs: workload.BatchV1Beta1.CronJobs(""),
Sync: f,
}
if f != nil {
workload.Apps.Deployments("").AddHandler(getName(), c.syncDeployments)
workload.Core.ReplicationControllers("").AddHandler(getName(), c.syncReplicationControllers)
workload.Apps.ReplicaSets("").AddHandler(getName(), c.syncReplicaSet)
workload.Apps.DaemonSets("").AddHandler(getName(), c.syncDaemonSet)
workload.Apps.StatefulSets("").AddHandler(getName(), c.syncStatefulSet)
workload.BatchV1.Jobs("").AddHandler(getName(), c.syncJob)
workload.BatchV1Beta1.CronJobs("").AddHandler(getName(), c.syncCronJob)
}
return c
}
func (c *CommonController) syncDeployments(key string, obj *corev1beta2.Deployment) error {
if obj == nil || obj.DeletionTimestamp != nil {
return nil
}
w, err := c.getWorkload(key, DeploymentType)
if err != nil || w == nil {
return err
}
return c.Sync(key, w)
}
func (c *CommonController) syncReplicationControllers(key string, obj *corev1.ReplicationController) error {
if obj == nil || obj.DeletionTimestamp != nil {
return nil
}
w, err := c.getWorkload(key, ReplicationControllerType)
if err != nil || w == nil {
return err
}
return c.Sync(key, w)
}
func (c *CommonController) syncReplicaSet(key string, obj *corev1beta2.ReplicaSet) error {
if obj == nil || obj.DeletionTimestamp != nil {
return nil
}
w, err := c.getWorkload(key, ReplicaSetType)
if err != nil || w == nil {
return err
}
return c.Sync(key, w)
}
func (c *CommonController) syncDaemonSet(key string, obj *corev1beta2.DaemonSet) error {
if obj == nil || obj.DeletionTimestamp != nil {
return nil
}
w, err := c.getWorkload(key, DaemonSetType)
if err != nil || w == nil {
return err
}
return c.Sync(key, w)
}
func (c *CommonController) syncStatefulSet(key string, obj *corev1beta2.StatefulSet) error {
if obj == nil || obj.DeletionTimestamp != nil {
return nil
}
w, err := c.getWorkload(key, StatefulSetType)
if err != nil || w == nil {
return err
}
return c.Sync(key, w)
}
func (c *CommonController) syncJob(key string, obj *corebatchv1.Job) error {
if obj == nil || obj.DeletionTimestamp != nil {
return nil
}
w, err := c.getWorkload(key, JobType)
if err != nil || w == nil {
return err
}
return c.Sync(key, w)
}
func (c *CommonController) syncCronJob(key string, obj *corebatchv1beta1.CronJob) error {
if obj == nil || obj.DeletionTimestamp != nil {
return nil
}
w, err := c.getWorkload(key, CronJobType)
if err != nil || w == nil {
return err
}
return c.Sync(key, w)
}
func (c CommonController) getWorkload(key string, objectType string) (*Workload, error) {
splitted := strings.Split(key, "/")
namespace := splitted[0]
name := splitted[1]
return c.GetByWorkloadID(getWorkloadID(objectType, namespace, name))
}
func (c CommonController) GetByWorkloadID(key string) (*Workload, error) {
return c.getByWorkloadIDFromCacheOrAPI(key, false)
}
func (c CommonController) GetByWorkloadIDRetryAPIIfNotFound(key string) (*Workload, error) {
return c.getByWorkloadIDFromCacheOrAPI(key, true)
}
func (c CommonController) getByWorkloadIDFromCacheOrAPI(key string, retry bool) (*Workload, error) {
splitted := strings.Split(key, ":")
if len(splitted) != 3 {
return nil, fmt.Errorf("workload name [%s] is invalid", key)
}
workloadType := strings.ToLower(splitted[0])
namespace := splitted[1]
name := splitted[2]
var workload *Workload
switch workloadType {
case ReplicationControllerType:
o, err := c.ReplicationControllerLister.Get(namespace, name)
if err != nil && apierrors.IsNotFound(err) && retry {
o, err = c.ReplicationControllers.GetNamespaced(namespace, name, metav1.GetOptions{})
}
if err != nil || o.DeletionTimestamp != nil {
return nil, err
}
labelSelector := &metav1.LabelSelector{
MatchLabels: o.Spec.Selector,
}
workload = getWorkload(namespace, name, workloadType, AppVersion, o.UID, labelSelector, o.Annotations, o.Spec.Template, o.OwnerReferences, o.Labels, o.Status.Replicas, o.Status.AvailableReplicas)
case ReplicaSetType:
o, err := c.ReplicaSetLister.Get(namespace, name)
if err != nil && apierrors.IsNotFound(err) && retry {
o, err = c.ReplicaSets.GetNamespaced(namespace, name, metav1.GetOptions{})
}
if err != nil || o.DeletionTimestamp != nil {
return nil, err
}
workload = getWorkload(namespace, name, workloadType, AppVersion, o.UID, o.Spec.Selector, o.Annotations, &o.Spec.Template, o.OwnerReferences, o.Labels, o.Status.Replicas, o.Status.AvailableReplicas)
case DaemonSetType:
o, err := c.DaemonSetLister.Get(namespace, name)
if err != nil && apierrors.IsNotFound(err) && retry {
o, err = c.DaemonSets.GetNamespaced(namespace, name, metav1.GetOptions{})
}
if err != nil || o.DeletionTimestamp != nil {
return nil, err
}
workload = getWorkload(namespace, name, workloadType, AppVersion, o.UID, o.Spec.Selector, o.Annotations, &o.Spec.Template, o.OwnerReferences, o.Labels, o.Status.DesiredNumberScheduled, o.Status.NumberAvailable)
case StatefulSetType:
o, err := c.StatefulSetLister.Get(namespace, name)
if err != nil && apierrors.IsNotFound(err) && retry {
o, err = c.StatefulSets.GetNamespaced(namespace, name, metav1.GetOptions{})
}
if err != nil || o.DeletionTimestamp != nil {
return nil, err
}
workload = getWorkload(namespace, name, workloadType, AppVersion, o.UID, o.Spec.Selector, o.Annotations, &o.Spec.Template, o.OwnerReferences, o.Labels, o.Status.Replicas, o.Status.ReadyReplicas)
case JobType:
o, err := c.JobLister.Get(namespace, name)
if err != nil && apierrors.IsNotFound(err) && retry {
o, err = c.Jobs.GetNamespaced(namespace, name, metav1.GetOptions{})
}
if err != nil || o.DeletionTimestamp != nil {
return nil, err
}
var labelSelector *metav1.LabelSelector
if o.Spec.Selector != nil {
labelSelector = &metav1.LabelSelector{
MatchLabels: o.Spec.Selector.MatchLabels,
}
}
workload = getWorkload(namespace, name, workloadType, BatchVersion, o.UID, labelSelector, o.Annotations, &o.Spec.Template, o.OwnerReferences, o.Labels, 0, 0)
case CronJobType:
o, err := c.CronJobLister.Get(namespace, name)
if err != nil && apierrors.IsNotFound(err) && retry {
o, err = c.CronJobs.GetNamespaced(namespace, name, metav1.GetOptions{})
}
if err != nil || o.DeletionTimestamp != nil {
return nil, err
}
var labelSelector *metav1.LabelSelector
if o.Spec.JobTemplate.Spec.Selector != nil {
labelSelector = &metav1.LabelSelector{
MatchLabels: o.Spec.JobTemplate.Spec.Selector.MatchLabels,
}
}
workload = getWorkload(namespace, name, workloadType, BatchBetaVersion, o.UID, labelSelector, o.Annotations, &o.Spec.JobTemplate.Spec.Template, o.OwnerReferences, o.Labels, 0, 0)
default:
o, err := c.DeploymentLister.Get(namespace, name)
if err != nil && apierrors.IsNotFound(err) && retry {
o, err = c.Deployments.GetNamespaced(namespace, name, metav1.GetOptions{})
}
if err != nil || o.DeletionTimestamp != nil {
return nil, err
}
workload = getWorkload(namespace, name, DeploymentType, AppVersion, o.UID, o.Spec.Selector, o.Annotations, &o.Spec.Template, o.OwnerReferences, o.Labels, o.Status.Replicas, o.Status.AvailableReplicas)
}
return workload, nil
}
func getWorkload(namespace string, name string, kind string, apiVersion string, UUID types.UID, selectorLabels *metav1.LabelSelector,
annotations map[string]string, podTemplateSpec *corev1.PodTemplateSpec, ownerRefs []metav1.OwnerReference, labels map[string]string,
replicas, availableReplicas int32) *Workload {
w := &Workload{
Name: name,
Namespace: namespace,
SelectorLabels: getSelectorLables(selectorLabels),
UUID: UUID,
Annotations: annotations,
TemplateSpec: podTemplateSpec,
OwnerReferences: ownerRefs,
Kind: kind,
APIVersion: apiVersion,
Labels: labels,
Key: fmt.Sprintf("%s:%s:%s", kind, namespace, name),
Status: &Status{
Replicas: replicas,
AvailableReplicas: availableReplicas,
},
}
return w
}
func (c CommonController) GetAllWorkloads(namespace string) ([]*Workload, error) {
var workloads []*Workload
// deployments
ds, err := c.DeploymentLister.List(namespace, labels.NewSelector())
if err != nil {
return workloads, err
}
for _, o := range ds {
workload, err := c.GetByWorkloadID(getWorkloadID(DeploymentType, o.Namespace, o.Name))
if err != nil || workload == nil {
return workloads, err
}
workloads = append(workloads, workload)
}
// replication controllers
rcs, err := c.ReplicationControllerLister.List(namespace, labels.NewSelector())
if err != nil {
return workloads, err
}
for _, o := range rcs {
workload, err := c.GetByWorkloadID(getWorkloadID(ReplicationControllerType, o.Namespace, o.Name))
if err != nil || workload == nil {
return workloads, err
}
workloads = append(workloads, workload)
}
// replica sets
rss, err := c.ReplicaSetLister.List(namespace, labels.NewSelector())
if err != nil {
return workloads, err
}
for _, o := range rss {
workload, err := c.GetByWorkloadID(getWorkloadID(ReplicaSetType, o.Namespace, o.Name))
if err != nil || workload == nil {
return workloads, err
}
workloads = append(workloads, workload)
}
// daemon sets
dss, err := c.DaemonSetLister.List(namespace, labels.NewSelector())
if err != nil {
return workloads, err
}
for _, o := range dss {
workload, err := c.GetByWorkloadID(getWorkloadID(DaemonSetType, o.Namespace, o.Name))
if err != nil || workload == nil {
return workloads, err
}
workloads = append(workloads, workload)
}
// stateful sets
sts, err := c.StatefulSetLister.List(namespace, labels.NewSelector())
if err != nil {
return workloads, err
}
for _, o := range sts {
workload, err := c.GetByWorkloadID(getWorkloadID(StatefulSetType, o.Namespace, o.Name))
if err != nil || workload == nil {
return workloads, err
}
workloads = append(workloads, workload)
}
// jobs
jobs, err := c.JobLister.List(namespace, labels.NewSelector())
if err != nil {
return workloads, err
}
for _, o := range jobs {
workload, err := c.GetByWorkloadID(getWorkloadID(JobType, o.Namespace, o.Name))
if err != nil || workload == nil {
return workloads, err
}
workloads = append(workloads, workload)
}
// cron jobs
cronJobs, err := c.CronJobLister.List(namespace, labels.NewSelector())
if err != nil {
return workloads, err
}
for _, o := range cronJobs {
workload, err := c.GetByWorkloadID(getWorkloadID(CronJobType, o.Namespace, o.Name))
if err != nil || workload == nil {
return workloads, err
}
workloads = append(workloads, workload)
}
return workloads, nil
}
func (c CommonController) GetWorkloadsMatchingLabels(namespace string, targetLabels map[string]string) ([]*Workload, error) {
var workloads []*Workload
allWorkloads, err := c.GetAllWorkloads(namespace)
if err != nil {
return workloads, err
}
for _, workload := range allWorkloads {
workloadSelector := labels.SelectorFromSet(workload.SelectorLabels)
if workloadSelector.Matches(labels.Set(targetLabels)) {
workloads = append(workloads, workload)
}
}
return workloads, nil
}
func (c CommonController) GetWorkloadsMatchingSelector(namespace string, selectorLabels map[string]string) ([]*Workload, error) {
var workloads []*Workload
allWorkloads, err := c.GetAllWorkloads(namespace)
if err != nil {
return workloads, err
}
selector := labels.SelectorFromSet(selectorLabels)
for _, workload := range allWorkloads {
if selector.Matches(labels.Set(workload.Labels)) {
workloads = append(workloads, workload)
}
}
return workloads, nil
}
func getSelectorLables(s *metav1.LabelSelector) map[string]string {
if s == nil {
return nil
}
selectorLabels := map[string]string{}
for key, value := range s.MatchLabels {
selectorLabels[key] = value
}
return selectorLabels
}
type Service struct {
Type corev1.ServiceType
ClusterIP string
ServicePorts []corev1.ServicePort
Name string
}
type ContainerPort struct {
Kind string `json:"kind,omitempty"`
SourcePort int `json:"sourcePort,omitempty"`
DNSName string `json:"dnsName,omitempty"`
Name string `json:"name,omitempty"`
Protocol string `json:"protocol,omitempty"`
ContainerPort int32 `json:"containerPort,omitempty"`
}
func generateClusterIPServiceFromContainers(workload *Workload) *Service {
var servicePorts []corev1.ServicePort
for _, c := range workload.TemplateSpec.Spec.Containers {
for _, p := range c.Ports {
var portName string
if p.Name == "" {
portName = fmt.Sprintf("%s-%s", strconv.FormatInt(int64(p.ContainerPort), 10), c.Name)
} else {
portName = fmt.Sprintf("%s-%s", p.Name, c.Name)
}
servicePort := corev1.ServicePort{
Port: p.ContainerPort,
TargetPort: intstr.Parse(strconv.FormatInt(int64(p.ContainerPort), 10)),
Protocol: p.Protocol,
Name: portName,
}
servicePorts = append(servicePorts, servicePort)
}
}
clusterIP := ""
// append default port as sky dns won't work w/o at least one port being set
if len(servicePorts) == 0 {
servicePort := corev1.ServicePort{
Port: 42,
TargetPort: intstr.Parse(strconv.FormatInt(42, 10)),
Protocol: corev1.Protocol(corev1.ProtocolTCP),
Name: "default",
}
clusterIP = "None"
servicePorts = append(servicePorts, servicePort)
}
return &Service{
Type: ClusterIPServiceType,
ClusterIP: clusterIP,
ServicePorts: servicePorts,
Name: workload.Name,
}
}
func generateServicesFromPortsAnnotation(workload *Workload) ([]Service, error) {
var services []Service
val, ok := workload.TemplateSpec.Annotations[PortsAnnotation]
if !ok {
return services, nil
}
var portList [][]ContainerPort
err := json.Unmarshal([]byte(val), &portList)
if err != nil {
return services, err
}
svcTypeToPort := map[corev1.ServiceType][]ContainerPort{}
for _, l := range portList {
for _, port := range l {
if port.Kind == "HostPort" {
continue
}
svcType := corev1.ServiceType(port.Kind)
svcTypeToPort[svcType] = append(svcTypeToPort[svcType], port)
}
}
for svcType, ports := range svcTypeToPort {
servicePorts := map[string][]corev1.ServicePort{}
for _, p := range ports {
var nodePort int32
var clusterIPPort int32
if svcType == corev1.ServiceTypeNodePort {
nodePort = int32(p.SourcePort)
clusterIPPort = p.ContainerPort
} else {
if p.SourcePort == 0 {
clusterIPPort = p.ContainerPort
} else {
clusterIPPort = int32(p.SourcePort)
}
}
servicePort := corev1.ServicePort{
Port: clusterIPPort,
TargetPort: intstr.Parse(strconv.FormatInt(int64(p.ContainerPort), 10)),
NodePort: nodePort,
Protocol: corev1.Protocol(p.Protocol),
Name: p.Name,
}
dnsName := p.DNSName
if dnsName == "" {
dnsName = workload.Name
}
servicePorts[dnsName] = append(servicePorts[dnsName], servicePort)
}
// append default port as sky dns won't work w/o at least one port being set
if len(servicePorts) == 0 {
servicePort := corev1.ServicePort{
Port: 42,
TargetPort: intstr.Parse(strconv.FormatInt(42, 10)),
Protocol: corev1.Protocol(corev1.ProtocolTCP),
Name: "default",
}
servicePorts[workload.Name] = append(servicePorts[workload.Name], servicePort)
}
for dnsName, servicePorts := range servicePorts {
services = append(services, Service{
Type: svcType,
ServicePorts: servicePorts,
Name: dnsName,
})
}
}
return services, nil
}
func getWorkloadID(objectType string, namespace string, name string) string {
return fmt.Sprintf("%s:%s:%s", objectType, namespace, name)
}
func (c CommonController) UpdateWorkload(w *Workload, annotations map[string]string) error {
// only annotations updates are supported
switch w.Kind {
case DeploymentType:
o, err := c.DeploymentLister.Get(w.Namespace, w.Name)
if err != nil {
if apierrors.IsNotFound(err) {
return nil
}
return err
}
toUpdate := o.DeepCopy()
if toUpdate.Annotations == nil {
toUpdate.Annotations = make(map[string]string)
}
for key, value := range annotations {
toUpdate.Annotations[key] = value
}
_, err = c.Deployments.Update(toUpdate)
if err != nil {
return err
}
case ReplicationControllerType:
o, err := c.ReplicationControllerLister.Get(w.Namespace, w.Name)
if err != nil {
if apierrors.IsNotFound(err) {
return nil
}
return err
}
toUpdate := o.DeepCopy()
if toUpdate.Annotations == nil {
toUpdate.Annotations = make(map[string]string)
}
for key, value := range annotations {
toUpdate.Annotations[key] = value
}
_, err = c.ReplicationControllers.Update(toUpdate)
if err != nil {
return err
}
case ReplicaSetType:
o, err := c.ReplicaSetLister.Get(w.Namespace, w.Name)
if err != nil {
if apierrors.IsNotFound(err) {
return nil
}
return err
}
toUpdate := o.DeepCopy()
if toUpdate.Annotations == nil {
toUpdate.Annotations = make(map[string]string)
}
for key, value := range annotations {
toUpdate.Annotations[key] = value
}
_, err = c.ReplicaSets.Update(toUpdate)
if err != nil {
return err
}
case DaemonSetType:
o, err := c.DaemonSetLister.Get(w.Namespace, w.Name)
if err != nil {
if apierrors.IsNotFound(err) {
return nil
}
return err
}
toUpdate := o.DeepCopy()
if toUpdate.Annotations == nil {
toUpdate.Annotations = make(map[string]string)
}
for key, value := range annotations {
toUpdate.Annotations[key] = value
}
_, err = c.DaemonSets.Update(toUpdate)
if err != nil {
return err
}
case StatefulSetType:
o, err := c.StatefulSetLister.Get(w.Namespace, w.Name)
if err != nil {
if apierrors.IsNotFound(err) {
return nil
}
return err
}
toUpdate := o.DeepCopy()
if toUpdate.Annotations == nil {
toUpdate.Annotations = make(map[string]string)
}
for key, value := range annotations {
toUpdate.Annotations[key] = value
}
_, err = c.StatefulSets.Update(toUpdate)
if err != nil {
return err
}
case JobType:
o, err := c.JobLister.Get(w.Namespace, w.Name)
if err != nil {
if apierrors.IsNotFound(err) {
return nil
}
return err
}
toUpdate := o.DeepCopy()
if toUpdate.Annotations == nil {
toUpdate.Annotations = make(map[string]string)
}
for key, value := range annotations {
toUpdate.Annotations[key] = value
}
_, err = c.Jobs.Update(toUpdate)
if err != nil {
return err
}
case CronJobType:
o, err := c.CronJobLister.Get(w.Namespace, w.Name)
if err != nil {
if apierrors.IsNotFound(err) {
return nil
}
return err
}
toUpdate := o.DeepCopy()
if toUpdate.Annotations == nil {
toUpdate.Annotations = make(map[string]string)
}
for key, value := range annotations {
toUpdate.Annotations[key] = value
}
_, err = c.CronJobs.Update(toUpdate)
if err != nil {
return err
}
}
return nil
}
func (c CommonController) EnqueueWorkload(w *Workload) {
switch w.Kind {
case DeploymentType:
c.Deployments.Controller().Enqueue(w.Namespace, w.Name)
case ReplicationControllerType:
c.ReplicationControllers.Controller().Enqueue(w.Namespace, w.Name)
case ReplicaSetType:
c.ReplicaSets.Controller().Enqueue(w.Namespace, w.Name)
case DaemonSetType:
c.DaemonSets.Controller().Enqueue(w.Namespace, w.Name)
case StatefulSetType:
c.StatefulSets.Controller().Enqueue(w.Namespace, w.Name)
case JobType:
c.Jobs.Controller().Enqueue(w.Namespace, w.Name)
case CronJobType:
c.CronJobs.Controller().Enqueue(w.Namespace, w.Name)
}
}
func (c CommonController) EnqueueAllWorkloads(namespace string) error {
ws, err := c.GetAllWorkloads(namespace)
if err != nil {
return err
}
for _, w := range ws {
c.EnqueueWorkload(w)
}
return nil
}