@@ -30,6 +30,7 @@ import (
3030 . "github.com/onsi/gomega"
3131 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
3232 "k8s.io/apimachinery/pkg/runtime"
33+ "k8s.io/utils/pointer"
3334 "sigs.k8s.io/controller-runtime/pkg/client"
3435 "sigs.k8s.io/controller-runtime/pkg/client/fake"
3536
@@ -570,7 +571,7 @@ func TestServiceCreateASG(t *testing.T) {
570571 mps .AWSMachinePool .Spec .MaxSize = 5
571572 mps .MachinePool .Spec .Replicas = aws .Int32 (1 )
572573 mps .MachinePool .Annotations = map [string ]string {
573- scope .ReplicasManagedByAnnotation : scope . ExternalAutoscalerReplicasManagedByAnnotationValue ,
574+ clusterv1 .ReplicasManagedByAnnotation : "" , // empty value counts as true (= externally managed)
574575 }
575576 },
576577 wantErr : false ,
@@ -592,7 +593,7 @@ func TestServiceCreateASG(t *testing.T) {
592593 mps .AWSMachinePool .Spec .MaxSize = 5
593594 mps .MachinePool .Spec .Replicas = aws .Int32 (6 )
594595 mps .MachinePool .Annotations = map [string ]string {
595- scope .ReplicasManagedByAnnotation : scope . ExternalAutoscalerReplicasManagedByAnnotationValue ,
596+ clusterv1 .ReplicasManagedByAnnotation : "truthy" ,
596597 }
597598 },
598599 wantErr : false ,
@@ -699,17 +700,26 @@ func TestServiceUpdateASG(t *testing.T) {
699700 machinePoolName string
700701 setupMachinePoolScope func (* scope.MachinePoolScope )
701702 wantErr bool
702- expect func (e * mocks.MockEC2APIMockRecorder , m * mock_autoscalingiface.MockAutoScalingAPIMockRecorder )
703+ expect func (e * mocks.MockEC2APIMockRecorder , m * mock_autoscalingiface.MockAutoScalingAPIMockRecorder , g * WithT )
703704 }{
704705 {
705706 name : "should return without error if update ASG is successful" ,
706707 machinePoolName : "update-asg-success" ,
707708 wantErr : false ,
708709 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
710713 },
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+ })
713723 },
714724 },
715725 {
@@ -719,10 +729,31 @@ func TestServiceUpdateASG(t *testing.T) {
719729 setupMachinePoolScope : func (mps * scope.MachinePoolScope ) {
720730 mps .AWSMachinePool .Spec .MixedInstancesPolicy = nil
721731 },
722- expect : func (e * mocks.MockEC2APIMockRecorder , m * mock_autoscalingiface.MockAutoScalingAPIMockRecorder ) {
732+ expect : func (e * mocks.MockEC2APIMockRecorder , m * mock_autoscalingiface.MockAutoScalingAPIMockRecorder , g * WithT ) {
723733 m .UpdateAutoScalingGroupWithContext (context .TODO (), gomock .AssignableToTypeOf (& autoscaling.UpdateAutoScalingGroupInput {})).Return (nil , awserrors .NewFailedDependency ("dependency failure" ))
724734 },
725735 },
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+ },
726757 }
727758 for _ , tt := range tests {
728759 t .Run (tt .name , func (t * testing.T ) {
@@ -733,13 +764,14 @@ func TestServiceUpdateASG(t *testing.T) {
733764 g .Expect (err ).ToNot (HaveOccurred ())
734765 ec2Mock := mocks .NewMockEC2API (mockCtrl )
735766 asgMock := mock_autoscalingiface .NewMockAutoScalingAPI (mockCtrl )
736- tt .expect (ec2Mock .EXPECT (), asgMock .EXPECT ())
767+ tt .expect (ec2Mock .EXPECT (), asgMock .EXPECT (), g )
737768 s := NewService (clusterScope )
738769 s .ASGClient = asgMock
739770
740771 mps , err := getMachinePoolScope (fakeClient , clusterScope )
741772 g .Expect (err ).ToNot (HaveOccurred ())
742773 mps .AWSMachinePool .Name = tt .machinePoolName
774+ tt .setupMachinePoolScope (mps )
743775
744776 err = s .UpdateASG (mps )
745777 checkErr (tt .wantErr , err , g )
0 commit comments