-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathapply.go
342 lines (290 loc) · 11.2 KB
/
apply.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
package kube
// nolint:deadcode,golint
import (
"context"
"encoding/json"
"reflect"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
netv1 "k8s.io/api/networking/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
k8stypes "k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/intstr"
"k8s.io/client-go/kubernetes"
metricsutils "github.com/akash-network/node/util/metrics"
"github.com/akash-network/provider/cluster/kube/builder"
crd "github.com/akash-network/provider/pkg/apis/akash.network/v2beta2"
crdapi "github.com/akash-network/provider/pkg/client/clientset/versioned"
)
type k8sPatch struct {
Op string `json:"op"`
Path string `json:"path"`
Value interface{} `json:"value"`
}
func applyNS(ctx context.Context, kc kubernetes.Interface, b builder.NS) (*corev1.Namespace, *corev1.Namespace, *corev1.Namespace, error) {
oobj, err := kc.CoreV1().Namespaces().Get(ctx, b.Name(), metav1.GetOptions{})
metricsutils.IncCounterVecWithLabelValuesFiltered(kubeCallsCounter, "namespaces-get", err, errors.IsNotFound)
var nobj *corev1.Namespace
var uobj *corev1.Namespace
switch {
case err == nil:
curr := oobj.DeepCopy()
oobj, err = b.Update(oobj)
if err == nil && (!b.IsObjectRevisionLatest(oobj.Labels) ||
!reflect.DeepEqual(&curr.Spec, &oobj.Spec) ||
!reflect.DeepEqual(curr.Labels, oobj.Labels)) {
uobj, err = kc.CoreV1().Namespaces().Update(ctx, oobj, metav1.UpdateOptions{})
metricsutils.IncCounterVecWithLabelValues(kubeCallsCounter, "namespaces-update", err)
}
case errors.IsNotFound(err):
oobj, err = b.Create()
if err == nil {
nobj, err = kc.CoreV1().Namespaces().Create(ctx, oobj, metav1.CreateOptions{})
metricsutils.IncCounterVecWithLabelValues(kubeCallsCounter, "namespaces-create", err)
}
}
return nobj, uobj, oobj, err
}
// Apply list of Network Policies
func applyNetPolicies(ctx context.Context, kc kubernetes.Interface, b builder.NetPol) ([]netv1.NetworkPolicy, []netv1.NetworkPolicy, []netv1.NetworkPolicy, error) {
var err error
policies, err := b.Create()
if err != nil {
return nil, nil, nil, err
}
currPolicies, err := kc.NetworkingV1().NetworkPolicies(b.NS()).List(ctx, metav1.ListOptions{})
if err != nil {
return nil, nil, nil, err
}
var nobjs []netv1.NetworkPolicy
var uobjs []netv1.NetworkPolicy
oobjs := currPolicies.Items
for _, pol := range policies {
oobj, err := kc.NetworkingV1().NetworkPolicies(b.NS()).Get(ctx, pol.Name, metav1.GetOptions{})
metricsutils.IncCounterVecWithLabelValuesFiltered(kubeCallsCounter, "networking-policies-get", err, errors.IsNotFound)
var nobj *netv1.NetworkPolicy
var uobj *netv1.NetworkPolicy
switch {
case err == nil:
curr := oobj.DeepCopy()
oobj, err = b.Update(oobj)
if err == nil && (!b.IsObjectRevisionLatest(curr.Labels) ||
!reflect.DeepEqual(&curr.Spec, &oobj.Spec) ||
!reflect.DeepEqual(curr.Labels, oobj.Labels)) {
uobj, err = kc.NetworkingV1().NetworkPolicies(b.NS()).Update(ctx, pol, metav1.UpdateOptions{})
metricsutils.IncCounterVecWithLabelValues(kubeCallsCounter, "networking-policies-update", err)
if err == nil {
uobjs = append(uobjs, *uobj)
}
}
case errors.IsNotFound(err):
nobj, err = kc.NetworkingV1().NetworkPolicies(b.NS()).Create(ctx, pol, metav1.CreateOptions{})
metricsutils.IncCounterVecWithLabelValues(kubeCallsCounter, "networking-policies-create", err)
if err == nil {
nobjs = append(nobjs, *nobj)
}
}
if err != nil {
break
}
}
return nobjs, uobjs, oobjs, err
}
func applyServiceCredentials(ctx context.Context, kc kubernetes.Interface, b builder.ServiceCredentials) (*corev1.Secret, *corev1.Secret, *corev1.Secret, error) {
oobj, err := kc.CoreV1().Secrets(b.NS()).Get(ctx, b.Name(), metav1.GetOptions{})
metricsutils.IncCounterVecWithLabelValuesFiltered(kubeCallsCounter, "secrets-get", err, errors.IsNotFound)
var nobj *corev1.Secret
var uobj *corev1.Secret
switch {
case err == nil:
curr := oobj.DeepCopy()
oobj, err = b.Update(oobj)
if err == nil && (!b.IsObjectRevisionLatest(curr.Labels) ||
!reflect.DeepEqual(&curr.Data, &oobj.Data) ||
!reflect.DeepEqual(curr.Labels, oobj.Labels)) {
uobj, err = kc.CoreV1().Secrets(b.NS()).Update(ctx, oobj, metav1.UpdateOptions{})
metricsutils.IncCounterVecWithLabelValues(kubeCallsCounter, "secrets-get", err)
}
case errors.IsNotFound(err):
oobj, err = b.Create()
if err == nil {
nobj, err = kc.CoreV1().Secrets(b.NS()).Create(ctx, oobj, metav1.CreateOptions{})
metricsutils.IncCounterVecWithLabelValues(kubeCallsCounter, "secrets-create", err)
}
}
return nobj, uobj, oobj, err
}
func applyDeployment(ctx context.Context, kc kubernetes.Interface, b builder.Deployment) (*appsv1.Deployment, *appsv1.Deployment, *appsv1.Deployment, error) {
oobj, err := kc.AppsV1().Deployments(b.NS()).Get(ctx, b.Name(), metav1.GetOptions{})
metricsutils.IncCounterVecWithLabelValuesFiltered(kubeCallsCounter, "deployments-get", err, errors.IsNotFound)
var nobj *appsv1.Deployment
var uobj *appsv1.Deployment
switch {
case err == nil:
curr := oobj.DeepCopy()
oobj, err = b.Update(oobj)
if err != nil {
break
}
if b.IsObjectRevisionLatest(curr.Labels) ||
!reflect.DeepEqual(&curr.Spec, &oobj.Spec) ||
!reflect.DeepEqual(curr.Labels, oobj.Labels) {
uobj, err = kc.AppsV1().Deployments(b.NS()).Update(ctx, oobj, metav1.UpdateOptions{})
metricsutils.IncCounterVecWithLabelValues(kubeCallsCounter, "deployments-update", err)
}
var patches []k8sPatch
if rev := curr.Spec.RevisionHistoryLimit; rev == nil || *rev != 10 {
patches = append(patches, k8sPatch{
Op: "add",
Path: "/spec/revisionHistoryLimit",
Value: int32(10),
})
}
ustrategy := &oobj.Spec.Strategy
if uobj != nil {
ustrategy = &uobj.Spec.Strategy
}
maxSurge := intstr.FromInt32(0)
maxUnavailable := intstr.FromInt32(1)
strategy := appsv1.DeploymentStrategy{
Type: appsv1.RollingUpdateDeploymentStrategyType,
RollingUpdate: &appsv1.RollingUpdateDeployment{
MaxUnavailable: &maxUnavailable,
MaxSurge: &maxSurge,
},
}
if !reflect.DeepEqual(&strategy, &ustrategy) {
patches = append(patches, k8sPatch{
Op: "replace",
Path: "/spec/strategy",
Value: strategy,
})
}
if len(patches) > 0 {
data, _ := json.Marshal(patches)
oobj, err = kc.AppsV1().Deployments(b.NS()).Patch(ctx, oobj.Name, k8stypes.JSONPatchType, data, metav1.PatchOptions{})
metricsutils.IncCounterVecWithLabelValues(kubeCallsCounter, "deployments-patch", err)
}
case errors.IsNotFound(err):
oobj, err = b.Create()
if err == nil {
nobj, err = kc.AppsV1().Deployments(b.NS()).Create(ctx, oobj, metav1.CreateOptions{})
metricsutils.IncCounterVecWithLabelValues(kubeCallsCounter, "deployments-create", err)
}
}
return nobj, uobj, oobj, err
}
func applyStatefulSet(ctx context.Context, kc kubernetes.Interface, b builder.StatefulSet) (*appsv1.StatefulSet, *appsv1.StatefulSet, *appsv1.StatefulSet, error) {
oobj, err := kc.AppsV1().StatefulSets(b.NS()).Get(ctx, b.Name(), metav1.GetOptions{})
metricsutils.IncCounterVecWithLabelValuesFiltered(kubeCallsCounter, "statefulset-get", err, errors.IsNotFound)
var nobj *appsv1.StatefulSet
var uobj *appsv1.StatefulSet
switch {
case err == nil:
curr := oobj.DeepCopy()
oobj, err = b.Update(oobj)
if err != nil {
break
}
if b.IsObjectRevisionLatest(curr.Labels) ||
!reflect.DeepEqual(&curr.Spec, &oobj.Spec) ||
!reflect.DeepEqual(curr.Labels, oobj.Labels) {
uobj, err = kc.AppsV1().StatefulSets(b.NS()).Update(ctx, oobj, metav1.UpdateOptions{})
metricsutils.IncCounterVecWithLabelValues(kubeCallsCounter, "statefulset-update", err)
}
// var patches []k8sPatch
//
// if rev := curr.Spec.RevisionHistoryLimit; rev == nil || *rev != 10 {
// patches = append(patches, k8sPatch{
// Op: "add",
// Path: "/spec/revisionHistoryLimit",
// Value: int32(10),
// })
// }
//
// ustrategy := &oobj.Spec.UpdateStrategy
// if uobj != nil {
// ustrategy = &uobj.Spec.UpdateStrategy
// }
//
// partition := int32(0)
// maxUnavailable := intstr.FromInt32(1)
//
// strategy := appsv1.StatefulSetUpdateStrategy{
// Type: appsv1.RollingUpdateStatefulSetStrategyType,
// RollingUpdate: &appsv1.RollingUpdateStatefulSetStrategy{
// Partition: &partition,
// MaxUnavailable: &maxUnavailable,
// },
// }
//
// if !reflect.DeepEqual(&strategy, ustrategy) {
// patches = append(patches, k8sPatch{
// Op: "replace",
// Path: "/spec/updateStrategy",
// Value: strategy,
// })
// }
//
// if len(patches) > 0 {
// data, _ := json.Marshal(patches)
//
// oobj, err = kc.AppsV1().StatefulSets(b.NS()).Patch(ctx, oobj.Name, k8stypes.JSONPatchType, data, metav1.PatchOptions{})
// metricsutils.IncCounterVecWithLabelValues(kubeCallsCounter, "statefulset-patch", err)
// }
case errors.IsNotFound(err):
oobj, err = b.Create()
if err == nil {
nobj, err = kc.AppsV1().StatefulSets(b.NS()).Create(ctx, oobj, metav1.CreateOptions{})
metricsutils.IncCounterVecWithLabelValues(kubeCallsCounter, "statefulset-create", err)
}
}
return nobj, uobj, oobj, err
}
func applyService(ctx context.Context, kc kubernetes.Interface, b builder.Service) (*corev1.Service, *corev1.Service, *corev1.Service, error) {
oobj, err := kc.CoreV1().Services(b.NS()).Get(ctx, b.Name(), metav1.GetOptions{})
metricsutils.IncCounterVecWithLabelValuesFiltered(kubeCallsCounter, "services-get", err, errors.IsNotFound)
var nobj *corev1.Service
var uobj *corev1.Service
switch {
case err == nil:
curr := oobj.DeepCopy()
oobj, err = b.Update(oobj)
if err == nil && (b.IsObjectRevisionLatest(curr.Labels) ||
!reflect.DeepEqual(&curr.Spec, &oobj.Spec) ||
!reflect.DeepEqual(curr.Labels, oobj.Labels)) {
uobj, err = kc.CoreV1().Services(b.NS()).Update(ctx, oobj, metav1.UpdateOptions{})
metricsutils.IncCounterVecWithLabelValues(kubeCallsCounter, "services-update", err)
}
case errors.IsNotFound(err):
oobj, err = b.Create()
if err == nil {
nobj, err = kc.CoreV1().Services(b.NS()).Create(ctx, oobj, metav1.CreateOptions{})
metricsutils.IncCounterVecWithLabelValues(kubeCallsCounter, "services-create", err)
}
}
return nobj, uobj, oobj, err
}
func applyManifest(ctx context.Context, kc crdapi.Interface, b builder.Manifest) (*crd.Manifest, *crd.Manifest, *crd.Manifest, error) {
oobj, err := kc.AkashV2beta2().Manifests(b.NS()).Get(ctx, b.Name(), metav1.GetOptions{})
metricsutils.IncCounterVecWithLabelValuesFiltered(kubeCallsCounter, "akash-manifests-get", err, errors.IsNotFound)
var nobj *crd.Manifest
var uobj *crd.Manifest
switch {
case err == nil:
curr := oobj.DeepCopy()
oobj, err = b.Update(oobj)
if err == nil && (!reflect.DeepEqual(&curr.Spec, &oobj.Spec) || !reflect.DeepEqual(curr.Labels, oobj.Labels)) {
uobj, err = kc.AkashV2beta2().Manifests(b.NS()).Update(ctx, oobj, metav1.UpdateOptions{})
metricsutils.IncCounterVecWithLabelValues(kubeCallsCounter, "akash-manifests-update", err)
}
case errors.IsNotFound(err):
oobj, err = b.Create()
if err == nil {
nobj, err = kc.AkashV2beta2().Manifests(b.NS()).Create(ctx, oobj, metav1.CreateOptions{})
metricsutils.IncCounterVecWithLabelValues(kubeCallsCounter, "akash-manifests-create", err)
}
}
return nobj, uobj, oobj, err
}