-
Notifications
You must be signed in to change notification settings - Fork 10
/
update.go
791 lines (682 loc) · 29.1 KB
/
update.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
/*
Copyright 2023 The KusionStack Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package synccontrol
import (
"context"
"encoding/json"
"fmt"
"sort"
"time"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/equality"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/client-go/tools/record"
"k8s.io/client-go/util/retry"
"sigs.k8s.io/controller-runtime/pkg/client"
appsv1alpha1 "kusionstack.io/operating/apis/apps/v1alpha1"
"kusionstack.io/operating/pkg/controllers/collaset/podcontext"
"kusionstack.io/operating/pkg/controllers/collaset/podcontrol"
"kusionstack.io/operating/pkg/controllers/collaset/pvccontrol"
"kusionstack.io/operating/pkg/controllers/collaset/utils"
collasetutils "kusionstack.io/operating/pkg/controllers/collaset/utils"
controllerutils "kusionstack.io/operating/pkg/controllers/utils"
"kusionstack.io/operating/pkg/controllers/utils/expectations"
utilspoddecoration "kusionstack.io/operating/pkg/controllers/utils/poddecoration"
"kusionstack.io/operating/pkg/controllers/utils/podopslifecycle"
commonutils "kusionstack.io/operating/pkg/utils"
)
type PodUpdateInfo struct {
*utils.PodWrapper
UpdatedPod *corev1.Pod
InPlaceUpdateSupport bool
OnlyMetadataChanged bool
// indicate if this pod has up-to-date revision from its owner, like CollaSet
IsUpdatedRevision bool
// carry the pod's current revision
CurrentRevision *appsv1.ControllerRevision
// carry the desired update revision
UpdateRevision *appsv1.ControllerRevision
// indicates effected PodDecorations changed
PodDecorationChanged bool
// indicate if the pvc template changed
PvcTmpHashChanged bool
CurrentPodDecorations map[string]*appsv1alpha1.PodDecoration
UpdatedPodDecorations map[string]*appsv1alpha1.PodDecoration
// indicates the PodOpsLifecycle is started.
isDuringOps bool
// for replace update
// judge pod in replace updating
isInReplacing bool
// replace new created pod
replacePairNewPodInfo *PodUpdateInfo
// replace origin pod
replacePairOriginPodName string
}
func attachPodUpdateInfo(ctx context.Context, cls *appsv1alpha1.CollaSet, pods []*collasetutils.PodWrapper, resource *collasetutils.RelatedResources) ([]*PodUpdateInfo, error) {
podUpdateInfoList := make([]*PodUpdateInfo, len(pods))
for i, pod := range pods {
updateInfo := &PodUpdateInfo{
PodWrapper: pod,
}
currentPDs, err := resource.PDGetter.GetOnPod(ctx, pod.Pod)
if err != nil {
return nil, err
}
updatedPDs, err := resource.PDGetter.GetEffective(ctx, pod.Pod)
if err != nil {
return nil, err
}
if len(currentPDs) != len(updatedPDs) {
updateInfo.PodDecorationChanged = true
} else {
revisionSets := sets.NewString()
for rev := range currentPDs {
revisionSets.Insert(rev)
}
for rev := range updatedPDs {
if !revisionSets.Has(rev) {
updateInfo.PodDecorationChanged = true
break
}
}
}
updateInfo.CurrentPodDecorations = currentPDs
updateInfo.UpdatedPodDecorations = updatedPDs
updateInfo.UpdateRevision = resource.UpdatedRevision
// decide this pod current revision, or nil if not indicated
if pod.Labels != nil {
currentRevisionName, exist := pod.Labels[appsv1.ControllerRevisionHashLabelKey]
if exist {
if currentRevisionName == resource.UpdatedRevision.Name {
updateInfo.IsUpdatedRevision = true
updateInfo.CurrentRevision = resource.UpdatedRevision
} else {
updateInfo.IsUpdatedRevision = false
for _, rv := range resource.Revisions {
if currentRevisionName == rv.Name {
updateInfo.CurrentRevision = rv
}
}
}
}
}
// decide whether the PodOpsLifecycle is during ops or not
updateInfo.isDuringOps = podopslifecycle.IsDuringOps(utils.UpdateOpsLifecycleAdapter, pod)
updateInfo.PvcTmpHashChanged, err = pvccontrol.IsPodPvcTmpChanged(cls, pod.Pod, resource.ExistingPvcs)
if err != nil {
return nil, fmt.Errorf("fail to check pvc template changed, %v", err)
}
podUpdateInfoList[i] = updateInfo
}
// attach replace info
var podUpdateInfoMap = make(map[string]*PodUpdateInfo)
for _, podUpdateInfo := range podUpdateInfoList {
podUpdateInfoMap[podUpdateInfo.Name] = podUpdateInfo
}
replacePodMap := classifyPodReplacingMapping(pods)
for originPodName, replacePairNewPod := range replacePodMap {
originPodInfo := podUpdateInfoMap[originPodName]
if replacePairNewPod != nil {
originPodInfo.isInReplacing = true
// replace origin pod not go through lifecycle, mark during ops manual
originPodInfo.isDuringOps = true
replacePairNewPodInfo := podUpdateInfoMap[replacePairNewPod.Name]
replacePairNewPodInfo.isInReplacing = true
replacePairNewPodInfo.replacePairOriginPodName = originPodName
originPodInfo.replacePairNewPodInfo = replacePairNewPodInfo
} else {
_, replaceIndicated := originPodInfo.Labels[appsv1alpha1.PodReplaceIndicationLabelKey]
_, replaceByReplaceUpdate := originPodInfo.Labels[appsv1alpha1.PodReplaceByReplaceUpdateLabelKey]
if replaceIndicated && replaceByReplaceUpdate {
originPodInfo.isInReplacing = true
originPodInfo.isDuringOps = true
}
}
}
return podUpdateInfoList, nil
}
func decidePodToUpdate(cls *appsv1alpha1.CollaSet, podInfos []*PodUpdateInfo) []*PodUpdateInfo {
if cls.Spec.UpdateStrategy.RollingUpdate != nil && cls.Spec.UpdateStrategy.RollingUpdate.ByLabel != nil {
return decidePodToUpdateByLabel(cls, podInfos)
}
return decidePodToUpdateByPartition(cls, podInfos)
}
func decidePodToUpdateByLabel(_ *appsv1alpha1.CollaSet, podInfos []*PodUpdateInfo) (podToUpdate []*PodUpdateInfo) {
for i := range podInfos {
if _, exist := podInfos[i].Labels[appsv1alpha1.CollaSetUpdateIndicateLabelKey]; exist {
// filter pod which is in replace update and is the new created pod
if podInfos[i].isInReplacing && podInfos[i].replacePairOriginPodName != "" {
continue
}
podToUpdate = append(podToUpdate, podInfos[i])
continue
}
// already in replace update.
if podInfos[i].isInReplacing && podInfos[i].replacePairNewPodInfo != nil {
podToUpdate = append(podToUpdate, podInfos[i])
continue
}
if podInfos[i].PodDecorationChanged {
if podInfos[i].isInReplacing && podInfos[i].replacePairOriginPodName != "" {
continue
}
podToUpdate = append(podToUpdate, podInfos[i])
}
}
return podToUpdate
}
func decidePodToUpdateByPartition(cls *appsv1alpha1.CollaSet, podInfos []*PodUpdateInfo) (podToUpdate []*PodUpdateInfo) {
filteredPodInfos := filterReplacingNewCreatedPod(podInfos)
if cls.Spec.UpdateStrategy.RollingUpdate == nil ||
cls.Spec.UpdateStrategy.RollingUpdate.ByPartition.Partition == nil {
return filteredPodInfos
}
ordered := orderByDefault(filteredPodInfos)
sort.Sort(ordered)
partition := int(*cls.Spec.UpdateStrategy.RollingUpdate.ByPartition.Partition)
if partition >= len(filteredPodInfos) {
return filteredPodInfos
}
podToUpdate = filteredPodInfos[:partition]
for i := partition; i < len(filteredPodInfos); i++ {
if podInfos[i].PodDecorationChanged {
podToUpdate = append(podToUpdate, podInfos[i])
}
}
return podToUpdate
}
// filter these pods in replacing and is new created pod
func filterReplacingNewCreatedPod(podInfos []*PodUpdateInfo) (filteredPodInfos []*PodUpdateInfo) {
for _, podInfo := range podInfos {
if podInfo.isInReplacing && podInfo.replacePairOriginPodName != "" {
continue
}
filteredPodInfos = append(filteredPodInfos, podInfo)
}
return filteredPodInfos
}
type orderByDefault []*PodUpdateInfo
func (o orderByDefault) Len() int {
return len(o)
}
func (o orderByDefault) Swap(i, j int) { o[i], o[j] = o[j], o[i] }
func (o orderByDefault) Less(i, j int) bool {
l, r := o[i], o[j]
if l.IsUpdatedRevision != r.IsUpdatedRevision {
return l.IsUpdatedRevision
}
if l.isDuringOps != r.isDuringOps {
return l.isDuringOps
}
if controllerutils.BeforeReady(l.Pod) == controllerutils.BeforeReady(r.Pod) &&
l.PodDecorationChanged != r.PodDecorationChanged {
return l.PodDecorationChanged
}
return utils.ComparePod(l.Pod, r.Pod)
}
type PodUpdater interface {
FulfillPodUpdatedInfo(revision *appsv1.ControllerRevision, podUpdateInfo *PodUpdateInfo) error
BeginUpdatePod(resources *collasetutils.RelatedResources, podCh chan *PodUpdateInfo) (bool, error)
FilterAllowOpsPods(podToUpdate []*PodUpdateInfo, ownedIDs map[int]*appsv1alpha1.ContextDetail, resources *collasetutils.RelatedResources, podCh chan *PodUpdateInfo) (*time.Duration, error)
UpgradePod(podInfo *PodUpdateInfo) error
GetPodUpdateFinishStatus(podUpdateInfo *PodUpdateInfo) (bool, string, error)
FinishUpdatePod(podInfo *PodUpdateInfo) error
}
type GenericPodUpdater struct {
collaSet *appsv1alpha1.CollaSet
ctx context.Context
podControl podcontrol.Interface
recorder record.EventRecorder
client.Client
}
func (u *GenericPodUpdater) BeginUpdatePod(resources *collasetutils.RelatedResources, podCh chan *PodUpdateInfo) (bool, error) {
succCount, err := controllerutils.SlowStartBatch(len(podCh), controllerutils.SlowStartInitialBatchSize, false, func(int, error) error {
podInfo := <-podCh
u.recorder.Eventf(podInfo.Pod, corev1.EventTypeNormal, "PodUpdateLifecycle", "try to begin PodOpsLifecycle for updating Pod of CollaSet")
if updated, err := podopslifecycle.Begin(u.Client, collasetutils.UpdateOpsLifecycleAdapter, podInfo.Pod, func(obj client.Object) (bool, error) {
if !podInfo.OnlyMetadataChanged && !podInfo.InPlaceUpdateSupport {
return podopslifecycle.WhenBeginDelete(obj)
}
return false, nil
}); err != nil {
return fmt.Errorf("fail to begin PodOpsLifecycle for updating Pod %s/%s: %s", podInfo.Namespace, podInfo.Name, err)
} else if updated {
// add an expectation for this pod update, before next reconciling
if err := collasetutils.ActiveExpectations.ExpectUpdate(u.collaSet, expectations.Pod, podInfo.Name, podInfo.ResourceVersion); err != nil {
return err
}
}
return nil
})
updating := succCount > 0
if err != nil {
collasetutils.AddOrUpdateCondition(resources.NewStatus, appsv1alpha1.CollaSetUpdate, err, "UpdateFailed", err.Error())
return updating, err
} else {
collasetutils.AddOrUpdateCondition(resources.NewStatus, appsv1alpha1.CollaSetUpdate, nil, "Updated", "")
}
return updating, nil
}
func (u *GenericPodUpdater) FilterAllowOpsPods(podToUpdate []*PodUpdateInfo, ownedIDs map[int]*appsv1alpha1.ContextDetail, resources *collasetutils.RelatedResources, podCh chan *PodUpdateInfo) (*time.Duration, error) {
var recordedRequeueAfter *time.Duration
needUpdateContext := false
for i := range podToUpdate {
podInfo := podToUpdate[i]
requeueAfter, allowed := podopslifecycle.AllowOps(collasetutils.UpdateOpsLifecycleAdapter, realValue(u.collaSet.Spec.UpdateStrategy.OperationDelaySeconds), podInfo.Pod)
if !allowed {
u.recorder.Eventf(podInfo, corev1.EventTypeNormal, "PodUpdateLifecycle", "Pod %s is not allowed to update", commonutils.ObjectKeyString(podInfo.Pod))
continue
}
if requeueAfter != nil {
u.recorder.Eventf(podInfo, corev1.EventTypeNormal, "PodUpdateLifecycle", "delay Pod update for %d seconds", requeueAfter.Seconds())
if recordedRequeueAfter == nil || *requeueAfter < *recordedRequeueAfter {
recordedRequeueAfter = requeueAfter
}
continue
}
if !ownedIDs[podInfo.ID].Contains(podcontext.RevisionContextDataKey, resources.UpdatedRevision.Name) {
needUpdateContext = true
ownedIDs[podInfo.ID].Put(podcontext.RevisionContextDataKey, resources.UpdatedRevision.Name)
}
if podInfo.PodDecorationChanged {
decorationStr := utilspoddecoration.GetDecorationInfoString(podInfo.UpdatedPodDecorations)
if val, ok := ownedIDs[podInfo.ID].Get(podcontext.PodDecorationRevisionKey); !ok || val != decorationStr {
needUpdateContext = true
ownedIDs[podInfo.ID].Put(podcontext.PodDecorationRevisionKey, decorationStr)
}
}
if podInfo.IsUpdatedRevision && !podInfo.PodDecorationChanged && !podInfo.PvcTmpHashChanged {
continue
}
// if Pod has not been updated, update it.
podCh <- podToUpdate[i]
}
// mark Pod to use updated revision before updating it.
if needUpdateContext {
u.recorder.Eventf(u.collaSet, corev1.EventTypeNormal, "UpdateToPodContext", "try to update ResourceContext for CollaSet")
err := retry.RetryOnConflict(retry.DefaultRetry, func() error {
return podcontext.UpdateToPodContext(u.Client, u.collaSet, ownedIDs)
})
return recordedRequeueAfter, err
}
return recordedRequeueAfter, nil
}
func (u *GenericPodUpdater) FinishUpdatePod(podInfo *PodUpdateInfo) error {
if updated, err := podopslifecycle.Finish(u.Client, collasetutils.UpdateOpsLifecycleAdapter, podInfo.Pod); err != nil {
return fmt.Errorf("failed to finish PodOpsLifecycle for updating Pod %s/%s: %s", podInfo.Namespace, podInfo.Name, err)
} else if updated {
// add an expectation for this pod update, before next reconciling
if err := collasetutils.ActiveExpectations.ExpectUpdate(u.collaSet, expectations.Pod, podInfo.Name, podInfo.ResourceVersion); err != nil {
return err
}
u.recorder.Eventf(podInfo.Pod,
corev1.EventTypeNormal,
"UpdateReady", "pod %s/%s update finished", podInfo.Namespace, podInfo.Name)
}
return nil
}
func newPodUpdater(ctx context.Context, client client.Client, cls *appsv1alpha1.CollaSet, podControl podcontrol.Interface, recorder record.EventRecorder) PodUpdater {
genericPodUpdater := &GenericPodUpdater{collaSet: cls, ctx: ctx, Client: client, podControl: podControl, recorder: recorder}
switch cls.Spec.UpdateStrategy.PodUpdatePolicy {
case appsv1alpha1.CollaSetRecreatePodUpdateStrategyType:
return &recreatePodUpdater{collaSet: cls, ctx: ctx, Client: client, podControl: podControl, recorder: recorder, GenericPodUpdater: *genericPodUpdater}
case appsv1alpha1.CollaSetInPlaceOnlyPodUpdateStrategyType:
// In case of using native K8s, Pod is only allowed to update with container image, so InPlaceOnly policy is
// implemented with InPlaceIfPossible policy as default for compatibility.
return &inPlaceIfPossibleUpdater{collaSet: cls, ctx: ctx, Client: client}
case appsv1alpha1.CollaSetReplacePodUpdateStrategyType:
return &replaceUpdatePodUpdater{collaSet: cls, ctx: ctx, Client: client, podControl: podControl, recorder: recorder}
default:
return &inPlaceIfPossibleUpdater{collaSet: cls, ctx: ctx, Client: client, podControl: podControl, recorder: recorder, GenericPodUpdater: *genericPodUpdater}
}
}
type PodStatus struct {
ContainerStates map[string]*ContainerStatus `json:"containerStates,omitempty"`
}
type ContainerStatus struct {
LatestImage string `json:"latestImage,omitempty"`
LastImageID string `json:"lastImageID,omitempty"`
}
type inPlaceIfPossibleUpdater struct {
collaSet *appsv1alpha1.CollaSet
ctx context.Context
podControl podcontrol.Interface
recorder record.EventRecorder
GenericPodUpdater
client.Client
}
func (u *inPlaceIfPossibleUpdater) FulfillPodUpdatedInfo(
updatedRevision *appsv1.ControllerRevision,
podUpdateInfo *PodUpdateInfo) error {
// 1. build pod from current and updated revision
ownerRef := metav1.NewControllerRef(u.collaSet, appsv1alpha1.GroupVersion.WithKind("CollaSet"))
// TODO: use cache
currentPod, err := collasetutils.NewPodFrom(u.collaSet, ownerRef, podUpdateInfo.CurrentRevision, func(in *corev1.Pod) error {
return utilspoddecoration.PatchListOfDecorations(in, podUpdateInfo.CurrentPodDecorations)
})
if err != nil {
return fmt.Errorf("fail to build Pod from current revision %s: %v", podUpdateInfo.CurrentRevision.Name, err)
}
// TODO: use cache
podUpdateInfo.UpdatedPod, err = collasetutils.NewPodFrom(u.collaSet, ownerRef, updatedRevision, func(in *corev1.Pod) error {
return utilspoddecoration.PatchListOfDecorations(in, podUpdateInfo.UpdatedPodDecorations)
})
if err != nil {
return fmt.Errorf("fail to build Pod from updated revision %s: %v", updatedRevision.Name, err)
}
if podUpdateInfo.PvcTmpHashChanged {
podUpdateInfo.InPlaceUpdateSupport, podUpdateInfo.OnlyMetadataChanged = false, false
return nil
}
// 2. compare current and updated pods. Only pod image and metadata are supported to update in-place
// TODO: use cache
podUpdateInfo.InPlaceUpdateSupport, podUpdateInfo.OnlyMetadataChanged = u.diffPod(currentPod, podUpdateInfo.UpdatedPod)
// 3. if pod has changes more than metadata and image
if !podUpdateInfo.InPlaceUpdateSupport {
return nil
}
podUpdateInfo.UpdatedPod, err = utils.PatchToPod(currentPod, podUpdateInfo.UpdatedPod, podUpdateInfo.Pod)
if err != nil {
return err
}
if podUpdateInfo.OnlyMetadataChanged {
if podUpdateInfo.UpdatedPod.Annotations != nil {
delete(podUpdateInfo.UpdatedPod.Annotations, appsv1alpha1.LastPodStatusAnnotationKey)
}
} else {
containerCurrentStatusMapping := map[string]*corev1.ContainerStatus{}
for i := range podUpdateInfo.Status.ContainerStatuses {
status := podUpdateInfo.Status.ContainerStatuses[i]
containerCurrentStatusMapping[status.Name] = &status
}
podStatus := &PodStatus{ContainerStates: map[string]*ContainerStatus{}}
for _, container := range podUpdateInfo.UpdatedPod.Spec.Containers {
podStatus.ContainerStates[container.Name] = &ContainerStatus{
// store image of each container in updated Pod
LatestImage: container.Image,
}
containerCurrentStatus, exist := containerCurrentStatusMapping[container.Name]
if !exist {
continue
}
// store image ID of each container in current Pod
podStatus.ContainerStates[container.Name].LastImageID = containerCurrentStatus.ImageID
}
podStatusStr, err := json.Marshal(podStatus)
if err != nil {
return err
}
if podUpdateInfo.UpdatedPod.Annotations == nil {
podUpdateInfo.UpdatedPod.Annotations = map[string]string{}
}
podUpdateInfo.UpdatedPod.Annotations[appsv1alpha1.LastPodStatusAnnotationKey] = string(podStatusStr)
}
return nil
}
func (u *inPlaceIfPossibleUpdater) UpgradePod(podInfo *PodUpdateInfo) error {
if podInfo.OnlyMetadataChanged || podInfo.InPlaceUpdateSupport {
// if pod template changes only include metadata or support in-place update, just apply these changes to pod directly
if err := u.podControl.UpdatePod(podInfo.UpdatedPod); err != nil {
return fmt.Errorf("fail to update Pod %s/%s when updating by in-place: %s", podInfo.Namespace, podInfo.Name, err)
} else {
podInfo.Pod = podInfo.UpdatedPod
u.recorder.Eventf(podInfo.Pod,
corev1.EventTypeNormal,
"UpdatePod",
"succeed to update Pod %s/%s to from revision %s to revision %s by in-place",
podInfo.Namespace, podInfo.Name,
podInfo.CurrentRevision.Name,
podInfo.UpdateRevision.Name)
if err := collasetutils.ActiveExpectations.ExpectUpdate(u.collaSet, expectations.Pod, podInfo.Name, podInfo.UpdatedPod.ResourceVersion); err != nil {
return err
}
}
} else {
// if pod has changes not in-place supported, recreate it
return recreatePod(u.collaSet, podInfo, u.podControl, u.recorder)
}
return nil
}
func recreatePod(collaSet *appsv1alpha1.CollaSet, podInfo *PodUpdateInfo, podControl podcontrol.Interface, recorder record.EventRecorder) error {
if err := podControl.DeletePod(podInfo.Pod); err != nil {
return fmt.Errorf("fail to delete Pod %s/%s when updating by recreate: %s", podInfo.Namespace, podInfo.Name, err)
}
recorder.Eventf(podInfo.Pod,
corev1.EventTypeNormal,
"UpdatePod",
"succeed to update Pod %s/%s to from revision %s to revision %s by recreate",
podInfo.Namespace,
podInfo.Name,
podInfo.CurrentRevision.Name,
podInfo.UpdateRevision.Name)
if err := collasetutils.ActiveExpectations.ExpectDelete(collaSet, expectations.Pod, podInfo.Name); err != nil {
return err
}
return nil
}
func (u *inPlaceIfPossibleUpdater) diffPod(currentPod, updatedPod *corev1.Pod) (inPlaceSetUpdateSupport bool, onlyMetadataChanged bool) {
if len(currentPod.Spec.Containers) != len(updatedPod.Spec.Containers) {
return false, false
}
currentPod = currentPod.DeepCopy()
// sync metadata
currentPod.ObjectMeta = updatedPod.ObjectMeta
// sync image
imageChanged := false
for i := range currentPod.Spec.Containers {
if currentPod.Spec.Containers[i].Image != updatedPod.Spec.Containers[i].Image {
imageChanged = true
currentPod.Spec.Containers[i].Image = updatedPod.Spec.Containers[i].Image
}
}
if !equality.Semantic.DeepEqual(currentPod, updatedPod) {
return false, false
}
if !imageChanged {
return true, true
}
return true, false
}
func (u *inPlaceIfPossibleUpdater) GetPodUpdateFinishStatus(podUpdateInfo *PodUpdateInfo) (finished bool, msg string, err error) {
if !podUpdateInfo.IsUpdatedRevision || podUpdateInfo.PodDecorationChanged {
return false, "not updated revision", nil
}
if podUpdateInfo.Status.ContainerStatuses == nil {
return false, "no container status", nil
}
if podUpdateInfo.Spec.Containers == nil {
return false, "no container spec", nil
}
if len(podUpdateInfo.Spec.Containers) != len(podUpdateInfo.Status.ContainerStatuses) {
return false, "container status number does not match", nil
}
if podUpdateInfo.Annotations == nil {
return true, "no annotations for last container status", nil
}
podLastState := &PodStatus{}
if lastStateJson, exist := podUpdateInfo.Annotations[appsv1alpha1.LastPodStatusAnnotationKey]; !exist {
return true, "no pod last state annotation", nil
} else if err := json.Unmarshal([]byte(lastStateJson), podLastState); err != nil {
msg := fmt.Sprintf("malformat pod last state annotation [%s]: %s", lastStateJson, err)
return false, msg, fmt.Errorf(msg)
}
if podLastState.ContainerStates == nil {
return true, "empty last container state recorded", nil
}
imageMapping := map[string]string{}
for _, containerSpec := range podUpdateInfo.Spec.Containers {
imageMapping[containerSpec.Name] = containerSpec.Image
}
imageIdMapping := map[string]string{}
for _, containerStatus := range podUpdateInfo.Status.ContainerStatuses {
imageIdMapping[containerStatus.Name] = containerStatus.ImageID
}
for containerName, lastContaienrState := range podLastState.ContainerStates {
latestImage := lastContaienrState.LatestImage
lastImageId := lastContaienrState.LastImageID
if currentImage, exist := imageMapping[containerName]; !exist {
// If no this container image recorded, ignore this container.
continue
} else if currentImage != latestImage {
// If container image in pod spec has changed, ignore this container.
continue
}
if currentImageId, exist := imageIdMapping[containerName]; !exist {
// If no this container image id recorded, ignore this container.
continue
} else if currentImageId == lastImageId {
// No image id changed means the pod in-place update has not finished by kubelet.
return false, fmt.Sprintf("container has %s not been updated: last image id %s, current image id %s", containerName, lastImageId, currentImageId), nil
}
}
return true, "", nil
}
// TODO
type inPlaceOnlyPodUpdater struct {
}
func (u *inPlaceOnlyPodUpdater) BeginUpdate(_ *collasetutils.RelatedResources, _ chan *PodUpdateInfo) (updating bool, err error) {
return
}
func (u *inPlaceOnlyPodUpdater) FilterAllowOpsPodsAndUpdatePodContext(_ []*PodUpdateInfo, _ map[int]*appsv1alpha1.ContextDetail, _ *collasetutils.RelatedResources, _ chan *PodUpdateInfo) (requeueAfter *time.Duration, err error) {
return
}
func (u *inPlaceOnlyPodUpdater) FulfillPodUpdatedInfo(_ *appsv1.ControllerRevision, _ *PodUpdateInfo) (inPlaceUpdateSupport bool, onlyMetadataChanged bool, updatedPod *corev1.Pod, err error) {
return
}
func (u *inPlaceOnlyPodUpdater) UpgradePod(_ *appsv1alpha1.CollaSet, _ *PodUpdateInfo) (err error) {
return
}
func (u *inPlaceOnlyPodUpdater) GetPodUpdateFinishStatus(_ *PodUpdateInfo) (finished bool, msg string, err error) {
return
}
type recreatePodUpdater struct {
collaSet *appsv1alpha1.CollaSet
ctx context.Context
podControl podcontrol.Interface
recorder record.EventRecorder
GenericPodUpdater
client.Client
}
func (u *recreatePodUpdater) FulfillPodUpdatedInfo(_ *appsv1.ControllerRevision, _ *PodUpdateInfo) error {
return nil
}
func (u *recreatePodUpdater) UpgradePod(podInfo *PodUpdateInfo) error {
return recreatePod(u.collaSet, podInfo, u.podControl, u.recorder)
}
func (u *recreatePodUpdater) GetPodUpdateFinishStatus(podInfo *PodUpdateInfo) (finished bool, msg string, err error) {
// Recreate policy alway treat Pod as update finished
return podInfo.IsUpdatedRevision && !podInfo.PodDecorationChanged, "", nil
}
type replaceUpdatePodUpdater struct {
collaSet *appsv1alpha1.CollaSet
ctx context.Context
podControl podcontrol.Interface
recorder record.EventRecorder
client.Client
}
func (u *replaceUpdatePodUpdater) BeginUpdatePod(resources *collasetutils.RelatedResources, podCh chan *PodUpdateInfo) (bool, error) {
succCount, err := controllerutils.SlowStartBatch(len(podCh), controllerutils.SlowStartInitialBatchSize, false, func(int, error) error {
podInfo := <-podCh
if podInfo.replacePairNewPodInfo != nil {
replacePairNewPod := podInfo.replacePairNewPodInfo.Pod
newPodRevision, exist := replacePairNewPod.Labels[appsv1.ControllerRevisionHashLabelKey]
if exist && newPodRevision == resources.UpdatedRevision.Name {
return nil
}
u.recorder.Eventf(podInfo.Pod,
corev1.EventTypeNormal,
"ReplaceUpdatePod",
"label to-delete on new pair pod %s/%s because it is not updated revision, current revision: %s, updated revision: %s",
replacePairNewPod.Namespace,
replacePairNewPod.Name,
newPodRevision,
resources.UpdatedRevision.Name)
patch := client.RawPatch(types.StrategicMergePatchType, []byte(fmt.Sprintf(`{"metadata":{"labels":{"%s":"%d"}}}`, appsv1alpha1.PodDeletionIndicationLabelKey, time.Now().UnixNano())))
if patchErr := u.Patch(u.ctx, podInfo.replacePairNewPodInfo.Pod, patch); patchErr != nil {
err := fmt.Errorf("failed to delete replace pair new pod %s/%s %s",
podInfo.replacePairNewPodInfo.Namespace, podInfo.replacePairNewPodInfo.Name, patchErr)
return err
}
}
return nil
})
return succCount > 0, err
}
func (u *replaceUpdatePodUpdater) FilterAllowOpsPods(podToUpdate []*PodUpdateInfo, _ map[int]*appsv1alpha1.ContextDetail, _ *collasetutils.RelatedResources, podCh chan *PodUpdateInfo) (requeueAfter *time.Duration, err error) {
for i, podInfo := range podToUpdate {
if podInfo.IsUpdatedRevision && !podInfo.PodDecorationChanged && !podInfo.PvcTmpHashChanged {
continue
}
podCh <- podToUpdate[i]
}
return nil, err
}
func (u *replaceUpdatePodUpdater) FulfillPodUpdatedInfo(_ *appsv1.ControllerRevision, _ *PodUpdateInfo) (err error) {
return
}
func (u *replaceUpdatePodUpdater) UpgradePod(podInfo *PodUpdateInfo) error {
// add replace indicate label only and wait to replace when syncPods
if _, exist := podInfo.Pod.Labels[appsv1alpha1.PodReplaceIndicationLabelKey]; !exist {
// need replace pod, label pod with replace-indicate
now := time.Now().UnixNano()
patch := client.RawPatch(types.StrategicMergePatchType, []byte(fmt.Sprintf(`{"metadata":{"labels":{"%s":"%v", "%s": "%v"}}}`, appsv1alpha1.PodReplaceIndicationLabelKey, now, appsv1alpha1.PodReplaceByReplaceUpdateLabelKey, true)))
if err := u.Patch(u.ctx, podInfo.Pod, patch); err != nil {
return fmt.Errorf("fail to label origin pod %s/%s with replace indicate label by replaceUpdate: %s", podInfo.Namespace, podInfo.Name, err)
}
u.recorder.Eventf(podInfo.Pod,
corev1.EventTypeNormal,
"UpdatePod",
"succeed to update Pod %s/%s by label to-replace",
podInfo.Namespace,
podInfo.Name,
)
}
return nil
}
func (u *replaceUpdatePodUpdater) GetPodUpdateFinishStatus(podUpdateInfo *PodUpdateInfo) (finished bool, msg string, err error) {
replaceNewPodInfo := podUpdateInfo.replacePairNewPodInfo
if replaceNewPodInfo == nil {
return
}
return isPodUpdatedServiceAvailable(replaceNewPodInfo)
}
func (u *replaceUpdatePodUpdater) FinishUpdatePod(podInfo *PodUpdateInfo) error {
replacePairNewPodInfo := podInfo.replacePairNewPodInfo
if replacePairNewPodInfo != nil {
if _, exist := podInfo.Labels[appsv1alpha1.PodDeletionIndicationLabelKey]; !exist {
patch := client.RawPatch(types.StrategicMergePatchType, []byte(fmt.Sprintf(`{"metadata":{"labels":{"%s":"%d"}}}`, appsv1alpha1.PodDeletionIndicationLabelKey, time.Now().UnixNano())))
if err := u.podControl.PatchPod(podInfo.Pod, patch); err != nil {
return fmt.Errorf("failed to delete replace pair origin pod %s/%s %s", podInfo.Namespace, podInfo.replacePairNewPodInfo.Name, err)
}
}
}
return nil
}
func isPodUpdatedServiceAvailable(podInfo *PodUpdateInfo) (finished bool, msg string, err error) {
if !podInfo.IsUpdatedRevision || podInfo.PodDecorationChanged {
return false, "not updated revision", nil
}
if podInfo.Labels == nil {
return false, "no labels on pod", nil
}
if podInfo.isInReplacing && podInfo.replacePairNewPodInfo != nil {
return false, "replace origin pod", nil
}
if _, serviceAvailable := podInfo.Labels[appsv1alpha1.PodServiceAvailableLabel]; serviceAvailable {
return true, "", nil
}
return false, "pod not service available", nil
}