@@ -30,6 +30,7 @@ import (
30
30
. "github.com/onsi/gomega"
31
31
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
32
32
"k8s.io/apimachinery/pkg/runtime"
33
+ "k8s.io/utils/pointer"
33
34
"sigs.k8s.io/controller-runtime/pkg/client"
34
35
"sigs.k8s.io/controller-runtime/pkg/client/fake"
35
36
@@ -570,7 +571,7 @@ func TestServiceCreateASG(t *testing.T) {
570
571
mps .AWSMachinePool .Spec .MaxSize = 5
571
572
mps .MachinePool .Spec .Replicas = aws .Int32 (1 )
572
573
mps .MachinePool .Annotations = map [string ]string {
573
- scope .ReplicasManagedByAnnotation : scope . ExternalAutoscalerReplicasManagedByAnnotationValue ,
574
+ clusterv1 .ReplicasManagedByAnnotation : "" , // empty value counts as true (= externally managed)
574
575
}
575
576
},
576
577
wantErr : false ,
@@ -592,7 +593,7 @@ func TestServiceCreateASG(t *testing.T) {
592
593
mps .AWSMachinePool .Spec .MaxSize = 5
593
594
mps .MachinePool .Spec .Replicas = aws .Int32 (6 )
594
595
mps .MachinePool .Annotations = map [string ]string {
595
- scope .ReplicasManagedByAnnotation : scope . ExternalAutoscalerReplicasManagedByAnnotationValue ,
596
+ clusterv1 .ReplicasManagedByAnnotation : "truthy" ,
596
597
}
597
598
},
598
599
wantErr : false ,
@@ -699,17 +700,26 @@ func TestServiceUpdateASG(t *testing.T) {
699
700
machinePoolName string
700
701
setupMachinePoolScope func (* scope.MachinePoolScope )
701
702
wantErr bool
702
- expect func (e * mocks.MockEC2APIMockRecorder , m * mock_autoscalingiface.MockAutoScalingAPIMockRecorder )
703
+ expect func (e * mocks.MockEC2APIMockRecorder , m * mock_autoscalingiface.MockAutoScalingAPIMockRecorder , g * WithT )
703
704
}{
704
705
{
705
706
name : "should return without error if update ASG is successful" ,
706
707
machinePoolName : "update-asg-success" ,
707
708
wantErr : false ,
708
709
setupMachinePoolScope : func (mps * scope.MachinePoolScope ) {
709
- mps .AWSMachinePool .Spec .Subnets = nil
710
+ mps .MachinePool .Spec .Replicas = pointer .Int32 (3 )
711
+ mps .AWSMachinePool .Spec .MinSize = 2
712
+ mps .AWSMachinePool .Spec .MaxSize = 5
710
713
},
711
- expect : func (e * mocks.MockEC2APIMockRecorder , m * mock_autoscalingiface.MockAutoScalingAPIMockRecorder ) {
712
- m .UpdateAutoScalingGroupWithContext (context .TODO (), gomock .AssignableToTypeOf (& autoscaling.UpdateAutoScalingGroupInput {})).Return (& autoscaling.UpdateAutoScalingGroupOutput {}, nil )
714
+ expect : func (e * mocks.MockEC2APIMockRecorder , m * mock_autoscalingiface.MockAutoScalingAPIMockRecorder , g * WithT ) {
715
+ m .UpdateAutoScalingGroupWithContext (context .TODO (), gomock .AssignableToTypeOf (& autoscaling.UpdateAutoScalingGroupInput {})).DoAndReturn (func (ctx context.Context , input * autoscaling.UpdateAutoScalingGroupInput , options ... request.Option ) (* autoscaling.UpdateAutoScalingGroupOutput , error ) {
716
+ // CAPA should set min/max, and because there's no "externally managed" annotation, also the
717
+ // "desired" number of instances
718
+ g .Expect (input .MinSize ).To (BeComparableTo (pointer .Int64 (2 )))
719
+ g .Expect (input .MaxSize ).To (BeComparableTo (pointer .Int64 (5 )))
720
+ g .Expect (input .DesiredCapacity ).To (BeComparableTo (pointer .Int64 (3 )))
721
+ return & autoscaling.UpdateAutoScalingGroupOutput {}, nil
722
+ })
713
723
},
714
724
},
715
725
{
@@ -719,10 +729,31 @@ func TestServiceUpdateASG(t *testing.T) {
719
729
setupMachinePoolScope : func (mps * scope.MachinePoolScope ) {
720
730
mps .AWSMachinePool .Spec .MixedInstancesPolicy = nil
721
731
},
722
- expect : func (e * mocks.MockEC2APIMockRecorder , m * mock_autoscalingiface.MockAutoScalingAPIMockRecorder ) {
732
+ expect : func (e * mocks.MockEC2APIMockRecorder , m * mock_autoscalingiface.MockAutoScalingAPIMockRecorder , g * WithT ) {
723
733
m .UpdateAutoScalingGroupWithContext (context .TODO (), gomock .AssignableToTypeOf (& autoscaling.UpdateAutoScalingGroupInput {})).Return (nil , awserrors .NewFailedDependency ("dependency failure" ))
724
734
},
725
735
},
736
+ {
737
+ name : "externally managed replicas annotation" ,
738
+ machinePoolName : "update-asg-externally-managed-replicas-annotation" ,
739
+ wantErr : false ,
740
+ setupMachinePoolScope : func (mps * scope.MachinePoolScope ) {
741
+ mps .MachinePool .SetAnnotations (map [string ]string {clusterv1 .ReplicasManagedByAnnotation : "anything-that-is-not-false" })
742
+
743
+ mps .MachinePool .Spec .Replicas = pointer .Int32 (40 )
744
+ mps .AWSMachinePool .Spec .MinSize = 20
745
+ mps .AWSMachinePool .Spec .MaxSize = 50
746
+ },
747
+ expect : func (e * mocks.MockEC2APIMockRecorder , m * mock_autoscalingiface.MockAutoScalingAPIMockRecorder , g * WithT ) {
748
+ m .UpdateAutoScalingGroupWithContext (context .TODO (), gomock .AssignableToTypeOf (& autoscaling.UpdateAutoScalingGroupInput {})).DoAndReturn (func (ctx context.Context , input * autoscaling.UpdateAutoScalingGroupInput , options ... request.Option ) (* autoscaling.UpdateAutoScalingGroupOutput , error ) {
749
+ // CAPA should set min/max, but not the externally managed "desired" number of instances
750
+ g .Expect (input .MinSize ).To (BeComparableTo (pointer .Int64 (20 )))
751
+ g .Expect (input .MaxSize ).To (BeComparableTo (pointer .Int64 (50 )))
752
+ g .Expect (input .DesiredCapacity ).To (BeNil ())
753
+ return & autoscaling.UpdateAutoScalingGroupOutput {}, nil
754
+ })
755
+ },
756
+ },
726
757
}
727
758
for _ , tt := range tests {
728
759
t .Run (tt .name , func (t * testing.T ) {
@@ -733,13 +764,14 @@ func TestServiceUpdateASG(t *testing.T) {
733
764
g .Expect (err ).ToNot (HaveOccurred ())
734
765
ec2Mock := mocks .NewMockEC2API (mockCtrl )
735
766
asgMock := mock_autoscalingiface .NewMockAutoScalingAPI (mockCtrl )
736
- tt .expect (ec2Mock .EXPECT (), asgMock .EXPECT ())
767
+ tt .expect (ec2Mock .EXPECT (), asgMock .EXPECT (), g )
737
768
s := NewService (clusterScope )
738
769
s .ASGClient = asgMock
739
770
740
771
mps , err := getMachinePoolScope (fakeClient , clusterScope )
741
772
g .Expect (err ).ToNot (HaveOccurred ())
742
773
mps .AWSMachinePool .Name = tt .machinePoolName
774
+ tt .setupMachinePoolScope (mps )
743
775
744
776
err = s .UpdateASG (mps )
745
777
checkErr (tt .wantErr , err , g )
0 commit comments