-
Notifications
You must be signed in to change notification settings - Fork 0
/
cloneset_scale.go
209 lines (175 loc) · 6.95 KB
/
cloneset_scale.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
package scale
import (
"context"
"fmt"
"sync"
appsv1alpha1 "github.com/openkruise/kruise/apis/apps/v1alpha1"
clonesetcore "github.com/openkruise/kruise/pkg/controller/cloneset/core"
clonesetutils "github.com/openkruise/kruise/pkg/controller/cloneset/utils"
"github.com/openkruise/kruise/pkg/util"
"github.com/openkruise/kruise/pkg/util/expectations"
apps "k8s.io/api/apps/v1"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/client-go/tools/record"
"k8s.io/klog"
"sigs.k8s.io/controller-runtime/pkg/client"
)
const (
// LengthOfInstanceID is the length of instance-id
LengthOfInstanceID = 5
// When batching pod creates, initialBatchSize is the size of the initial batch.
initialBatchSize = 1
)
// Interface for managing replicas including create and delete pod/pvc.
type Interface interface {
Manage(
currentCS, updateCS *appsv1alpha1.CloneSet,
currentRevision, updateRevision string,
pods []*v1.Pod, pvcs []*v1.PersistentVolumeClaim,
) (bool, error)
}
// New returns a scale control.
func New(c client.Client, recorder record.EventRecorder, exp expectations.ScaleExpectations) Interface {
return &realControl{Client: c, recorder: recorder, exp: exp}
}
type realControl struct {
client.Client
recorder record.EventRecorder
exp expectations.ScaleExpectations
}
func (r *realControl) Manage(
currentCS, updateCS *appsv1alpha1.CloneSet,
currentRevision, updateRevision string,
pods []*v1.Pod, pvcs []*v1.PersistentVolumeClaim,
) (bool, error) {
if updateCS.Spec.Replicas == nil {
return false, fmt.Errorf("spec.Replicas is nil")
}
controllerKey := clonesetutils.GetControllerKey(updateCS)
coreControl := clonesetcore.New(updateCS)
if !coreControl.IsReadyToScale() {
klog.Warningf("CloneSet %s skip scaling for not ready to scale", controllerKey)
return false, nil
}
if podsToDelete := getPodsToDelete(updateCS, pods); len(podsToDelete) > 0 {
klog.V(3).Infof("CloneSet %s begin to delete pods in podsToDelete: %v", controllerKey, podsToDelete)
return r.deletePods(updateCS, podsToDelete, pvcs)
}
updatedPods, notUpdatedPods := clonesetutils.SplitPodsByRevision(pods, updateRevision)
diff, currentRevDiff := calculateDiffs(updateCS, updateRevision == currentRevision, len(pods), len(notUpdatedPods))
if diff < 0 {
// total number of this creation
expectedCreations := diff * -1
// lack number of current version
expectedCurrentCreations := 0
if currentRevDiff < 0 {
expectedCurrentCreations = currentRevDiff * -1
}
klog.V(3).Infof("CloneSet %s begin to scale out %d pods including %d (current rev)",
controllerKey, expectedCreations, expectedCurrentCreations)
// available instance-id come from free pvc
availableIDs := getOrGenAvailableIDs(expectedCreations, pods, pvcs)
// existing pvc names
existingPVCNames := sets.NewString()
for _, pvc := range pvcs {
existingPVCNames.Insert(pvc.Name)
}
return r.createPods(expectedCreations, expectedCurrentCreations,
currentCS, updateCS, currentRevision, updateRevision, availableIDs.List(), existingPVCNames)
} else if diff > 0 {
klog.V(3).Infof("CloneSet %s begin to scale in %d pods including %d (current rev)",
controllerKey, diff, currentRevDiff)
podsToDelete := choosePodsToDelete(diff, currentRevDiff, notUpdatedPods, updatedPods)
return r.deletePods(updateCS, podsToDelete, pvcs)
}
return false, nil
}
func (r *realControl) createPods(
expectedCreations, expectedCurrentCreations int,
currentCS, updateCS *appsv1alpha1.CloneSet,
currentRevision, updateRevision string,
availableIDs []string, existingPVCNames sets.String,
) (bool, error) {
// new all pods need to create
coreControl := clonesetcore.New(updateCS)
newPods, err := coreControl.NewVersionedPods(currentCS, updateCS, currentRevision, updateRevision,
expectedCreations, expectedCurrentCreations, availableIDs)
if err != nil {
return false, err
}
podsCreationChan := make(chan *v1.Pod, len(newPods))
for _, p := range newPods {
r.exp.ExpectScale(clonesetutils.GetControllerKey(updateCS), expectations.Create, p.Name)
podsCreationChan <- p
}
var created bool
successPodNames := sync.Map{}
_, err = clonesetutils.DoItSlowly(len(newPods), initialBatchSize, func() error {
pod := <-podsCreationChan
cs := updateCS
if pod.Labels[apps.ControllerRevisionHashLabelKey] == currentRevision {
cs = currentCS
}
var createErr error
if createErr = r.createOnePod(cs, pod, existingPVCNames); createErr != nil {
return createErr
}
created = true
successPodNames.Store(pod.Name, struct{}{})
return nil
})
// rollback to ignore failure pods because the informer won't observe these pods
for _, pod := range newPods {
if _, ok := successPodNames.Load(pod.Name); !ok {
r.exp.ObserveScale(clonesetutils.GetControllerKey(updateCS), expectations.Create, pod.Name)
}
}
return created, err
}
func (r *realControl) createOnePod(cs *appsv1alpha1.CloneSet, pod *v1.Pod, existingPVCNames sets.String) error {
claims := clonesetutils.GetPersistentVolumeClaims(cs, pod)
for _, c := range claims {
if existingPVCNames.Has(c.Name) {
continue
}
r.exp.ExpectScale(clonesetutils.GetControllerKey(cs), expectations.Create, c.Name)
if err := r.Create(context.TODO(), &c); err != nil {
r.exp.ObserveScale(clonesetutils.GetControllerKey(cs), expectations.Create, c.Name)
r.recorder.Eventf(cs, v1.EventTypeWarning, "FailedCreate", "failed to create pvc: %v, pvc: %v", err, util.DumpJSON(c))
return err
}
}
if err := r.Create(context.TODO(), pod); err != nil {
r.recorder.Eventf(cs, v1.EventTypeWarning, "FailedCreate", "failed to create pod: %v, pod: %v", err, util.DumpJSON(pod))
return err
}
r.recorder.Eventf(cs, v1.EventTypeNormal, "SuccessfulCreate", "succeed to create pod %s", pod.Name)
return nil
}
func (r *realControl) deletePods(cs *appsv1alpha1.CloneSet, podsToDelete []*v1.Pod, pvcs []*v1.PersistentVolumeClaim) (bool, error) {
var deleted bool
for _, pod := range podsToDelete {
r.exp.ExpectScale(clonesetutils.GetControllerKey(cs), expectations.Delete, pod.Name)
if err := r.Delete(context.TODO(), pod); err != nil {
r.exp.ObserveScale(clonesetutils.GetControllerKey(cs), expectations.Delete, pod.Name)
r.recorder.Eventf(cs, v1.EventTypeWarning, "FailedDelete", "failed to delete pod %s: %v", pod.Name, err)
return deleted, err
}
deleted = true
r.recorder.Event(cs, v1.EventTypeNormal, "SuccessfulDelete", fmt.Sprintf("succeed to delete pod %s", pod.Name))
// delete pvcs which have the same instance-id
for _, pvc := range pvcs {
if pvc.Labels[appsv1alpha1.CloneSetInstanceID] != pod.Labels[appsv1alpha1.CloneSetInstanceID] {
continue
}
r.exp.ExpectScale(clonesetutils.GetControllerKey(cs), expectations.Delete, pvc.Name)
if err := r.Delete(context.TODO(), pvc); err != nil {
r.exp.ObserveScale(clonesetutils.GetControllerKey(cs), expectations.Delete, pvc.Name)
r.recorder.Eventf(cs, v1.EventTypeWarning, "FailedDelete", "failed to delete pvc %s: %v", pvc.Name, err)
return deleted, err
}
}
}
return deleted, nil
}