From 79db022205ed7566dfc5804d2ed3be0abd169e16 Mon Sep 17 00:00:00 2001 From: Shreyas Badiger <7680410+shreyas-badiger@users.noreply.github.com> Date: Mon, 21 Jun 2021 17:01:36 -0700 Subject: [PATCH] Add a mock for test and update version in Makefile (#262) Signed-off-by: sbadiger --- Makefile | 2 +- api/v1alpha1/zz_generated.deepcopy.go | 15 +++++++++++++++ controllers/helpers_test.go | 16 ++++++++++++++++ controllers/providers/aws/autoscaling.go | 5 +++++ controllers/upgrade.go | 9 +++++---- controllers/upgrade_test.go | 4 ++-- go.mod | 3 ++- go.sum | 2 ++ 8 files changed, 48 insertions(+), 8 deletions(-) diff --git a/Makefile b/Makefile index c4f78a33..72bcec3d 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ - +VERSION=1.0.0-RC1 # Image URL to use all building/pushing image targets IMG ?= controller:latest # Produce CRDs that work back to Kubernetes 1.11 (no version conversion) diff --git a/api/v1alpha1/zz_generated.deepcopy.go b/api/v1alpha1/zz_generated.deepcopy.go index 5e13ec90..62489a6f 100644 --- a/api/v1alpha1/zz_generated.deepcopy.go +++ b/api/v1alpha1/zz_generated.deepcopy.go @@ -273,6 +273,21 @@ func (in *RollingUpgradeStatus) DeepCopyInto(out *RollingUpgradeStatus) { *out = make([]string, len(*in)) copy(*out, *in) } + if in.NodeInProcessing != nil { + in, out := &in.NodeInProcessing, &out.NodeInProcessing + *out = make(map[string]*NodeInProcessing, len(*in)) + for key, val := range *in { + var outVal *NodeInProcessing + if val == nil { + (*out)[key] = nil + } else { + in, out := &val, &outVal + *out = new(NodeInProcessing) + (*in).DeepCopyInto(*out) + } + (*out)[key] = outVal + } + } } // DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new RollingUpgradeStatus. diff --git a/controllers/helpers_test.go b/controllers/helpers_test.go index c9e98dca..3faeae5b 100644 --- a/controllers/helpers_test.go +++ b/controllers/helpers_test.go @@ -3,6 +3,7 @@ package controllers import ( "sync" "testing" + "time" "k8s.io/client-go/kubernetes/fake" ctrl "sigs.k8s.io/controller-runtime" @@ -197,3 +198,18 @@ func createAmazonClient(t *testing.T) *awsprovider.AmazonClientSet { Ec2Client: createEc2Client(), } } + +func (mockAutoscalingGroup MockAutoscalingGroup) TerminateInstanceInAutoScalingGroup(input *autoscaling.TerminateInstanceInAutoScalingGroupInput) (*autoscaling.TerminateInstanceInAutoScalingGroupOutput, error) { + output := &autoscaling.TerminateInstanceInAutoScalingGroupOutput{} + if mockAutoscalingGroup.errorFlag { + if mockAutoscalingGroup.awsErr != nil { + if len(mockAutoscalingGroup.errorInstanceId) <= 0 || + mockAutoscalingGroup.errorInstanceId == *input.InstanceId { + return output, mockAutoscalingGroup.awsErr + } + } + } + asgChange := autoscaling.Activity{ActivityId: aws.String("xxx"), AutoScalingGroupName: aws.String("sss"), Cause: aws.String("xxx"), StartTime: aws.Time(time.Now()), StatusCode: aws.String("200"), StatusMessage: aws.String("success")} + output.Activity = &asgChange + return output, nil +} diff --git a/controllers/providers/aws/autoscaling.go b/controllers/providers/aws/autoscaling.go index a971bdbf..3d66cee9 100644 --- a/controllers/providers/aws/autoscaling.go +++ b/controllers/providers/aws/autoscaling.go @@ -26,6 +26,11 @@ var ( autoscaling.LifecycleStateTerminating, autoscaling.LifecycleStateTerminatingWait, autoscaling.LifecycleStateTerminatingProceed, + autoscaling.LifecycleStateTerminated, + autoscaling.LifecycleStateWarmedTerminating, + autoscaling.LifecycleStateWarmedTerminatingWait, + autoscaling.LifecycleStateWarmedTerminatingProceed, + autoscaling.LifecycleStateWarmedTerminated, } ) diff --git a/controllers/upgrade.go b/controllers/upgrade.go index e27df803..77954e6d 100644 --- a/controllers/upgrade.go +++ b/controllers/upgrade.go @@ -357,7 +357,10 @@ func (r *RollingUpgradeContext) SelectTargets(scalingGroup *autoscaling.Group) [ if r.RollingUpgrade.UpdateStrategyType() == v1alpha1.RandomUpdateStrategy { for _, instance := range scalingGroup.Instances { if r.IsInstanceDrifted(instance) && !common.ContainsEqualFold(awsprovider.GetInstanceIDs(targets), aws.StringValue(instance.InstanceId)) { - targets = append(targets, instance) + //In-progress instances shouldn't be considered if they are in terminating state. + if !common.ContainsEqualFold(awsprovider.TerminatingInstanceStates, aws.StringValue(instance.LifecycleState)) { + targets = append(targets, instance) + } } } if unavailableInt > len(targets) { @@ -368,9 +371,7 @@ func (r *RollingUpgradeContext) SelectTargets(scalingGroup *autoscaling.Group) [ } else if r.RollingUpgrade.UpdateStrategyType() == v1alpha1.UniformAcrossAzUpdateStrategy { for _, instance := range scalingGroup.Instances { if r.IsInstanceDrifted(instance) && !common.ContainsEqualFold(awsprovider.GetInstanceIDs(targets), aws.StringValue(instance.InstanceId)) { - if !common.ContainsEqualFold(awsprovider.TerminatingInstanceStates, aws.StringValue(instance.LifecycleState)) { - targets = append(targets, instance) - } + targets = append(targets, instance) } } diff --git a/controllers/upgrade_test.go b/controllers/upgrade_test.go index 45890395..ee96189b 100644 --- a/controllers/upgrade_test.go +++ b/controllers/upgrade_test.go @@ -291,7 +291,7 @@ func TestRotateNodes(t *testing.T) { v1alpha1.StatusRunning, }, { - "All instances have different launch config as the ASG, RotateNodes() will mark CR complete", + "All instances have same launch config as the ASG, RotateNodes() will mark CR complete", createRollingUpgradeReconciler(t), createASGClient(), false, @@ -305,7 +305,7 @@ func TestRotateNodes(t *testing.T) { err := rollupCtx.RotateNodes() if err != nil { - t.Errorf("Test Description: \n expected value: nil, actual value: %v", err) + t.Errorf("Test Description: %s \n error: %v", test.TestDescription, err) } if rollupCtx.RollingUpgrade.CurrentStatus() != test.ExpectedStatusValue { t.Errorf("Test Description: %s \n expected value: %s, actual value: %s", test.TestDescription, test.ExpectedStatusValue, rollupCtx.RollingUpgrade.CurrentStatus()) diff --git a/go.mod b/go.mod index b444654f..f114b375 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module github.com/keikoproj/upgrade-manager go 1.15 require ( - github.com/aws/aws-sdk-go v1.36.24 + github.com/aws/aws-sdk-go v1.38.24 github.com/go-logr/logr v0.3.0 github.com/keikoproj/aws-sdk-go-cache v0.0.0-20201118182730-f6f418a4e2df github.com/onsi/gomega v1.10.2 @@ -16,4 +16,5 @@ require ( k8s.io/client-go v0.20.4 k8s.io/kubectl v0.20.4 sigs.k8s.io/controller-runtime v0.7.0 + ) diff --git a/go.sum b/go.sum index 36cb2d64..b2aebc80 100644 --- a/go.sum +++ b/go.sum @@ -88,6 +88,8 @@ github.com/asaskevich/govalidator v0.0.0-20190424111038-f61b66f89f4a/go.mod h1:l github.com/aws/aws-sdk-go v1.35.7/go.mod h1:tlPOdRjfxPBpNIwqDj61rmsnA85v9jc0Ps9+muhnW+k= github.com/aws/aws-sdk-go v1.36.24 h1:uVuio0zA5ideP3DGZDpIoExQJd0WcoNUVlNZaKwBnf8= github.com/aws/aws-sdk-go v1.36.24/go.mod h1:hcU610XS61/+aQV88ixoOzUoG7v3b31pl2zKMmprdro= +github.com/aws/aws-sdk-go v1.38.24 h1:zbKHDxFepE77ihVMZ+wZ62Ci646zkorN8rB5s4fj4kU= +github.com/aws/aws-sdk-go v1.38.24/go.mod h1:hcU610XS61/+aQV88ixoOzUoG7v3b31pl2zKMmprdro= github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q= github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+CedLV8= github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=