Skip to content

Commit cbfff9a

Browse files
committed
feat(eks-v2-alpha): support eks with k8s 1.33
1 parent 8a77828 commit cbfff9a

File tree

135 files changed

+1237
-529
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

135 files changed

+1237
-529
lines changed

packages/@aws-cdk/aws-eks-v2-alpha/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -983,7 +983,7 @@ This is why we used `new cdk8s.App()` as the scope of the chart above.
983983
```ts
984984
import * as constructs from 'constructs';
985985
import * as cdk8s from 'cdk8s';
986-
import * as kplus from 'cdk8s-plus-25';
986+
import * as kplus from 'cdk8s-plus-32';
987987

988988
interface LoadBalancedWebService {
989989
readonly port: number;

packages/@aws-cdk/aws-eks-v2-alpha/lib/cluster.ts

Lines changed: 116 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ import { KubernetesManifest, KubernetesManifestOptions } from './k8s-manifest';
1212
import { KubernetesObjectValue } from './k8s-object-value';
1313
import { KubernetesPatch } from './k8s-patch';
1414
import { IKubectlProvider, KubectlProvider, KubectlProviderOptions } from './kubectl-provider';
15-
import { Nodegroup, NodegroupOptions } from './managed-nodegroup';
15+
import { Nodegroup, NodegroupAmiType, NodegroupOptions } from './managed-nodegroup';
1616
import { OpenIdConnectProvider } from './oidc-provider';
1717
import { BottleRocketImage } from './private/bottlerocket';
1818
import { ServiceAccount, ServiceAccountOptions } from './service-account';
19-
import { renderAmazonLinuxUserData, renderBottlerocketUserData } from './user-data';
19+
import { renderAmazonLinux2023UserData, renderAmazonLinuxUserData, renderBottlerocketUserData } from './user-data';
2020
import * as autoscaling from 'aws-cdk-lib/aws-autoscaling';
2121
import * as ec2 from 'aws-cdk-lib/aws-ec2';
2222
import * as iam from 'aws-cdk-lib/aws-iam';
@@ -65,6 +65,12 @@ export interface ICluster extends IResource, ec2.IConnectable {
6565
*/
6666
readonly clusterCertificateAuthorityData: string;
6767

68+
/**
69+
* The CIDR block to assign Kubernetes service IP addresses from
70+
* @attribute
71+
*/
72+
readonly serviceIpv4Cidr?: string;
73+
6874
/**
6975
* The id of the cluster security group that was created by Amazon EKS for the cluster.
7076
* @attribute
@@ -676,6 +682,15 @@ export class KubernetesVersion {
676682
*/
677683
public static readonly V1_32 = KubernetesVersion.of('1.32');
678684

685+
/**
686+
* Kubernetes version 1.33
687+
*
688+
* When creating a `Cluster` with this version, you need to also specify the
689+
* `kubectlLayer` property with a `KubectlV33Layer` from
690+
* `@aws-cdk/lambda-layer-kubectl-v33`.
691+
*/
692+
public static readonly V1_33 = KubernetesVersion.of('1.33');
693+
679694
/**
680695
* Custom cluster version
681696
* @param version custom version number
@@ -837,9 +852,18 @@ abstract class ClusterBase extends Resource implements ICluster {
837852
}
838853

839854
if (bootstrapEnabled) {
840-
const userData = options.machineImageType === MachineImageType.BOTTLEROCKET ?
841-
renderBottlerocketUserData(this) :
842-
renderAmazonLinuxUserData(this, autoScalingGroup, options.bootstrapOptions);
855+
let userData = [];
856+
switch (options.machineImageType) {
857+
case MachineImageType.AMAZON_LINUX_2023:
858+
userData = renderAmazonLinux2023UserData(this, autoScalingGroup);
859+
break;
860+
case MachineImageType.BOTTLEROCKET:
861+
userData = renderBottlerocketUserData(this);
862+
break;
863+
default:
864+
userData = renderAmazonLinuxUserData(this, autoScalingGroup, options.bootstrapOptions);
865+
break;
866+
}
843867
autoScalingGroup.addUserData(...userData);
844868
}
845869

@@ -967,6 +991,11 @@ export class Cluster extends ClusterBase {
967991
*/
968992
public readonly clusterEncryptionConfigKeyArn: string;
969993

994+
/**
995+
* The CIDR block to assign Kubernetes service IP addresses from
996+
*/
997+
public readonly serviceIpv4Cidr?: string;
998+
970999
/**
9711000
* Manages connection rules (Security Group Rules) for the cluster
9721001
*
@@ -1079,6 +1108,7 @@ export class Cluster extends ClusterBase {
10791108
this.prune = props.prune ?? true;
10801109
this.vpc = props.vpc || new ec2.Vpc(this, 'DefaultVpc');
10811110
this.version = props.version;
1111+
this.serviceIpv4Cidr = props.serviceIpv4Cidr;
10821112

10831113
this._kubectlProviderOptions = props.kubectlProviderOptions;
10841114

@@ -1305,12 +1335,25 @@ export class Cluster extends ClusterBase {
13051335
const instanceType = props.defaultCapacityInstance || DEFAULT_CAPACITY_TYPE;
13061336
// If defaultCapacityType is undefined, use AUTOMODE as the default
13071337
const capacityType = props.defaultCapacityType ?? DefaultCapacityType.AUTOMODE;
1338+
const arch = cpuArchForInstanceType(instanceType);
1339+
const minorVersion = +this.version.version.split('.')[1];
13081340

13091341
// Only create EC2 or Nodegroup capacity if not using AUTOMODE
13101342
if (capacityType === DefaultCapacityType.EC2) {
1311-
this.defaultCapacity = this.addAutoScalingGroupCapacity('DefaultCapacity', { instanceType, minCapacity });
1343+
this.defaultCapacity = this.addAutoScalingGroupCapacity('DefaultCapacity', {
1344+
instanceType,
1345+
minCapacity,
1346+
machineImageType: minorVersion > 32 ? MachineImageType.AMAZON_LINUX_2023 : undefined,
1347+
});
13121348
} else if (capacityType === DefaultCapacityType.NODEGROUP) {
1313-
this.defaultNodegroup = this.addNodegroupCapacity('DefaultCapacity', { instanceTypes: [instanceType], minSize: minCapacity });
1349+
this.defaultNodegroup = this.addNodegroupCapacity('DefaultCapacity', {
1350+
instanceTypes: [instanceType],
1351+
minSize: minCapacity,
1352+
amiType: minorVersion > 32 ? arch === CpuArch.ARM_64
1353+
? NodegroupAmiType.AL2023_ARM_64_STANDARD
1354+
: NodegroupAmiType.AL2023_X86_64_STANDARD
1355+
: undefined,
1356+
});
13141357
}
13151358
// For AUTOMODE, we don't create any explicit capacity as it's managed by EKS
13161359
}
@@ -1424,21 +1467,39 @@ export class Cluster extends ClusterBase {
14241467
*/
14251468
@MethodMetadata()
14261469
public addAutoScalingGroupCapacity(id: string, options: AutoScalingGroupCapacityOptions): autoscaling.AutoScalingGroup {
1427-
if (options.machineImageType === MachineImageType.BOTTLEROCKET && options.bootstrapOptions !== undefined) {
1428-
throw new Error('bootstrapOptions is not supported for Bottlerocket');
1470+
if (
1471+
(options.machineImageType === MachineImageType.BOTTLEROCKET || options.machineImageType === MachineImageType.AMAZON_LINUX_2023)
1472+
&& options.bootstrapOptions !== undefined) {
1473+
throw new Error('bootstrapOptions is not supported for Bottlerocket and Amazon Linux 2023');
14291474
}
1430-
const asg = new autoscaling.AutoScalingGroup(this, id, {
1431-
...options,
1432-
vpc: this.vpc,
1433-
machineImage: options.machineImageType === MachineImageType.BOTTLEROCKET ?
1434-
new BottleRocketImage({
1475+
1476+
let machineImage: ec2.IMachineImage;
1477+
switch (options.machineImageType) {
1478+
case MachineImageType.AMAZON_LINUX_2023:
1479+
machineImage = new Eks2023OptimizedImage({
1480+
nodeType: nodeTypeForInstanceType(options.instanceType),
1481+
cpuArch: cpuArchForInstanceType(options.instanceType),
1482+
kubernetesVersion: this.version.version,
1483+
});
1484+
break;
1485+
case MachineImageType.BOTTLEROCKET:
1486+
machineImage = new BottleRocketImage({
14351487
kubernetesVersion: this.version.version,
1436-
}) :
1437-
new EksOptimizedImage({
1488+
});
1489+
break;
1490+
default:
1491+
machineImage = new EksOptimizedImage({
14381492
nodeType: nodeTypeForInstanceType(options.instanceType),
14391493
cpuArch: cpuArchForInstanceType(options.instanceType),
14401494
kubernetesVersion: this.version.version,
1441-
}),
1495+
});
1496+
break;
1497+
}
1498+
1499+
const asg = new autoscaling.AutoScalingGroup(this, id, {
1500+
...options,
1501+
vpc: this.vpc,
1502+
machineImage,
14421503
});
14431504

14441505
this.connectAutoScalingGroupCapacity(asg, {
@@ -2046,6 +2107,40 @@ export class EksOptimizedImage implements ec2.IMachineImage {
20462107
}
20472108
}
20482109

2110+
/**
2111+
* Construct an Amazon Linux 2023 image from the latest EKS Optimized AMI published in SSM
2112+
*/
2113+
export class Eks2023OptimizedImage implements ec2.IMachineImage {
2114+
private readonly cpuArch?: CpuArch;
2115+
private readonly kubernetesVersion?: string;
2116+
private readonly amiParameterName: string;
2117+
2118+
/**
2119+
* Constructs a new instance of the EksOptimizedAmi class.
2120+
*/
2121+
public constructor(props: EksOptimizedImageProps = {}) {
2122+
this.cpuArch = props.cpuArch ?? CpuArch.X86_64;
2123+
this.kubernetesVersion = props.kubernetesVersion ?? LATEST_KUBERNETES_VERSION;
2124+
2125+
this.amiParameterName = `/aws/service/eks/optimized-ami/${this.kubernetesVersion}/amazon-linux-2023/`
2126+
+ (this.cpuArch === CpuArch.ARM_64 ? 'arm64/' : 'x86_64/')
2127+
+ 'standard/recommended/image_id';
2128+
}
2129+
2130+
/**
2131+
* Return the correct image
2132+
*/
2133+
public getImage(scope: Construct): ec2.MachineImageConfig {
2134+
const ami = ssm.StringParameter.valueForStringParameter(scope, this.amiParameterName);
2135+
2136+
return {
2137+
imageId: ami,
2138+
osType: ec2.OperatingSystemType.LINUX,
2139+
userData: ec2.UserData.custom(''),
2140+
};
2141+
}
2142+
}
2143+
20492144
// MAINTAINERS: use ./scripts/kube_bump.sh to update LATEST_KUBERNETES_VERSION
20502145
const LATEST_KUBERNETES_VERSION = '1.24';
20512146

@@ -2126,6 +2221,10 @@ export enum DefaultCapacityType {
21262221
* The machine image type
21272222
*/
21282223
export enum MachineImageType {
2224+
/**
2225+
* Amazon EKS-optimized Linux 2023 AMI
2226+
*/
2227+
AMAZON_LINUX_2023,
21292228
/**
21302229
* Amazon EKS-optimized Linux AMI
21312230
*/

packages/@aws-cdk/aws-eks-v2-alpha/lib/user-data.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,31 @@ export function renderAmazonLinuxUserData(cluster: ICluster, autoScalingGroup: a
6565
];
6666
}
6767

68+
// eslint-disable-next-line max-len
69+
export function renderAmazonLinux2023UserData(cluster: ICluster, autoScalingGroup: autoscaling.AutoScalingGroup): string[] {
70+
// TODO: Support ipv6
71+
if (!cluster.serviceIpv4Cidr) {
72+
throw new Error('serviceIpv4Cidr must be defined in the cluster to render Amazon Linux 2023 user data');
73+
}
74+
75+
// determine lifecycle label based on whether the ASG has a spot price.
76+
const lifecycleLabel = autoScalingGroup.spotPrice ? LifecycleLabel.SPOT : LifecycleLabel.ON_DEMAND;
77+
78+
return [`---
79+
apiVersion: node.eks.aws/v1alpha1
80+
kind: NodeConfig
81+
spec:
82+
cluster:
83+
name: ${cluster.clusterName}
84+
apiServerEndpoint: ${cluster.clusterEndpoint}
85+
certificateAuthority: ${cluster.clusterCertificateAuthorityData}
86+
cidr: ${cluster.serviceIpv4Cidr}
87+
kubelet:
88+
flags:
89+
- "--node-labels=lifecycle=${lifecycleLabel}"
90+
`];
91+
}
92+
6893
export function renderBottlerocketUserData(cluster: ICluster): string[] {
6994
return [
7095
'[settings.kubernetes]',

packages/@aws-cdk/aws-eks-v2-alpha/package.json

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,18 +87,18 @@
8787
"@aws-cdk/integ-runner": "^2.188.3",
8888
"@aws-cdk/integ-tests-alpha": "0.0.0",
8989
"@aws-cdk/pkglint": "0.0.0",
90-
"@aws-cdk/lambda-layer-kubectl-v24": "^2.0.242",
9190
"@aws-cdk/lambda-layer-kubectl-v29": "^2.1.1",
9291
"@aws-cdk/lambda-layer-kubectl-v30": "^2.0.4",
9392
"@aws-cdk/lambda-layer-kubectl-v31": "^2.1.0",
9493
"@aws-cdk/lambda-layer-kubectl-v32": "^2.1.0",
94+
"@aws-cdk/lambda-layer-kubectl-v33": "^2.0.0",
9595
"@types/jest": "^29.5.14",
9696
"aws-sdk": "^2.1692.0",
9797
"aws-cdk-lib": "0.0.0",
9898
"constructs": "^10.0.0",
9999
"sinon": "^9.2.4",
100-
"cdk8s": "2.69.74",
101-
"cdk8s-plus-27": "2.9.5"
100+
"cdk8s": "^2.69.74",
101+
"cdk8s-plus-32": "^2.1.6"
102102
},
103103
"dependencies": {
104104
"yaml": "1.10.2"
@@ -136,7 +136,8 @@
136136
"exampleDependencies": {
137137
"@aws-cdk/lambda-layer-kubectl-v31": "^2.0.0",
138138
"@aws-cdk/lambda-layer-kubectl-v32": "^2.0.0",
139-
"cdk8s-plus-25": "^2.7.0"
139+
"@aws-cdk/lambda-layer-kubectl-v33": "^2.0.0",
140+
"cdk8s-plus-32": "^2.1.6"
140141
}
141142
}
142143
}

packages/@aws-cdk/aws-eks-v2-alpha/test/automode.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import * as iam from 'aws-cdk-lib/aws-iam';
44
import * as eks from '../lib';
55
import { testFixtureNoVpc } from './util';
66

7-
const CLUSTER_VERSION = eks.KubernetesVersion.V1_32;
7+
const CLUSTER_VERSION = eks.KubernetesVersion.V1_33;
88

99
describe('eks auto mode', () => {
1010
describe('basic configuration', () => {

0 commit comments

Comments
 (0)