-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
291 lines (274 loc) · 9.42 KB
/
Copy pathindex.ts
File metadata and controls
291 lines (274 loc) · 9.42 KB
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
/**
* Operator module — Kubernetes operator deployment and resource provisioning.
*
* Supports CloudNativePG (PostgreSQL), MariaDB Operator, and MinIO via Helm.
* Database operators expose createCluster() / createDatabase(); the MinIO
* operator exposes createBucket() instead.
*
* @module operator
*/
import * as k8s from "@pulumi/kubernetes";
import { assertNever } from "../types";
import { ensureNamespace } from "../utils/ensure-namespace";
import { createCnpgDatabase } from "./cnpg";
import { createMariadbDatabase } from "./mariadb";
import { createMinioOperator } from "./minio";
import { createNeo4jCluster } from "./neo4j";
import type {
IOperator,
IMinIOOperator,
IOperatorConfig,
IOperatorClusterConfig,
IClusterInstance,
OperatorType,
} from "./interfaces";
export type {
OperatorType,
EnvironmentOverrides,
IBackupDefaults,
IOperatorConfig,
IOperatorClusterConfig,
IOperatorDatabaseConfig,
IDatabaseInstance,
IDatabaseGrant,
IDatabaseRole,
IDatabaseRoleConfig,
ReclaimPolicy,
IClusterInstance,
IOperator,
IMinIOOperator,
IMinIOBucket,
IMinIOBucketConfig,
IMinIOIngressConfig,
} from "./interfaces";
export { OPERATOR_TYPES } from "./interfaces";
export { createMinioIngress } from "./minio";
export type { INeo4jClusterConfig } from "./neo4j";
const DATA_NAMESPACE = "data";
/** Helm chart metadata for each operator type. */
interface OperatorChartInfo {
readonly repo: string;
readonly chart: string;
readonly defaultNamespace: string;
}
/** Separate CRDs chart (installed before the operator). */
interface OperatorCrdsInfo {
readonly repo: string;
readonly chart: string;
}
const OPERATOR_CHARTS: Record<OperatorType, OperatorChartInfo & { crds?: OperatorCrdsInfo }> = {
"cloudnative-pg": {
repo: "https://cloudnative-pg.github.io/charts",
chart: "cloudnative-pg",
defaultNamespace: "cnpg-system",
},
"mariadb-operator": {
repo: "https://helm.mariadb.com/mariadb-operator",
chart: "mariadb-operator",
defaultNamespace: "mariadb-system",
crds: {
repo: "https://helm.mariadb.com/mariadb-operator",
chart: "mariadb-operator-crds",
},
},
minio: {
repo: "https://operator.min.io",
chart: "operator",
defaultNamespace: "minio-operator",
},
neo4j: {
// Neo4j has no separate operator — the Helm chart deploys the instance directly.
// The operator release is a no-op; createCluster() deploys via its own Helm release.
repo: "https://helm.neo4j.com/neo4j",
chart: "neo4j",
defaultNamespace: DATA_NAMESPACE,
},
};
/**
* Deploy an operator to a Kubernetes cluster.
*
* - `"cloudnative-pg"` / `"mariadb-operator"` → returns IOperator with createCluster()
* - `"minio"` → returns IMinIOOperator with createBucket()
*
* @example Database operator
* ```typescript
* const op = createOperator("cloudnative-pg", {
* cluster,
* backup: {
* target: backupTarget,
* schedule: "0 3 * * *",
* retentionDays: 7,
* pitr: true,
* },
* });
* const cluster = op.createCluster("app-db", { replicas: 2, storageGb: 20 });
* ```
*
* @example MinIO operator
* ```typescript
* const minio = createOperator("minio", { cluster }) as IMinIOOperator;
* const bucket = minio.createBucket("uploads", { namespaces: ["app"] });
* ```
*
* @param type - Operator type
* @param config - Operator configuration
* @returns Deployed IOperator or IMinIOOperator instance
*/
export function createOperator(type: "minio", config: IOperatorConfig): IMinIOOperator;
export function createOperator(
type: "cloudnative-pg" | "mariadb-operator" | "neo4j",
config: IOperatorConfig
): IOperator;
export function createOperator(
type: OperatorType,
config: IOperatorConfig
): IOperator | IMinIOOperator;
export function createOperator(
type: OperatorType,
config: IOperatorConfig
): IOperator | IMinIOOperator {
const chartInfo = OPERATOR_CHARTS[type];
const provider = config.cluster.provider;
const namespace = config.namespace ?? chartInfo.defaultNamespace;
// Ensure operator namespace exists
const ns = ensureNamespace(namespace, provider);
// Install CRDs first if the operator needs a separate CRDs chart
const operatorDeps: k8s.helm.v3.Release[] = [];
if (chartInfo.crds) {
const crdsRelease = new k8s.helm.v3.Release(
`${type}-crds`,
{
chart: chartInfo.crds.chart,
repositoryOpts: { repo: chartInfo.crds.repo },
namespace,
createNamespace: false,
values: {},
},
{ provider, dependsOn: [ns] }
);
operatorDeps.push(crdsRelease);
}
// Neo4j has no separate operator — the Helm chart deploys the instance
// directly. Skip the operator install; createCluster() deploys via its own
// Helm release with instance-specific values.
const skipOperatorInstall = type === "neo4j";
const helmValues = config.values ?? {};
// Deploy Helm release (operator chart)
const helmRelease = skipOperatorInstall
? (ns as unknown as k8s.helm.v3.Release) // Placeholder — Neo4j deploys in createCluster()
: new k8s.helm.v3.Release(
`${type}-operator`,
{
chart: chartInfo.chart,
repositoryOpts: { repo: chartInfo.repo },
version: config.version,
namespace,
createNamespace: false,
values: helmValues,
},
{ provider, dependsOn: [ns, ...operatorDeps] }
);
// CNPG's admission webhooks ship with timeouts tuned for a control plane
// co-located with the cluster: 10s mutating, 15s validating, both with
// failurePolicy: Fail. On a hosted control plane — Rackspace Spot, for
// instance, where the API server runs outside the cluster and every
// admission call has to tunnel back in through konnectivity — that round
// trip costs seconds rather than milliseconds.
//
// Measured on iad-1, ten consecutive mcluster.cnpg.io probes: 2.0s, 2.2s,
// 3.0s, 3.1s, 3.4s, 4.7s, 6.5s, 7.2s, 7.8s, 10.2s. One in ten already
// exceeded the 10s ceiling while the operator sat idle at 8 millicores, so
// it is the network path that is slow, not the operator. Concurrent
// admission — a Pulumi preview dry-running several resources at once —
// pushes more of them over, and with failurePolicy: Fail that aborts the
// run partway through an update.
//
// The chart exposes only webhook.{mutating,validating}.{create,failurePolicy},
// not timeoutSeconds, so this cannot be expressed through Helm values. Patch
// the field instead: `webhooks` is a listType=map keyed by `name`, so each
// entry merges by key and no other field of the configuration is touched.
// In particular failurePolicy stays Fail — nothing bypasses admission.
if (type === "cloudnative-pg" && !skipOperatorInstall) {
const webhookTimeoutSeconds = 30;
new k8s.admissionregistration.v1.MutatingWebhookConfigurationPatch(
`${type}-mutating-webhook-timeout`,
{
metadata: { name: "cnpg-mutating-webhook-configuration" },
webhooks: [
"mbackup.cnpg.io",
"mcluster.cnpg.io",
"mdatabase.cnpg.io",
"mscheduledbackup.cnpg.io",
].map((name) => ({ name, timeoutSeconds: webhookTimeoutSeconds })),
},
{ provider, dependsOn: [helmRelease], retainOnDelete: true }
);
new k8s.admissionregistration.v1.ValidatingWebhookConfigurationPatch(
`${type}-validating-webhook-timeout`,
{
metadata: { name: "cnpg-validating-webhook-configuration" },
webhooks: [
"vbackup.cnpg.io",
"vcluster.cnpg.io",
"vdatabase.cnpg.io",
"vpooler.cnpg.io",
"vscheduledbackup.cnpg.io",
].map((name) => ({ name, timeoutSeconds: webhookTimeoutSeconds })),
},
{ provider, dependsOn: [helmRelease], retainOnDelete: true }
);
}
// MinIO returns a different operator shape (createBucket instead of createCluster)
if (type === "minio") {
return createMinioOperator(config, helmRelease);
}
return {
name: type,
type,
helmRelease,
createCluster(name: string, clusterConfig?: IOperatorClusterConfig) {
let result: IClusterInstance | Record<string, IClusterInstance>;
const tierMap = config.cluster.storageTiers;
switch (type) {
case "cloudnative-pg":
result = createCnpgDatabase(
name,
clusterConfig,
config.backup,
provider,
helmRelease,
tierMap
);
break;
case "mariadb-operator":
result = createMariadbDatabase(
name,
clusterConfig,
config.backup,
provider,
helmRelease,
tierMap
);
break;
case "neo4j":
// Neo4j Helm chart deploys the instance directly (no CRD operator).
// The helmRelease IS the Neo4j deployment; createCluster wraps it.
result = createNeo4jCluster(
name,
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- neo4j config type narrowing handled at runtime
clusterConfig as any,
config.backup,
provider,
helmRelease,
tierMap
);
break;
default:
return assertNever(type);
}
// Runtime: environments → Record, otherwise → IClusterInstance.
// Overload signatures on IOperator narrow the type for callers.
return result as IClusterInstance & Record<string, IClusterInstance>;
},
};
}