diff --git a/packages/@aws-cdk/aws-eks/README.md b/packages/@aws-cdk/aws-eks/README.md index c73fd1bd955cc..6330f7f837aae 100644 --- a/packages/@aws-cdk/aws-eks/README.md +++ b/packages/@aws-cdk/aws-eks/README.md @@ -17,20 +17,18 @@ This construct library allows you to define [Amazon Elastic Container Service for Kubernetes (EKS)](https://aws.amazon.com/eks/) clusters programmatically. - This library also supports programmatically defining Kubernetes resource manifests within EKS clusters. -This example defines an Amazon EKS cluster with a single pod: +This example defines an Amazon EKS cluster with the following configuration: + +- 2x **m5.large** instances (this instance type suits most common use-cases, and is good value for money) +- Dedicated VPC with default configuration (see [ec2.Vpc](https://docs.aws.amazon.com/cdk/api/latest/docs/aws-ec2-readme.html#vpc)) +- A Kubernetes pod with a container based on the [paulbouwer/hello-kubernetes](https://github.com/paulbouwer/hello-kubernetes) image. ```ts const cluster = new eks.Cluster(this, 'hello-eks'); -cluster.addCapacity('default', { - instanceType: new ec2.InstanceType('t2.medium'), - desiredCapacity: 10, -}); - cluster.addResource('mypod', { apiVersion: 'v1', kind: 'Pod', @@ -54,6 +52,51 @@ in the AWS CDK Developer Guide for more details. Here is a [complete sample](https://github.com/aws/aws-cdk/blob/master/packages/%40aws-cdk/aws-eks/test/integ.eks-kubectl.lit.ts). +### Capacity + +By default, `eks.Cluster` is created with x2 `m5.large` instances. + +```ts +new eks.Cluster(this, 'cluster-two-m5-large'); +``` + +The quantity and instance type for the default capacity can be specified through +the `defaultCapacity` and `defaultCapacityInstance` props: + +```ts +new eks.Cluster(this, 'cluster', { + defaultCapacity: 10, + defaultCapacityInstance: new ec2.InstanceType('m2.xlarge') +}); +``` +To disable the default capacity, simply set `defaultCapacity` to `0`: + + +```ts +new eks.Cluster(this, 'cluster-with-no-capacity', { defaultCapacity: 0 }); +``` + +The `cluster.defaultCapacity` property will reference the `AutoScalingGroup` +resource for the default capacity. It will be `undefined` if `defaultCapacity` +is set to `0`: + +```ts +const cluster = new eks.Cluster(this, 'my-cluster'); +cluster.defaultCapacity!.scaleOnCpuUtilization('up', { + targetUtilizationPercent: 80 +}); +``` + +You can add customized capacity through `cluster.addCapacity()`: + +```ts +cluster.addCapacity('frontend-nodes', { + instanceType: new ec2.InstanceType('t2.medium'), + desiredCapacity: 3, + vpcSubnets: { subnetType: ec2.SubnetType.PUBLIC } +}); +``` + ### Interacting with Your Cluster The Amazon EKS construct library allows you to specify an IAM role that will be diff --git a/packages/@aws-cdk/aws-eks/lib/cluster.ts b/packages/@aws-cdk/aws-eks/lib/cluster.ts index abbfec968ebb2..55231016fbce9 100644 --- a/packages/@aws-cdk/aws-eks/lib/cluster.ts +++ b/packages/@aws-cdk/aws-eks/lib/cluster.ts @@ -13,6 +13,10 @@ import { maxPodsForInstanceType } from './instance-data'; import { KubernetesResource } from './k8s-resource'; import { KubectlLayer } from './kubectl-layer'; +// defaults are based on https://eksctl.io +const DEFAULT_CAPACITY_COUNT = 2; +const DEFAULT_CAPACITY_TYPE = ec2.InstanceType.of(ec2.InstanceClass.M5, ec2.InstanceSize.LARGE); + /** * An EKS cluster */ @@ -172,6 +176,26 @@ export interface ClusterProps { * @default true The cluster can be managed by the AWS CDK application. */ readonly kubectlEnabled?: boolean; + + /** + * Number of instances to allocate as an initial capacity for this cluster. + * Instance type can be configured through `defaultCapacityInstanceType`, + * which defaults to `m5.large`. + * + * Use `cluster.addCapacity` to add additional customized capacity. Set this + * to `0` is you wish to avoid the initial capacity allocation. + * + * @default 2 + */ + readonly defaultCapacity?: number; + + /** + * The instance type to use for the default capacity. This will only be taken + * into account if `defaultCapacity` is > 0. + * + * @default m5.large + */ + readonly defaultCapacityInstance?: ec2.InstanceType; } /** @@ -249,6 +273,12 @@ export class Cluster extends Resource implements ICluster { */ public readonly _k8sResourceHandler?: lambda.Function; + /** + * The auto scaling group that hosts the default capacity for this cluster. + * This will be `undefined` if the default capacity is set to 0. + */ + public readonly defaultCapacity?: autoscaling.AutoScalingGroup; + /** * The IAM role that was used to create this cluster. This role is * automatically added by Amazon EKS to the `system:masters` RBAC group of the @@ -347,6 +377,13 @@ export class Cluster extends Resource implements ICluster { this.awsAuth.addMastersRole(props.mastersRole); } + + // allocate default capacity if non-zero (or default). + const desiredCapacity = props.defaultCapacity === undefined ? DEFAULT_CAPACITY_COUNT : props.defaultCapacity; + if (desiredCapacity > 0) { + const instanceType = props.defaultCapacityInstance || DEFAULT_CAPACITY_TYPE; + this.defaultCapacity = this.addCapacity('DefaultCapacity', { instanceType, desiredCapacity }); + } } /** diff --git a/packages/@aws-cdk/aws-eks/test/integ.eks-cluster.defaults.expected.json b/packages/@aws-cdk/aws-eks/test/integ.eks-cluster.defaults.expected.json index 2b22287b78df5..f3c7d34a8e036 100644 --- a/packages/@aws-cdk/aws-eks/test/integ.eks-cluster.defaults.expected.json +++ b/packages/@aws-cdk/aws-eks/test/integ.eks-cluster.defaults.expected.json @@ -591,6 +591,27 @@ } } }, + "ClusterControlPlaneSecurityGroupfromeksintegdefaultsClusterDefaultCapacityInstanceSecurityGroup913A261E44376C54A34": { + "Type": "AWS::EC2::SecurityGroupIngress", + "Properties": { + "IpProtocol": "tcp", + "Description": "from eksintegdefaultsClusterDefaultCapacityInstanceSecurityGroup913A261E:443", + "FromPort": 443, + "GroupId": { + "Fn::GetAtt": [ + "ClusterControlPlaneSecurityGroupD274242C", + "GroupId" + ] + }, + "SourceSecurityGroupId": { + "Fn::GetAtt": [ + "ClusterDefaultCapacityInstanceSecurityGroup8FDF4D48", + "GroupId" + ] + }, + "ToPort": 443 + } + }, "ClusterResourceHandlerServiceRole7FB16465": { "Type": "AWS::IAM::Role", "Properties": { @@ -855,6 +876,302 @@ "ClusterResourceHandlerServiceRole7FB16465" ] }, + "ClusterDefaultCapacityInstanceSecurityGroup8FDF4D48": { + "Type": "AWS::EC2::SecurityGroup", + "Properties": { + "GroupDescription": "eks-integ-defaults/Cluster/DefaultCapacity/InstanceSecurityGroup", + "SecurityGroupEgress": [ + { + "CidrIp": "0.0.0.0/0", + "Description": "Allow all outbound traffic by default", + "IpProtocol": "-1" + } + ], + "SecurityGroupIngress": [], + "Tags": [ + { + "Key": "Name", + "Value": "eks-integ-defaults/Cluster/DefaultCapacity" + }, + { + "Key": { + "Fn::Join": [ + "", + [ + "kubernetes.io/cluster/", + { + "Ref": "Cluster9EE0221C" + } + ] + ] + }, + "Value": "owned" + } + ], + "VpcId": { + "Ref": "ClusterDefaultVpcFA9F2722" + } + } + }, + "ClusterDefaultCapacityInstanceSecurityGroupfromeksintegdefaultsClusterDefaultCapacityInstanceSecurityGroup913A261EALLTRAFFICA8163873": { + "Type": "AWS::EC2::SecurityGroupIngress", + "Properties": { + "IpProtocol": "-1", + "Description": "from eksintegdefaultsClusterDefaultCapacityInstanceSecurityGroup913A261E:ALL TRAFFIC", + "GroupId": { + "Fn::GetAtt": [ + "ClusterDefaultCapacityInstanceSecurityGroup8FDF4D48", + "GroupId" + ] + }, + "SourceSecurityGroupId": { + "Fn::GetAtt": [ + "ClusterDefaultCapacityInstanceSecurityGroup8FDF4D48", + "GroupId" + ] + } + } + }, + "ClusterDefaultCapacityInstanceSecurityGroupfromeksintegdefaultsClusterControlPlaneSecurityGroup0FA4E3AB4436B585189": { + "Type": "AWS::EC2::SecurityGroupIngress", + "Properties": { + "IpProtocol": "tcp", + "Description": "from eksintegdefaultsClusterControlPlaneSecurityGroup0FA4E3AB:443", + "FromPort": 443, + "GroupId": { + "Fn::GetAtt": [ + "ClusterDefaultCapacityInstanceSecurityGroup8FDF4D48", + "GroupId" + ] + }, + "SourceSecurityGroupId": { + "Fn::GetAtt": [ + "ClusterControlPlaneSecurityGroupD274242C", + "GroupId" + ] + }, + "ToPort": 443 + } + }, + "ClusterDefaultCapacityInstanceSecurityGroupfromeksintegdefaultsClusterControlPlaneSecurityGroup0FA4E3AB102565535C02D6CB8": { + "Type": "AWS::EC2::SecurityGroupIngress", + "Properties": { + "IpProtocol": "tcp", + "Description": "from eksintegdefaultsClusterControlPlaneSecurityGroup0FA4E3AB:1025-65535", + "FromPort": 1025, + "GroupId": { + "Fn::GetAtt": [ + "ClusterDefaultCapacityInstanceSecurityGroup8FDF4D48", + "GroupId" + ] + }, + "SourceSecurityGroupId": { + "Fn::GetAtt": [ + "ClusterControlPlaneSecurityGroupD274242C", + "GroupId" + ] + }, + "ToPort": 65535 + } + }, + "ClusterDefaultCapacityInstanceRole3E209969": { + "Type": "AWS::IAM::Role", + "Properties": { + "AssumeRolePolicyDocument": { + "Statement": [ + { + "Action": "sts:AssumeRole", + "Effect": "Allow", + "Principal": { + "Service": { + "Fn::Join": [ + "", + [ + "ec2.", + { + "Ref": "AWS::URLSuffix" + } + ] + ] + } + } + } + ], + "Version": "2012-10-17" + }, + "ManagedPolicyArns": [ + { + "Fn::Join": [ + "", + [ + "arn:", + { + "Ref": "AWS::Partition" + }, + ":iam::aws:policy/AmazonEKSWorkerNodePolicy" + ] + ] + }, + { + "Fn::Join": [ + "", + [ + "arn:", + { + "Ref": "AWS::Partition" + }, + ":iam::aws:policy/AmazonEKS_CNI_Policy" + ] + ] + }, + { + "Fn::Join": [ + "", + [ + "arn:", + { + "Ref": "AWS::Partition" + }, + ":iam::aws:policy/AmazonEC2ContainerRegistryReadOnly" + ] + ] + } + ] + } + }, + "ClusterDefaultCapacityInstanceProfile70387741": { + "Type": "AWS::IAM::InstanceProfile", + "Properties": { + "Roles": [ + { + "Ref": "ClusterDefaultCapacityInstanceRole3E209969" + } + ] + } + }, + "ClusterDefaultCapacityLaunchConfig72790CF7": { + "Type": "AWS::AutoScaling::LaunchConfiguration", + "Properties": { + "ImageId": "ami-12345", + "InstanceType": "m5.large", + "IamInstanceProfile": { + "Ref": "ClusterDefaultCapacityInstanceProfile70387741" + }, + "SecurityGroups": [ + { + "Fn::GetAtt": [ + "ClusterDefaultCapacityInstanceSecurityGroup8FDF4D48", + "GroupId" + ] + } + ], + "UserData": { + "Fn::Base64": { + "Fn::Join": [ + "", + [ + "#!/bin/bash\nset -o xtrace\n/etc/eks/bootstrap.sh ", + { + "Ref": "Cluster9EE0221C" + }, + " --use-max-pods 29" + ] + ] + } + } + }, + "DependsOn": [ + "ClusterDefaultCapacityInstanceRole3E209969" + ] + }, + "ClusterDefaultCapacityASG00CC9431": { + "Type": "AWS::AutoScaling::AutoScalingGroup", + "Properties": { + "MaxSize": "2", + "MinSize": "1", + "DesiredCapacity": "2", + "LaunchConfigurationName": { + "Ref": "ClusterDefaultCapacityLaunchConfig72790CF7" + }, + "Tags": [ + { + "Key": "Name", + "PropagateAtLaunch": true, + "Value": "eks-integ-defaults/Cluster/DefaultCapacity" + }, + { + "Key": { + "Fn::Join": [ + "", + [ + "kubernetes.io/cluster/", + { + "Ref": "Cluster9EE0221C" + } + ] + ] + }, + "PropagateAtLaunch": true, + "Value": "owned" + } + ], + "VPCZoneIdentifier": [ + { + "Ref": "ClusterDefaultVpcPrivateSubnet1Subnet03F39409" + }, + { + "Ref": "ClusterDefaultVpcPrivateSubnet2SubnetA526AEA7" + }, + { + "Ref": "ClusterDefaultVpcPrivateSubnet3SubnetB64BC839" + } + ] + }, + "UpdatePolicy": { + "AutoScalingRollingUpdate": { + "WaitOnResourceSignals": false, + "PauseTime": "PT0S", + "SuspendProcesses": [ + "HealthCheck", + "ReplaceUnhealthy", + "AZRebalance", + "AlarmNotification", + "ScheduledActions" + ] + }, + "AutoScalingScheduledAction": { + "IgnoreUnmodifiedGroupSizeProperties": true + } + } + }, + "ClusterAwsAuthmanifestFE51F8AE": { + "Type": "Custom::AWSCDK-EKS-KubernetesResource", + "Properties": { + "ServiceToken": { + "Fn::GetAtt": [ + "ClusterKubernetesResourceHandler81C19BC8", + "Arn" + ] + }, + "Manifest": { + "Fn::Join": [ + "", + [ + "[{\"apiVersion\":\"v1\",\"kind\":\"ConfigMap\",\"metadata\":{\"name\":\"aws-auth\",\"namespace\":\"kube-system\"},\"data\":{\"mapRoles\":\"[{\\\"rolearn\\\":\\\"", + { + "Fn::GetAtt": [ + "ClusterDefaultCapacityInstanceRole3E209969", + "Arn" + ] + }, + "\\\",\\\"username\\\":\\\"system:node:{{EC2PrivateDNSName}}\\\",\\\"groups\\\":[\\\"system:bootstrappers\\\",\\\"system:nodes\\\"]}]\",\"mapUsers\":\"[]\",\"mapAccounts\":\"[]\"}}]" + ] + ] + } + }, + "UpdateReplacePolicy": "Delete", + "DeletionPolicy": "Delete" + }, "kubectllayer8C2542BCBF2B4DFEB765E181FD30A9A0617C4ADA": { "Type": "AWS::Serverless::Application", "Properties": { @@ -899,6 +1216,14 @@ "Value": { "Ref": "Cluster9EE0221C" } + }, + "ClusterDefaultCapacityInstanceRoleARN7DADF219": { + "Value": { + "Fn::GetAtt": [ + "ClusterDefaultCapacityInstanceRole3E209969", + "Arn" + ] + } } } } \ No newline at end of file diff --git a/packages/@aws-cdk/aws-eks/test/integ.eks-cluster.kubectl-disabled.ts b/packages/@aws-cdk/aws-eks/test/integ.eks-cluster.kubectl-disabled.ts index c324ee06b927f..26af21fec807f 100644 --- a/packages/@aws-cdk/aws-eks/test/integ.eks-cluster.kubectl-disabled.ts +++ b/packages/@aws-cdk/aws-eks/test/integ.eks-cluster.kubectl-disabled.ts @@ -11,7 +11,8 @@ class EksClusterStack extends cdk.Stack { const cluster = new eks.Cluster(this, 'EKSCluster', { vpc, - kubectlEnabled: false + kubectlEnabled: false, + defaultCapacity: 0, }); cluster.addCapacity('Nodes', { diff --git a/packages/@aws-cdk/aws-eks/test/integ.eks-cluster.lit.ts b/packages/@aws-cdk/aws-eks/test/integ.eks-cluster.lit.ts index aafc63398d573..f9360b2c4b848 100644 --- a/packages/@aws-cdk/aws-eks/test/integ.eks-cluster.lit.ts +++ b/packages/@aws-cdk/aws-eks/test/integ.eks-cluster.lit.ts @@ -11,6 +11,7 @@ class EksClusterStack extends cdk.Stack { const cluster = new eks.Cluster(this, 'EKSCluster', { vpc, + defaultCapacity: 0, }); cluster.addCapacity('Nodes', { diff --git a/packages/@aws-cdk/aws-eks/test/integ.eks-kubectl.lit.ts b/packages/@aws-cdk/aws-eks/test/integ.eks-kubectl.lit.ts index b02368ec43e3e..984704bc574d4 100644 --- a/packages/@aws-cdk/aws-eks/test/integ.eks-kubectl.lit.ts +++ b/packages/@aws-cdk/aws-eks/test/integ.eks-kubectl.lit.ts @@ -38,6 +38,7 @@ class ClusterStack extends DevelopmentStack { // define the cluster. kubectl is enabled by default. this.cluster = new Cluster(this, 'cluster22', { vpc: props.vpc, + defaultCapacity: 0, }); // define an IAM role assumable by anyone in the account and map it to the k8s diff --git a/packages/@aws-cdk/aws-eks/test/test.awsauth.ts b/packages/@aws-cdk/aws-eks/test/test.awsauth.ts index 4df334158f87f..7c7158488a3ad 100644 --- a/packages/@aws-cdk/aws-eks/test/test.awsauth.ts +++ b/packages/@aws-cdk/aws-eks/test/test.awsauth.ts @@ -1,20 +1,17 @@ import { countResources, expect, haveResource } from '@aws-cdk/assert'; -import ec2 = require('@aws-cdk/aws-ec2'); import iam = require('@aws-cdk/aws-iam'); -import { Stack } from '@aws-cdk/core'; import { Test } from 'nodeunit'; import { Cluster, KubernetesResource } from '../lib'; import { AwsAuth } from '../lib/aws-auth'; -import { testFixture } from './util'; +import { testFixtureNoVpc } from './util'; // tslint:disable:max-line-length export = { 'empty aws-auth'(test: Test) { // GIVEN - const stack = new Stack(); - const vpc = new ec2.Vpc(stack, 'vpc'); - const cluster = new Cluster(stack, 'cluster', { vpc }); + const { stack } = testFixtureNoVpc(); + const cluster = new Cluster(stack, 'cluster'); // WHEN new AwsAuth(stack, 'AwsAuth', { cluster }); @@ -33,8 +30,8 @@ export = { 'addRoleMapping and addUserMapping can be used to define the aws-auth ConfigMap'(test: Test) { // GIVEN - const { stack, vpc } = testFixture(); - const cluster = new Cluster(stack, 'Cluster', { vpc }); + const { stack } = testFixtureNoVpc(); + const cluster = new Cluster(stack, 'Cluster'); const role = new iam.Role(stack, 'role', { assumedBy: new iam.AnyPrincipal() }); const user = new iam.User(stack, 'user'); @@ -54,6 +51,13 @@ export = { "", [ "[{\"apiVersion\":\"v1\",\"kind\":\"ConfigMap\",\"metadata\":{\"name\":\"aws-auth\",\"namespace\":\"kube-system\"},\"data\":{\"mapRoles\":\"[{\\\"rolearn\\\":\\\"", + { + "Fn::GetAtt": [ + "ClusterDefaultCapacityInstanceRole3E209969", + "Arn" + ] + }, + "\\\",\\\"username\\\":\\\"system:node:{{EC2PrivateDNSName}}\\\",\\\"groups\\\":[\\\"system:bootstrappers\\\",\\\"system:nodes\\\"]},{\\\"rolearn\\\":\\\"", { "Fn::GetAtt": [ "roleC7B7E775", @@ -96,7 +100,7 @@ export = { ] } })); - test.done(); - }, + test.done(); + } }; diff --git a/packages/@aws-cdk/aws-eks/test/test.cluster.ts b/packages/@aws-cdk/aws-eks/test/test.cluster.ts index e11b73f2b059a..391f77d9b49dd 100644 --- a/packages/@aws-cdk/aws-eks/test/test.cluster.ts +++ b/packages/@aws-cdk/aws-eks/test/test.cluster.ts @@ -2,11 +2,11 @@ import { expect, haveResource, haveResourceLike, not } from '@aws-cdk/assert'; import ec2 = require('@aws-cdk/aws-ec2'); import iam = require('@aws-cdk/aws-iam'); import cdk = require('@aws-cdk/core'); -import { CfnOutput, Stack } from '@aws-cdk/core'; +import { CfnOutput } from '@aws-cdk/core'; import { Test } from 'nodeunit'; import eks = require('../lib'); import { KubernetesResource } from '../lib'; -import { testFixture } from './util'; +import { testFixture, testFixtureNoVpc } from './util'; // tslint:disable:max-line-length @@ -16,7 +16,7 @@ export = { const { stack, vpc } = testFixture(); // WHEN - new eks.Cluster(stack, 'Cluster', { vpc, kubectlEnabled: false }); + new eks.Cluster(stack, 'Cluster', { vpc, kubectlEnabled: false, defaultCapacity: 0 }); // THEN expect(stack).to(haveResourceLike('AWS::EKS::Cluster', { @@ -35,7 +35,7 @@ export = { 'if "vpc" is not specified, vpc with default configuration will be created'(test: Test) { // GIVEN - const stack = new Stack(); + const { stack } = testFixtureNoVpc(); // WHEN new eks.Cluster(stack, 'cluster'); @@ -45,12 +45,60 @@ export = { test.done(); }, + 'default capacity': { + + 'x2 m5.large by default'(test: Test) { + // GIVEN + const { stack } = testFixtureNoVpc(); + + // WHEN + const cluster = new eks.Cluster(stack, 'cluster'); + + // THEN + test.ok(cluster.defaultCapacity); + expect(stack).to(haveResource('AWS::AutoScaling::AutoScalingGroup', { DesiredCapacity: '2' })); + expect(stack).to(haveResource('AWS::AutoScaling::LaunchConfiguration', { InstanceType: 'm5.large' })); + test.done(); + }, + + 'quantity and type can be customized'(test: Test) { + // GIVEN + const { stack } = testFixtureNoVpc(); + + // WHEN + const cluster = new eks.Cluster(stack, 'cluster', { + defaultCapacity: 10, + defaultCapacityInstance: new ec2.InstanceType('m2.xlarge') + }); + + // THEN + test.ok(cluster.defaultCapacity); + expect(stack).to(haveResource('AWS::AutoScaling::AutoScalingGroup', { DesiredCapacity: '10' })); + expect(stack).to(haveResource('AWS::AutoScaling::LaunchConfiguration', { InstanceType: 'm2.xlarge' })); + test.done(); + }, + + 'defaultCapacity=0 will not allocate at all'(test: Test) { + // GIVEN + const { stack } = testFixtureNoVpc(); + + // WHEN + const cluster = new eks.Cluster(stack, 'cluster', { defaultCapacity: 0 }); + + // THEN + test.ok(!cluster.defaultCapacity); + expect(stack).notTo(haveResource('AWS::AutoScaling::AutoScalingGroup')); + expect(stack).notTo(haveResource('AWS::AutoScaling::LaunchConfiguration')); + test.done(); + } + }, + 'creating a cluster tags the private VPC subnets'(test: Test) { // GIVEN const { stack, vpc } = testFixture(); // WHEN - new eks.Cluster(stack, 'Cluster', { vpc, kubectlEnabled: false }); + new eks.Cluster(stack, 'Cluster', { vpc, kubectlEnabled: false, defaultCapacity: 0 }); // THEN expect(stack).to(haveResource('AWS::EC2::Subnet', { @@ -68,7 +116,7 @@ export = { 'adding capacity creates an ASG with tags'(test: Test) { // GIVEN const { stack, vpc } = testFixture(); - const cluster = new eks.Cluster(stack, 'Cluster', { vpc, kubectlEnabled: false }); + const cluster = new eks.Cluster(stack, 'Cluster', { vpc, kubectlEnabled: false, defaultCapacity: 0 }); // WHEN cluster.addCapacity('Default', { @@ -97,7 +145,7 @@ export = { 'adding capacity correctly deduces maxPods and adds userdata'(test: Test) { // GIVEN const { stack, vpc } = testFixture(); - const cluster = new eks.Cluster(stack, 'Cluster', { vpc, kubectlEnabled: false }); + const cluster = new eks.Cluster(stack, 'Cluster', { vpc, kubectlEnabled: false, defaultCapacity: 0 }); // WHEN cluster.addCapacity('Default', { @@ -127,7 +175,7 @@ export = { // GIVEN const { stack: stack1, vpc, app } = testFixture(); const stack2 = new cdk.Stack(app, 'stack2', { env: { region: 'us-east-1' } }); - const cluster = new eks.Cluster(stack1, 'Cluster', { vpc, kubectlEnabled: false }); + const cluster = new eks.Cluster(stack1, 'Cluster', { vpc, kubectlEnabled: false, defaultCapacity: 0 }); // WHEN const imported = eks.Cluster.fromClusterAttributes(stack2, 'Imported', { @@ -158,7 +206,7 @@ export = { 'disabled features when kubectl is disabled'(test: Test) { // GIVEN const { stack, vpc } = testFixture(); - const cluster = new eks.Cluster(stack, 'Cluster', { vpc, kubectlEnabled: false }); + const cluster = new eks.Cluster(stack, 'Cluster', { vpc, kubectlEnabled: false, defaultCapacity: 0 }); test.throws(() => cluster.awsAuth, /Cannot define aws-auth mappings if kubectl is disabled/); test.throws(() => cluster.addResource('foo', {}), /Cannot define a KubernetesManifest resource on a cluster with kubectl disabled/); @@ -173,7 +221,7 @@ export = { const role = new iam.Role(stack, 'role', { assumedBy: new iam.AnyPrincipal() }); // WHEN - new eks.Cluster(stack, 'Cluster', { vpc, mastersRole: role }); + new eks.Cluster(stack, 'Cluster', { vpc, mastersRole: role, defaultCapacity: 0 }); // THEN expect(stack).to(haveResource(KubernetesResource.RESOURCE_TYPE, { @@ -200,7 +248,7 @@ export = { 'addResource can be used to apply k8s manifests on this cluster'(test: Test) { // GIVEN const { stack, vpc } = testFixture(); - const cluster = new eks.Cluster(stack, 'Cluster', { vpc }); + const cluster = new eks.Cluster(stack, 'Cluster', { vpc, defaultCapacity: 0 }); // WHEN cluster.addResource('manifest1', { foo: 123 }); @@ -221,7 +269,7 @@ export = { 'when kubectl is enabled (default) adding capacity will automatically map its IAM role'(test: Test) { // GIVEN const { stack, vpc } = testFixture(); - const cluster = new eks.Cluster(stack, 'Cluster', { vpc }); + const cluster = new eks.Cluster(stack, 'Cluster', { vpc, defaultCapacity: 0 }); // WHEN cluster.addCapacity('default', { @@ -253,7 +301,7 @@ export = { 'addCapacity will *not* map the IAM role if mapRole is false'(test: Test) { // GIVEN const { stack, vpc } = testFixture(); - const cluster = new eks.Cluster(stack, 'Cluster', { vpc }); + const cluster = new eks.Cluster(stack, 'Cluster', { vpc, defaultCapacity: 0 }); // WHEN cluster.addCapacity('default', { @@ -269,7 +317,7 @@ export = { 'addCapacity will *not* map the IAM role if kubectl is disabled'(test: Test) { // GIVEN const { stack, vpc } = testFixture(); - const cluster = new eks.Cluster(stack, 'Cluster', { vpc, kubectlEnabled: false }); + const cluster = new eks.Cluster(stack, 'Cluster', { vpc, kubectlEnabled: false, defaultCapacity: 0 }); // WHEN cluster.addCapacity('default', { diff --git a/packages/@aws-cdk/aws-eks/test/test.manifest.ts b/packages/@aws-cdk/aws-eks/test/test.manifest.ts index 95112ac027f42..ca038ba148cc1 100644 --- a/packages/@aws-cdk/aws-eks/test/test.manifest.ts +++ b/packages/@aws-cdk/aws-eks/test/test.manifest.ts @@ -1,17 +1,15 @@ import { expect, haveResource } from '@aws-cdk/assert'; -import { Vpc } from '@aws-cdk/aws-ec2'; -import { Stack } from '@aws-cdk/core'; import { Test } from 'nodeunit'; import { Cluster, KubernetesResource } from '../lib'; +import { testFixtureNoVpc } from './util'; // tslint:disable:max-line-length export = { 'basic usage'(test: Test) { // GIVEN - const stack = new Stack(); - const vpc = new Vpc(stack, 'vpc'); - const cluster = new Cluster(stack, 'cluster', { vpc }); + const { stack } = testFixtureNoVpc(); + const cluster = new Cluster(stack, 'cluster'); const manifest = [ { diff --git a/packages/@aws-cdk/aws-eks/test/util.ts b/packages/@aws-cdk/aws-eks/test/util.ts index 12065b26588f3..ec04aad46e5d8 100644 --- a/packages/@aws-cdk/aws-eks/test/util.ts +++ b/packages/@aws-cdk/aws-eks/test/util.ts @@ -2,9 +2,14 @@ import ec2 = require('@aws-cdk/aws-ec2'); import { App, Stack } from '@aws-cdk/core'; export function testFixture() { - const app = new App(); - const stack = new Stack(app, 'Stack', { env: { region: 'us-east-1' }}); + const { stack, app } = testFixtureNoVpc(); const vpc = new ec2.Vpc(stack, 'VPC'); return { stack, vpc, app }; } + +export function testFixtureNoVpc() { + const app = new App(); + const stack = new Stack(app, 'Stack', { env: { region: 'us-east-1' }}); + return { stack, app }; +}