-
Notifications
You must be signed in to change notification settings - Fork 14
/
appsv1.go
181 lines (155 loc) · 4.4 KB
/
appsv1.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
package main
import (
"log"
appsv1 "k8s.io/api/apps/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes/typed/apps/v1"
)
func applyDeploymentAppsV1(deployment *appsv1.Deployment, deploymentSet v1.DeploymentInterface) error {
deploymentName := deployment.GetObjectMeta().GetName()
log.Println("Applying Deployment " + deploymentName)
deployments, err := deploymentSet.List(metav1.ListOptions{})
if err != nil {
log.Println("Error when listing deployments")
return err
}
update := false
for _, dep := range deployments.Items {
if dep.GetObjectMeta().GetName() == deploymentName {
update = true
}
}
if update {
_, err := deploymentSet.Get(deploymentName, metav1.GetOptions{})
if err != nil {
log.Println("Error when getting old deployment")
return err
}
_, err = deploymentSet.Update(deployment)
if err != nil {
log.Println("Error when updating deployment")
return err
}
log.Println("Deployment " + deploymentName + " updated")
return err
} else {
_, err := deploymentSet.Create(deployment)
if err != nil {
log.Println("Error when creating deployment")
return err
}
log.Println("Deployment " + deploymentName + " created")
return err
}
}
func applyDaemonSetAppsV1(daemonSet *appsv1.DaemonSet, daemonSetSet v1.DaemonSetInterface) error {
daemonSetName := daemonSet.GetObjectMeta().GetName()
log.Println("Applying DaemonSet " + daemonSetName)
daemonSets, err := daemonSetSet.List(metav1.ListOptions{})
if err != nil {
log.Println("Error when listing daemon sets")
return err
}
update := false
for _, dep := range daemonSets.Items {
if dep.GetObjectMeta().GetName() == daemonSetName {
update = true
}
}
if update {
_, err := daemonSetSet.Get(daemonSetName, metav1.GetOptions{})
if err != nil {
log.Println("Error when getting old daemon set")
return err
}
_, err = daemonSetSet.Update(daemonSet)
if err != nil {
log.Println("Error when updating daemonSet")
return err
}
log.Println("DaemonSet " + daemonSetName + " updated")
return err
} else {
_, err := daemonSetSet.Create(daemonSet)
if err != nil {
log.Println("Error when creating daemonSet")
return err
}
log.Println("DaemonSet " + daemonSetName + " created")
return err
}
}
func applyReplicaSetAppsV1(replicaSet *appsv1.ReplicaSet, replicaSetSet v1.ReplicaSetInterface) error {
replicaSetName := replicaSet.GetObjectMeta().GetName()
log.Println("Applying ReplicaSet " + replicaSetName)
replicaSets, err := replicaSetSet.List(metav1.ListOptions{})
if err != nil {
log.Println("Error when listing replica sets")
return err
}
update := false
for _, rep := range replicaSets.Items {
if rep.GetObjectMeta().GetName() == replicaSetName {
update = true
}
}
if update {
_, err := replicaSetSet.Get(replicaSetName, metav1.GetOptions{})
if err != nil {
log.Println("Error when getting old replica set")
return err
}
_, err = replicaSetSet.Update(replicaSet)
if err != nil {
log.Println("Error when updating replicaSet")
return err
}
log.Println("ReplicaSet " + replicaSetName + " updated")
return err
} else {
_, err := replicaSetSet.Create(replicaSet)
if err != nil {
log.Println("Error when creating replicaSet")
return err
}
log.Println("ReplicaSet " + replicaSetName + " created")
return err
}
}
func applyStatefulSetAppsV1(statefulSet *appsv1.StatefulSet, statefulSetSet v1.StatefulSetInterface) error {
statefulSetName := statefulSet.GetObjectMeta().GetName()
log.Println("Applying StatefulSet " + statefulSetName)
statefulSets, err := statefulSetSet.List(metav1.ListOptions{})
if err != nil {
log.Println("Error when listing stateful sets")
return err
}
update := false
for _, sta := range statefulSets.Items {
if sta.GetObjectMeta().GetName() == statefulSetName {
update = true
}
}
if update {
_, err := statefulSetSet.Get(statefulSetName, metav1.GetOptions{})
if err != nil {
log.Println("Error when getting old stateful set")
return err
}
_, err = statefulSetSet.Update(statefulSet)
if err != nil {
log.Println("Error when updating statefulSet")
return err
}
log.Println("StatefulSet " + statefulSetName + " updated")
return err
} else {
_, err := statefulSetSet.Create(statefulSet)
if err != nil {
log.Println("Error when creating statefulSet")
return err
}
log.Println("StatefulSet " + statefulSetName + " created")
return err
}
}