forked from gardener/machine-controller-manager
/
deployment_util.go
1058 lines (957 loc) · 45.4 KB
/
deployment_util.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
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Copyright 2016 The Kubernetes 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.
This file was copied and modified from the kubernetes/kubernetes project
https://github.com/kubernetes/kubernetes/release-1.8/pkg/controller/deployment/util/deployment_util.go
Modifications Copyright (c) 2017 SAP SE or an SAP affiliate company. All rights reserved.
*/
// Package controller is used to provide the core functionalities of machine-controller-manager
package controller
import (
"fmt"
"reflect"
"sort"
"strconv"
"strings"
"time"
"github.com/golang/glog"
common "github.com/gardener/machine-controller-manager/pkg/apis/cluster/common"
"github.com/gardener/machine-controller-manager/pkg/apis/cluster/v1alpha1"
v1alpha1client "github.com/gardener/machine-controller-manager/pkg/client/clientset/versioned/typed/cluster/v1alpha1"
labelsutil "github.com/gardener/machine-controller-manager/pkg/util/labels"
"k8s.io/api/core/v1"
apiequality "k8s.io/apimachinery/pkg/api/equality"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/errors"
intstrutil "k8s.io/apimachinery/pkg/util/intstr"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/client-go/util/integer"
v1alpha1listers "github.com/gardener/machine-controller-manager/pkg/client/listers/cluster/v1alpha1"
)
// MachineDeploymentListerExpansion allows custom methods to be added to MachineDeploymentLister.
type MachineDeploymentListerExpansion interface {
GetMachineDeploymentsForMachineSet(is *v1alpha1.MachineSet) ([]*v1alpha1.MachineDeployment, error)
}
// MachineDeploymentNamespaceListerExpansion allows custom methods to be added to MachineDeploymentNamespaceLister.
type MachineDeploymentNamespaceListerExpansion interface{}
// GetMachineDeploymentsForMachineSet returns a list of Deployments that potentially
// match a MachineSet. Only the one specified in the MachineSet's ControllerRef
// will actually manage it.
// Returns an error only if no matching Deployments are found.
func (c *controller) GetMachineDeploymentsForMachineSet(is *v1alpha1.MachineSet) ([]*v1alpha1.MachineDeployment, error) {
if len(is.Labels) == 0 {
return nil, fmt.Errorf("no deployments found for MachineSet %v because it has no labels", is.Name)
}
// TODO: MODIFY THIS METHOD so that it checks for the machineTemplateSpecHash label
dList, err := c.machineDeploymentLister.List(labels.Everything())
if err != nil {
return nil, err
}
var deployments []*v1alpha1.MachineDeployment
for _, d := range dList {
selector, err := metav1.LabelSelectorAsSelector(d.Spec.Selector)
if err != nil {
return nil, fmt.Errorf("invalid label selector: %v", err)
}
// If a deployment with a nil or empty selector creeps in, it should match nothing, not everything.
if selector.Empty() || !selector.Matches(labels.Set(is.Labels)) {
continue
}
deployments = append(deployments, d)
}
if len(deployments) == 0 {
return nil, fmt.Errorf("could not find deployments set for MachineSet %s with labels: %v", is.Name, is.Labels)
}
return deployments, nil
}
const (
// RevisionAnnotation is the revision annotation of a deployment's machine sets which records its rollout sequence
RevisionAnnotation = "deployment.kubernetes.io/revision"
// RevisionHistoryAnnotation maintains the history of all old revisions that a machine set has served for a deployment.
RevisionHistoryAnnotation = "deployment.kubernetes.io/revision-history"
// DesiredReplicasAnnotation is the desired replicas for a deployment recorded as an annotation
// in its machine sets. Helps in separating scaling events from the rollout process and for
// determining if the new machine set for a deployment is really saturated.
DesiredReplicasAnnotation = "deployment.kubernetes.io/desired-replicas"
// MaxReplicasAnnotation is the maximum replicas a deployment can have at a given point, which
// is deployment.spec.replicas + maxSurge. Used by the underlying machine sets to estimate their
// proportions in case the deployment has surge replicas.
MaxReplicasAnnotation = "deployment.kubernetes.io/max-replicas"
// RollbackRevisionNotFound is not found rollback event reason
RollbackRevisionNotFound = "DeploymentRollbackRevisionNotFound"
// RollbackTemplateUnchanged is the template unchanged rollback event reason
RollbackTemplateUnchanged = "DeploymentRollbackTemplateUnchanged"
// RollbackDone is the done rollback event reason
RollbackDone = "DeploymentRollback"
// MachineSetUpdatedReason is added in a deployment when one of its machine sets is updated as part
// of the rollout process.
MachineSetUpdatedReason = "MachineSetUpdated"
// FailedISCreateReason is added in a deployment when it cannot create a new machine set.
FailedISCreateReason = "MachineSetCreateError"
// NewMachineSetReason is added in a deployment when it creates a new machine set.
NewMachineSetReason = "NewMachineSetCreated"
// FoundNewISReason is added in a deployment when it adopts an existing machine set.
FoundNewISReason = "FoundNewMachineSet"
// NewISAvailableReason is added in a deployment when its newest machine set is made available
// ie. the number of new machines that have passed readiness checks and run for at least minReadySeconds
// is at least the minimum available machines that need to run for the deployment.
NewISAvailableReason = "NewMachineSetAvailable"
// TimedOutReason is added in a deployment when its newest machine set fails to show any progress
// within the given deadline (progressDeadlineSeconds).
TimedOutReason = "ProgressDeadlineExceeded"
// PausedMachineDeployReason is added in a deployment when it is paused. Lack of progress shouldn't be
// estimated once a deployment is paused.
PausedMachineDeployReason = "DeploymentPaused"
// ResumedMachineDeployReason is added in a deployment when it is resumed. Useful for not failing accidentally
// deployments that paused amidst a rollout and are bounded by a deadline.
ResumedMachineDeployReason = "DeploymentResumed"
// MinimumReplicasAvailable is added in a deployment when it has its minimum replicas required available.
MinimumReplicasAvailable = "MinimumReplicasAvailable"
// MinimumReplicasUnavailable is added in a deployment when it doesn't have the minimum required replicas
// available.
MinimumReplicasUnavailable = "MinimumReplicasUnavailable"
)
// NewMachineDeploymentCondition creates a new deployment condition.
func NewMachineDeploymentCondition(condType v1alpha1.MachineDeploymentConditionType, status v1alpha1.ConditionStatus, reason, message string) *v1alpha1.MachineDeploymentCondition {
return &v1alpha1.MachineDeploymentCondition{
Type: condType,
Status: status,
LastUpdateTime: metav1.Now(),
LastTransitionTime: metav1.Now(),
Reason: reason,
Message: message,
}
}
// GetMachineDeploymentCondition returns the condition with the provided type.
func GetMachineDeploymentCondition(status v1alpha1.MachineDeploymentStatus, condType v1alpha1.MachineDeploymentConditionType) *v1alpha1.MachineDeploymentCondition {
for i := range status.Conditions {
c := status.Conditions[i]
if c.Type == condType {
return &c
}
}
return nil
}
// GetMachineDeploymentConditionInternal returns the condition with the provided type.
// Avoiding Internal versions, use standard versions only.
// TODO: remove the duplicate
func GetMachineDeploymentConditionInternal(status v1alpha1.MachineDeploymentStatus, condType v1alpha1.MachineDeploymentConditionType) *v1alpha1.MachineDeploymentCondition {
for i := range status.Conditions {
c := status.Conditions[i]
if c.Type == condType {
return &c
}
}
return nil
}
// SetMachineDeploymentCondition updates the deployment to include the provided condition. If the condition that
// we are about to add already exists and has the same status and reason then we are not going to update.
func SetMachineDeploymentCondition(status *v1alpha1.MachineDeploymentStatus, condition v1alpha1.MachineDeploymentCondition) {
currentCond := GetMachineDeploymentCondition(*status, condition.Type)
if currentCond != nil && currentCond.Status == condition.Status && currentCond.Reason == condition.Reason {
return
}
// Do not update lastTransitionTime if the status of the condition doesn't change.
if currentCond != nil && currentCond.Status == condition.Status {
condition.LastTransitionTime = currentCond.LastTransitionTime
}
newConditions := filterOutDeploymentCondition(status.Conditions, condition.Type)
status.Conditions = append(newConditions, condition)
}
// RemoveMachineDeploymentCondition removes the deployment condition with the provided type.
func RemoveMachineDeploymentCondition(status *v1alpha1.MachineDeploymentStatus, condType v1alpha1.MachineDeploymentConditionType) {
status.Conditions = filterOutDeploymentCondition(status.Conditions, condType)
}
// filterOutDeploymentCondition returns a new slice of deployment conditions without conditions with the provided type.
func filterOutDeploymentCondition(conditions []v1alpha1.MachineDeploymentCondition, condType v1alpha1.MachineDeploymentConditionType) []v1alpha1.MachineDeploymentCondition {
var newConditions []v1alpha1.MachineDeploymentCondition
for _, c := range conditions {
if c.Type == condType {
continue
}
newConditions = append(newConditions, c)
}
return newConditions
}
// MachineSetToMachineDeploymentCondition converts a machine set condition into a deployment condition.
// Useful for promoting machine set failure conditions into deployments.
func MachineSetToMachineDeploymentCondition(cond v1alpha1.MachineSetCondition) v1alpha1.MachineDeploymentCondition {
return v1alpha1.MachineDeploymentCondition{
Type: v1alpha1.MachineDeploymentConditionType(cond.Type),
Status: cond.Status,
LastTransitionTime: cond.LastTransitionTime,
LastUpdateTime: cond.LastTransitionTime,
Reason: cond.Reason,
Message: cond.Message,
}
}
// SetMachineDeploymentRevision updates the revision for a deployment.
func SetMachineDeploymentRevision(deployment *v1alpha1.MachineDeployment, revision string) bool {
updated := false
if deployment.Annotations == nil {
deployment.Annotations = make(map[string]string)
}
if deployment.Annotations[RevisionAnnotation] != revision {
deployment.Annotations[RevisionAnnotation] = revision
updated = true
}
return updated
}
// MaxRevision finds the highest revision in the machine sets
func MaxRevision(allISs []*v1alpha1.MachineSet) int64 {
max := int64(0)
for _, is := range allISs {
if v, err := Revision(is); err != nil {
// Skip the machine sets when it failed to parse their revision information
glog.V(4).Infof("Error: %v. Couldn't parse revision for machine set %#v, machine deployment controller will skip it when reconciling revisions.", err, is)
} else if v > max {
max = v
}
}
return max
}
// LastRevision finds the second max revision number in all machine sets (the last revision)
func LastRevision(allISs []*v1alpha1.MachineSet) int64 {
max, secMax := int64(0), int64(0)
for _, is := range allISs {
if v, err := Revision(is); err != nil {
// Skip the machine sets when it failed to parse their revision information
glog.V(4).Infof("Error: %v. Couldn't parse revision for machine set %#v, machine deployment controller will skip it when reconciling revisions.", err, is)
} else if v >= max {
secMax = max
max = v
} else if v > secMax {
secMax = v
}
}
return secMax
}
// Revision returns the revision number of the input object.
func Revision(obj runtime.Object) (int64, error) {
acc, err := meta.Accessor(obj)
if err != nil {
return 0, err
}
v, ok := acc.GetAnnotations()[RevisionAnnotation]
if !ok {
return 0, nil
}
return strconv.ParseInt(v, 10, 64)
}
// SetNewMachineSetAnnotations sets new machine set's annotations appropriately by updating its revision and
// copying required deployment annotations to it; it returns true if machine set's annotation is changed.
func SetNewMachineSetAnnotations(deployment *v1alpha1.MachineDeployment, newIS *v1alpha1.MachineSet, newRevision string, exists bool) bool {
// First, copy deployment's annotations (except for apply and revision annotations)
annotationChanged := copyMachineDeploymentAnnotationsToMachineSet(deployment, newIS)
// Then, update machine set's revision annotation
if newIS.Annotations == nil {
newIS.Annotations = make(map[string]string)
}
oldRevision, ok := newIS.Annotations[RevisionAnnotation]
// The newRS's revision should be the greatest among all RSes. Usually, its revision number is newRevision (the max revision number
// of all old RSes + 1). However, it's possible that some of the old RSes are deleted after the newRS revision being updated, and
// newRevision becomes smaller than newRS's revision. We should only update newRS revision when it's smaller than newRevision.
oldRevisionInt, err := strconv.ParseInt(oldRevision, 10, 64)
if err != nil {
if oldRevision != "" {
glog.Warningf("Updating machine set revision OldRevision not int %s", err)
return false
}
//If the RS annotation is empty then initialise it to 0
oldRevisionInt = 0
}
newRevisionInt, err := strconv.ParseInt(newRevision, 10, 64)
if err != nil {
glog.Warningf("Updating machine set revision NewRevision not int %s", err)
return false
}
if oldRevisionInt < newRevisionInt {
newIS.Annotations[RevisionAnnotation] = newRevision
annotationChanged = true
glog.V(4).Infof("Updating machine set %q revision to %s", newIS.Name, newRevision)
}
// If a revision annotation already existed and this machine set was updated with a new revision
// then that means we are rolling back to this machine set. We need to preserve the old revisions
// for historical information.
if ok && annotationChanged {
revisionHistoryAnnotation := newIS.Annotations[RevisionHistoryAnnotation]
oldRevisions := strings.Split(revisionHistoryAnnotation, ",")
if len(oldRevisions[0]) == 0 {
newIS.Annotations[RevisionHistoryAnnotation] = oldRevision
} else {
oldRevisions = append(oldRevisions, oldRevision)
newIS.Annotations[RevisionHistoryAnnotation] = strings.Join(oldRevisions, ",")
}
}
// If the new machine set is about to be created, we need to add replica annotations to it.
if !exists && SetReplicasAnnotations(newIS, *(deployment.Spec.Replicas), *(deployment.Spec.Replicas)+MaxSurge(*deployment)) {
annotationChanged = true
}
return annotationChanged
}
var annotationsToSkip = map[string]bool{
v1.LastAppliedConfigAnnotation: true,
RevisionAnnotation: true,
RevisionHistoryAnnotation: true,
DesiredReplicasAnnotation: true,
MaxReplicasAnnotation: true,
}
// skipCopyAnnotation returns true if we should skip copying the annotation with the given annotation key
// TODO: How to decide which annotations should / should not be copied?
// See https://github.com/kubernetes/kubernetes/pull/20035#issuecomment-179558615
func skipCopyAnnotation(key string) bool {
return annotationsToSkip[key]
}
// copyDeploymentAnnotationsToMachineSet copies deployment's annotations to machine set's annotations,
// and returns true if machine set's annotation is changed.
// Note that apply and revision annotations are not copied.
func copyMachineDeploymentAnnotationsToMachineSet(deployment *v1alpha1.MachineDeployment, is *v1alpha1.MachineSet) bool {
isAnnotationsChanged := false
if is.Annotations == nil {
is.Annotations = make(map[string]string)
}
for k, v := range deployment.Annotations {
// newRS revision is updated automatically in getNewMachineSet, and the deployment's revision number is then updated
// by copying its newRS revision number. We should not copy deployment's revision to its newRS, since the update of
// deployment revision number may fail (revision becomes stale) and the revision number in newRS is more reliable.
if skipCopyAnnotation(k) || is.Annotations[k] == v {
continue
}
is.Annotations[k] = v
isAnnotationsChanged = true
}
return isAnnotationsChanged
}
// SetMachineDeploymentAnnotationsTo sets deployment's annotations as given RS's annotations.
// This action should be done if and only if the deployment is rolling back to this rs.
// Note that apply and revision annotations are not changed.
func SetMachineDeploymentAnnotationsTo(deployment *v1alpha1.MachineDeployment, rollbackToIS *v1alpha1.MachineSet) {
deployment.Annotations = getSkippedAnnotations(deployment.Annotations)
for k, v := range rollbackToIS.Annotations {
if !skipCopyAnnotation(k) {
deployment.Annotations[k] = v
}
}
}
func getSkippedAnnotations(annotations map[string]string) map[string]string {
skippedAnnotations := make(map[string]string)
for k, v := range annotations {
if skipCopyAnnotation(k) {
skippedAnnotations[k] = v
}
}
return skippedAnnotations
}
// FindActiveOrLatest returns the only active or the latest machine set in case there is at most one active
// machine set. If there are more active machine sets, then we should proportionally scale them.
func FindActiveOrLatest(newIS *v1alpha1.MachineSet, oldISs []*v1alpha1.MachineSet) *v1alpha1.MachineSet {
if newIS == nil && len(oldISs) == 0 {
return nil
}
sort.Sort(sort.Reverse(MachineSetsByCreationTimestamp(oldISs)))
allISs := FilterActiveMachineSets(append(oldISs, newIS))
switch len(allISs) {
case 0:
// If there is no active machine set then we should return the newest.
if newIS != nil {
return newIS
}
return oldISs[0]
case 1:
return allISs[0]
default:
return nil
}
}
// GetDesiredReplicasAnnotation returns the number of desired replicas
func GetDesiredReplicasAnnotation(is *v1alpha1.MachineSet) (int32, bool) {
return getIntFromAnnotation(is, DesiredReplicasAnnotation)
}
func getMaxReplicasAnnotation(is *v1alpha1.MachineSet) (int32, bool) {
return getIntFromAnnotation(is, MaxReplicasAnnotation)
}
func getIntFromAnnotation(is *v1alpha1.MachineSet, annotationKey string) (int32, bool) {
annotationValue, ok := is.Annotations[annotationKey]
if !ok {
return int32(0), false
}
intValue, err := strconv.Atoi(annotationValue)
if err != nil {
glog.V(2).Infof("Cannot convert the value %q with annotation key %q for the machine set %q", annotationValue, annotationKey, is.Name)
return int32(0), false
}
return int32(intValue), true
}
// SetReplicasAnnotations sets the desiredReplicas and maxReplicas into the annotations
func SetReplicasAnnotations(is *v1alpha1.MachineSet, desiredReplicas, maxReplicas int32) bool {
updated := false
if is.Annotations == nil {
is.Annotations = make(map[string]string)
}
desiredString := fmt.Sprintf("%d", desiredReplicas)
if hasString := is.Annotations[DesiredReplicasAnnotation]; hasString != desiredString {
is.Annotations[DesiredReplicasAnnotation] = desiredString
updated = true
}
maxString := fmt.Sprintf("%d", maxReplicas)
if hasString := is.Annotations[MaxReplicasAnnotation]; hasString != maxString {
is.Annotations[MaxReplicasAnnotation] = maxString
updated = true
}
return updated
}
// MaxUnavailable returns the maximum unavailable machines a rolling deployment can take.
func MaxUnavailable(deployment v1alpha1.MachineDeployment) int32 {
if !IsRollingUpdate(&deployment) || *(deployment.Spec.Replicas) == 0 {
return int32(0)
}
// Error caught by validation
_, maxUnavailable, _ := ResolveFenceposts(deployment.Spec.Strategy.RollingUpdate.MaxSurge, deployment.Spec.Strategy.RollingUpdate.MaxUnavailable, *(deployment.Spec.Replicas))
if maxUnavailable > *deployment.Spec.Replicas {
return *deployment.Spec.Replicas
}
return maxUnavailable
}
// MinAvailable returns the minimum available machines of a given deployment
func MinAvailable(deployment *v1alpha1.MachineDeployment) int32 {
if !IsRollingUpdate(deployment) {
return int32(0)
}
return *(deployment.Spec.Replicas) - MaxUnavailable(*deployment)
}
// MaxSurge returns the maximum surge machines a rolling deployment can take.
func MaxSurge(deployment v1alpha1.MachineDeployment) int32 {
if !IsRollingUpdate(&deployment) {
return int32(0)
}
// Error caught by validation
maxSurge, _, _ := ResolveFenceposts(deployment.Spec.Strategy.RollingUpdate.MaxSurge, deployment.Spec.Strategy.RollingUpdate.MaxUnavailable, *(deployment.Spec.Replicas))
return maxSurge
}
// GetProportion will estimate the proportion for the provided machine set using 1. the current size
// of the parent deployment, 2. the replica count that needs be added on the machine sets of the
// deployment, and 3. the total replicas added in the machine sets of the deployment so far.
func GetProportion(is *v1alpha1.MachineSet, d v1alpha1.MachineDeployment, deploymentReplicasToAdd, deploymentReplicasAdded int32) int32 {
if is == nil || *(is.Spec.Replicas) == 0 || deploymentReplicasToAdd == 0 || deploymentReplicasToAdd == deploymentReplicasAdded {
return int32(0)
}
isFraction := getMachineSetFraction(*is, d)
allowed := deploymentReplicasToAdd - deploymentReplicasAdded
if deploymentReplicasToAdd > 0 {
// Use the minimum between the machine set fraction and the maximum allowed replicas
// when scaling up. This way we ensure we will not scale up more than the allowed
// replicas we can add.
return integer.Int32Min(isFraction, allowed)
}
// Use the maximum between the machine set fraction and the maximum allowed replicas
// when scaling down. This way we ensure we will not scale down more than the allowed
// replicas we can remove.
return integer.Int32Max(isFraction, allowed)
}
// getMachineSetFraction estimates the fraction of replicas a machine set can have in
// 1. a scaling event during a rollout or 2. when scaling a paused deployment.
func getMachineSetFraction(is v1alpha1.MachineSet, d v1alpha1.MachineDeployment) int32 {
// If we are scaling down to zero then the fraction of this machine set is its whole size (negative)
if *(d.Spec.Replicas) == int32(0) {
return -*(is.Spec.Replicas)
}
deploymentReplicas := *(d.Spec.Replicas) + MaxSurge(d)
annotatedReplicas, ok := getMaxReplicasAnnotation(&is)
if !ok {
// If we cannot find the annotation then fallback to the current deployment size. Note that this
// will not be an accurate proportion estimation in case other machine sets have different values
// which means that the deployment was scaled at some point but we at least will stay in limits
// due to the min-max comparisons in getProportion.
annotatedReplicas = d.Status.Replicas
}
// We should never proportionally scale up from zero which means rs.spec.replicas and annotatedReplicas
// will never be zero here.
newISsize := (float64((*is.Spec.Replicas) * deploymentReplicas)) / float64(annotatedReplicas)
return integer.RoundToInt32(newISsize) - *(is.Spec.Replicas)
}
// GetAllMachineSets returns the old and new machine sets targeted by the given Deployment. It gets MachineList and MachineSetList from client interface.
// Note that the first set of old machine sets doesn't include the ones with no machines, and the second set of old machine sets include all old machine sets.
// The third returned value is the new machine set, and it may be nil if it doesn't exist yet.
func GetAllMachineSets(deployment *v1alpha1.MachineDeployment, c v1alpha1client.ClusterV1alpha1Interface) ([]*v1alpha1.MachineSet, []*v1alpha1.MachineSet, *v1alpha1.MachineSet, error) {
isList, err := ListMachineSets(deployment, IsListFromClient(c))
if err != nil {
return nil, nil, nil, err
}
oldISes, allOldISes := FindOldMachineSets(deployment, isList)
newIS := FindNewMachineSet(deployment, isList)
return oldISes, allOldISes, newIS, nil
}
// GetOldMachineSets returns the old machine sets targeted by the given Deployment; get MachineList and MachineSetList from client interface.
// Note that the first set of old machine sets doesn't include the ones with no machines, and the second set of old machine sets include all old machine sets.
func GetOldMachineSets(deployment *v1alpha1.MachineDeployment, c v1alpha1client.ClusterV1alpha1Interface) ([]*v1alpha1.MachineSet, []*v1alpha1.MachineSet, error) {
rsList, err := ListMachineSets(deployment, IsListFromClient(c))
if err != nil {
return nil, nil, err
}
oldRSes, allOldRSes := FindOldMachineSets(deployment, rsList)
return oldRSes, allOldRSes, nil
}
// GetNewMachineSet returns a machine set that matches the intent of the given deployment; get MachineSetList from client interface.
// Returns nil if the new machine set doesn't exist yet.
func GetNewMachineSet(deployment *v1alpha1.MachineDeployment, c v1alpha1client.ClusterV1alpha1Interface) (*v1alpha1.MachineSet, error) {
rsList, err := ListMachineSets(deployment, IsListFromClient(c))
if err != nil {
return nil, err
}
return FindNewMachineSet(deployment, rsList), nil
}
// IsListFromClient returns an rsListFunc that wraps the given client.
func IsListFromClient(c v1alpha1client.ClusterV1alpha1Interface) IsListFunc {
return func(namespace string, options metav1.ListOptions) ([]*v1alpha1.MachineSet, error) {
isList, err := c.MachineSets(namespace).List(options)
if err != nil {
return nil, err
}
var ret []*v1alpha1.MachineSet
for i := range isList.Items {
ret = append(ret, &isList.Items[i])
}
return ret, err
}
}
// IsListFunc is used to list all machineSets for a given list option
// TODO: switch this to full namespacers
type IsListFunc func(string, metav1.ListOptions) ([]*v1alpha1.MachineSet, error)
// IsListFunc is used to list all machineList for a given listOptions
type machineListFunc func(string, metav1.ListOptions) (*v1alpha1.MachineList, error)
// ListMachineSets returns a slice of RSes the given deployment targets.
// Note that this does NOT attempt to reconcile ControllerRef (adopt/orphan),
// because only the controller itself should do that.
// However, it does filter out anything whose ControllerRef doesn't match.
func ListMachineSets(deployment *v1alpha1.MachineDeployment, getISList IsListFunc) ([]*v1alpha1.MachineSet, error) {
// TODO: Right now we list machine sets by their labels. We should list them by selector, i.e. the machine set's selector
// should be a superset of the deployment's selector, see https://github.com/kubernetes/kubernetes/issues/19830.
namespace := deployment.Namespace
selector, err := metav1.LabelSelectorAsSelector(deployment.Spec.Selector)
if err != nil {
return nil, err
}
options := metav1.ListOptions{LabelSelector: selector.String()}
all, err := getISList(namespace, options)
if err != nil {
return nil, err
}
// Only include those whose ControllerRef matches the Deployment.
owned := make([]*v1alpha1.MachineSet, 0, len(all))
for _, is := range all {
if metav1.IsControlledBy(is, deployment) {
owned = append(owned, is)
}
}
return owned, nil
}
// ListMachineSetsInternal is ListMachineSets for v1alpha1.
// TODO: Remove the duplicate when call sites are updated to ListMachineSets.
func ListMachineSetsInternal(deployment *v1alpha1.MachineDeployment, getISList func(string, metav1.ListOptions) ([]*v1alpha1.MachineSet, error)) ([]*v1alpha1.MachineSet, error) {
namespace := deployment.Namespace
selector, err := metav1.LabelSelectorAsSelector(deployment.Spec.Selector)
if err != nil {
return nil, err
}
options := metav1.ListOptions{LabelSelector: selector.String()}
all, err := getISList(namespace, options)
if err != nil {
return nil, err
}
// Only include those whose ControllerRef matches the Deployment.
filtered := make([]*v1alpha1.MachineSet, 0, len(all))
for _, is := range all {
if metav1.IsControlledBy(is, deployment) {
filtered = append(filtered, is)
}
}
return filtered, nil
}
// ListMachines for given machineDeployment
func ListMachines(deployment *v1alpha1.MachineDeployment, isList []*v1alpha1.MachineSet, getMachineList machineListFunc) (*v1alpha1.MachineList, error) {
namespace := deployment.Namespace
selector, err := metav1.LabelSelectorAsSelector(deployment.Spec.Selector)
if err != nil {
return nil, err
}
options := metav1.ListOptions{LabelSelector: selector.String()}
all, err := getMachineList(namespace, options)
if err != nil {
return all, err
}
// Only include those whose ControllerRef points to a MachineSet that is in
// turn owned by this Deployment.
isMap := make(map[types.UID]bool, len(isList))
for _, is := range isList {
isMap[is.UID] = true
}
owned := &v1alpha1.MachineList{Items: make([]v1alpha1.Machine, 0, len(all.Items))}
for i := range all.Items {
machine := &all.Items[i]
controllerRef := metav1.GetControllerOf(machine)
if controllerRef != nil && isMap[controllerRef.UID] {
owned.Items = append(owned.Items, *machine)
}
}
return owned, nil
}
// EqualIgnoreHash returns true if two given machineTemplateSpec are equal, ignoring the diff in value of Labels[machine-template-hash]
// We ignore machine-template-hash because the hash result would be different upon machineTemplateSpec API changes
// (e.g. the addition of a new field will cause the hash code to change)
// Note that we assume input machineTemplateSpecs contain non-empty labels
func EqualIgnoreHash(template1, template2 *v1alpha1.MachineTemplateSpec) bool {
t1Copy := template1.DeepCopy()
t2Copy := template2.DeepCopy()
// First, compare template.Labels (ignoring hash)
labels1, labels2 := t1Copy.Labels, t2Copy.Labels
if len(labels1) > len(labels2) {
labels1, labels2 = labels2, labels1
}
// We make sure len(labels2) >= len(labels1)
for k, v := range labels2 {
if labels1[k] != v && k != v1alpha1.DefaultMachineDeploymentUniqueLabelKey {
return false
}
}
// Then, compare the templates without comparing their labels
t1Copy.Labels, t2Copy.Labels = nil, nil
return apiequality.Semantic.DeepEqual(t1Copy, t2Copy)
}
// FindNewMachineSet returns the new RS this given deployment targets (the one with the same machine template).
func FindNewMachineSet(deployment *v1alpha1.MachineDeployment, isList []*v1alpha1.MachineSet) *v1alpha1.MachineSet {
sort.Sort(MachineSetsByCreationTimestamp(isList))
for i := range isList {
if EqualIgnoreHash(&isList[i].Spec.Template, &deployment.Spec.Template) {
// In rare cases, such as after cluster upgrades, Deployment may end up with
// having more than one new MachineSets that have the same template as its template,
// see https://github.com/kubernetes/kubernetes/issues/40415
// We deterministically choose the oldest new MachineSet.
return isList[i]
}
}
// new MachineSet does not exist.
return nil
}
// FindOldMachineSets returns the old machine sets targeted by the given Deployment, with the given slice of RSes.
// Note that the first set of old machine sets doesn't include the ones with no machines, and the second set of old machine sets include all old machine sets.
func FindOldMachineSets(deployment *v1alpha1.MachineDeployment, isList []*v1alpha1.MachineSet) ([]*v1alpha1.MachineSet, []*v1alpha1.MachineSet) {
var requiredISs []*v1alpha1.MachineSet
var allISs []*v1alpha1.MachineSet
newIS := FindNewMachineSet(deployment, isList)
for _, is := range isList {
// Filter out new machine set
if newIS != nil && is.UID == newIS.UID {
continue
}
allISs = append(allISs, is)
if *(is.Spec.Replicas) != 0 {
requiredISs = append(requiredISs, is)
}
}
return requiredISs, allISs
}
// WaitForMachineSetUpdated polls the machine set until it is updated.
func WaitForMachineSetUpdated(c v1alpha1listers.MachineSetLister, desiredGeneration int64, namespace, name string) error {
return wait.PollImmediate(1*time.Second, 1*time.Minute, func() (bool, error) {
is, err := c.MachineSets(namespace).Get(name)
if err != nil {
return false, err
}
return is.Status.ObservedGeneration >= desiredGeneration, nil
})
}
// WaitForMachinesHashPopulated polls the machine set until updated and fully labeled.
func WaitForMachinesHashPopulated(c v1alpha1listers.MachineSetLister, desiredGeneration int64, namespace, name string) error {
return wait.PollImmediate(1*time.Second, 1*time.Minute, func() (bool, error) {
is, err := c.MachineSets(namespace).Get(name)
if err != nil {
return false, err
}
return is.Status.ObservedGeneration >= desiredGeneration &&
is.Status.FullyLabeledReplicas == *(is.Spec.Replicas), nil
})
}
// LabelMachinesWithHash labels all machines in the given machineList with the new hash label.
func LabelMachinesWithHash(machineList *v1alpha1.MachineList, c v1alpha1client.ClusterV1alpha1Interface, machineLister v1alpha1listers.MachineLister, namespace, name, hash string) error {
for _, machine := range machineList.Items {
// Ignore inactive Machines.
if !IsMachineActive(&machine) {
continue
}
// Only label the machine that doesn't already have the new hash
if machine.Labels[v1alpha1.DefaultMachineDeploymentUniqueLabelKey] != hash {
_, err := UpdateMachineWithRetries(c.Machines(machine.Namespace), machineLister, machine.Namespace, machine.Name,
func(machineToUpdate *v1alpha1.Machine) error {
// Precondition: the machine doesn't contain the new hash in its label.
if machineToUpdate.Labels[v1alpha1.DefaultMachineDeploymentUniqueLabelKey] == hash {
return errors.ErrPreconditionViolated
}
machineToUpdate.Labels = labelsutil.AddLabel(machineToUpdate.Labels, v1alpha1.DefaultMachineDeploymentUniqueLabelKey, hash)
return nil
})
if err != nil {
return fmt.Errorf("error in adding template hash label %s to machine %q: %v", hash, machine.Name, err)
}
glog.V(4).Infof("Labeled machine %s/%s of MachineSet %s/%s with hash %s.", machine.Namespace, machine.Name, namespace, name, hash)
}
}
return nil
}
// SetFromMachineSetTemplate sets the desired MachineTemplateSpec from a machine set template to the given deployment.
func SetFromMachineSetTemplate(deployment *v1alpha1.MachineDeployment, template v1alpha1.MachineTemplateSpec) *v1alpha1.MachineDeployment {
deployment.Spec.Template.ObjectMeta = template.ObjectMeta
deployment.Spec.Template.Spec = template.Spec
deployment.Spec.Template.ObjectMeta.Labels = labelsutil.CloneAndRemoveLabel(
deployment.Spec.Template.ObjectMeta.Labels,
v1alpha1.DefaultMachineDeploymentUniqueLabelKey)
return deployment
}
// GetReplicaCountForMachineSets returns the sum of Replicas of the given machine sets.
func GetReplicaCountForMachineSets(MachineSets []*v1alpha1.MachineSet) int32 {
totalReplicas := int32(0)
for _, is := range MachineSets {
if is != nil {
totalReplicas += *(is.Spec.Replicas)
}
}
return totalReplicas
}
// GetActualReplicaCountForMachineSets returns the sum of actual replicas of the given machine sets.
func GetActualReplicaCountForMachineSets(MachineSets []*v1alpha1.MachineSet) int32 {
totalActualReplicas := int32(0)
for _, is := range MachineSets {
if is != nil {
totalActualReplicas += is.Status.Replicas
}
}
return totalActualReplicas
}
// GetReadyReplicaCountForMachineSets returns the number of ready machines corresponding to the given machine sets.
func GetReadyReplicaCountForMachineSets(MachineSets []*v1alpha1.MachineSet) int32 {
totalReadyReplicas := int32(0)
for _, is := range MachineSets {
if is != nil {
totalReadyReplicas += is.Status.ReadyReplicas
}
}
return totalReadyReplicas
}
// GetAvailableReplicaCountForMachineSets returns the number of available machines corresponding to the given machine sets.
func GetAvailableReplicaCountForMachineSets(MachineSets []*v1alpha1.MachineSet) int32 {
totalAvailableReplicas := int32(0)
for _, is := range MachineSets {
if is != nil {
totalAvailableReplicas += is.Status.AvailableReplicas
}
}
return totalAvailableReplicas
}
// IsRollingUpdate returns true if the strategy type is a rolling update.
func IsRollingUpdate(deployment *v1alpha1.MachineDeployment) bool {
return deployment.Spec.Strategy.Type == common.RollingUpdateMachineDeploymentStrategyType
}
// MachineDeploymentComplete considers a deployment to be complete once all of its desired replicas
// are updated and available, and no old machines are running.
func MachineDeploymentComplete(deployment *v1alpha1.MachineDeployment, newStatus *v1alpha1.MachineDeploymentStatus) bool {
return newStatus.UpdatedReplicas == *(deployment.Spec.Replicas) &&
newStatus.Replicas == *(deployment.Spec.Replicas) &&
newStatus.AvailableReplicas == *(deployment.Spec.Replicas) &&
newStatus.ObservedGeneration >= deployment.Generation
}
// MachineDeploymentProgressing reports progress for a deployment. Progress is estimated by comparing the
// current with the new status of the deployment that the controller is observing. More specifically,
// when new machines are scaled up or become ready or available, or old machines are scaled down, then we
// consider the deployment is progressing.
func MachineDeploymentProgressing(deployment *v1alpha1.MachineDeployment, newStatus *v1alpha1.MachineDeploymentStatus) bool {
oldStatus := deployment.Status
// Old replicas that need to be scaled down
oldStatusOldReplicas := oldStatus.Replicas - oldStatus.UpdatedReplicas
newStatusOldReplicas := newStatus.Replicas - newStatus.UpdatedReplicas
return (newStatus.UpdatedReplicas > oldStatus.UpdatedReplicas) ||
(newStatusOldReplicas < oldStatusOldReplicas) ||
newStatus.ReadyReplicas > deployment.Status.ReadyReplicas ||
newStatus.AvailableReplicas > deployment.Status.AvailableReplicas
}
// used for unit testing
var nowFn = func() time.Time { return time.Now() }
// MachineDeploymentTimedOut considers a deployment to have timed out once its condition that reports progress
// is older than progressDeadlineSeconds or a Progressing condition with a TimedOutReason reason already
// exists.
func MachineDeploymentTimedOut(deployment *v1alpha1.MachineDeployment, newStatus *v1alpha1.MachineDeploymentStatus) bool {
if deployment.Spec.ProgressDeadlineSeconds == nil {
return false
}
// Look for the Progressing condition. If it doesn't exist, we have no base to estimate progress.
// If it's already set with a TimedOutReason reason, we have already timed out, no need to check
// again.
condition := GetMachineDeploymentCondition(*newStatus, v1alpha1.MachineDeploymentProgressing)
if condition == nil {
return false
}
// If the previous condition has been a successful rollout then we shouldn't try to
// estimate any progress. Scenario:
//
// * progressDeadlineSeconds is smaller than the difference between now and the time
// the last rollout finished in the past.
// * the creation of a new MachineSet triggers a resync of the Deployment prior to the
// cached copy of the Deployment getting updated with the status.condition that indicates
// the creation of the new MachineSet.
//
// The Deployment will be resynced and eventually its Progressing condition will catch
// up with the state of the world.
if condition.Reason == NewISAvailableReason {
return false
}
if condition.Reason == TimedOutReason {
return true
}
// Look at the difference in seconds between now and the last time we reported any
// progress or tried to create a machine set, or resumed a paused deployment and
// compare against progressDeadlineSeconds.
from := condition.LastUpdateTime
now := nowFn()
delta := time.Duration(*deployment.Spec.ProgressDeadlineSeconds) * time.Second
timedOut := from.Add(delta).Before(now)
glog.V(4).Infof("MachineDeployment %q timed out (%t) [last progress check: %v - now: %v]", deployment.Name, timedOut, from, now)
return timedOut
}
// NewISNewReplicas calculates the number of replicas a deployment's new IS should have.
// When one of the followings is true, we're rolling out the deployment; otherwise, we're scaling it.
// 1) The new RS is saturated: newRS's replicas == deployment's replicas
// 2) Max number of machines allowed is reached: deployment's replicas + maxSurge == all RSs' replicas
func NewISNewReplicas(deployment *v1alpha1.MachineDeployment, allISs []*v1alpha1.MachineSet, newIS *v1alpha1.MachineSet) (int32, error) {
switch deployment.Spec.Strategy.Type {
case common.RollingUpdateMachineDeploymentStrategyType:
// Check if we can scale up.
maxSurge, err := intstrutil.GetValueFromIntOrPercent(deployment.Spec.Strategy.RollingUpdate.MaxSurge, int(*(deployment.Spec.Replicas)), true)
if err != nil {
return 0, err
}
// Find the total number of machines
currentMachineCount := GetReplicaCountForMachineSets(allISs)
maxTotalMachines := *(deployment.Spec.Replicas) + int32(maxSurge)
if currentMachineCount >= maxTotalMachines {
// Cannot scale up.
return *(newIS.Spec.Replicas), nil
}
// Scale up.
scaleUpCount := maxTotalMachines - currentMachineCount
// Do not exceed the number of desired replicas.
scaleUpCount = int32(integer.IntMin(int(scaleUpCount), int(*(deployment.Spec.Replicas)-*(newIS.Spec.Replicas))))
return *(newIS.Spec.Replicas) + scaleUpCount, nil
case common.RecreateMachineDeploymentStrategyType:
return *(deployment.Spec.Replicas), nil
default:
return 0, fmt.Errorf("machine deployment type %v isn't supported", deployment.Spec.Strategy.Type)
}
}
// IsSaturated checks if the new machine set is saturated by comparing its size with its deployment size.
// Both the deployment and the machine set have to believe this machine set can own all of the desired
// replicas in the deployment and the annotation helps in achieving that. All machines of the MachineSet
// need to be available.
func IsSaturated(deployment *v1alpha1.MachineDeployment, is *v1alpha1.MachineSet) bool {
if is == nil {
return false
}
desiredString := is.Annotations[DesiredReplicasAnnotation]
desired, err := strconv.Atoi(desiredString)
if err != nil {
return false
}
return *(is.Spec.Replicas) == *(deployment.Spec.Replicas) &&
int32(desired) == *(deployment.Spec.Replicas) &&
is.Status.AvailableReplicas == *(deployment.Spec.Replicas)
}
// WaitForObservedMachineDeployment polls for deployment to be updated so that deployment.Status.ObservedGeneration >= desiredGeneration.
// Returns error if polling timesout.
func WaitForObservedMachineDeployment(getDeploymentFunc func() (*v1alpha1.MachineDeployment, error), desiredGeneration int64, interval, timeout time.Duration) error {
// TODO: This should take clientset.Interface when all code is updated to use clientset. Keeping it this way allows the function to be used by callers who have client.Interface.
return wait.PollImmediate(interval, timeout, func() (bool, error) {
deployment, err := getDeploymentFunc()
if err != nil {
return false, err
}
return deployment.Status.ObservedGeneration >= desiredGeneration, nil
})
}
// WaitForObservedDeploymentInternal polls for deployment to be updated so that deployment.Status.ObservedGeneration >= desiredGeneration.
// Returns error if polling timesout.
// TODO: remove the duplicate
func WaitForObservedDeploymentInternal(getDeploymentFunc func() (*v1alpha1.MachineDeployment, error), desiredGeneration int64, interval, timeout time.Duration) error {
return wait.Poll(interval, timeout, func() (bool, error) {
deployment, err := getDeploymentFunc()
if err != nil {
return false, err
}
return deployment.Status.ObservedGeneration >= desiredGeneration, nil
})
}
// ResolveFenceposts resolves both maxSurge and maxUnavailable. This needs to happen in one
// step. For example:
//
// 2 desired, max unavailable 1%, surge 0% - should scale old(-1), then new(+1), then old(-1), then new(+1)
// 1 desired, max unavailable 1%, surge 0% - should scale old(-1), then new(+1)
// 2 desired, max unavailable 25%, surge 1% - should scale new(+1), then old(-1), then new(+1), then old(-1)
// 1 desired, max unavailable 25%, surge 1% - should scale new(+1), then old(-1)
// 2 desired, max unavailable 0%, surge 1% - should scale new(+1), then old(-1), then new(+1), then old(-1)