@mrsimpson/homelab-core-components 0.2.1 → 0.2.2

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/index.d.ts CHANGED
@@ -11,4 +11,6 @@ export { createGhcrImagePullSecret, createDockerHubImagePullSecret, } from "./re
11
11
  export type { ExposedWebAppArgs, StorageConfig, CloudflareConfig, TLSConfig, GatewayApiConfig, ExternalSecretsConfig, } from "./ExposedWebApp";
12
12
  export type { HomelabContextConfig } from "./homelab-context";
13
13
  export type { CreateImagePullSecretArgs } from "./registry-secrets";
14
+ export { PostgresInstance } from "./postgres/PostgresInstance";
15
+ export type { PostgresInstanceArgs } from "./postgres/PostgresInstance";
14
16
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EAAE,aAAa,EAAE,QAAQ,EAAE,MAAM,iBAAiB,CAAC;AAC1D,OAAO,EAAE,cAAc,EAAE,MAAM,mBAAmB,CAAC;AACnD,OAAO,EAAE,6BAA6B,EAAE,MAAM,iCAAiC,CAAC;AAChF,YAAY,EAAE,8BAA8B,EAAE,MAAM,iCAAiC,CAAC;AACtF,OAAO,EACL,yBAAyB,EACzB,8BAA8B,GAC/B,MAAM,oBAAoB,CAAC;AAC5B,YAAY,EACV,iBAAiB,EACjB,aAAa,EACb,gBAAgB,EAChB,SAAS,EACT,gBAAgB,EAChB,qBAAqB,GACtB,MAAM,iBAAiB,CAAC;AACzB,YAAY,EAAE,oBAAoB,EAAE,MAAM,mBAAmB,CAAC;AAC9D,YAAY,EAAE,yBAAyB,EAAE,MAAM,oBAAoB,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EAAE,aAAa,EAAE,QAAQ,EAAE,MAAM,iBAAiB,CAAC;AAC1D,OAAO,EAAE,cAAc,EAAE,MAAM,mBAAmB,CAAC;AACnD,OAAO,EAAE,6BAA6B,EAAE,MAAM,iCAAiC,CAAC;AAChF,YAAY,EAAE,8BAA8B,EAAE,MAAM,iCAAiC,CAAC;AACtF,OAAO,EACL,yBAAyB,EACzB,8BAA8B,GAC/B,MAAM,oBAAoB,CAAC;AAC5B,YAAY,EACV,iBAAiB,EACjB,aAAa,EACb,gBAAgB,EAChB,SAAS,EACT,gBAAgB,EAChB,qBAAqB,GACtB,MAAM,iBAAiB,CAAC;AACzB,YAAY,EAAE,oBAAoB,EAAE,MAAM,mBAAmB,CAAC;AAC9D,YAAY,EAAE,yBAAyB,EAAE,MAAM,oBAAoB,CAAC;AACpE,OAAO,EAAE,gBAAgB,EAAE,MAAM,6BAA6B,CAAC;AAC/D,YAAY,EAAE,oBAAoB,EAAE,MAAM,6BAA6B,CAAC"}
package/dist/index.js CHANGED
@@ -5,7 +5,7 @@
5
5
  * Reusable Pulumi components for homelab infrastructure
6
6
  */
7
7
  Object.defineProperty(exports, "__esModule", { value: true });
8
- exports.createDockerHubImagePullSecret = exports.createGhcrImagePullSecret = exports.createHomelabContextFromStack = exports.HomelabContext = exports.AuthType = exports.ExposedWebApp = void 0;
8
+ exports.PostgresInstance = exports.createDockerHubImagePullSecret = exports.createGhcrImagePullSecret = exports.createHomelabContextFromStack = exports.HomelabContext = exports.AuthType = exports.ExposedWebApp = void 0;
9
9
  var ExposedWebApp_1 = require("./ExposedWebApp");
10
10
  Object.defineProperty(exports, "ExposedWebApp", { enumerable: true, get: function () { return ExposedWebApp_1.ExposedWebApp; } });
11
11
  Object.defineProperty(exports, "AuthType", { enumerable: true, get: function () { return ExposedWebApp_1.AuthType; } });
@@ -16,4 +16,6 @@ Object.defineProperty(exports, "createHomelabContextFromStack", { enumerable: tr
16
16
  var registry_secrets_1 = require("./registry-secrets");
17
17
  Object.defineProperty(exports, "createGhcrImagePullSecret", { enumerable: true, get: function () { return registry_secrets_1.createGhcrImagePullSecret; } });
18
18
  Object.defineProperty(exports, "createDockerHubImagePullSecret", { enumerable: true, get: function () { return registry_secrets_1.createDockerHubImagePullSecret; } });
19
+ var PostgresInstance_1 = require("./postgres/PostgresInstance");
20
+ Object.defineProperty(exports, "PostgresInstance", { enumerable: true, get: function () { return PostgresInstance_1.PostgresInstance; } });
19
21
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";AAAA;;;;GAIG;;;AAEH,iDAA0D;AAAjD,8GAAA,aAAa,OAAA;AAAE,yGAAA,QAAQ,OAAA;AAChC,qDAAmD;AAA1C,iHAAA,cAAc,OAAA;AACvB,iFAAgF;AAAvE,8IAAA,6BAA6B,OAAA;AAEtC,uDAG4B;AAF1B,6HAAA,yBAAyB,OAAA;AACzB,kIAAA,8BAA8B,OAAA"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";AAAA;;;;GAIG;;;AAEH,iDAA0D;AAAjD,8GAAA,aAAa,OAAA;AAAE,yGAAA,QAAQ,OAAA;AAChC,qDAAmD;AAA1C,iHAAA,cAAc,OAAA;AACvB,iFAAgF;AAAvE,8IAAA,6BAA6B,OAAA;AAEtC,uDAG4B;AAF1B,6HAAA,yBAAyB,OAAA;AACzB,kIAAA,8BAA8B,OAAA;AAYhC,gEAA+D;AAAtD,oHAAA,gBAAgB,OAAA"}
@@ -0,0 +1,72 @@
1
+ import * as k8s from "@pulumi/kubernetes";
2
+ import * as pulumi from "@pulumi/pulumi";
3
+ export interface PostgresInstanceArgs {
4
+ /** Pre-created Kubernetes namespace to deploy into */
5
+ namespace: k8s.core.v1.Namespace;
6
+ /** Name of the database to create */
7
+ databaseName: pulumi.Input<string>;
8
+ /**
9
+ * Container image for Postgres.
10
+ * Must start with the PG major version (e.g. `18`) — CNPG's webhook rejects `latest`.
11
+ * @default "postgres:18"
12
+ */
13
+ image?: pulumi.Input<string>;
14
+ /** @default "app" — CNPG convention */
15
+ username?: pulumi.Input<string>;
16
+ /** @default "5Gi" */
17
+ storageSize?: pulumi.Input<string>;
18
+ /**
19
+ * @default "longhorn-persistent" — daily R2 backup, 7-day retention
20
+ */
21
+ storageClass?: pulumi.Input<string>;
22
+ /**
23
+ * Primary + hot-standby count. Set to 3 for HA — CNPG handles promotion automatically.
24
+ * @default 1
25
+ */
26
+ instances?: number;
27
+ /**
28
+ * UID/GID for the postgres OS user. Official postgres image uses 26; paradedb uses 999.
29
+ * **Immutable** — changing requires cluster deletion and recreation.
30
+ * @default 26
31
+ */
32
+ postgresUID?: number;
33
+ postgresGID?: number;
34
+ /**
35
+ * Libraries for `shared_preload_libraries`. Required for extensions that cannot load
36
+ * on demand (e.g. `pg_search`, `pg_cron`).
37
+ * @default []
38
+ */
39
+ sharedPreloadLibraries?: string[];
40
+ /**
41
+ * SQL run as superuser in the app database at bootstrap — before the app user connects.
42
+ * Use to pre-install superuser-only extensions so app migrations are no-ops.
43
+ * Only runs once at cluster creation; not re-applied on updates.
44
+ * @default []
45
+ */
46
+ postInitApplicationSQL?: string[];
47
+ /**
48
+ * CNPG operator Helm Release. Pass `baseInfra.cnpg.operator` to ensure the webhook
49
+ * is ready before the `Cluster` CRD is applied.
50
+ */
51
+ cnpgOperator?: pulumi.Resource;
52
+ }
53
+ export declare class PostgresInstance extends pulumi.ComponentResource {
54
+ /** Read-write (primary) service name: `<name>-postgres-rw` */
55
+ readonly serviceName: pulumi.Output<string>;
56
+ /** In-cluster hostname: `<service>.<namespace>.svc.cluster.local` */
57
+ readonly host: pulumi.Output<string>;
58
+ readonly port: pulumi.Output<number>;
59
+ /** Full connection string — treat as secret. */
60
+ readonly connectionString: pulumi.Output<string>;
61
+ /**
62
+ * Adapter Secret name containing `POSTGRES_DB`, `POSTGRES_USER`,
63
+ * `POSTGRES_PASSWORD`, `DATABASE_URL`.
64
+ *
65
+ * ```typescript
66
+ * envFrom: [{ secretRef: { name: db.credentialsSecretName } }]
67
+ * ```
68
+ */
69
+ readonly credentialsSecretName: pulumi.Output<string>;
70
+ constructor(name: string, args: PostgresInstanceArgs, opts?: pulumi.ComponentResourceOptions);
71
+ }
72
+ //# sourceMappingURL=PostgresInstance.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"PostgresInstance.d.ts","sourceRoot":"","sources":["../../src/postgres/PostgresInstance.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,GAAG,MAAM,oBAAoB,CAAC;AAC1C,OAAO,KAAK,MAAM,MAAM,gBAAgB,CAAC;AAuBzC,MAAM,WAAW,oBAAoB;IACnC,sDAAsD;IACtD,SAAS,EAAE,GAAG,CAAC,IAAI,CAAC,EAAE,CAAC,SAAS,CAAC;IAEjC,qCAAqC;IACrC,YAAY,EAAE,MAAM,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC;IAEnC;;;;OAIG;IACH,KAAK,CAAC,EAAE,MAAM,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC;IAE7B,uCAAuC;IACvC,QAAQ,CAAC,EAAE,MAAM,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC;IAEhC,qBAAqB;IACrB,WAAW,CAAC,EAAE,MAAM,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC;IAEnC;;OAEG;IACH,YAAY,CAAC,EAAE,MAAM,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC;IAEpC;;;OAGG;IACH,SAAS,CAAC,EAAE,MAAM,CAAC;IAEnB;;;;OAIG;IACH,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,WAAW,CAAC,EAAE,MAAM,CAAC;IAErB;;;;OAIG;IACH,sBAAsB,CAAC,EAAE,MAAM,EAAE,CAAC;IAElC;;;;;OAKG;IACH,sBAAsB,CAAC,EAAE,MAAM,EAAE,CAAC;IAElC;;;OAGG;IACH,YAAY,CAAC,EAAE,MAAM,CAAC,QAAQ,CAAC;CAChC;AAED,qBAAa,gBAAiB,SAAQ,MAAM,CAAC,iBAAiB;IAC5D,8DAA8D;IAC9D,QAAQ,CAAC,WAAW,EAAE,MAAM,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC;IAE5C,qEAAqE;IACrE,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC;IAErC,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC;IAErC,gDAAgD;IAChD,QAAQ,CAAC,gBAAgB,EAAE,MAAM,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC;IAEjD;;;;;;;OAOG;IACH,QAAQ,CAAC,qBAAqB,EAAE,MAAM,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC;gBAE1C,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,oBAAoB,EAAE,IAAI,CAAC,EAAE,MAAM,CAAC,wBAAwB;CAyH7F"}
@@ -0,0 +1,174 @@
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.PostgresInstance = void 0;
37
+ const k8s = __importStar(require("@pulumi/kubernetes"));
38
+ const pulumi = __importStar(require("@pulumi/pulumi"));
39
+ /**
40
+ * PostgresInstance — per-app PostgreSQL backed by the CloudNativePG (CNPG) operator.
41
+ *
42
+ * Creates a CNPG `Cluster` CRD and an adapter `Secret` that exposes the generated
43
+ * credentials under canonical env var names (`POSTGRES_DB`, `POSTGRES_USER`,
44
+ * `POSTGRES_PASSWORD`, `DATABASE_URL`).
45
+ *
46
+ * See docs/howto/deploy-database.md for usage and the restore runbook.
47
+ */
48
+ // CNPG webhook rejects tags that don't start with the PG major version (e.g. "latest-pg18").
49
+ const DEFAULT_IMAGE = "postgres:18";
50
+ const DEFAULT_USERNAME = "app"; // CNPG convention: app user owns the app database
51
+ const DEFAULT_STORAGE_SIZE = "5Gi";
52
+ const DEFAULT_STORAGE_CLASS = "longhorn-persistent";
53
+ const DEFAULT_INSTANCES = 1;
54
+ const PG_PORT = 5432;
55
+ // Official postgres image (Debian) uses UID/GID 26 — matches CNPG's own default.
56
+ // Override at call site for non-standard images (paradedb uses 999).
57
+ const DEFAULT_POSTGRES_UID = 26;
58
+ const DEFAULT_POSTGRES_GID = 26;
59
+ class PostgresInstance extends pulumi.ComponentResource {
60
+ /** Read-write (primary) service name: `<name>-postgres-rw` */
61
+ serviceName;
62
+ /** In-cluster hostname: `<service>.<namespace>.svc.cluster.local` */
63
+ host;
64
+ port;
65
+ /** Full connection string — treat as secret. */
66
+ connectionString;
67
+ /**
68
+ * Adapter Secret name containing `POSTGRES_DB`, `POSTGRES_USER`,
69
+ * `POSTGRES_PASSWORD`, `DATABASE_URL`.
70
+ *
71
+ * ```typescript
72
+ * envFrom: [{ secretRef: { name: db.credentialsSecretName } }]
73
+ * ```
74
+ */
75
+ credentialsSecretName;
76
+ constructor(name, args, opts) {
77
+ super("homelab:components:PostgresInstance", name, args, opts);
78
+ const childOpts = { parent: this };
79
+ const namespaceName = args.namespace.metadata.name;
80
+ const image = args.image ?? DEFAULT_IMAGE;
81
+ const username = args.username ?? DEFAULT_USERNAME;
82
+ const storageSize = args.storageSize ?? DEFAULT_STORAGE_SIZE;
83
+ const storageClass = args.storageClass ?? DEFAULT_STORAGE_CLASS;
84
+ const instances = args.instances ?? DEFAULT_INSTANCES;
85
+ const postgresUID = args.postgresUID ?? DEFAULT_POSTGRES_UID;
86
+ const postgresGID = args.postgresGID ?? DEFAULT_POSTGRES_GID;
87
+ const sharedPreloadLibraries = args.sharedPreloadLibraries ?? [];
88
+ const postInitApplicationSQL = args.postInitApplicationSQL ?? [];
89
+ // CNPG names its objects as "<cluster-name>-<suffix>"
90
+ const clusterName = `${name}-postgres`;
91
+ const cnpgSecretName = `${clusterName}-app`;
92
+ const adapterSecretName = `${name}-postgres-credentials`;
93
+ const serviceName = `${clusterName}-rw`;
94
+ const clusterDependsOn = [args.namespace];
95
+ if (args.cnpgOperator)
96
+ clusterDependsOn.push(args.cnpgOperator);
97
+ const cluster = new k8s.apiextensions.CustomResource(clusterName, {
98
+ apiVersion: "postgresql.cnpg.io/v1",
99
+ kind: "Cluster",
100
+ metadata: {
101
+ name: clusterName,
102
+ namespace: namespaceName,
103
+ labels: {
104
+ app: name,
105
+ "app.kubernetes.io/managed-by": "pulumi",
106
+ },
107
+ },
108
+ spec: {
109
+ instances,
110
+ imageName: image,
111
+ postgresUID,
112
+ postgresGID,
113
+ bootstrap: {
114
+ initdb: {
115
+ database: args.databaseName,
116
+ owner: username,
117
+ postInitApplicationSQL,
118
+ },
119
+ },
120
+ postgresql: {
121
+ shared_preload_libraries: sharedPreloadLibraries,
122
+ },
123
+ storage: {
124
+ size: storageSize,
125
+ storageClass,
126
+ },
127
+ },
128
+ }, { ...childOpts, dependsOn: clusterDependsOn });
129
+ // CNPG auto-creates `<cluster>-app` after bootstrap. We read it to project its
130
+ // base64-encoded fields into our canonical adapter secret.
131
+ const cnpgSecret = k8s.core.v1.Secret.get(cnpgSecretName, pulumi.interpolate `${namespaceName}/${cnpgSecretName}`, { ...childOpts, dependsOn: [cluster] });
132
+ const cnpgData = cnpgSecret.data;
133
+ const b64decode = (v) => v.apply((s) => Buffer.from(s, "base64").toString("utf-8"));
134
+ const decodedUsername = b64decode(cnpgData.apply((d) => d.username ?? ""));
135
+ const decodedPassword = b64decode(cnpgData.apply((d) => d.password ?? ""));
136
+ const decodedDbname = b64decode(cnpgData.apply((d) => d.dbname ?? ""));
137
+ const decodedUri = b64decode(cnpgData.apply((d) => d.uri ?? ""));
138
+ new k8s.core.v1.Secret(adapterSecretName, {
139
+ metadata: {
140
+ name: adapterSecretName,
141
+ namespace: namespaceName,
142
+ labels: {
143
+ app: name,
144
+ "app.kubernetes.io/managed-by": "pulumi",
145
+ },
146
+ annotations: {
147
+ "homelab/cnpg-source-secret": cnpgSecretName,
148
+ },
149
+ },
150
+ type: "Opaque",
151
+ stringData: {
152
+ POSTGRES_DB: decodedDbname,
153
+ POSTGRES_USER: decodedUsername,
154
+ POSTGRES_PASSWORD: decodedPassword,
155
+ DATABASE_URL: decodedUri,
156
+ },
157
+ }, { ...childOpts, dependsOn: [cnpgSecret] });
158
+ const host = pulumi.interpolate `${serviceName}.${namespaceName}.svc.cluster.local`;
159
+ this.serviceName = pulumi.output(serviceName);
160
+ this.host = host;
161
+ this.port = pulumi.output(PG_PORT);
162
+ this.connectionString = decodedUri;
163
+ this.credentialsSecretName = pulumi.output(adapterSecretName);
164
+ this.registerOutputs({
165
+ serviceName: this.serviceName,
166
+ host: this.host,
167
+ port: this.port,
168
+ connectionString: this.connectionString,
169
+ credentialsSecretName: this.credentialsSecretName,
170
+ });
171
+ }
172
+ }
173
+ exports.PostgresInstance = PostgresInstance;
174
+ //# sourceMappingURL=PostgresInstance.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"PostgresInstance.js","sourceRoot":"","sources":["../../src/postgres/PostgresInstance.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,wDAA0C;AAC1C,uDAAyC;AAEzC;;;;;;;;GAQG;AAEH,6FAA6F;AAC7F,MAAM,aAAa,GAAG,aAAa,CAAC;AACpC,MAAM,gBAAgB,GAAG,KAAK,CAAC,CAAC,kDAAkD;AAClF,MAAM,oBAAoB,GAAG,KAAK,CAAC;AACnC,MAAM,qBAAqB,GAAG,qBAAqB,CAAC;AACpD,MAAM,iBAAiB,GAAG,CAAC,CAAC;AAC5B,MAAM,OAAO,GAAG,IAAI,CAAC;AACrB,iFAAiF;AACjF,qEAAqE;AACrE,MAAM,oBAAoB,GAAG,EAAE,CAAC;AAChC,MAAM,oBAAoB,GAAG,EAAE,CAAC;AA8DhC,MAAa,gBAAiB,SAAQ,MAAM,CAAC,iBAAiB;IAC5D,8DAA8D;IACrD,WAAW,CAAwB;IAE5C,qEAAqE;IAC5D,IAAI,CAAwB;IAE5B,IAAI,CAAwB;IAErC,gDAAgD;IACvC,gBAAgB,CAAwB;IAEjD;;;;;;;OAOG;IACM,qBAAqB,CAAwB;IAEtD,YAAY,IAAY,EAAE,IAA0B,EAAE,IAAsC;QAC1F,KAAK,CAAC,qCAAqC,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,CAAC,CAAC;QAE/D,MAAM,SAAS,GAAoC,EAAE,MAAM,EAAE,IAAI,EAAE,CAAC;QAEpE,MAAM,aAAa,GAAG,IAAI,CAAC,SAAS,CAAC,QAAQ,CAAC,IAA6B,CAAC;QAE5E,MAAM,KAAK,GAAG,IAAI,CAAC,KAAK,IAAI,aAAa,CAAC;QAC1C,MAAM,QAAQ,GAAG,IAAI,CAAC,QAAQ,IAAI,gBAAgB,CAAC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AC,qBAAqB;SAClD,CAAC,CAAC;IACL,CAAC;CACF;AA/ID,4CA+IC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mrsimpson/homelab-core-components",
3
- "version": "0.2.1",
3
+ "version": "0.2.2",
4
4
  "description": "Reusable Pulumi components for secure homelab infrastructure",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
package/src/index.ts CHANGED
@@ -22,3 +22,5 @@ export type {
22
22
  } from "./ExposedWebApp";
23
23
  export type { HomelabContextConfig } from "./homelab-context";
24
24
  export type { CreateImagePullSecretArgs } from "./registry-secrets";
25
+ export { PostgresInstance } from "./postgres/PostgresInstance";
26
+ export type { PostgresInstanceArgs } from "./postgres/PostgresInstance";
@@ -0,0 +1,229 @@
1
+ import * as k8s from "@pulumi/kubernetes";
2
+ import * as pulumi from "@pulumi/pulumi";
3
+
4
+ /**
5
+ * PostgresInstance — per-app PostgreSQL backed by the CloudNativePG (CNPG) operator.
6
+ *
7
+ * Creates a CNPG `Cluster` CRD and an adapter `Secret` that exposes the generated
8
+ * credentials under canonical env var names (`POSTGRES_DB`, `POSTGRES_USER`,
9
+ * `POSTGRES_PASSWORD`, `DATABASE_URL`).
10
+ *
11
+ * See docs/howto/deploy-database.md for usage and the restore runbook.
12
+ */
13
+
14
+ // CNPG webhook rejects tags that don't start with the PG major version (e.g. "latest-pg18").
15
+ const DEFAULT_IMAGE = "postgres:18";
16
+ const DEFAULT_USERNAME = "app"; // CNPG convention: app user owns the app database
17
+ const DEFAULT_STORAGE_SIZE = "5Gi";
18
+ const DEFAULT_STORAGE_CLASS = "longhorn-persistent";
19
+ const DEFAULT_INSTANCES = 1;
20
+ const PG_PORT = 5432;
21
+ // Official postgres image (Debian) uses UID/GID 26 — matches CNPG's own default.
22
+ // Override at call site for non-standard images (paradedb uses 999).
23
+ const DEFAULT_POSTGRES_UID = 26;
24
+ const DEFAULT_POSTGRES_GID = 26;
25
+ export interface PostgresInstanceArgs {
26
+ /** Pre-created Kubernetes namespace to deploy into */
27
+ namespace: k8s.core.v1.Namespace;
28
+
29
+ /** Name of the database to create */
30
+ databaseName: pulumi.Input<string>;
31
+
32
+ /**
33
+ * Container image for Postgres.
34
+ * Must start with the PG major version (e.g. `18`) — CNPG's webhook rejects `latest`.
35
+ * @default "postgres:18"
36
+ */
37
+ image?: pulumi.Input<string>;
38
+
39
+ /** @default "app" — CNPG convention */
40
+ username?: pulumi.Input<string>;
41
+
42
+ /** @default "5Gi" */
43
+ storageSize?: pulumi.Input<string>;
44
+
45
+ /**
46
+ * @default "longhorn-persistent" — daily R2 backup, 7-day retention
47
+ */
48
+ storageClass?: pulumi.Input<string>;
49
+
50
+ /**
51
+ * Primary + hot-standby count. Set to 3 for HA — CNPG handles promotion automatically.
52
+ * @default 1
53
+ */
54
+ instances?: number;
55
+
56
+ /**
57
+ * UID/GID for the postgres OS user. Official postgres image uses 26; paradedb uses 999.
58
+ * **Immutable** — changing requires cluster deletion and recreation.
59
+ * @default 26
60
+ */
61
+ postgresUID?: number;
62
+ postgresGID?: number;
63
+
64
+ /**
65
+ * Libraries for `shared_preload_libraries`. Required for extensions that cannot load
66
+ * on demand (e.g. `pg_search`, `pg_cron`).
67
+ * @default []
68
+ */
69
+ sharedPreloadLibraries?: string[];
70
+
71
+ /**
72
+ * SQL run as superuser in the app database at bootstrap — before the app user connects.
73
+ * Use to pre-install superuser-only extensions so app migrations are no-ops.
74
+ * Only runs once at cluster creation; not re-applied on updates.
75
+ * @default []
76
+ */
77
+ postInitApplicationSQL?: string[];
78
+
79
+ /**
80
+ * CNPG operator Helm Release. Pass `baseInfra.cnpg.operator` to ensure the webhook
81
+ * is ready before the `Cluster` CRD is applied.
82
+ */
83
+ cnpgOperator?: pulumi.Resource;
84
+ }
85
+
86
+ export class PostgresInstance extends pulumi.ComponentResource {
87
+ /** Read-write (primary) service name: `<name>-postgres-rw` */
88
+ readonly serviceName: pulumi.Output<string>;
89
+
90
+ /** In-cluster hostname: `<service>.<namespace>.svc.cluster.local` */
91
+ readonly host: pulumi.Output<string>;
92
+
93
+ readonly port: pulumi.Output<number>;
94
+
95
+ /** Full connection string — treat as secret. */
96
+ readonly connectionString: pulumi.Output<string>;
97
+
98
+ /**
99
+ * Adapter Secret name containing `POSTGRES_DB`, `POSTGRES_USER`,
100
+ * `POSTGRES_PASSWORD`, `DATABASE_URL`.
101
+ *
102
+ * ```typescript
103
+ * envFrom: [{ secretRef: { name: db.credentialsSecretName } }]
104
+ * ```
105
+ */
106
+ readonly credentialsSecretName: pulumi.Output<string>;
107
+
108
+ constructor(name: string, args: PostgresInstanceArgs, opts?: pulumi.ComponentResourceOptions) {
109
+ super("homelab:components:PostgresInstance", name, args, opts);
110
+
111
+ const childOpts: pulumi.ComponentResourceOptions = { parent: this };
112
+
113
+ const namespaceName = args.namespace.metadata.name as pulumi.Output<string>;
114
+
115
+ const image = args.image ?? DEFAULT_IMAGE;
116
+ const username = args.username ?? DEFAULT_USERNAME;
117
+ const storageSize = args.storageSize ?? DEFAULT_STORAGE_SIZE;
118
+ const storageClass = args.storageClass ?? DEFAULT_STORAGE_CLASS;
119
+ const instances = args.instances ?? DEFAULT_INSTANCES;
120
+ const postgresUID = args.postgresUID ?? DEFAULT_POSTGRES_UID;
121
+ const postgresGID = args.postgresGID ?? DEFAULT_POSTGRES_GID;
122
+ const sharedPreloadLibraries = args.sharedPreloadLibraries ?? [];
123
+ const postInitApplicationSQL = args.postInitApplicationSQL ?? [];
124
+
125
+ // CNPG names its objects as "<cluster-name>-<suffix>"
126
+ const clusterName = `${name}-postgres`;
127
+ const cnpgSecretName = `${clusterName}-app`;
128
+ const adapterSecretName = `${name}-postgres-credentials`;
129
+ const serviceName = `${clusterName}-rw`;
130
+
131
+ const clusterDependsOn: pulumi.Resource[] = [args.namespace];
132
+ if (args.cnpgOperator) clusterDependsOn.push(args.cnpgOperator);
133
+
134
+ const cluster = new k8s.apiextensions.CustomResource(
135
+ clusterName,
136
+ {
137
+ apiVersion: "postgresql.cnpg.io/v1",
138
+ kind: "Cluster",
139
+ metadata: {
140
+ name: clusterName,
141
+ namespace: namespaceName,
142
+ labels: {
143
+ app: name,
144
+ "app.kubernetes.io/managed-by": "pulumi",
145
+ },
146
+ },
147
+ spec: {
148
+ instances,
149
+ imageName: image,
150
+ postgresUID,
151
+ postgresGID,
152
+ bootstrap: {
153
+ initdb: {
154
+ database: args.databaseName,
155
+ owner: username,
156
+ postInitApplicationSQL,
157
+ },
158
+ },
159
+ postgresql: {
160
+ shared_preload_libraries: sharedPreloadLibraries,
161
+ },
162
+ storage: {
163
+ size: storageSize,
164
+ storageClass,
165
+ },
166
+ },
167
+ },
168
+ { ...childOpts, dependsOn: clusterDependsOn }
169
+ );
170
+
171
+ // CNPG auto-creates `<cluster>-app` after bootstrap. We read it to project its
172
+ // base64-encoded fields into our canonical adapter secret.
173
+ const cnpgSecret = k8s.core.v1.Secret.get(
174
+ cnpgSecretName,
175
+ pulumi.interpolate`${namespaceName}/${cnpgSecretName}`,
176
+ { ...childOpts, dependsOn: [cluster] }
177
+ );
178
+
179
+ const cnpgData = cnpgSecret.data;
180
+ const b64decode = (v: pulumi.Output<string>): pulumi.Output<string> =>
181
+ v.apply((s) => Buffer.from(s, "base64").toString("utf-8"));
182
+
183
+ const decodedUsername = b64decode(cnpgData.apply((d) => d.username ?? ""));
184
+ const decodedPassword = b64decode(cnpgData.apply((d) => d.password ?? ""));
185
+ const decodedDbname = b64decode(cnpgData.apply((d) => d.dbname ?? ""));
186
+ const decodedUri = b64decode(cnpgData.apply((d) => d.uri ?? ""));
187
+
188
+ new k8s.core.v1.Secret(
189
+ adapterSecretName,
190
+ {
191
+ metadata: {
192
+ name: adapterSecretName,
193
+ namespace: namespaceName,
194
+ labels: {
195
+ app: name,
196
+ "app.kubernetes.io/managed-by": "pulumi",
197
+ },
198
+ annotations: {
199
+ "homelab/cnpg-source-secret": cnpgSecretName,
200
+ },
201
+ },
202
+ type: "Opaque",
203
+ stringData: {
204
+ POSTGRES_DB: decodedDbname,
205
+ POSTGRES_USER: decodedUsername,
206
+ POSTGRES_PASSWORD: decodedPassword,
207
+ DATABASE_URL: decodedUri,
208
+ },
209
+ },
210
+ { ...childOpts, dependsOn: [cnpgSecret] }
211
+ );
212
+
213
+ const host = pulumi.interpolate`${serviceName}.${namespaceName}.svc.cluster.local`;
214
+
215
+ this.serviceName = pulumi.output(serviceName);
216
+ this.host = host;
217
+ this.port = pulumi.output(PG_PORT);
218
+ this.connectionString = decodedUri;
219
+ this.credentialsSecretName = pulumi.output(adapterSecretName);
220
+
221
+ this.registerOutputs({
222
+ serviceName: this.serviceName,
223
+ host: this.host,
224
+ port: this.port,
225
+ connectionString: this.connectionString,
226
+ credentialsSecretName: this.credentialsSecretName,
227
+ });
228
+ }
229
+ }