1
16
17 package deployment
18
19 import (
20 "context"
21 apps "k8s.io/api/apps/v1"
22 v1 "k8s.io/api/core/v1"
23 "k8s.io/apimachinery/pkg/types"
24 "k8s.io/kubernetes/pkg/controller"
25 "k8s.io/kubernetes/pkg/controller/deployment/util"
26 )
27
28
29 func (dc *DeploymentController) rolloutRecreate(ctx context.Context, d *apps.Deployment, rsList []*apps.ReplicaSet, podMap map[types.UID][]*v1.Pod) error {
30
31 newRS, oldRSs, err := dc.getAllReplicaSetsAndSyncRevision(ctx, d, rsList, false)
32 if err != nil {
33 return err
34 }
35 allRSs := append(oldRSs, newRS)
36 activeOldRSs := controller.FilterActiveReplicaSets(oldRSs)
37
38
39 scaledDown, err := dc.scaleDownOldReplicaSetsForRecreate(ctx, activeOldRSs, d)
40 if err != nil {
41 return err
42 }
43 if scaledDown {
44
45 return dc.syncRolloutStatus(ctx, allRSs, newRS, d)
46 }
47
48
49 if oldPodsRunning(newRS, oldRSs, podMap) {
50 return dc.syncRolloutStatus(ctx, allRSs, newRS, d)
51 }
52
53
54 if newRS == nil {
55 newRS, oldRSs, err = dc.getAllReplicaSetsAndSyncRevision(ctx, d, rsList, true)
56 if err != nil {
57 return err
58 }
59 allRSs = append(oldRSs, newRS)
60 }
61
62
63 if _, err := dc.scaleUpNewReplicaSetForRecreate(ctx, newRS, d); err != nil {
64 return err
65 }
66
67 if util.DeploymentComplete(d, &d.Status) {
68 if err := dc.cleanupDeployment(ctx, oldRSs, d); err != nil {
69 return err
70 }
71 }
72
73
74 return dc.syncRolloutStatus(ctx, allRSs, newRS, d)
75 }
76
77
78 func (dc *DeploymentController) scaleDownOldReplicaSetsForRecreate(ctx context.Context, oldRSs []*apps.ReplicaSet, deployment *apps.Deployment) (bool, error) {
79 scaled := false
80 for i := range oldRSs {
81 rs := oldRSs[i]
82
83 if *(rs.Spec.Replicas) == 0 {
84 continue
85 }
86 scaledRS, updatedRS, err := dc.scaleReplicaSetAndRecordEvent(ctx, rs, 0, deployment)
87 if err != nil {
88 return false, err
89 }
90 if scaledRS {
91 oldRSs[i] = updatedRS
92 scaled = true
93 }
94 }
95 return scaled, nil
96 }
97
98
99 func oldPodsRunning(newRS *apps.ReplicaSet, oldRSs []*apps.ReplicaSet, podMap map[types.UID][]*v1.Pod) bool {
100 if oldPods := util.GetActualReplicaCountForReplicaSets(oldRSs); oldPods > 0 {
101 return true
102 }
103 for rsUID, podList := range podMap {
104
105 if newRS != nil && newRS.UID == rsUID {
106 continue
107 }
108 for _, pod := range podList {
109 switch pod.Status.Phase {
110 case v1.PodFailed, v1.PodSucceeded:
111
112 continue
113 case v1.PodUnknown:
114
115
116
117
118 return true
119 default:
120
121 return true
122 }
123 }
124 }
125 return false
126 }
127
128
129 func (dc *DeploymentController) scaleUpNewReplicaSetForRecreate(ctx context.Context, newRS *apps.ReplicaSet, deployment *apps.Deployment) (bool, error) {
130 scaled, _, err := dc.scaleReplicaSetAndRecordEvent(ctx, newRS, *(deployment.Spec.Replicas), deployment)
131 return scaled, err
132 }
133
View as plain text