Skip to content

Commit

Permalink
fix(aws-eks): Allow desiredsize minsize and maxsize to accept CfnPara…
Browse files Browse the repository at this point in the history
…meters. (aws#15487)

----
Added logic similar to AutoscalngGroup    module to allow EKS NodeGroups to accept CfnParameters for desiredsize minsize and maxsize as CfnParameter.valueAsNumber. 

Helps with issue aws#15485
*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
hemandee authored and hollanddd committed Aug 26, 2021
1 parent 6ad0684 commit caa674a
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 7 deletions.
21 changes: 14 additions & 7 deletions packages/@aws-cdk/aws-eks/lib/managed-nodegroup.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { InstanceType, ISecurityGroup, SubnetSelection } from '@aws-cdk/aws-ec2';
import { IRole, ManagedPolicy, Role, ServicePrincipal } from '@aws-cdk/aws-iam';
import { IResource, Resource, Annotations } from '@aws-cdk/core';
import { IResource, Resource, Annotations, withResolved } from '@aws-cdk/core';
import { Construct } from 'constructs';
import { Cluster, ICluster } from './cluster';
import { CfnNodegroup } from './eks.generated';
Expand Down Expand Up @@ -321,12 +321,19 @@ export class Nodegroup extends Resource implements INodegroup {
this.maxSize = props.maxSize ?? this.desiredSize;
this.minSize = props.minSize ?? 1;

if (this.desiredSize > this.maxSize) {
throw new Error(`Desired capacity ${this.desiredSize} can't be greater than max size ${this.maxSize}`);
}
if (this.desiredSize < this.minSize) {
throw new Error(`Minimum capacity ${this.minSize} can't be greater than desired size ${this.desiredSize}`);
}
withResolved(this.desiredSize, this.maxSize, (desired, max) => {
if (desired === undefined) {return ;}
if (desired > max) {
throw new Error(`Desired capacity ${desired} can't be greater than max size ${max}`);
}
});

withResolved(this.desiredSize, this.minSize, (desired, min) => {
if (desired === undefined) {return ;}
if (desired < min) {
throw new Error(`Minimum capacity ${min} can't be greater than desired size ${desired}`);
}
});

if (props.launchTemplateSpec && props.diskSize) {
// see - https://docs.aws.amazon.com/eks/latest/userguide/launch-templates.html
Expand Down
52 changes: 52 additions & 0 deletions packages/@aws-cdk/aws-eks/test/test.nodegroup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -598,6 +598,58 @@ export = {
test.throws(() => cluster.addNodegroupCapacity('ng', { desiredSize: 2, minSize: 3 }), /Minimum capacity 3 can't be greater than desired size 2/);
test.done();
},
'can set minSize , maxSize and DesiredSize'(test: Test) {
// GIVEN
const { stack, vpc } = testFixture();
const cluster = new eks.Cluster(stack, 'Cluster', {
vpc,
defaultCapacity: 0,
version: CLUSTER_VERSION,
});
// WHEN
new eks.Nodegroup(stack, 'NodeGroup', {
cluster: cluster,
minSize: 2,
maxSize: 6,
desiredSize: 4,
});
// THEN
expect(stack).to(haveResourceLike('AWS::EKS::Nodegroup', {
ScalingConfig: {
MinSize: 2,
MaxSize: 6,
DesiredSize: 4,
},
},
));
test.done();
},
'validation is not performed when using Tokens'(test: Test) {
// GIVEN
const { stack, vpc } = testFixture();
const cluster = new eks.Cluster(stack, 'Cluster', {
vpc,
defaultCapacity: 0,
version: CLUSTER_VERSION,
});
// WHEN
new eks.Nodegroup(stack, 'NodeGroup', {
cluster: cluster,
minSize: cdk.Lazy.number({ produce: () => 5 }),
maxSize: cdk.Lazy.number({ produce: () => 1 }),
desiredSize: cdk.Lazy.number({ produce: () => 20 }),
});
// THEN
expect(stack).to(haveResourceLike('AWS::EKS::Nodegroup', {
ScalingConfig: {
MinSize: 5,
MaxSize: 1,
DesiredSize: 20,
},
},
));
test.done();
},
'create nodegroup correctly with launch template'(test: Test) {
// GIVEN
const { stack, vpc } = testFixture();
Expand Down

0 comments on commit caa674a

Please sign in to comment.