/
replicationcontroller.go
126 lines (118 loc) · 3.76 KB
/
replicationcontroller.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
package kube_client
import (
"sync"
"go.uber.org/zap"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
func (ctx *KubeClient) CreateReplicationControllers(count, replicas, containers int32, namespace string, excludeNamespaces []string) {
var created int
var syncer sync.WaitGroup
syncer.Add(int(count))
for counter := 0; counter < int(count); counter++ {
created++
if namespace == "" {
namespace = ctx.generateNamespace(excludeNamespaces)
}
rcClient := ctx.Client.CoreV1().ReplicationControllers(namespace)
go func() {
defer syncer.Done()
rc := generateReplicationControllerSpec(containers, replicas)
_, err := rcClient.Create(rc)
if err != nil {
created--
ctx.Logger.Error("Failed to create replication controller ", zap.String("name", rc.Name), zap.Error(err))
}
}()
}
syncer.Wait()
ctx.Logger.Info("Successfully created", zap.Int("ReplicationControllers", created))
}
func generateReplicationControllerSpec(containers, replicas int32) *corev1.ReplicationController {
name := generateName()
labels := generateLabels(replicationcontrollerName, name)
rc := &corev1.ReplicationController{
ObjectMeta: metav1.ObjectMeta{
Name: name,
},
Spec: corev1.ReplicationControllerSpec{
Replicas: &replicas,
Selector: labels,
Template: &corev1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Labels: labels,
},
Spec: corev1.PodSpec{
Containers: generateContainers(containers, name),
Tolerations: Config.Tolerations,
NodeSelector: Config.NodeSelector,
},
},
},
}
return rc
}
func (ctx *KubeClient) DeleteReplicationControllers(count int32, providedNamespace string, excludeNamespaces []string) {
var counter int
var calcNamespace []string
var syncer sync.WaitGroup
syncer.Add(int(count))
rcClient := ctx.Client.CoreV1().ReplicationControllers(providedNamespace)
rcList, err := rcClient.List(metav1.ListOptions{})
if err != nil {
ctx.Logger.Error("Unable to list replication controllers", zap.Error(err))
}
if providedNamespace != "" {
if len(rcList.Items) < int(count) {
panic("The number of replication controllers in the provided namespaces are lesser than the the provided scale to delete")
}
for _, d := range rcList.Items {
counter++
// This is to make sure goroutines get the actual name.
name := d.Name
go func() {
defer syncer.Done()
err = ctx.Client.CoreV1().ReplicationControllers(providedNamespace).Delete(name, &metav1.DeleteOptions{})
if err != nil {
counter--
ctx.Logger.Error("Failed to delete replication controllers ", zap.String("name", name), zap.Error(err))
}
}()
if counter == int(count) {
break
}
}
} else {
calcNamespace = ctx.namespacesForDeletion(excludeNamespaces)
for _, namespace := range calcNamespace {
rcClient = ctx.Client.CoreV1().ReplicationControllers(namespace)
rcList, err = rcClient.List(metav1.ListOptions{})
if rcList != nil && len(rcList.Items) > 0 {
for _, d := range rcList.Items {
// This is to make sure goroutines get the actual name.
name := d.Name
counter++
go func() {
defer syncer.Done()
err = rcClient.Delete(name, &metav1.DeleteOptions{})
if err != nil {
counter--
ctx.Logger.Error("Failed to delete replication controllers ", zap.String("name", name), zap.Error(err))
}
}()
if counter == int(count) {
break
}
}
}
if counter == int(count) {
break
}
}
if counter != int(count) {
ctx.Logger.Error("Unable to delete the replication controllers. As provided scale of replication controllers doesn't exits across the namespaces.", zap.Int("Deleted", counter))
}
}
syncer.Wait()
ctx.Logger.Info("Successfully deleted ", zap.Int("Replication Controllers", counter))
}