@reyemtech/nimbus 3.11.0 → 3.12.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/aws/cluster.d.ts +25 -0
- package/dist/cjs/aws/cluster.d.ts.map +1 -1
- package/dist/cjs/aws/cluster.js +59 -14
- package/dist/cjs/aws/cluster.js.map +1 -1
- package/dist/cjs/aws/eks-autoscaling.d.ts +13 -0
- package/dist/cjs/aws/eks-autoscaling.d.ts.map +1 -0
- package/dist/cjs/aws/eks-autoscaling.js +648 -0
- package/dist/cjs/aws/eks-autoscaling.js.map +1 -0
- package/dist/cjs/aws/index.d.ts +1 -1
- package/dist/cjs/aws/index.d.ts.map +1 -1
- package/dist/cjs/aws/index.js.map +1 -1
- package/dist/cjs/aws/network.d.ts.map +1 -1
- package/dist/cjs/aws/network.js +1 -0
- package/dist/cjs/aws/network.js.map +1 -1
- package/dist/cjs/azure/cluster.d.ts.map +1 -1
- package/dist/cjs/azure/cluster.js +24 -18
- package/dist/cjs/azure/cluster.js.map +1 -1
- package/dist/cjs/cluster/index.d.ts +1 -1
- package/dist/cjs/cluster/index.d.ts.map +1 -1
- package/dist/cjs/cluster/interfaces.d.ts +24 -1
- package/dist/cjs/cluster/interfaces.d.ts.map +1 -1
- package/dist/cjs/cluster/scaling.d.ts +4 -0
- package/dist/cjs/cluster/scaling.d.ts.map +1 -0
- package/dist/cjs/cluster/scaling.js +12 -0
- package/dist/cjs/cluster/scaling.js.map +1 -0
- package/dist/cjs/factories/types.d.ts +17 -0
- package/dist/cjs/factories/types.d.ts.map +1 -1
- package/dist/cjs/factories/types.js.map +1 -1
- package/dist/cjs/index.d.ts +1 -1
- package/dist/cjs/index.d.ts.map +1 -1
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/network/interfaces.d.ts +2 -0
- package/dist/cjs/network/interfaces.d.ts.map +1 -1
- package/dist/cjs/rackspace/cluster.d.ts.map +1 -1
- package/dist/cjs/rackspace/cluster.js +17 -9
- package/dist/cjs/rackspace/cluster.js.map +1 -1
- package/dist/esm/aws/cluster.d.ts +25 -0
- package/dist/esm/aws/cluster.d.ts.map +1 -1
- package/dist/esm/aws/cluster.js +59 -14
- package/dist/esm/aws/cluster.js.map +1 -1
- package/dist/esm/aws/eks-autoscaling.d.ts +13 -0
- package/dist/esm/aws/eks-autoscaling.d.ts.map +1 -0
- package/dist/esm/aws/eks-autoscaling.js +648 -0
- package/dist/esm/aws/eks-autoscaling.js.map +1 -0
- package/dist/esm/aws/index.d.ts +1 -1
- package/dist/esm/aws/index.d.ts.map +1 -1
- package/dist/esm/aws/index.js.map +1 -1
- package/dist/esm/aws/network.d.ts.map +1 -1
- package/dist/esm/aws/network.js +1 -0
- package/dist/esm/aws/network.js.map +1 -1
- package/dist/esm/azure/cluster.d.ts.map +1 -1
- package/dist/esm/azure/cluster.js +24 -18
- package/dist/esm/azure/cluster.js.map +1 -1
- package/dist/esm/cluster/index.d.ts +1 -1
- package/dist/esm/cluster/index.d.ts.map +1 -1
- package/dist/esm/cluster/interfaces.d.ts +24 -1
- package/dist/esm/cluster/interfaces.d.ts.map +1 -1
- package/dist/esm/cluster/scaling.d.ts +4 -0
- package/dist/esm/cluster/scaling.d.ts.map +1 -0
- package/dist/esm/cluster/scaling.js +12 -0
- package/dist/esm/cluster/scaling.js.map +1 -0
- package/dist/esm/factories/types.d.ts +17 -0
- package/dist/esm/factories/types.d.ts.map +1 -1
- package/dist/esm/factories/types.js.map +1 -1
- package/dist/esm/index.d.ts +1 -1
- package/dist/esm/index.d.ts.map +1 -1
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/network/interfaces.d.ts +2 -0
- package/dist/esm/network/interfaces.d.ts.map +1 -1
- package/dist/esm/rackspace/cluster.d.ts.map +1 -1
- package/dist/esm/rackspace/cluster.js +17 -9
- package/dist/esm/rackspace/cluster.js.map +1 -1
- package/nimbus.spdx.json +11 -11
- package/package.json +1 -1
|
@@ -0,0 +1,648 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
35
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
+
exports.isKarpenterPool = isKarpenterPool;
|
|
37
|
+
exports.isManagedPool = isManagedPool;
|
|
38
|
+
exports.validateEksNodePoolScaling = validateEksNodePoolScaling;
|
|
39
|
+
exports.createEksAutoscaling = createEksAutoscaling;
|
|
40
|
+
const aws = __importStar(require("@pulumi/aws"));
|
|
41
|
+
const k8s = __importStar(require("@pulumi/kubernetes"));
|
|
42
|
+
const pulumi = __importStar(require("@pulumi/pulumi"));
|
|
43
|
+
const STABLE_LABEL = "nimbus.dev/node-pool";
|
|
44
|
+
const KARPENTER_VERSION = "1.15.3";
|
|
45
|
+
function isKarpenterPool(pool) {
|
|
46
|
+
return pool.scaling?.mode === "karpenter";
|
|
47
|
+
}
|
|
48
|
+
function isManagedPool(pool) {
|
|
49
|
+
return pool.scaling?.mode !== "karpenter";
|
|
50
|
+
}
|
|
51
|
+
/** Reject unsafe or ambiguous autoscaler ownership before registering AWS resources. */
|
|
52
|
+
function validateEksNodePoolScaling(config, options) {
|
|
53
|
+
const names = new Set();
|
|
54
|
+
const controllersEnabled = config.nodePools.some((pool) => pool.scaling?.mode === "cluster-autoscaler" || isKarpenterPool(pool));
|
|
55
|
+
for (const pool of config.nodePools) {
|
|
56
|
+
if (isKarpenterPool(pool) && !/^[a-z0-9]([a-z0-9-]*[a-z0-9])?$/.test(pool.name)) {
|
|
57
|
+
throw new Error(`Karpenter node pool name "${pool.name}" must use lowercase letters, numbers, and hyphens.`);
|
|
58
|
+
}
|
|
59
|
+
if (names.has(pool.name))
|
|
60
|
+
throw new Error(`Duplicate node pool name "${pool.name}".`);
|
|
61
|
+
names.add(pool.name);
|
|
62
|
+
if (controllersEnabled && pool.labels?.[STABLE_LABEL] !== undefined) {
|
|
63
|
+
throw new Error(`The label "${STABLE_LABEL}" is reserved by Nimbus.`);
|
|
64
|
+
}
|
|
65
|
+
if (isKarpenterPool(pool) && pool.scaling.instanceTypes.length === 0) {
|
|
66
|
+
throw new Error(`Karpenter node pool "${pool.name}" must specify at least one instance type.`);
|
|
67
|
+
}
|
|
68
|
+
if (isManagedPool(pool) && (pool.minNodes < 0 || pool.maxNodes < pool.minNodes)) {
|
|
69
|
+
throw new Error(`Node pool "${pool.name}" must have nonnegative minNodes and maxNodes >= minNodes.`);
|
|
70
|
+
}
|
|
71
|
+
if (isManagedPool(pool) && pool.scaling?.mode === "fixed") {
|
|
72
|
+
const count = pool.desiredNodes ?? pool.minNodes;
|
|
73
|
+
if (count < 0 || count > pool.maxNodes) {
|
|
74
|
+
throw new Error(`Fixed node pool "${pool.name}" desiredNodes must be between 0 and maxNodes.`);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
const caPools = config.nodePools.filter((pool) => pool.scaling?.mode === "cluster-autoscaler");
|
|
79
|
+
const hasKarpenter = config.nodePools.some(isKarpenterPool);
|
|
80
|
+
const hasController = caPools.length > 0 || hasKarpenter;
|
|
81
|
+
if (options.autoMode && hasController) {
|
|
82
|
+
throw new Error("Cluster Autoscaler and Karpenter cannot be combined with EKS Auto Mode.");
|
|
83
|
+
}
|
|
84
|
+
if (hasKarpenter && options.authenticationMode === "CONFIG_MAP") {
|
|
85
|
+
throw new Error("Karpenter requires EKS API authentication to register its EC2_LINUX node access entry.");
|
|
86
|
+
}
|
|
87
|
+
if (hasController) {
|
|
88
|
+
const stable = config.nodePools.find((pool) => pool.name === options.autoscalerControllerNodePoolName);
|
|
89
|
+
const stableGuaranteedNodes = stable && isManagedPool(stable)
|
|
90
|
+
? stable.scaling?.mode === "fixed"
|
|
91
|
+
? (stable.desiredNodes ?? stable.minNodes)
|
|
92
|
+
: stable.minNodes
|
|
93
|
+
: 0;
|
|
94
|
+
if (!stable || !isManagedPool(stable) || stableGuaranteedNodes < 1 || stable.spot === true) {
|
|
95
|
+
throw new Error("autoscalerControllerNodePoolName must name an on-demand managed node pool with at least one guaranteed node.");
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
/** Provision cluster-scoped autoscaler controllers and per-pool resources. */
|
|
100
|
+
function createEksAutoscaling(name, cluster, provider, network, pools, options, nodeGroupsByPool, podIdentityAgent, kubernetesVersion, tags) {
|
|
101
|
+
const resources = [];
|
|
102
|
+
const clusterRegion = cluster.arn.apply((arn) => arn.split(":")[3] ?? "us-east-1");
|
|
103
|
+
const caPools = pools.filter((pool) => pool.scaling?.mode === "cluster-autoscaler");
|
|
104
|
+
const karpenterPools = pools.filter(isKarpenterPool);
|
|
105
|
+
const stablePool = options.autoscalerControllerNodePoolName;
|
|
106
|
+
const stableSelector = { matchLabels: { [STABLE_LABEL]: stablePool ?? "" } };
|
|
107
|
+
const stablePoolConfig = pools.find((pool) => pool.name === stablePool);
|
|
108
|
+
const stablePoolGuaranteedNodes = stablePoolConfig && isManagedPool(stablePoolConfig)
|
|
109
|
+
? stablePoolConfig.scaling?.mode === "fixed"
|
|
110
|
+
? (stablePoolConfig.desiredNodes ?? stablePoolConfig.minNodes)
|
|
111
|
+
: stablePoolConfig.minNodes
|
|
112
|
+
: 1;
|
|
113
|
+
const controllerReplicas = stablePoolGuaranteedNodes >= 2 ? 2 : 1;
|
|
114
|
+
if (caPools.length) {
|
|
115
|
+
const issuer = cluster.identities.apply((identities) => {
|
|
116
|
+
const url = identities[0]?.oidcs[0]?.issuer;
|
|
117
|
+
if (!url)
|
|
118
|
+
throw new Error("EKS cluster does not expose an OIDC issuer for Cluster Autoscaler.");
|
|
119
|
+
return url;
|
|
120
|
+
});
|
|
121
|
+
const oidcProvider = options.clusterAutoscaler?.oidcProviderArn
|
|
122
|
+
? undefined
|
|
123
|
+
: new aws.iam.OpenIdConnectProvider(`${name}-cluster-autoscaler-oidc-provider`, { url: issuer, clientIdLists: ["sts.amazonaws.com"], tags }, { dependsOn: [cluster] });
|
|
124
|
+
const oidcProviderArn = options.clusterAutoscaler?.oidcProviderArn ?? oidcProvider?.arn;
|
|
125
|
+
if (!oidcProviderArn) {
|
|
126
|
+
throw new Error("Cluster Autoscaler requires an EKS OIDC provider ARN.");
|
|
127
|
+
}
|
|
128
|
+
const role = new aws.iam.Role(`${name}-cluster-autoscaler-role`, {
|
|
129
|
+
namePrefix: `${name}-cluster-autoscaler-`,
|
|
130
|
+
assumeRolePolicy: pulumi
|
|
131
|
+
.all([issuer, oidcProviderArn])
|
|
132
|
+
.apply(([issuerUrl, providerArn]) => {
|
|
133
|
+
const issuerKey = issuerUrl.replace(/^https?:\/\//, "");
|
|
134
|
+
return JSON.stringify({
|
|
135
|
+
Version: "2012-10-17",
|
|
136
|
+
Statement: [
|
|
137
|
+
{
|
|
138
|
+
Effect: "Allow",
|
|
139
|
+
Principal: { Federated: providerArn },
|
|
140
|
+
Action: "sts:AssumeRoleWithWebIdentity",
|
|
141
|
+
Condition: {
|
|
142
|
+
StringEquals: {
|
|
143
|
+
[`${issuerKey}:aud`]: "sts.amazonaws.com",
|
|
144
|
+
[`${issuerKey}:sub`]: "system:serviceaccount:kube-system:cluster-autoscaler",
|
|
145
|
+
},
|
|
146
|
+
},
|
|
147
|
+
},
|
|
148
|
+
],
|
|
149
|
+
});
|
|
150
|
+
}),
|
|
151
|
+
tags,
|
|
152
|
+
}, { dependsOn: [cluster, ...(oidcProvider ? [oidcProvider] : [])] });
|
|
153
|
+
const policy = new aws.iam.RolePolicy(`${name}-cluster-autoscaler-policy`, {
|
|
154
|
+
role: role.name,
|
|
155
|
+
policy: pulumi.all([cluster.name]).apply(([clusterName]) => JSON.stringify({
|
|
156
|
+
Version: "2012-10-17",
|
|
157
|
+
Statement: [
|
|
158
|
+
{
|
|
159
|
+
Effect: "Allow",
|
|
160
|
+
Action: [
|
|
161
|
+
"autoscaling:DescribeAutoScalingGroups",
|
|
162
|
+
"autoscaling:DescribeAutoScalingInstances",
|
|
163
|
+
"autoscaling:DescribeLaunchConfigurations",
|
|
164
|
+
"autoscaling:DescribeScalingActivities",
|
|
165
|
+
"autoscaling:DescribeTags",
|
|
166
|
+
"ec2:DescribeLaunchTemplateVersions",
|
|
167
|
+
"ec2:DescribeInstanceTypes",
|
|
168
|
+
"eks:DescribeNodegroup",
|
|
169
|
+
],
|
|
170
|
+
Resource: "*",
|
|
171
|
+
},
|
|
172
|
+
{
|
|
173
|
+
Effect: "Allow",
|
|
174
|
+
Action: [
|
|
175
|
+
"autoscaling:SetDesiredCapacity",
|
|
176
|
+
"autoscaling:TerminateInstanceInAutoScalingGroup",
|
|
177
|
+
],
|
|
178
|
+
Resource: "*",
|
|
179
|
+
Condition: {
|
|
180
|
+
StringEquals: {
|
|
181
|
+
[`autoscaling:ResourceTag/nimbus.dev/cluster-autoscaler/${clusterName}`]: "owned",
|
|
182
|
+
},
|
|
183
|
+
},
|
|
184
|
+
},
|
|
185
|
+
],
|
|
186
|
+
})),
|
|
187
|
+
});
|
|
188
|
+
resources.push(role, policy, ...(oidcProvider ? [oidcProvider] : []));
|
|
189
|
+
for (const pool of caPools) {
|
|
190
|
+
const group = nodeGroupsByPool.get(pool.name);
|
|
191
|
+
if (!group)
|
|
192
|
+
throw new Error(`Cluster Autoscaler pool "${pool.name}" has no managed node group.`);
|
|
193
|
+
resources.push(new aws.autoscaling.Tag(`${name}-ca-tag-${pool.name}`, {
|
|
194
|
+
autoscalingGroupName: group.resources.apply((items) => items[0]?.autoscalingGroups[0]?.name ?? ""),
|
|
195
|
+
tag: {
|
|
196
|
+
key: `nimbus.dev/cluster-autoscaler/${name}`,
|
|
197
|
+
value: "owned",
|
|
198
|
+
propagateAtLaunch: false,
|
|
199
|
+
},
|
|
200
|
+
}, { dependsOn: [group] }));
|
|
201
|
+
}
|
|
202
|
+
const caNodeGroups = caPools.flatMap((pool) => {
|
|
203
|
+
const group = nodeGroupsByPool.get(pool.name);
|
|
204
|
+
return group ? [group] : [];
|
|
205
|
+
});
|
|
206
|
+
const chart = new k8s.helm.v3.Chart(`${name}-cluster-autoscaler`, {
|
|
207
|
+
chart: "cluster-autoscaler",
|
|
208
|
+
version: options.clusterAutoscaler?.version ??
|
|
209
|
+
pulumi
|
|
210
|
+
.all([kubernetesVersion])
|
|
211
|
+
.apply(([clusterVersion]) => clusterAutoscalerChartVersion(clusterVersion ?? "")),
|
|
212
|
+
namespace: "kube-system",
|
|
213
|
+
fetchOpts: { repo: "https://kubernetes.github.io/autoscaler" },
|
|
214
|
+
values: {
|
|
215
|
+
autoDiscovery: {
|
|
216
|
+
clusterName: name,
|
|
217
|
+
tags: [`nimbus.dev/cluster-autoscaler/${name}`],
|
|
218
|
+
},
|
|
219
|
+
awsRegion: clusterRegion,
|
|
220
|
+
rbac: {
|
|
221
|
+
serviceAccount: {
|
|
222
|
+
create: true,
|
|
223
|
+
name: "cluster-autoscaler",
|
|
224
|
+
annotations: { "eks.amazonaws.com/role-arn": role.arn },
|
|
225
|
+
},
|
|
226
|
+
},
|
|
227
|
+
nodeSelector: stableSelector.matchLabels,
|
|
228
|
+
extraArgs: {
|
|
229
|
+
"skip-nodes-with-local-storage": "false",
|
|
230
|
+
"balance-similar-node-groups": "true",
|
|
231
|
+
},
|
|
232
|
+
},
|
|
233
|
+
}, {
|
|
234
|
+
provider,
|
|
235
|
+
dependsOn: [role, policy, ...(oidcProvider ? [oidcProvider] : []), ...caNodeGroups],
|
|
236
|
+
});
|
|
237
|
+
resources.push(chart);
|
|
238
|
+
}
|
|
239
|
+
if (karpenterPools.length) {
|
|
240
|
+
const namespace = options.karpenter?.namespace ?? "karpenter";
|
|
241
|
+
const usesSpot = karpenterPools.some((pool) => pool.scaling.capacityTypes?.includes("spot"));
|
|
242
|
+
const interruptionEnabled = usesSpot && options.karpenter?.interruptionHandling !== false;
|
|
243
|
+
const interruptionQueue = interruptionEnabled
|
|
244
|
+
? new aws.sqs.Queue(`${name}-karpenter-interruption`, {
|
|
245
|
+
messageRetentionSeconds: 300,
|
|
246
|
+
sqsManagedSseEnabled: true,
|
|
247
|
+
tags,
|
|
248
|
+
})
|
|
249
|
+
: undefined;
|
|
250
|
+
if (interruptionQueue) {
|
|
251
|
+
const patterns = [
|
|
252
|
+
{ source: ["aws.ec2"], "detail-type": ["EC2 Spot Instance Interruption Warning"] },
|
|
253
|
+
{ source: ["aws.ec2"], "detail-type": ["EC2 Instance Rebalance Recommendation"] },
|
|
254
|
+
{
|
|
255
|
+
source: ["aws.ec2"],
|
|
256
|
+
"detail-type": ["EC2 Instance State-change Notification"],
|
|
257
|
+
},
|
|
258
|
+
{ source: ["aws.health"], "detail-type": ["AWS Health Event"] },
|
|
259
|
+
{
|
|
260
|
+
source: ["aws.ec2"],
|
|
261
|
+
"detail-type": ["EC2 Capacity Reservation Instance Interruption Warning"],
|
|
262
|
+
},
|
|
263
|
+
];
|
|
264
|
+
const rules = patterns.map((pattern, index) => new aws.cloudwatch.EventRule(`${name}-karpenter-interruption-${index}`, {
|
|
265
|
+
eventPattern: JSON.stringify(pattern),
|
|
266
|
+
tags,
|
|
267
|
+
}));
|
|
268
|
+
const accountId = cluster.arn.apply((arn) => arn.split(":")[4] ?? "*");
|
|
269
|
+
const queuePolicy = new aws.sqs.QueuePolicy(`${name}-karpenter-interruption-policy`, {
|
|
270
|
+
queueUrl: interruptionQueue.url,
|
|
271
|
+
policy: pulumi.all([interruptionQueue.arn, accountId]).apply(([queueArn, account]) => JSON.stringify({
|
|
272
|
+
Version: "2012-10-17",
|
|
273
|
+
Statement: [
|
|
274
|
+
{
|
|
275
|
+
Effect: "Allow",
|
|
276
|
+
Principal: { Service: "events.amazonaws.com" },
|
|
277
|
+
Action: "sqs:SendMessage",
|
|
278
|
+
Resource: queueArn,
|
|
279
|
+
Condition: {
|
|
280
|
+
ArnLike: {
|
|
281
|
+
"aws:SourceArn": `arn:aws:events:*:${account}:rule/${name}-karpenter-interruption-*`,
|
|
282
|
+
},
|
|
283
|
+
},
|
|
284
|
+
},
|
|
285
|
+
],
|
|
286
|
+
})),
|
|
287
|
+
});
|
|
288
|
+
const targets = rules.map((rule, index) => new aws.cloudwatch.EventTarget(`${name}-karpenter-interruption-target-${index}`, {
|
|
289
|
+
rule: rule.name,
|
|
290
|
+
arn: interruptionQueue.arn,
|
|
291
|
+
}, { dependsOn: [queuePolicy] }));
|
|
292
|
+
resources.push(interruptionQueue, queuePolicy, ...rules, ...targets);
|
|
293
|
+
}
|
|
294
|
+
const nodeRole = new aws.iam.Role(`${name}-karpenter-node-role`, {
|
|
295
|
+
namePrefix: `${name}-karpenter-node-`,
|
|
296
|
+
assumeRolePolicy: JSON.stringify({
|
|
297
|
+
Version: "2012-10-17",
|
|
298
|
+
Statement: [
|
|
299
|
+
{
|
|
300
|
+
Effect: "Allow",
|
|
301
|
+
Principal: { Service: "ec2.amazonaws.com" },
|
|
302
|
+
Action: "sts:AssumeRole",
|
|
303
|
+
},
|
|
304
|
+
],
|
|
305
|
+
}),
|
|
306
|
+
tags,
|
|
307
|
+
});
|
|
308
|
+
const role = createPodRole(`${name}-karpenter-controller`, tags);
|
|
309
|
+
const policy = new aws.iam.RolePolicy(`${name}-karpenter-controller-policy`, {
|
|
310
|
+
role: role.name,
|
|
311
|
+
policy: pulumi
|
|
312
|
+
.all([cluster.arn, nodeRole.arn, interruptionQueue?.arn ?? ""])
|
|
313
|
+
.apply(([clusterArn, workerRoleArn, queueArn]) => buildKarpenterControllerPolicy(clusterArn, workerRoleArn, queueArn, name, interruptionEnabled)),
|
|
314
|
+
});
|
|
315
|
+
const association = new aws.eks.PodIdentityAssociation(`${name}-karpenter-identity`, {
|
|
316
|
+
clusterName: cluster.name,
|
|
317
|
+
namespace,
|
|
318
|
+
serviceAccount: "karpenter",
|
|
319
|
+
roleArn: role.arn,
|
|
320
|
+
}, { dependsOn: [cluster, policy, ...(podIdentityAgent ? [podIdentityAgent] : [])] });
|
|
321
|
+
const nodeAttachments = [
|
|
322
|
+
"arn:aws:iam::aws:policy/AmazonEKSWorkerNodePolicy",
|
|
323
|
+
"arn:aws:iam::aws:policy/AmazonEKS_CNI_Policy",
|
|
324
|
+
"arn:aws:iam::aws:policy/AmazonEC2ContainerRegistryPullOnly",
|
|
325
|
+
].map((policyArn) => new aws.iam.RolePolicyAttachment(`${name}-karpenter-node-${policyArn.split("/").pop()}`, {
|
|
326
|
+
role: nodeRole.name,
|
|
327
|
+
policyArn,
|
|
328
|
+
}));
|
|
329
|
+
const access = new aws.eks.AccessEntry(`${name}-karpenter-node-access`, {
|
|
330
|
+
clusterName: cluster.name,
|
|
331
|
+
principalArn: nodeRole.arn,
|
|
332
|
+
type: "EC2_LINUX",
|
|
333
|
+
tags,
|
|
334
|
+
}, { dependsOn: [cluster, nodeRole, ...nodeAttachments] });
|
|
335
|
+
const karpenterRelease = new k8s.helm.v3.Release(`${name}-karpenter`, {
|
|
336
|
+
chart: "oci://public.ecr.aws/karpenter/karpenter",
|
|
337
|
+
version: options.karpenter?.version ?? KARPENTER_VERSION,
|
|
338
|
+
namespace,
|
|
339
|
+
createNamespace: true,
|
|
340
|
+
values: {
|
|
341
|
+
settings: {
|
|
342
|
+
clusterName: name,
|
|
343
|
+
clusterEndpoint: cluster.endpoint,
|
|
344
|
+
interruptionQueue: interruptionQueue?.name,
|
|
345
|
+
eksControlPlane: true,
|
|
346
|
+
},
|
|
347
|
+
serviceAccount: { create: true, name: "karpenter" },
|
|
348
|
+
nodeSelector: stableSelector.matchLabels,
|
|
349
|
+
controller: {
|
|
350
|
+
env: [{ name: "AWS_REGION", value: clusterRegion }],
|
|
351
|
+
},
|
|
352
|
+
replicas: controllerReplicas,
|
|
353
|
+
},
|
|
354
|
+
}, {
|
|
355
|
+
provider,
|
|
356
|
+
dependsOn: [
|
|
357
|
+
association,
|
|
358
|
+
access,
|
|
359
|
+
...nodeAttachments,
|
|
360
|
+
...(podIdentityAgent ? [podIdentityAgent] : []),
|
|
361
|
+
...nodeGroupsByPool.values(),
|
|
362
|
+
],
|
|
363
|
+
});
|
|
364
|
+
resources.push(role, policy, association, nodeRole, ...nodeAttachments, access, karpenterRelease);
|
|
365
|
+
const nodeClass = new k8s.apiextensions.CustomResource(`${name}-karpenter-nodeclass`, {
|
|
366
|
+
apiVersion: "karpenter.k8s.aws/v1",
|
|
367
|
+
kind: "EC2NodeClass",
|
|
368
|
+
metadata: { name: "nimbus-default" },
|
|
369
|
+
spec: {
|
|
370
|
+
role: nodeRole.name,
|
|
371
|
+
amiSelectorTerms: [{ alias: "al2023@latest" }],
|
|
372
|
+
subnetSelectorTerms: pulumi
|
|
373
|
+
.all([
|
|
374
|
+
network.privateSubnetIds,
|
|
375
|
+
network.privateSubnetAvailabilityZones ?? pulumi.output([]),
|
|
376
|
+
])
|
|
377
|
+
.apply(([ids, zones]) => {
|
|
378
|
+
const requested = new Set(karpenterPools.flatMap((pool) => pool.scaling.availabilityZones ?? []));
|
|
379
|
+
if (requested.size && !network.privateSubnetAvailabilityZones) {
|
|
380
|
+
throw new Error("Karpenter availabilityZones require a Nimbus AWS network with private-subnet AZ metadata.");
|
|
381
|
+
}
|
|
382
|
+
const missing = [...requested].filter((zone) => !zones.includes(zone));
|
|
383
|
+
if (missing.length)
|
|
384
|
+
throw new Error(`Karpenter availability zones are outside Nimbus private subnets: ${missing.join(", ")}.`);
|
|
385
|
+
// A NodeClass is shared across pools. Keep every private subnet
|
|
386
|
+
// available here; each NodePool's topology requirement applies
|
|
387
|
+
// its own optional AZ restriction.
|
|
388
|
+
return ids.map((id) => ({ id }));
|
|
389
|
+
}),
|
|
390
|
+
securityGroupSelectorTerms: [
|
|
391
|
+
{ id: cluster.vpcConfig.apply((v) => v.clusterSecurityGroupId) },
|
|
392
|
+
],
|
|
393
|
+
},
|
|
394
|
+
}, { provider, dependsOn: [karpenterRelease, access, network.nativeResource] });
|
|
395
|
+
resources.push(nodeClass);
|
|
396
|
+
for (const pool of karpenterPools) {
|
|
397
|
+
resources.push(new k8s.apiextensions.CustomResource(`${name}-karpenter-pool-${pool.name}`, {
|
|
398
|
+
apiVersion: "karpenter.sh/v1",
|
|
399
|
+
kind: "NodePool",
|
|
400
|
+
metadata: { name: pool.name },
|
|
401
|
+
spec: {
|
|
402
|
+
template: {
|
|
403
|
+
metadata: { labels: { ...(pool.labels ?? {}), [STABLE_LABEL]: pool.name } },
|
|
404
|
+
spec: {
|
|
405
|
+
nodeClassRef: {
|
|
406
|
+
group: "karpenter.k8s.aws",
|
|
407
|
+
kind: "EC2NodeClass",
|
|
408
|
+
name: "nimbus-default",
|
|
409
|
+
},
|
|
410
|
+
taints: pool.taints?.map((taint) => ({
|
|
411
|
+
...taint,
|
|
412
|
+
effect: taint.effect === "NoExecute" ? "NoExecute" : taint.effect,
|
|
413
|
+
})),
|
|
414
|
+
requirements: [
|
|
415
|
+
{
|
|
416
|
+
key: "node.kubernetes.io/instance-type",
|
|
417
|
+
operator: "In",
|
|
418
|
+
values: [...pool.scaling.instanceTypes],
|
|
419
|
+
},
|
|
420
|
+
{
|
|
421
|
+
key: "karpenter.sh/capacity-type",
|
|
422
|
+
operator: "In",
|
|
423
|
+
values: [...(pool.scaling.capacityTypes ?? ["on-demand"])],
|
|
424
|
+
},
|
|
425
|
+
...(pool.scaling.availabilityZones
|
|
426
|
+
? [
|
|
427
|
+
{
|
|
428
|
+
key: "topology.kubernetes.io/zone",
|
|
429
|
+
operator: "In",
|
|
430
|
+
values: [...pool.scaling.availabilityZones],
|
|
431
|
+
},
|
|
432
|
+
]
|
|
433
|
+
: []),
|
|
434
|
+
],
|
|
435
|
+
},
|
|
436
|
+
},
|
|
437
|
+
limits: pool.scaling.limits,
|
|
438
|
+
disruption: { consolidationPolicy: "WhenEmpty", consolidateAfter: "1m" },
|
|
439
|
+
},
|
|
440
|
+
}, { provider, dependsOn: [nodeClass] }));
|
|
441
|
+
}
|
|
442
|
+
}
|
|
443
|
+
return resources;
|
|
444
|
+
}
|
|
445
|
+
function clusterAutoscalerChartVersion(kubernetesVersion) {
|
|
446
|
+
const minor = /^1\.(\d+)(?:\.|$)/.exec(kubernetesVersion)?.[1];
|
|
447
|
+
const versions = {
|
|
448
|
+
"29": "9.35.0",
|
|
449
|
+
"30": "9.37.0",
|
|
450
|
+
"31": "9.38.0",
|
|
451
|
+
"32": "9.46.6",
|
|
452
|
+
"33": "9.47.0",
|
|
453
|
+
"34": "9.51.0",
|
|
454
|
+
"35": "9.54.0",
|
|
455
|
+
"36": "9.60.0",
|
|
456
|
+
};
|
|
457
|
+
if (!minor || !versions[minor]) {
|
|
458
|
+
throw new Error(`No pinned Cluster Autoscaler chart mapping is available for Kubernetes ${kubernetesVersion}; set clusterAutoscaler.version explicitly.`);
|
|
459
|
+
}
|
|
460
|
+
return versions[minor];
|
|
461
|
+
}
|
|
462
|
+
function createPodRole(name, tags) {
|
|
463
|
+
return new aws.iam.Role(`${name}-role`, {
|
|
464
|
+
namePrefix: `${name}-`,
|
|
465
|
+
assumeRolePolicy: JSON.stringify({
|
|
466
|
+
Version: "2012-10-17",
|
|
467
|
+
Statement: [
|
|
468
|
+
{
|
|
469
|
+
Effect: "Allow",
|
|
470
|
+
Principal: { Service: "pods.eks.amazonaws.com" },
|
|
471
|
+
Action: ["sts:AssumeRole", "sts:TagSession"],
|
|
472
|
+
},
|
|
473
|
+
],
|
|
474
|
+
}),
|
|
475
|
+
tags,
|
|
476
|
+
});
|
|
477
|
+
}
|
|
478
|
+
function buildKarpenterControllerPolicy(clusterArn, workerRoleArn, queueArn, clusterName, interruptionEnabled) {
|
|
479
|
+
const [, partition = "aws", , region = "us-east-1", account = "*"] = clusterArn.split(":");
|
|
480
|
+
const ec2Arn = `arn:${partition}:ec2:${region}:`;
|
|
481
|
+
const instanceProfileArn = `arn:${partition}:iam::${account}:instance-profile/*`;
|
|
482
|
+
const nodePoolTag = "karpenter.sh/nodepool";
|
|
483
|
+
const resourceTags = {
|
|
484
|
+
StringEquals: { [`aws:ResourceTag/kubernetes.io/cluster/${clusterName}`]: "owned" },
|
|
485
|
+
StringLike: { [`aws:ResourceTag/${nodePoolTag}`]: "*" },
|
|
486
|
+
};
|
|
487
|
+
const requestTags = {
|
|
488
|
+
StringEquals: {
|
|
489
|
+
[`aws:RequestTag/kubernetes.io/cluster/${clusterName}`]: "owned",
|
|
490
|
+
"aws:RequestTag/eks:eks-cluster-name": clusterName,
|
|
491
|
+
},
|
|
492
|
+
StringLike: { [`aws:RequestTag/${nodePoolTag}`]: "*" },
|
|
493
|
+
};
|
|
494
|
+
const profileRequestTags = {
|
|
495
|
+
StringEquals: {
|
|
496
|
+
[`aws:RequestTag/kubernetes.io/cluster/${clusterName}`]: "owned",
|
|
497
|
+
"aws:RequestTag/eks:eks-cluster-name": clusterName,
|
|
498
|
+
"aws:RequestTag/topology.kubernetes.io/region": region,
|
|
499
|
+
},
|
|
500
|
+
StringLike: { "aws:RequestTag/karpenter.k8s.aws/ec2nodeclass": "*" },
|
|
501
|
+
};
|
|
502
|
+
const launchResources = [
|
|
503
|
+
`${ec2Arn}*:fleet/*`,
|
|
504
|
+
`${ec2Arn}*:instance/*`,
|
|
505
|
+
`${ec2Arn}*:volume/*`,
|
|
506
|
+
`${ec2Arn}*:network-interface/*`,
|
|
507
|
+
`${ec2Arn}*:launch-template/*`,
|
|
508
|
+
`${ec2Arn}*:spot-instances-request/*`,
|
|
509
|
+
];
|
|
510
|
+
const statements = [
|
|
511
|
+
{
|
|
512
|
+
Effect: "Allow",
|
|
513
|
+
Action: ["ec2:RunInstances", "ec2:CreateFleet"],
|
|
514
|
+
Resource: [
|
|
515
|
+
`arn:${partition}:ec2:${region}::image/*`,
|
|
516
|
+
`${ec2Arn}*:snapshot/*`,
|
|
517
|
+
`${ec2Arn}*:security-group/*`,
|
|
518
|
+
`${ec2Arn}*:subnet/*`,
|
|
519
|
+
`${ec2Arn}*:capacity-reservation/*`,
|
|
520
|
+
`${ec2Arn}*:placement-group/*`,
|
|
521
|
+
],
|
|
522
|
+
},
|
|
523
|
+
{
|
|
524
|
+
Effect: "Allow",
|
|
525
|
+
Action: ["ec2:RunInstances", "ec2:CreateFleet"],
|
|
526
|
+
Resource: `${ec2Arn}*:launch-template/*`,
|
|
527
|
+
Condition: resourceTags,
|
|
528
|
+
},
|
|
529
|
+
{
|
|
530
|
+
Effect: "Allow",
|
|
531
|
+
Action: ["ec2:RunInstances", "ec2:CreateFleet", "ec2:CreateLaunchTemplate"],
|
|
532
|
+
Resource: launchResources,
|
|
533
|
+
Condition: requestTags,
|
|
534
|
+
},
|
|
535
|
+
{
|
|
536
|
+
Effect: "Allow",
|
|
537
|
+
Action: "ec2:CreateTags",
|
|
538
|
+
Resource: launchResources,
|
|
539
|
+
Condition: {
|
|
540
|
+
...requestTags,
|
|
541
|
+
StringEquals: {
|
|
542
|
+
...requestTags.StringEquals,
|
|
543
|
+
"ec2:CreateAction": ["RunInstances", "CreateFleet", "CreateLaunchTemplate"],
|
|
544
|
+
},
|
|
545
|
+
},
|
|
546
|
+
},
|
|
547
|
+
{
|
|
548
|
+
Effect: "Allow",
|
|
549
|
+
Action: "ec2:CreateTags",
|
|
550
|
+
Resource: `${ec2Arn}*:instance/*`,
|
|
551
|
+
Condition: {
|
|
552
|
+
...resourceTags,
|
|
553
|
+
StringEqualsIfExists: { "aws:RequestTag/eks:eks-cluster-name": clusterName },
|
|
554
|
+
"ForAllValues:StringEquals": {
|
|
555
|
+
"aws:TagKeys": ["eks:eks-cluster-name", "karpenter.sh/nodeclaim", "Name"],
|
|
556
|
+
},
|
|
557
|
+
},
|
|
558
|
+
},
|
|
559
|
+
{
|
|
560
|
+
Effect: "Allow",
|
|
561
|
+
Action: ["ec2:TerminateInstances", "ec2:RebootInstances", "ec2:DeleteLaunchTemplate"],
|
|
562
|
+
Resource: [`${ec2Arn}*:instance/*`, `${ec2Arn}*:launch-template/*`],
|
|
563
|
+
Condition: resourceTags,
|
|
564
|
+
},
|
|
565
|
+
{
|
|
566
|
+
Effect: "Allow",
|
|
567
|
+
Action: "iam:PassRole",
|
|
568
|
+
Resource: workerRoleArn,
|
|
569
|
+
Condition: {
|
|
570
|
+
StringEquals: { "iam:PassedToService": ["ec2.amazonaws.com", "ec2.amazonaws.com.cn"] },
|
|
571
|
+
},
|
|
572
|
+
},
|
|
573
|
+
{
|
|
574
|
+
Effect: "Allow",
|
|
575
|
+
Action: "iam:CreateInstanceProfile",
|
|
576
|
+
Resource: instanceProfileArn,
|
|
577
|
+
Condition: profileRequestTags,
|
|
578
|
+
},
|
|
579
|
+
{
|
|
580
|
+
Effect: "Allow",
|
|
581
|
+
Action: "iam:TagInstanceProfile",
|
|
582
|
+
Resource: instanceProfileArn,
|
|
583
|
+
Condition: {
|
|
584
|
+
StringEquals: {
|
|
585
|
+
[`aws:ResourceTag/kubernetes.io/cluster/${clusterName}`]: "owned",
|
|
586
|
+
"aws:ResourceTag/topology.kubernetes.io/region": region,
|
|
587
|
+
...profileRequestTags.StringEquals,
|
|
588
|
+
},
|
|
589
|
+
StringLike: {
|
|
590
|
+
"aws:ResourceTag/karpenter.k8s.aws/ec2nodeclass": "*",
|
|
591
|
+
...profileRequestTags.StringLike,
|
|
592
|
+
},
|
|
593
|
+
},
|
|
594
|
+
},
|
|
595
|
+
{
|
|
596
|
+
Effect: "Allow",
|
|
597
|
+
Action: [
|
|
598
|
+
"iam:AddRoleToInstanceProfile",
|
|
599
|
+
"iam:RemoveRoleFromInstanceProfile",
|
|
600
|
+
"iam:DeleteInstanceProfile",
|
|
601
|
+
],
|
|
602
|
+
Resource: instanceProfileArn,
|
|
603
|
+
Condition: {
|
|
604
|
+
StringEquals: {
|
|
605
|
+
[`aws:ResourceTag/kubernetes.io/cluster/${clusterName}`]: "owned",
|
|
606
|
+
"aws:ResourceTag/topology.kubernetes.io/region": region,
|
|
607
|
+
},
|
|
608
|
+
StringLike: { "aws:ResourceTag/karpenter.k8s.aws/ec2nodeclass": "*" },
|
|
609
|
+
},
|
|
610
|
+
},
|
|
611
|
+
{ Effect: "Allow", Action: "iam:GetInstanceProfile", Resource: instanceProfileArn },
|
|
612
|
+
{ Effect: "Allow", Action: "iam:ListInstanceProfiles", Resource: "*" },
|
|
613
|
+
{
|
|
614
|
+
Effect: "Allow",
|
|
615
|
+
Action: [
|
|
616
|
+
"ec2:DescribeCapacityReservations",
|
|
617
|
+
"ec2:DescribeImages",
|
|
618
|
+
"ec2:DescribeInstances",
|
|
619
|
+
"ec2:DescribeInstanceStatus",
|
|
620
|
+
"ec2:DescribeInstanceTypeOfferings",
|
|
621
|
+
"ec2:DescribeInstanceTypes",
|
|
622
|
+
"ec2:DescribeLaunchTemplates",
|
|
623
|
+
"ec2:DescribePlacementGroups",
|
|
624
|
+
"ec2:DescribeSecurityGroups",
|
|
625
|
+
"ec2:DescribeSpotPriceHistory",
|
|
626
|
+
"ec2:DescribeSubnets",
|
|
627
|
+
],
|
|
628
|
+
Resource: "*",
|
|
629
|
+
Condition: { StringEquals: { "aws:RequestedRegion": region } },
|
|
630
|
+
},
|
|
631
|
+
{
|
|
632
|
+
Effect: "Allow",
|
|
633
|
+
Action: "ssm:GetParameter",
|
|
634
|
+
Resource: `arn:${partition}:ssm:${region}::parameter/aws/service/*`,
|
|
635
|
+
},
|
|
636
|
+
{ Effect: "Allow", Action: "pricing:GetProducts", Resource: "*" },
|
|
637
|
+
{ Effect: "Allow", Action: "eks:DescribeCluster", Resource: clusterArn },
|
|
638
|
+
];
|
|
639
|
+
if (interruptionEnabled) {
|
|
640
|
+
statements.push({
|
|
641
|
+
Effect: "Allow",
|
|
642
|
+
Action: ["sqs:DeleteMessage", "sqs:GetQueueUrl", "sqs:ReceiveMessage"],
|
|
643
|
+
Resource: queueArn,
|
|
644
|
+
});
|
|
645
|
+
}
|
|
646
|
+
return JSON.stringify({ Version: "2012-10-17", Statement: statements });
|
|
647
|
+
}
|
|
648
|
+
//# sourceMappingURL=eks-autoscaling.js.map
|