Skip to content

Commit

Permalink
handle uncordon scenario properly
Browse files Browse the repository at this point in the history
  • Loading branch information
shreyas-badiger committed Dec 4, 2023
1 parent 717b481 commit 5a2a243
Show file tree
Hide file tree
Showing 6 changed files with 371 additions and 279 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ all: manager
# Run tests
ENVTEST_ASSETS_DIR=$(shell pwd)/testbin
test: manifests generate fmt vet envtest
go test ./controllers/... ./api/...
go tool cover -html=./coverage.txt -o cover.html

# Build manager binary
Expand Down
4 changes: 4 additions & 0 deletions controllers/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -426,3 +426,7 @@ func (mockAutoscalingGroup MockAutoscalingGroup) EnterStandby(_ *autoscaling.Ent
output := &autoscaling.EnterStandbyOutput{}
return output, nil
}

func (m *MockEC2) DeleteTags(input *ec2.DeleteTagsInput) (*ec2.DeleteTagsOutput, error) {
return &ec2.DeleteTagsOutput{}, nil
}
4 changes: 2 additions & 2 deletions controllers/metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import (
func TestNodeTurnsOntoStep(t *testing.T) {
g := gomega.NewGomegaWithT(t)

reconsiler := createRollingUpgradeReconciler(t)
r := createRollingUpgradeContext(reconsiler)
reconciler := createRollingUpgradeReconciler(t)
r := createRollingUpgradeContext(reconciler)

//A map to retain the steps for multiple nodes
nodeSteps := make(map[string][]v1alpha1.NodeStepDuration)
Expand Down
12 changes: 6 additions & 6 deletions controllers/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -802,12 +802,12 @@ func (r *RollingUpgradeContext) CordonUncordonAllNodes(cordonNode bool) (bool, e
r.Error(err, "failed to early cordon the nodes", "instanceID", instance.InstanceId, "name", r.RollingUpgrade.NamespacedName())
return false, err
}
}
// Set instance-state to early-cordoned tag
r.Info("tagging instances with cordoned=true", "instanceID", instance.InstanceId, "name", r.RollingUpgrade.NamespacedName())
if err := r.Auth.TagEC2instances([]string{*instance.InstanceId}, instanceStateTagKey, earlyCordonedTagValue); err != nil {
r.Error(err, "failed to tag instances with cordoned=true", "instanceID", instance.InstanceId, "name", r.RollingUpgrade.NamespacedName())
return true, err
// Set instance-state to early-cordoned tag
r.Info("tagging instances with cordoned=true", "instanceID", instance.InstanceId, "name", r.RollingUpgrade.NamespacedName())
if err := r.Auth.TagEC2instances([]string{*instance.InstanceId}, instanceStateTagKey, earlyCordonedTagValue); err != nil {
r.Error(err, "failed to tag instances with cordoned=true", "instanceID", instance.InstanceId, "name", r.RollingUpgrade.NamespacedName())
return true, err
}
}
}
}
Expand Down
54 changes: 32 additions & 22 deletions controllers/upgrade_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package controllers

import (
"context"
"os"
"testing"

Expand Down Expand Up @@ -105,6 +106,7 @@ func TestRunCordonOrUncordon(t *testing.T) {
for _, test := range tests {
rollupCtx := createRollingUpgradeContext(test.Reconciler)
helper := &drain.Helper{
Ctx: context.Background(),
Client: rollupCtx.Auth.Kubernetes,
Force: true,
GracePeriodSeconds: -1,
Expand Down Expand Up @@ -621,50 +623,58 @@ func TestClusterBallooning(t *testing.T) {
}
}

func TestEarlyCordonFunction(t *testing.T) {
func TestCordoningAndUncordoningOfNodes(t *testing.T) {
var tests = []struct {
TestDescription string
Reconciler *RollingUpgradeReconciler
RollingUpgrade *v1alpha1.RollingUpgrade
AsgClient *MockAutoscalingGroup
ClusterNodes []*corev1.Node
Node *corev1.Node
CordonNodeFlag bool
ExpectedUnschdeulableValue bool
ExpectedError bool
}{
{
"Test if all the nodes are cordoned by default.",
"Test if all the nodes are cordoned.",
createRollingUpgradeReconciler(t),
createRollingUpgrade(),
createASGClient(),
createNodeSlice(),
createNode("mock-node-1"),
true,
true,
false,
},
{
"Test if all the nodes are cordoned by default.",
"Test if all the nodes are uncordoned",
createRollingUpgradeReconciler(t),
createRollingUpgrade(),
createASGClient(),
createNodeSlice(),
createNode("mock-node-1"),
false,
false,
false,
},
{
"Try to cordon an unknown node.",
createRollingUpgradeReconciler(t),
createNode("mock-node-4"),
true,
true,
true,
},
}
for _, test := range tests {
rollupCtx := createRollingUpgradeContext(test.Reconciler)
rollupCtx.RollingUpgrade = test.RollingUpgrade
rollupCtx.Cloud.ScalingGroups = test.AsgClient.autoScalingGroups
rollupCtx.Cloud.ClusterNodes = test.ClusterNodes
rollupCtx.Auth.AmazonClientSet.AsgClient = test.AsgClient

_, err := rollupCtx.CordonUncordonAllNodes(test.CordonNodeFlag)
if err != nil {
if err := rollupCtx.Auth.CordonUncordonNode(test.Node, rollupCtx.Auth.Kubernetes, test.CordonNodeFlag); err != nil && test.ExpectedError {
continue
}

// By default, nodes are uncordoned. Therefore, before testing uncordoning the node, first cordon it.
if !test.CordonNodeFlag {
rollupCtx.Auth.CordonUncordonNode(test.Node, rollupCtx.Auth.Kubernetes, true)

Check failure on line 669 in controllers/upgrade_test.go

View workflow job for this annotation

GitHub Actions / CI

Error return value of `rollupCtx.Auth.CordonUncordonNode` is not checked (errcheck)
}

if err := rollupCtx.Auth.CordonUncordonNode(test.Node, rollupCtx.Auth.Kubernetes, test.CordonNodeFlag); err != nil {
t.Errorf("Test Description: %s \n error: %v", test.TestDescription, err)
}
for _, node := range rollupCtx.Cloud.ClusterNodes {
if test.ExpectedUnschdeulableValue != node.Spec.Unschedulable {
t.Errorf("Test Description: %s \n expectedValue: %v, actualValue: %v", test.TestDescription, test.ExpectedUnschdeulableValue, node.Spec.Unschedulable)
}

if test.ExpectedUnschdeulableValue != test.Node.Spec.Unschedulable {
t.Errorf("Test Description: %s \n expectedValue: %v, actualValue: %v", test.TestDescription, test.ExpectedUnschdeulableValue, test.Node.Spec.Unschedulable)
}
}
}
Loading

0 comments on commit 5a2a243

Please sign in to comment.