-
Notifications
You must be signed in to change notification settings - Fork 32
/
apply_utils.go
515 lines (454 loc) · 12.8 KB
/
apply_utils.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
package utils
import (
errors2 "errors"
"fmt"
"github.com/codablock/kluctl/pkg/deployment"
"github.com/codablock/kluctl/pkg/diff"
"github.com/codablock/kluctl/pkg/k8s"
"github.com/codablock/kluctl/pkg/types"
k8s2 "github.com/codablock/kluctl/pkg/types/k8s"
"github.com/codablock/kluctl/pkg/utils"
"github.com/codablock/kluctl/pkg/utils/uo"
"github.com/codablock/kluctl/pkg/validation"
log "github.com/sirupsen/logrus"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/meta"
"reflect"
"sync"
"time"
)
type ApplyUtilOptions struct {
ForceApply bool
ReplaceOnError bool
ForceReplaceOnError bool
DryRun bool
AbortOnError bool
WaitObjectTimeout time.Duration
}
type ApplyUtil struct {
dew *DeploymentErrorsAndWarnings
deployments []*deployment.DeploymentItem
ru *RemoteObjectUtils
k *k8s.K8sCluster
o ApplyUtilOptions
AppliedObjects map[k8s2.ObjectRef]*uo.UnstructuredObject
appliedHookObjects map[k8s2.ObjectRef]*uo.UnstructuredObject
deletedObjects map[k8s2.ObjectRef]bool
deletedHookObjects map[k8s2.ObjectRef]bool
abortSignal bool
deployedNewCRD bool
mutex sync.Mutex
}
func NewApplyUtil(dew *DeploymentErrorsAndWarnings, deployments []*deployment.DeploymentItem, ru *RemoteObjectUtils, k *k8s.K8sCluster, o ApplyUtilOptions) *ApplyUtil {
return &ApplyUtil{
dew: dew,
deployments: deployments,
ru: ru,
k: k,
o: o,
AppliedObjects: map[k8s2.ObjectRef]*uo.UnstructuredObject{},
appliedHookObjects: map[k8s2.ObjectRef]*uo.UnstructuredObject{},
deletedObjects: map[k8s2.ObjectRef]bool{},
deletedHookObjects: map[k8s2.ObjectRef]bool{},
deployedNewCRD: true, // assume someone deployed CRDs in the meantime
}
}
func (a *ApplyUtil) handleResult(appliedObject *uo.UnstructuredObject, hook bool) {
a.mutex.Lock()
defer a.mutex.Unlock()
ref := appliedObject.GetK8sRef()
if hook {
a.appliedHookObjects[ref] = appliedObject
} else {
a.AppliedObjects[ref] = appliedObject
}
}
func (a *ApplyUtil) handleApiWarnings(ref k8s2.ObjectRef, warnings []k8s.ApiWarning) {
a.dew.AddApiWarnings(ref, warnings)
}
func (a *ApplyUtil) HandleWarning(ref k8s2.ObjectRef, warning error) {
a.dew.AddWarning(ref, warning)
}
func (a *ApplyUtil) HandleError(ref k8s2.ObjectRef, err error) {
a.mutex.Lock()
defer a.mutex.Unlock()
if a.o.AbortOnError {
a.abortSignal = true
}
a.dew.AddError(ref, err)
}
func (a *ApplyUtil) HadError(ref k8s2.ObjectRef) bool {
return a.dew.HadError(ref)
}
func (a *ApplyUtil) DeleteObject(ref k8s2.ObjectRef, hook bool) bool {
o := k8s.DeleteOptions{
ForceDryRun: a.o.DryRun,
}
apiWarnings, err := a.k.DeleteSingleObject(ref, o)
a.handleApiWarnings(ref, apiWarnings)
if err != nil {
if !errors.IsNotFound(err) {
a.HandleError(ref, err)
}
return false
}
a.mutex.Lock()
defer a.mutex.Unlock()
if hook {
a.deletedHookObjects[ref] = true
} else {
a.deletedObjects[ref] = true
}
return true
}
func (a *ApplyUtil) retryApplyForceReplace(x *uo.UnstructuredObject, hook bool, applyError error) {
ref := x.GetK8sRef()
log2 := log.WithField("ref", ref)
if !a.o.ForceReplaceOnError {
a.HandleError(ref, applyError)
return
}
log2.Warningf("Patching failed, retrying by deleting and re-applying")
if !a.DeleteObject(ref, hook) {
return
}
if !a.o.DryRun {
o := k8s.PatchOptions{
ForceDryRun: a.o.DryRun,
}
r, apiWarnings, err := a.k.PatchObject(x, o)
a.handleApiWarnings(ref, apiWarnings)
if err != nil {
a.HandleError(ref, err)
return
}
a.handleResult(r, hook)
} else {
a.handleResult(x, hook)
}
}
func (a *ApplyUtil) retryApplyWithReplace(x *uo.UnstructuredObject, hook bool, remoteObject *uo.UnstructuredObject, applyError error) {
ref := x.GetK8sRef()
log2 := log.WithField("ref", ref)
if !a.o.ReplaceOnError || remoteObject == nil {
a.HandleError(ref, applyError)
return
}
log2.Warningf("Patching failed, retrying with replace instead of patch")
rv := remoteObject.GetK8sResourceVersion()
x2 := x.Clone()
x2.SetK8sResourceVersion(rv)
o := k8s.UpdateOptions{
ForceDryRun: a.o.DryRun,
}
r, apiWarnings, err := a.k.UpdateObject(x, o)
a.handleApiWarnings(ref, apiWarnings)
if err != nil {
a.retryApplyForceReplace(x, hook, err)
return
}
a.handleResult(r, hook)
}
func (a *ApplyUtil) retryApplyWithConflicts(x *uo.UnstructuredObject, hook bool, remoteObject *uo.UnstructuredObject, applyError error) {
ref := x.GetK8sRef()
if remoteObject == nil {
a.HandleError(ref, applyError)
return
}
var x2 *uo.UnstructuredObject
if !a.o.ForceApply {
var statusError *errors.StatusError
if !errors2.As(applyError, &statusError) {
a.HandleError(ref, applyError)
return
}
x3, lostOwnership, err := diff.ResolveFieldManagerConflicts(x, remoteObject, statusError.ErrStatus)
if err != nil {
a.HandleError(ref, err)
return
}
for _, lo := range lostOwnership {
a.dew.AddWarning(ref, fmt.Errorf("%s. Not updating field '%s' as we lost field ownership", lo.Message, lo.Field))
}
x2 = x3
} else {
x2 = x
}
options := k8s.PatchOptions{
ForceDryRun: a.o.DryRun,
ForceApply: true,
}
r, apiWarnings, err := a.k.PatchObject(x2, options)
a.handleApiWarnings(ref, apiWarnings)
if err != nil {
// We didn't manage to solve it, better to abort (and not retry with replace!)
a.HandleError(ref, err)
return
}
a.handleResult(r, hook)
}
func (a *ApplyUtil) ApplyObject(x *uo.UnstructuredObject, replaced bool, hook bool) {
ref := x.GetK8sRef()
log2 := log.WithField("ref", ref)
log2.Debugf("applying object")
x = a.k.FixObjectForPatch(x)
remoteObject := a.ru.GetRemoteObject(ref)
if a.o.DryRun && replaced && remoteObject != nil {
// Let's simulate that this object was deleted in dry-run mode. If we'd actually try a dry-run apply with
// this object, it might fail as it is expected to not exist.
a.handleResult(x, hook)
return
}
options := k8s.PatchOptions{
ForceDryRun: a.o.DryRun,
}
r, apiWarnings, err := a.k.PatchObject(x, options)
retry, err := a.handleNewCRDs(r, err)
if retry {
r, apiWarnings, err = a.k.PatchObject(x, options)
}
a.handleApiWarnings(ref, apiWarnings)
if err == nil {
a.handleResult(r, hook)
} else if meta.IsNoMatchError(err) {
a.HandleError(ref, err)
} else if errors.IsConflict(err) {
a.retryApplyWithConflicts(x, hook, remoteObject, err)
} else if errors.IsInternalError(err) {
a.HandleError(ref, err)
} else {
a.retryApplyWithReplace(x, hook, remoteObject, err)
}
}
func (a *ApplyUtil) handleNewCRDs(x *uo.UnstructuredObject, err error) (bool, error) {
if err != nil && meta.IsNoMatchError(err) {
// maybe this was a resource for which the CRD was only deployed recently, so we should do rediscovery and then
// retry the patch
a.mutex.Lock()
defer a.mutex.Unlock()
if a.deployedNewCRD {
a.deployedNewCRD = false
err = a.k.RediscoverResources()
if err != nil {
return false, err
}
return true, nil
}
} else if err == nil {
ref := x.GetK8sRef()
if ref.GVK.Group == "apiextensions.k8s.io" && ref.GVK.Kind == "CustomResourceDefinition" {
// this is a freshly deployed CRD, so we must perform rediscovery in case an api resource can't be found
a.mutex.Lock()
defer a.mutex.Unlock()
a.deployedNewCRD = true
return true, nil
}
return false, nil
}
return false, err
}
func (a *ApplyUtil) WaitReadiness(ref k8s2.ObjectRef) bool {
if a.o.DryRun {
return true
}
log2 := log.WithField("ref", ref)
log2.Debugf("Waiting for object to get ready")
lastLogTime := time.Now()
didLog := false
startTime := time.Now()
for true {
o, apiWarnings, err := a.k.GetSingleObject(ref)
a.handleApiWarnings(ref, apiWarnings)
if err != nil {
if errors.IsNotFound(err) {
if didLog {
log2.Warningf("Cancelled waiting for object as it disappeared while waiting for it")
}
a.HandleError(ref, fmt.Errorf("object disappeared while waiting for it to become ready"))
return false
}
a.HandleError(ref, err)
return false
}
v := validation.ValidateObject(o, false)
if v.Ready {
if didLog {
log2.Infof("Finished waiting for object")
}
return true
}
if len(v.Errors) != 0 {
if didLog {
log2.Warningf("Cancelled waiting for object due to errors")
}
for _, e := range v.Errors {
a.HandleError(ref, fmt.Errorf(e.Error))
}
return false
}
if a.o.WaitObjectTimeout != 0 && time.Now().Sub(startTime) >= a.o.WaitObjectTimeout {
err := fmt.Errorf("timed out while waiting for object")
log2.Warningf(err.Error())
a.HandleError(ref, err)
return false
}
if !didLog {
log2.Infof("Waiting for object to get ready...")
didLog = true
lastLogTime = time.Now()
} else if didLog && time.Now().Sub(lastLogTime) >= 10*time.Second {
log2.Infof("Still waiting for hook to get ready (%s)...", time.Now().Sub(startTime).String())
lastLogTime = time.Now()
}
time.Sleep(500 * time.Millisecond)
}
return false
}
func (a *ApplyUtil) applyDeploymentItem(d *deployment.DeploymentItem) {
if !d.CheckInclusionForDeploy() {
a.DoLog(d, log.InfoLevel, "Skipping")
return
}
var toDelete []k8s2.ObjectRef
for _, x := range d.Config.DeleteObjects {
for _, gvk := range a.k.GetGVKs(x.Group, x.Version, x.Kind) {
ref := k8s2.ObjectRef{
GVK: gvk,
Name: x.Name,
Namespace: x.Namespace,
}
toDelete = append(toDelete, ref)
}
}
if len(toDelete) != 0 {
log.Infof("Deleting %d objects", len(toDelete))
for _, ref := range toDelete {
a.DeleteObject(ref, false)
}
}
initialDeploy := true
for _, o := range d.Objects {
if a.ru.GetRemoteObject(o.GetK8sRef()) != nil {
initialDeploy = false
}
}
h := HooksUtil{a: a}
if initialDeploy {
h.RunHooks(d, []string{"pre-deploy-initial", "pre-deploy"})
} else {
h.RunHooks(d, []string{"pre-deploy-upgrade", "pre-deploy"})
}
var applyObjects []*uo.UnstructuredObject
for _, o := range d.Objects {
if h.GetHook(o) != nil {
continue
}
applyObjects = append(applyObjects, o)
}
if len(applyObjects) != 0 {
a.DoLog(d, log.InfoLevel, "Applying %d objects", len(applyObjects))
}
startTime := time.Now()
didLog := false
for i, o := range applyObjects {
a.ApplyObject(o, false, false)
if time.Now().Sub(startTime) >= 10*time.Second || (didLog && i == len(applyObjects)-1) {
a.DoLog(d, log.InfoLevel, "...applied %d of %d objects", i+1, len(applyObjects))
startTime = time.Now()
didLog = true
}
}
if initialDeploy {
h.RunHooks(d, []string{"post-deploy-initial", "post-deploy"})
} else {
h.RunHooks(d, []string{"post-deploy-upgrade", "post-deploy"})
}
}
func (a *ApplyUtil) ApplyDeployments() {
log.Infof("Running server-side apply for all objects")
var wg sync.WaitGroup
for _, d_ := range a.deployments {
d := d_
if a.abortSignal {
break
}
wg.Add(1)
go func() {
defer wg.Done()
a.applyDeploymentItem(d)
}()
barrier := (d.Config.Barrier != nil && *d.Config.Barrier) || d.Barrier
if barrier {
log.Infof("Waiting on barrier...")
wg.Wait()
}
}
wg.Wait()
}
func (a *ApplyUtil) ReplaceObject(ref k8s2.ObjectRef, firstVersion *uo.UnstructuredObject, callback func(o *uo.UnstructuredObject) (*uo.UnstructuredObject, error)) {
firstCall := true
for true {
var remote *uo.UnstructuredObject
if firstCall && firstVersion != nil {
remote = firstVersion
} else {
o2, apiWarnings, err := a.k.GetSingleObject(ref)
a.dew.AddApiWarnings(ref, apiWarnings)
if err != nil && !errors.IsNotFound(err) {
a.HandleError(ref, err)
return
}
remote = o2
}
if remote == nil {
a.handleResult(remote, false)
return
}
firstCall = false
remoteCopy := remote.Clone()
modified, err := callback(remoteCopy)
if err != nil {
a.HandleError(ref, err)
return
}
if reflect.DeepEqual(remote.Object, modified.Object) {
a.handleResult(remote, false)
return
}
result, apiWarnings, err := a.k.UpdateObject(modified, k8s.UpdateOptions{})
a.dew.AddApiWarnings(ref, apiWarnings)
if err != nil {
if errors.IsConflict(err) {
log.Warningf("Conflict while patching %s. Retrying...", ref.String())
continue
} else {
a.HandleError(ref, err)
return
}
}
a.handleResult(result, false)
return
}
a.HandleError(ref, fmt.Errorf("unexpected end of loop"))
}
func (a *ApplyUtil) DoLog(d *deployment.DeploymentItem, level log.Level, s string, f ...interface{}) {
s = fmt.Sprintf("%s: %s", d.RelToProjectItemDir, fmt.Sprintf(s, f...))
log.StandardLogger().Logf(level, s)
}
func (a *ApplyUtil) GetDeletedObjectsList() []k8s2.ObjectRef {
var ret []k8s2.ObjectRef
for ref := range a.deletedObjects {
ret = append(ret, ref)
}
return ret
}
func (a *ApplyUtil) GetAppliedHookObjects() []*types.RefAndObject {
var ret []*types.RefAndObject
for _, o := range a.appliedHookObjects {
ret = append(ret, &types.RefAndObject{
Ref: o.GetK8sRef(),
Object: o,
})
}
return ret
}