Skip to content

Commit 3b7fc9e

Browse files
authored
Enhance Nodegroup documentation, logging and e2e tests (#89)
Issue N/A Description of changes: - Fix a crucial "typo" in the `Nodegroup` annotations docs - Add some more logging to help debugging the controller behaviour when dealing with the `desired-size-managed-by` annotation - Add some e2e tests to simulate the "existence" of an external autoscaler By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
1 parent 076ce79 commit 3b7fc9e

File tree

4 files changed

+108
-7
lines changed

4 files changed

+108
-7
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,12 @@ For some resources, the `eks-controller` supports annotations to customize the
2929
behavior of the controller. The following annotations are supported:
3030

3131
- **Nodegroup**
32-
- `eks.service.k8s.aws/desired-size-managed-by`: used to control whether the
32+
- `eks.services.k8s.aws/desired-size-managed-by`: used to control whether the
3333
controller should manage the desiredSize of the Nodegroup `spec.ScalingConfig.DesiredSize`.
3434
It supports the following values:
35-
- `ack-eks-controller`: If set the controller will be responsible for
35+
- `ack-eks-controller`: If set, the controller will be responsible for
3636
managing the desired size of the nodegroup.
37-
- `external-autoscaler`: If set will ignore any changes to the
37+
- `external-autoscaler`: If set, will ignore any changes to the
3838
`spec.ScalingConfig.DesiredSize` and will not manage the desired size
3939
of the nodegroup.
4040

pkg/resource/nodegroup/hook.go

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,14 @@ import (
1919
"reflect"
2020
"time"
2121

22-
svcapitypes "github.com/aws-controllers-k8s/eks-controller/apis/v1alpha1"
2322
ackcompare "github.com/aws-controllers-k8s/runtime/pkg/compare"
2423
ackcondition "github.com/aws-controllers-k8s/runtime/pkg/condition"
2524
ackrequeue "github.com/aws-controllers-k8s/runtime/pkg/requeue"
2625
ackrtlog "github.com/aws-controllers-k8s/runtime/pkg/runtime/log"
2726
svcsdk "github.com/aws/aws-sdk-go/service/eks"
2827
corev1 "k8s.io/api/core/v1"
28+
29+
svcapitypes "github.com/aws-controllers-k8s/eks-controller/apis/v1alpha1"
2930
)
3031

3132
// Taken from the list of nodegroup statuses on the boto3 documentation
@@ -390,7 +391,19 @@ func (rm *resourceManager) newUpdateScalingConfigPayload(
390391
sc := rm.newNodegroupScalingConfig(desired)
391392
// We need to default the desiredSize to the current observed
392393
// value in the case where the desiredSize is managed externally.
393-
if isManagedByExternalAutoscaler(desired.ko) && latest.ko.Spec.ScalingConfig != nil {
394+
isManagedExternally := isManagedByExternalAutoscaler(desired.ko)
395+
if isManagedExternally {
396+
rm.log.Info(
397+
"detected that the desiredSize is managed by an external entity.",
398+
"annotation", fmt.Sprintf("%s: '%s'", svcapitypes.DesiredSizeManagedByAnnotation, svcapitypes.DesiredSizeManagedByExternalAutoscaler),
399+
)
400+
}
401+
if isManagedExternally && latest.ko.Spec.ScalingConfig != nil {
402+
rm.log.Info(
403+
"ignoring the difference in desiredSize as it is managed by an external entity.",
404+
"external_desired_size", latest.ko.Spec.ScalingConfig.DesiredSize,
405+
"ack_desired_size", desired.ko.Spec.ScalingConfig.DesiredSize,
406+
)
394407
sc.DesiredSize = latest.ko.Spec.ScalingConfig.DesiredSize
395408
}
396409
return sc

pkg/resource/nodegroup/hook_test.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ package nodegroup
1515

1616
import (
1717
"fmt"
18+
"io"
1819
"reflect"
1920
"testing"
2021

@@ -23,6 +24,7 @@ import (
2324
svcsdk "github.com/aws/aws-sdk-go/service/eks"
2425
"github.com/stretchr/testify/assert"
2526
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
27+
"sigs.k8s.io/controller-runtime/pkg/log/zap"
2628

2729
"github.com/aws-controllers-k8s/eks-controller/apis/v1alpha1"
2830
)
@@ -354,7 +356,11 @@ func Test_resourceManager_newUpdateScalingConfigPayload_ManagedByExternalAutosca
354356
}
355357
for _, tt := range tests {
356358
t.Run(tt.name, func(t *testing.T) {
357-
rm := resourceManager{}
359+
rm := resourceManager{
360+
log: zap.New(zap.UseFlagOptions(&zap.Options{
361+
DestWriter: io.Discard,
362+
})),
363+
}
358364
if got := rm.newUpdateScalingConfigPayload(&resource{tt.args.desired}, &resource{tt.args.latest}); !reflect.DeepEqual(got, tt.want) {
359365
t.Errorf("resourceManager.newUpdateScalingConfigPayload() = %v, want %v", got, tt.want)
360366
}

test/e2e/tests/test_nodegroup.py

Lines changed: 83 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,4 +237,86 @@ def test_create_update_delete_nodegroup(self, simple_nodegroup, eks_client):
237237
nodegroupName=nodegroup_name
238238
)
239239

240-
assert "taints" not in aws_res["nodegroup"]
240+
assert "taints" not in aws_res["nodegroup"]
241+
242+
def test_nodegroup_custom_annotations(self, simple_nodegroup, eks_client):
243+
(ref, cr) = simple_nodegroup
244+
245+
cluster_name = cr["spec"]["clusterName"]
246+
cr_name = ref.name
247+
248+
nodegroup_name = cr["spec"]["name"]
249+
250+
try:
251+
aws_res = eks_client.describe_nodegroup(
252+
clusterName=cluster_name,
253+
nodegroupName=nodegroup_name
254+
)
255+
assert aws_res is not None
256+
257+
assert aws_res["nodegroup"]["nodegroupName"] == nodegroup_name
258+
assert aws_res["nodegroup"]["nodegroupArn"] is not None
259+
except eks_client.exceptions.ResourceNotFoundException:
260+
pytest.fail(f"Could not find Nodegroup '{cr_name}' in EKS")
261+
262+
wait_for_nodegroup_active(eks_client, cluster_name, nodegroup_name)
263+
264+
# Set the eks.services.k8s.aws/desired-size-managed-by annotations and
265+
# update the desiredSize along with the maxSize
266+
updates = {
267+
"metadata": {
268+
"annotations": {
269+
"eks.services.k8s.aws/desired-size-managed-by": "external-autoscaler",
270+
}
271+
},
272+
"spec": {
273+
"scalingConfig": {
274+
"minSize": 1,
275+
"maxSize": 2,
276+
"desiredSize": 3, # Controller should not apply this value
277+
}
278+
}
279+
}
280+
281+
k8s.patch_custom_resource(ref, updates)
282+
time.sleep(MODIFY_WAIT_AFTER_SECONDS)
283+
284+
wait_for_nodegroup_active(eks_client, cluster_name, nodegroup_name)
285+
286+
aws_res = eks_client.describe_nodegroup(
287+
clusterName=cluster_name,
288+
nodegroupName=nodegroup_name
289+
)
290+
assert aws_res["nodegroup"]["scalingConfig"]["minSize"] == 1
291+
assert aws_res["nodegroup"]["scalingConfig"]["maxSize"] == 2
292+
assert aws_res["nodegroup"]["scalingConfig"]["desiredSize"] == 1 # Should be 1, not 3
293+
294+
# Set the eks.services.k8s.aws/desired-size-managed-by annotations and
295+
# update the desiredSize only
296+
updates = {
297+
"metadata": {
298+
"annotations": {
299+
"eks.services.k8s.aws/desired-size-managed-by": "external-autoscaler",
300+
}
301+
},
302+
"spec": {
303+
"scalingConfig": {
304+
"minSize": 1,
305+
"maxSize": 2,
306+
"desiredSize": 10, # Controller should not apply this value
307+
}
308+
}
309+
}
310+
311+
k8s.patch_custom_resource(ref, updates)
312+
time.sleep(MODIFY_WAIT_AFTER_SECONDS)
313+
314+
wait_for_nodegroup_active(eks_client, cluster_name, nodegroup_name)
315+
316+
aws_res = eks_client.describe_nodegroup(
317+
clusterName=cluster_name,
318+
nodegroupName=nodegroup_name
319+
)
320+
assert aws_res["nodegroup"]["scalingConfig"]["minSize"] == 1
321+
assert aws_res["nodegroup"]["scalingConfig"]["maxSize"] == 2
322+
assert aws_res["nodegroup"]["scalingConfig"]["desiredSize"] == 1 # Should be 1, not 10

0 commit comments

Comments
 (0)