@mrsimpson/homelab-core-components 0.1.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.
@@ -0,0 +1,120 @@
1
+ import * as pulumi from "@pulumi/pulumi";
2
+ import { HomelabContext } from "./homelab-context";
3
+ import type {
4
+ ExternalSecretsConfig,
5
+ GatewayApiConfig,
6
+ TLSConfig,
7
+ } from "./ExposedWebApp";
8
+
9
+ /**
10
+ * Optional overrides for infrastructure configuration that cannot be derived
11
+ * from StackReference outputs (e.g. live Pulumi resources, non-default names).
12
+ *
13
+ * All fields have sensible homelab defaults and may be omitted in most cases.
14
+ */
15
+ export interface HomelabContextFromStackOptions {
16
+ /**
17
+ * TLS / cert-manager configuration.
18
+ *
19
+ * Defaults to `{ clusterIssuerName: "letsencrypt-prod" }` which matches the
20
+ * name created by the homelab base-infra stack.
21
+ */
22
+ tls?: TLSConfig;
23
+
24
+ /**
25
+ * Gateway API / Traefik configuration.
26
+ *
27
+ * Defaults mirror the values set by the homelab base-infra stack:
28
+ * - gatewayClass: "traefik"
29
+ * - gatewayName: "homelab-gateway"
30
+ * - gatewayNamespace: "traefik-system"
31
+ * - forwardAuthMiddleware: "authelia-forwardauth"
32
+ */
33
+ gatewayApi?: GatewayApiConfig;
34
+
35
+ /**
36
+ * External Secrets Operator configuration.
37
+ *
38
+ * Defaults to `{ storeName: "pulumi-esc" }` which matches the
39
+ * ClusterSecretStore created by the homelab base-infra stack.
40
+ */
41
+ externalSecrets?: ExternalSecretsConfig;
42
+ }
43
+
44
+ /**
45
+ * Creates a {@link HomelabContext} by reading infrastructure facts from a
46
+ * Pulumi StackReference to the homelab base stack.
47
+ *
48
+ * Use this in external repos instead of constructing HomelabContext manually.
49
+ * It is the zero-boilerplate entry point for apps that live outside the
50
+ * homelab monorepo.
51
+ *
52
+ * ## Required stack outputs
53
+ *
54
+ * The referenced stack must export:
55
+ * - `tunnelCname` — Cloudflare tunnel CNAME hostname (e.g. `<id>.cfargotunnel.com`)
56
+ * - `cloudflareZoneId` — Cloudflare Zone ID for the homelab domain
57
+ *
58
+ * These are exported by `src/index.ts` in the homelab monorepo.
59
+ *
60
+ * ## Live resources (controller, clusterIssuer)
61
+ *
62
+ * `GatewayApiConfig.controller` and `TLSConfig.clusterIssuer` are live Pulumi
63
+ * resource references and cannot be reconstructed from a StackReference.
64
+ * They are intentionally omitted — ExposedWebApp works fine without them
65
+ * (they only affect Pulumi's dependency graph, not the actual Kubernetes
66
+ * resources that get created). If you need explicit cross-stack dependencies,
67
+ * pass a custom `options.gatewayApi` or `options.tls` object.
68
+ *
69
+ * @param stackRef - A Pulumi StackReference pointing to the homelab base stack.
70
+ * @param options - Optional overrides for TLS, Gateway API, and External
71
+ * Secrets configuration. Omit to use homelab defaults.
72
+ * @returns A {@link HomelabContext} ready to use with `createExposedWebApp()`.
73
+ *
74
+ * @example
75
+ * ```typescript
76
+ * import * as pulumi from "@pulumi/pulumi";
77
+ * import { createHomelabContextFromStack } from "@mrsimpson/homelab-core-components";
78
+ *
79
+ * const homelabStack = new pulumi.StackReference("org/homelab/prod");
80
+ * const homelab = createHomelabContextFromStack(homelabStack);
81
+ *
82
+ * homelab.createExposedWebApp("my-app", {
83
+ * image: "my-org/my-app:latest",
84
+ * domain: pulumi.interpolate`my-app.${homelabStack.getOutput("domain")}`,
85
+ * port: 3000,
86
+ * });
87
+ * ```
88
+ */
89
+ export function createHomelabContextFromStack(
90
+ stackRef: pulumi.StackReference,
91
+ options?: HomelabContextFromStackOptions
92
+ ): HomelabContext {
93
+ const tunnelCname = stackRef.getOutput("tunnelCname") as pulumi.Output<string>;
94
+ const cloudflareZoneId = stackRef.getOutput("cloudflareZoneId") as pulumi.Output<string>;
95
+
96
+ const tls: TLSConfig = options?.tls ?? {
97
+ clusterIssuerName: "letsencrypt-prod",
98
+ };
99
+
100
+ const gatewayApi: GatewayApiConfig = options?.gatewayApi ?? {
101
+ gatewayClass: "traefik",
102
+ gatewayName: "homelab-gateway",
103
+ gatewayNamespace: "traefik-system",
104
+ forwardAuthMiddleware: "authelia-forwardauth",
105
+ };
106
+
107
+ const externalSecrets: ExternalSecretsConfig = options?.externalSecrets ?? {
108
+ storeName: "pulumi-esc",
109
+ };
110
+
111
+ return new HomelabContext({
112
+ cloudflare: {
113
+ zoneId: cloudflareZoneId,
114
+ tunnelCname,
115
+ },
116
+ tls,
117
+ gatewayApi,
118
+ externalSecrets,
119
+ });
120
+ }
@@ -0,0 +1,49 @@
1
+ import * as k8s from "@pulumi/kubernetes";
2
+ import * as pulumi from "@pulumi/pulumi";
3
+ import type {
4
+ CloudflareConfig,
5
+ ExposedWebAppArgs,
6
+ ExternalSecretsConfig,
7
+ GatewayApiConfig,
8
+ TLSConfig,
9
+ } from "./ExposedWebApp";
10
+ import { ExposedWebApp } from "./ExposedWebApp";
11
+
12
+ /**
13
+ * HomelabContext - Bundles all infrastructure dependencies for easy injection
14
+ *
15
+ * Create this once in your infrastructure setup, then use it to easily create
16
+ * ExposedWebApp instances without needing to pass all dependencies every time.
17
+ */
18
+ export interface HomelabContextConfig {
19
+ cloudflare?: CloudflareConfig;
20
+ tls?: TLSConfig;
21
+ gatewayApi?: GatewayApiConfig;
22
+ externalSecrets?: ExternalSecretsConfig;
23
+ namespaces?: Record<string, k8s.core.v1.Namespace>;
24
+ }
25
+
26
+ export class HomelabContext {
27
+ constructor(private readonly config: HomelabContextConfig) {}
28
+
29
+ /**
30
+ * Creates an ExposedWebApp with infrastructure dependencies automatically injected
31
+ */
32
+ createExposedWebApp(
33
+ name: string,
34
+ args: Omit<ExposedWebAppArgs, "cloudflare" | "tls" | "gatewayApi" | "externalSecrets">,
35
+ opts?: pulumi.ComponentResourceOptions
36
+ ): ExposedWebApp {
37
+ return new ExposedWebApp(
38
+ name,
39
+ {
40
+ ...args,
41
+ cloudflare: this.config.cloudflare,
42
+ tls: this.config.tls,
43
+ gatewayApi: this.config.gatewayApi,
44
+ externalSecrets: this.config.externalSecrets,
45
+ },
46
+ opts
47
+ );
48
+ }
49
+ }
package/src/index.ts ADDED
@@ -0,0 +1,24 @@
1
+ /**
2
+ * @mrsimpson/homelab-components
3
+ *
4
+ * Reusable Pulumi components for homelab infrastructure
5
+ */
6
+
7
+ export { ExposedWebApp, AuthType } from "./ExposedWebApp";
8
+ export { HomelabContext } from "./homelab-context";
9
+ export { createHomelabContextFromStack } from "./createHomelabContextFromStack";
10
+ export type { HomelabContextFromStackOptions } from "./createHomelabContextFromStack";
11
+ export {
12
+ createGhcrImagePullSecret,
13
+ createDockerHubImagePullSecret,
14
+ } from "./registry-secrets";
15
+ export type {
16
+ ExposedWebAppArgs,
17
+ StorageConfig,
18
+ CloudflareConfig,
19
+ TLSConfig,
20
+ GatewayApiConfig,
21
+ ExternalSecretsConfig,
22
+ } from "./ExposedWebApp";
23
+ export type { HomelabContextConfig } from "./homelab-context";
24
+ export type { CreateImagePullSecretArgs } from "./registry-secrets";
@@ -0,0 +1,173 @@
1
+ import * as k8s from "@pulumi/kubernetes";
2
+ import * as pulumi from "@pulumi/pulumi";
3
+
4
+ /**
5
+ * Helper functions for creating ImagePullSecrets for private container registries.
6
+ *
7
+ * These functions create ExternalSecrets that sync credentials from Pulumi ESC
8
+ * to Kubernetes secrets for pulling private container images.
9
+ */
10
+
11
+ export interface CreateImagePullSecretArgs {
12
+ /** Namespace to create the secret in */
13
+ namespace: string;
14
+ /** ClusterSecretStore name (defaults to "pulumi-esc") */
15
+ storeName?: string;
16
+ /** External Secrets Operator to depend on (optional) */
17
+ dependsOn?: pulumi.Resource[];
18
+ }
19
+
20
+ /**
21
+ * Creates a GHCR (GitHub Container Registry) ImagePullSecret in the specified namespace.
22
+ *
23
+ * This is a convenience function for apps deployed outside the main homelab infrastructure.
24
+ * It creates an ExternalSecret that pulls GitHub credentials from Pulumi ESC and creates
25
+ * a kubernetes.io/dockerconfigjson secret for pulling private images from ghcr.io.
26
+ *
27
+ * Prerequisites:
28
+ * 1. External Secrets Operator must be installed in the cluster
29
+ * 2. ClusterSecretStore "pulumi-esc" must exist
30
+ * 3. GitHub credentials must be stored in Pulumi ESC:
31
+ * - Key: github-credentials/username (your GitHub username)
32
+ * - Key: github-credentials/token (GitHub PAT with read:packages scope)
33
+ *
34
+ * Usage in app deployment:
35
+ * ```typescript
36
+ * import { createGhcrImagePullSecret } from "@mrsimpson/homelab-core-components";
37
+ *
38
+ * // Create the secret in your app's namespace
39
+ * const pullSecret = createGhcrImagePullSecret({
40
+ * namespace: "my-app",
41
+ * });
42
+ *
43
+ * // Then reference it in your deployment
44
+ * const app = new ExposedWebApp("my-app", {
45
+ * image: "ghcr.io/username/my-app:latest",
46
+ * imagePullSecrets: [{ name: "ghcr-pull-secret" }],
47
+ * // ...
48
+ * });
49
+ * ```
50
+ *
51
+ * @param args Configuration for the ImagePullSecret
52
+ * @returns The created ExternalSecret resource
53
+ */
54
+ export function createGhcrImagePullSecret(
55
+ args: CreateImagePullSecretArgs,
56
+ ): k8s.apiextensions.CustomResource {
57
+ const storeName = args.storeName || "pulumi-esc";
58
+
59
+ return new k8s.apiextensions.CustomResource(
60
+ `ghcr-pull-secret-${args.namespace}`,
61
+ {
62
+ apiVersion: "external-secrets.io/v1beta1",
63
+ kind: "ExternalSecret",
64
+ metadata: {
65
+ name: "ghcr-pull-secret",
66
+ namespace: args.namespace,
67
+ },
68
+ spec: {
69
+ refreshInterval: "1h",
70
+ secretStoreRef: {
71
+ name: storeName,
72
+ kind: "ClusterSecretStore",
73
+ },
74
+ target: {
75
+ name: "ghcr-pull-secret",
76
+ creationPolicy: "Owner",
77
+ template: {
78
+ type: "kubernetes.io/dockerconfigjson",
79
+ data: {
80
+ ".dockerconfigjson": `{"auths":{"ghcr.io":{"username":"{{ .github_username }}","password":"{{ .github_token }}","auth":"{{ printf "%s:%s" .github_username .github_token | b64enc }}"}}}`,
81
+ },
82
+ },
83
+ },
84
+ data: [
85
+ {
86
+ secretKey: "github_username",
87
+ remoteRef: {
88
+ key: "github-username",
89
+ },
90
+ },
91
+ {
92
+ secretKey: "github_token",
93
+ remoteRef: {
94
+ key: "github-token",
95
+ },
96
+ },
97
+ ],
98
+ },
99
+ },
100
+ { dependsOn: args.dependsOn },
101
+ );
102
+ }
103
+
104
+ /**
105
+ * Creates a Docker Hub ImagePullSecret in the specified namespace.
106
+ *
107
+ * Prerequisites:
108
+ * 1. External Secrets Operator must be installed
109
+ * 2. ClusterSecretStore "pulumi-esc" must exist
110
+ * 3. Docker Hub credentials in Pulumi ESC:
111
+ * - Key: dockerhub-credentials/username
112
+ * - Key: dockerhub-credentials/token
113
+ *
114
+ * @param args Configuration for the ImagePullSecret
115
+ * @returns The created ExternalSecret resource
116
+ */
117
+ export function createDockerHubImagePullSecret(
118
+ args: CreateImagePullSecretArgs,
119
+ ): k8s.apiextensions.CustomResource {
120
+ const storeName = args.storeName || "pulumi-esc";
121
+
122
+ return new k8s.apiextensions.CustomResource(
123
+ `dockerhub-pull-secret-${args.namespace}`,
124
+ {
125
+ apiVersion: "external-secrets.io/v1beta1",
126
+ kind: "ExternalSecret",
127
+ metadata: {
128
+ name: "dockerhub-pull-secret",
129
+ namespace: args.namespace,
130
+ },
131
+ spec: {
132
+ refreshInterval: "1h",
133
+ secretStoreRef: {
134
+ name: storeName,
135
+ kind: "ClusterSecretStore",
136
+ },
137
+ target: {
138
+ name: "dockerhub-pull-secret",
139
+ creationPolicy: "Owner",
140
+ template: {
141
+ type: "kubernetes.io/dockerconfigjson",
142
+ data: {
143
+ ".dockerconfigjson": `{
144
+ "auths": {
145
+ "https://index.docker.io/v1/": {
146
+ "username": "{{ .dockerhub_username }}",
147
+ "password": "{{ .dockerhub_token }}",
148
+ "auth": "{{ printf "%s:%s" .dockerhub_username .dockerhub_token | b64enc }}"
149
+ }
150
+ }
151
+ }`,
152
+ },
153
+ },
154
+ },
155
+ data: [
156
+ {
157
+ secretKey: "dockerhub_username",
158
+ remoteRef: {
159
+ key: "dockerhub-credentials/username",
160
+ },
161
+ },
162
+ {
163
+ secretKey: "dockerhub_token",
164
+ remoteRef: {
165
+ key: "dockerhub-credentials/token",
166
+ },
167
+ },
168
+ ],
169
+ },
170
+ },
171
+ { dependsOn: args.dependsOn },
172
+ );
173
+ }