forked from aws-samples/cdk-eks-karpenter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
integ.karpenter.ts
122 lines (106 loc) · 3.32 KB
/
integ.karpenter.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import { KubectlV27Layer } from '@aws-cdk/lambda-layer-kubectl-v27';
import { App, CfnOutput, Stack, StackProps } from 'aws-cdk-lib';
import { Vpc } from 'aws-cdk-lib/aws-ec2';
import { Cluster, CoreDnsComputeType, KubernetesVersion } from 'aws-cdk-lib/aws-eks';
import { ManagedPolicy, Role, ServicePrincipal } from 'aws-cdk-lib/aws-iam';
import { Construct } from 'constructs';
import { Karpenter } from '../src';
class TestEKSStack extends Stack {
constructor(scope: Construct, id: string, props: StackProps = {}) {
super(scope, id, props);
const vpc = new Vpc(this, 'testVPC', {
natGateways: 1,
});
const clusterRole = new Role(this, 'clusterRole', {
assumedBy: new ServicePrincipal('eks.amazonaws.com'),
managedPolicies: [
ManagedPolicy.fromAwsManagedPolicyName('AmazonEKSClusterPolicy'),
ManagedPolicy.fromAwsManagedPolicyName('AmazonEKSVPCResourceController'),
],
});
const cluster = new Cluster(this, 'testCluster', {
vpc: vpc,
role: clusterRole,
version: KubernetesVersion.V1_27, // OCI HELM repo only supported by new version.
defaultCapacity: 0,
coreDnsComputeType: CoreDnsComputeType.FARGATE,
kubectlLayer: new KubectlV27Layer(this, 'KubectlLayer'), // new Kubectl lambda layer
});
cluster.addFargateProfile('karpenter', {
selectors: [
{
namespace: 'karpenter',
},
{
namespace: 'kube-system',
labels: {
'k8s-app': 'kube-dns',
},
},
],
});
const karpenter = new Karpenter(this, 'Karpenter', {
cluster: cluster,
version: 'v0.32.0', // test the newest version
});
const nodeClass = karpenter.addEC2NodeClass('nodeclass', {
amiFamily: 'AL2',
subnetSelectorTerms: [
{
tags: {
Name: `${this.stackName}/${vpc.node.id}/PrivateSubnet*`,
},
},
],
securityGroupSelectorTerms: [
{
tags: {
'aws:eks:cluster-name': cluster.clusterName,
},
},
],
role: karpenter.nodeRole.roleName,
});
karpenter.addNodePool('nodepool', {
template: {
spec: {
nodeClassRef: {
apiVersion: 'karpenter.k8s.aws/v1beta1',
kind: 'EC2NodeClass',
name: nodeClass.name,
},
requirements: [
{
key: 'karpenter.k8s.aws/instance-category',
operator: 'In',
values: ['m'],
},
{
key: 'kubernetes.io/arch',
operator: 'In',
values: ['amd64'],
},
],
},
},
});
karpenter.addManagedPolicyToKarpenterRole(ManagedPolicy.fromAwsManagedPolicyName('AmazonSSMManagedInstanceCore'));
new CfnOutput(this, 'ClusterName', {
value: cluster.clusterName,
});
new CfnOutput(this, 'ClusterAdminRole', {
value: cluster.adminRole.roleArn,
});
new CfnOutput(this, 'UpdateKubeConfigCommand', {
value: `aws eks update-kubeconfig --name ${cluster.clusterName} --role-arn ${cluster.adminRole.roleArn}`,
});
}
}
const app = new App();
new TestEKSStack(app, 'test', {
env: {
account: process.env.CDK_DEFAULT_ACCOUNT,
region: process.env.CDK_DEFAULT_REGION,
},
});
app.synth();