Compare commits

..

56 Commits

Author SHA1 Message Date
43f15c7957 feat: GoCache | deploy go package cache 2025-12-01 23:00:32 +05:00
8dc22ff13b fix: RateLimit | increase rate limits for public usage 2025-12-01 18:44:29 +05:00
69bdd52df6 feat: Netbird | set up netbird vpn 2025-11-29 13:20:50 +05:00
2c57f8005d feat: MediaServices | add ingress for arr services 2025-11-29 13:20:31 +05:00
33a8dcdaf2 fix: Traefik | abstract TLSOptions class 2025-11-29 13:20:10 +05:00
ca8d140baf fix: Postgres | enable WAL archiver 2025-11-29 13:19:43 +05:00
874f5e2dc2 fix: Gitea | fix internal tls cert 2025-11-29 13:19:33 +05:00
ff2174205a feat: Traefik | add custom TLSOptions 2025-11-29 13:19:14 +05:00
ad3478c48c sec: Traefik | disable ingressClass 2025-11-29 13:19:01 +05:00
3f6f4550d1 chore: TFG | update to 0.11.8 2025-11-29 13:18:48 +05:00
3c947c05ad feat: CertManager | update to latest version
Also improve pki
2025-11-29 13:18:34 +05:00
a753fc0e1e fix: Utils | update public and internal cert algorithms 2025-11-24 22:08:49 +05:00
d6c534378a chore: Minecraft | clean up old servers 2025-11-24 20:21:38 +05:00
10ed028c4b feat: Jellyfin | enable server discovery 2025-11-24 20:20:25 +05:00
b4e57b4f0c feat: MediaServices | deploy through cdktf 2025-11-24 12:14:27 +05:00
d003c3f280 chore: Authentik | remove unused ingress values 2025-11-24 11:29:52 +05:00
d75671f5dd fix: Ingress | simplify https infrastructure 2025-11-24 11:29:27 +05:00
497331e585 feat: Minecraft | add StarTechnology server 2025-11-24 09:28:15 +05:00
91720e6860 feat: Gitea | activate internal tls 2025-11-24 09:28:00 +05:00
c53fe7b2d1 feat: Network | enable internal TLS 2025-11-24 09:27:48 +05:00
bff4762e30 fix: Gitea | keep ssh ui enabled 2025-11-23 21:25:29 +05:00
7aca7c5fe2 fix: Gitea | use gitea ssh server instead of built in ssh 2025-11-23 20:59:44 +05:00
e1ce407b55 chore: Traefik | remove entrypoints not to be used 2025-11-23 18:19:48 +05:00
92921568da chore: move types into project root 2025-11-23 15:33:45 +05:00
a589671078 chore: MetalLB | move kustomize stuff into metallb dir 2025-11-23 15:33:33 +05:00
c690ce72f5 chore: Minecraft | clean up old gtng manifests 2025-11-23 15:28:24 +05:00
ac2153cce5 feat: GamingServices | add GTNH server 2025-11-23 15:24:45 +05:00
78aa702fa0 chore: Minecraft | remove old TFG manifests 2025-11-23 15:20:01 +05:00
56d1ad22ec fix: MinecraftServer | enable backups for pvcs 2025-11-23 15:19:50 +05:00
de09ffb189 fix: MinecraftServer | ensure correct tcp routing 2025-11-23 15:16:59 +05:00
8172a327e4 fix: MinecraftServer | use volume claim templates 2025-11-23 14:57:07 +05:00
1bdc20b215 feat: GamingServices | add TFG server 2025-11-23 14:45:06 +05:00
3038ddea26 feat: CoreServices | add entrypoints for minecraft servers 2025-11-23 14:44:56 +05:00
9ac2326308 fix: Gitea | update root url 2025-11-23 13:44:18 +05:00
7961321238 fix: delete old devpy yaml manifests 2025-11-23 13:44:11 +05:00
a96558eb80 fix: PipCache | MUST be singleton 2025-11-23 01:23:01 +05:00
0862e196cc fix: Traefik | external traffic policy local 2025-11-23 01:17:49 +05:00
1205cca3d3 fix: MetalLB | do not allow running on control-plane 2025-11-23 01:17:36 +05:00
5906fdc2b4 fix: NetworkSecurity | incorrect IP cidr in allow list 2025-11-23 01:04:28 +05:00
008ef748c6 fix: PipCache | must be singleton 2025-11-23 01:04:17 +05:00
2b49cc4ce1 feat: PipCache | add pip cache 2025-11-23 00:42:39 +05:00
d1260ecb8b chore: delete old npmcache yaml 2025-11-23 00:27:38 +05:00
945be1fa0a feat: Cache | implement npm cache 2025-11-23 00:20:55 +05:00
c4a94772d9 feat: NetworkSecurity | add secure routes for longhorn+grafana 2025-11-23 00:20:36 +05:00
5d87f6ae52 fix: Valkey | add user shahab with password 2025-11-23 00:20:22 +05:00
433193fff4 fix: Traefik | enable cross namespace middleware refs 2025-11-23 00:19:58 +05:00
84eb44a147 feat: add network security stack 2025-11-22 23:21:40 +05:00
5b6f0398f9 fix: NixCache | remove insecure ingress and use secure one 2025-11-22 23:21:30 +05:00
244accede7 fix: Longhorn | remove insecure ingress 2025-11-22 23:21:09 +05:00
a7d4878365 chore: CertManager | remove import calls 2025-11-22 23:21:01 +05:00
454b299e1c fix: Prometheus | remove insecure grafana ingress 2025-11-22 23:20:41 +05:00
b2fd9d100a feat: 1PasswordOperator | add to k8s operators stack 2025-11-22 23:20:22 +05:00
65ed6ea664 fix: UtilityServices | use secure ingress routes 2025-11-22 23:19:44 +05:00
4def414c16 feat: NetworkSecurity | add traefik middleware and valkey 2025-11-22 23:19:14 +05:00
4f5fbcf83a feat: Utils | add public and internal ingress routes 2025-11-22 23:18:56 +05:00
35c3c70b08 chore: delete old gitea runner yaml 2025-11-22 20:28:20 +05:00
77 changed files with 3352 additions and 1631 deletions

View File

@@ -0,0 +1,41 @@
import * as fs from "fs";
import * as path from "path";
import { Release } from "@cdktf/provider-helm/lib/release";
import { Construct } from "constructs";
import { PublicIngressRoute } from "../../utils";
import { Providers } from "../../types";
type GoCacheOptions = {
providers: Providers;
namespace: string;
name: string;
host: string;
};
export class GoCache extends Construct {
constructor(scope: Construct, id: string, opts: GoCacheOptions) {
super(scope, id);
const { namespace, name, host } = opts;
const { helm, kubernetes } = opts.providers;
new Release(this, "helm-release", {
provider: helm,
name,
namespace,
repository: "https://gomods.github.io/athens-charts",
chart: "athens-proxy",
values: [fs.readFileSync(path.join(__dirname, "values.yaml"), "utf8")],
});
new PublicIngressRoute(this, "ingress", {
provider: kubernetes,
namespace,
name,
host,
serviceName: `${name}-athens-proxy`,
servicePort: 80,
});
}
}

View File

@@ -0,0 +1,20 @@
replicaCount: 3
image:
runAsNonRoot: true
nodeSelector:
nodepool: worker
strategy:
type: Recreate
storage:
disk:
persistence:
enabled: true
accessMode: ReadWriteMany
size: 64Gi
storageClass: longhorn
configEnvVars:
- name: ATHENS_DOWNLOAD_MODE
value: "sync"
upstreamProxy:
enabled: true
url: "https://proxy.golang.org"

View File

@@ -1,7 +1,12 @@
import { Construct } from "constructs"; import { Construct } from "constructs";
import { TerraformStack } from "cdktf"; import { TerraformStack } from "cdktf";
import { KubernetesProvider } from "@cdktf/provider-kubernetes/lib/provider"; import { KubernetesProvider } from "@cdktf/provider-kubernetes/lib/provider";
import { NixCache } from "./nixcache"; import { NamespaceV1 } from "@cdktf/provider-kubernetes/lib/namespace-v1";
import { NixCache } from "./nix";
import { NpmCache } from "./npm";
import { PipCache } from "./pip";
import { GoCache } from "./go";
import { HelmProvider } from "@cdktf/provider-helm/lib/provider";
export class CacheInfrastructure extends TerraformStack { export class CacheInfrastructure extends TerraformStack {
constructor(scope: Construct, id: string) { constructor(scope: Construct, id: string) {
@@ -11,7 +16,50 @@ export class CacheInfrastructure extends TerraformStack {
configPath: "~/.kube/config", configPath: "~/.kube/config",
}); });
const helm = new HelmProvider(this, "helm", {
kubernetes: {
configPath: "~/.kube/config",
},
});
const namespace = "package-cache";
new NamespaceV1(this, "package-cache-namespace", {
metadata: {
name: namespace,
},
});
// Add cache-related infrastructure components here // Add cache-related infrastructure components here
new NixCache(this, "nix-cache", kubernetes); new NixCache(this, "nix-cache", {
provider: kubernetes,
namespace,
name: "nix-cache",
host: "nix.dogar.dev",
});
new NpmCache(this, "npm-cache", {
provider: kubernetes,
namespace,
name: "npm-cache",
host: "npm.dogar.dev",
});
new PipCache(this, "pip-cache", {
provider: kubernetes,
namespace,
name: "pip-cache",
host: "pip.dogar.dev",
});
new GoCache(this, "go-cache", {
providers: {
kubernetes,
helm,
},
namespace,
name: "go-cache",
host: "go.dogar.dev",
});
} }
} }

View File

@@ -6,16 +6,25 @@ import { DeploymentV1 } from "@cdktf/provider-kubernetes/lib/deployment-v1";
import { KubernetesProvider } from "@cdktf/provider-kubernetes/lib/provider"; import { KubernetesProvider } from "@cdktf/provider-kubernetes/lib/provider";
import { ServiceV1 } from "@cdktf/provider-kubernetes/lib/service-v1"; import { ServiceV1 } from "@cdktf/provider-kubernetes/lib/service-v1";
import { IngressRoute, LonghornPvc } from "../../utils"; import { PublicIngressRoute, LonghornPvc } from "../../utils";
type NixCacheOptions = {
provider: KubernetesProvider;
name: string;
namespace: string;
host: string;
};
export class NixCache extends Construct { export class NixCache extends Construct {
constructor(scope: Construct, id: string, provider: KubernetesProvider) { constructor(scope: Construct, id: string, options: NixCacheOptions) {
super(scope, id); super(scope, id);
const { provider, name, namespace, host } = options;
const pvc = new LonghornPvc(this, "pvc", { const pvc = new LonghornPvc(this, "pvc", {
provider, provider,
name: "nix-cache", name,
namespace: "homelab", namespace,
accessModes: ["ReadWriteMany"], accessModes: ["ReadWriteMany"],
size: "64Gi", size: "64Gi",
}); });
@@ -25,11 +34,11 @@ export class NixCache extends Construct {
"utf-8", "utf-8",
); );
const configMap = new ConfigMapV1(this, "config-map", { new ConfigMapV1(this, "config", {
provider, provider,
metadata: { metadata: {
name: "nix-cache", name,
namespace: "homelab", namespace,
}, },
data: { data: {
"nix-cache.conf": nginxConfig, "nix-cache.conf": nginxConfig,
@@ -39,12 +48,12 @@ export class NixCache extends Construct {
new ServiceV1(this, "service", { new ServiceV1(this, "service", {
provider, provider,
metadata: { metadata: {
name: "nix-cache", name,
namespace: "homelab", namespace,
}, },
spec: { spec: {
selector: { selector: {
app: "nix-cache", app: name,
}, },
port: [ port: [
{ {
@@ -60,20 +69,20 @@ export class NixCache extends Construct {
new DeploymentV1(this, "deployment", { new DeploymentV1(this, "deployment", {
provider, provider,
metadata: { metadata: {
name: "nix-cache", name,
namespace: "homelab", namespace,
}, },
spec: { spec: {
replicas: "3", replicas: "3",
selector: { selector: {
matchLabels: { matchLabels: {
app: "nix-cache", app: name,
}, },
}, },
template: { template: {
metadata: { metadata: {
labels: { labels: {
app: "nix-cache", app: name,
}, },
}, },
spec: { spec: {
@@ -104,7 +113,7 @@ export class NixCache extends Construct {
{ {
name: "nginx-config", name: "nginx-config",
configMap: { configMap: {
name: configMap.metadata.name, name,
items: [ items: [
{ {
key: "nix-cache.conf", key: "nix-cache.conf",
@@ -119,14 +128,13 @@ export class NixCache extends Construct {
}, },
}); });
new IngressRoute(this, "ingress-route", { new PublicIngressRoute(this, "ingress-route", {
provider, provider,
namespace: "homelab", name,
host: "nix.dogar.dev", namespace,
serviceName: "nix-cache", host,
serviceName: name,
servicePort: 80, servicePort: 80,
entryPoints: ["websecure"],
tlsSecretName: "nix-cache-tls",
}); });
} }
} }

View File

@@ -0,0 +1,19 @@
storage: /verdaccio/storage
uplinks:
npmjs:
url: https://registry.npmjs.org/
packages:
"@*/*":
access: $all
publish: never
proxy: npmjs
"**":
access: $all
publish: never
proxy: npmjs
log:
- {type: stdout, format: pretty, level: http}

View File

@@ -0,0 +1,184 @@
import * as fs from "fs";
import * as path from "path";
import { Construct } from "constructs";
import { KubernetesProvider } from "@cdktf/provider-kubernetes/lib/provider";
import { DeploymentV1 } from "@cdktf/provider-kubernetes/lib/deployment-v1";
import { ServiceV1 } from "@cdktf/provider-kubernetes/lib/service-v1";
import { ConfigMapV1 } from "@cdktf/provider-kubernetes/lib/config-map-v1";
import { LonghornPvc, PublicIngressRoute } from "../../utils";
type NpmCacheOptions = {
provider: KubernetesProvider;
namespace: string;
host: string;
name: string;
};
export class NpmCache extends Construct {
constructor(scope: Construct, id: string, opts: NpmCacheOptions) {
super(scope, id);
const { provider, namespace, name, host } = opts;
new ConfigMapV1(this, "config", {
provider,
metadata: {
name,
namespace,
},
data: {
"config.yaml": fs.readFileSync(
path.join(__dirname, "config.yaml"),
"utf8",
),
},
});
const pvc = new LonghornPvc(this, "pvc", {
provider,
namespace,
name,
size: "128Gi",
accessModes: ["ReadWriteMany"],
});
new ServiceV1(this, "service", {
provider,
metadata: {
name,
namespace,
},
spec: {
selector: {
app: name,
},
port: [
{
port: 4873,
targetPort: name,
},
],
type: "ClusterIP",
},
});
new DeploymentV1(this, "deployment", {
provider,
metadata: {
name,
namespace,
},
spec: {
replicas: "3",
selector: {
matchLabels: {
app: name,
},
},
template: {
metadata: {
labels: {
app: name,
},
},
spec: {
nodeSelector: {
nodepool: "worker",
},
topologySpreadConstraint: [
{
maxSkew: 1,
topologyKey: "kubernetes.io/hostname",
whenUnsatisfiable: "DoNotSchedule",
labelSelector: [
{
matchLabels: {
app: name,
},
},
],
},
],
affinity: {
podAntiAffinity: {
requiredDuringSchedulingIgnoredDuringExecution: [
{
topologyKey: "kubernetes.io/hostname",
labelSelector: [
{
matchExpressions: [
{
key: "app",
operator: "In",
values: [name],
},
],
},
],
},
],
},
},
volume: [
{
name: "storage",
persistentVolumeClaim: {
claimName: pvc.name,
},
},
{
name: "config",
configMap: {
name,
},
},
],
container: [
{
name,
image: "verdaccio/verdaccio:latest",
env: [
{
name: "VERDACCIO_APP_CONFIG",
value: "/verdaccio/conf/custom.yaml",
},
{
name: "VERDACCIO_PORT",
value: "4873",
},
],
port: [
{
name,
containerPort: 4873,
},
],
volumeMount: [
{
name: "storage",
mountPath: "/verdaccio/storage",
},
{
name: "config",
mountPath: "/verdaccio/conf/config.yaml",
subPath: "config.yaml",
},
],
},
],
},
},
},
});
new PublicIngressRoute(this, "ingress", {
provider,
namespace,
name,
host,
serviceName: name,
servicePort: 4873,
});
}
}

View File

@@ -0,0 +1,136 @@
import { Construct } from "constructs";
import { KubernetesProvider } from "@cdktf/provider-kubernetes/lib/provider";
import { DeploymentV1 } from "@cdktf/provider-kubernetes/lib/deployment-v1";
import { ServiceV1 } from "@cdktf/provider-kubernetes/lib/service-v1";
import {
LonghornPvc,
OnePasswordSecret,
PublicIngressRoute,
} from "../../utils";
type PipCacheOptions = {
provider: KubernetesProvider;
namespace: string;
name: string;
host: string;
};
export class PipCache extends Construct {
constructor(scope: Construct, id: string, opts: PipCacheOptions) {
super(scope, id);
const { provider, namespace, name, host } = opts;
new OnePasswordSecret(this, "devpi-secret", {
provider,
namespace,
name: "devpi",
itemPath: "vaults/Lab/items/devpi",
});
const pvc = new LonghornPvc(this, "pvc", {
provider,
namespace,
name,
size: "128Gi",
});
new DeploymentV1(this, "deployment", {
provider,
metadata: {
name,
namespace,
},
spec: {
replicas: "1",
strategy: {
type: "Recreate",
},
selector: {
matchLabels: {
app: name,
},
},
template: {
metadata: {
labels: {
app: name,
},
},
spec: {
nodeSelector: {
nodepool: "worker",
},
volume: [
{
name: "data",
persistentVolumeClaim: {
claimName: pvc.name,
},
},
],
container: [
{
name,
image: "jonasal/devpi-server:latest",
env: [
{
name: "DEVPI_PASSWORD",
valueFrom: {
secretKeyRef: {
name: "devpi",
key: "password",
},
},
},
],
port: [
{
name,
containerPort: 3141,
},
],
volumeMount: [
{
name: "data",
mountPath: "/devpi",
},
],
},
],
},
},
},
});
new ServiceV1(this, "service", {
provider,
metadata: {
name,
namespace,
},
spec: {
selector: {
app: name,
},
port: [
{
port: 3141,
targetPort: name,
},
],
type: "ClusterIP",
},
});
new PublicIngressRoute(this, "ingress", {
provider,
namespace,
name,
host,
serviceName: name,
servicePort: 3141,
});
}
}

View File

@@ -3,32 +3,23 @@ import * as path from "path";
import { HelmProvider } from "@cdktf/provider-helm/lib/provider"; import { HelmProvider } from "@cdktf/provider-helm/lib/provider";
import { Release } from "@cdktf/provider-helm/lib/release"; import { Release } from "@cdktf/provider-helm/lib/release";
import { Construct } from "constructs"; import { Construct } from "constructs";
import { KubernetesProvider } from "@cdktf/provider-kubernetes/lib/provider";
import { Manifest } from "@cdktf/provider-kubernetes/lib/manifest";
type CertManagerOptions = { type CertManagerOptions = {
providers: { provider: HelmProvider;
kubernetes: KubernetesProvider;
helm: HelmProvider;
};
version: string;
name: string; name: string;
namespace: string; namespace: string;
certManagerApiVersion: string;
}; };
export class CertManager extends Construct { export class CertManager extends Construct {
constructor(scope: Construct, id: string, options: CertManagerOptions) { constructor(scope: Construct, id: string, options: CertManagerOptions) {
super(scope, id); super(scope, id);
const { helm, kubernetes } = options.providers; const { namespace, name, provider } = options;
const { certManagerApiVersion } = options;
new Release(this, id, { new Release(this, id, {
provider: helm, provider,
name: options.name, name,
namespace: options.namespace, namespace,
version: options.version,
repository: "https://charts.jetstack.io", repository: "https://charts.jetstack.io",
chart: "cert-manager", chart: "cert-manager",
createNamespace: true, createNamespace: true,
@@ -38,107 +29,5 @@ export class CertManager extends Construct {
}), }),
], ],
}); });
// "apiVersion=v1,kind=Secret,namespace=default,name=sample"
// Self-signed ClusterIssuer for initial CA
new Manifest(this, "ca-issuer", {
provider: kubernetes,
manifest: {
apiVersion: certManagerApiVersion,
kind: "ClusterIssuer",
metadata: {
name: "ca-issuer",
},
spec: {
selfSigned: {},
},
},
}).importFrom(
`apiVersion=${certManagerApiVersion},kind=ClusterIssuer,name=ca-issuer`,
);
// Self-signed CA Certificate
new Manifest(this, "selfsigned-ca", {
provider: kubernetes,
manifest: {
apiVersion: certManagerApiVersion,
kind: "Certificate",
metadata: {
name: "selfsigned-ca",
namespace: options.namespace,
},
spec: {
isCA: true,
commonName: "Shahab Dogar",
secretName: "root-secret",
privateKey: {
algorithm: "ECDSA",
size: 256,
},
issuerRef: {
name: "ca-issuer",
kind: "ClusterIssuer",
group: "cert-manager.io",
},
},
},
}).importFrom(
`apiVersion=${certManagerApiVersion},kind=Certificate,name=selfsigned-ca,namespace=${options.namespace}`,
);
// CA-based ClusterIssuer
new Manifest(this, "cluster-issuer", {
provider: kubernetes,
manifest: {
apiVersion: certManagerApiVersion,
kind: "ClusterIssuer",
metadata: {
name: "cluster-issuer",
},
spec: {
ca: {
secretName: "root-secret",
},
},
},
}).importFrom(
`apiVersion=${certManagerApiVersion},kind=ClusterIssuer,name=cluster-issuer`,
);
// Cloudflare ACME ClusterIssuer
new Manifest(this, "cloudflare-issuer", {
provider: kubernetes,
manifest: {
apiVersion: certManagerApiVersion,
kind: "ClusterIssuer",
metadata: {
name: "cloudflare-issuer",
},
spec: {
acme: {
email: "shahab@dogar.dev",
server: "https://acme-v02.api.letsencrypt.org/directory",
privateKeySecretRef: {
name: "cloudflare-cluster-issuer-account-key",
},
solvers: [
{
dns01: {
cloudflare: {
apiTokenSecretRef: {
name: "cloudflare-token",
key: "token",
},
},
},
},
],
},
},
},
}).importFrom(
`apiVersion=${certManagerApiVersion},kind=ClusterIssuer,name=cloudflare-issuer`,
);
} }
} }

View File

@@ -1,6 +1,8 @@
crds: crds:
enabled: true enabled: true
keep: true
prometheus: prometheus:
enabled: true enabled: true
webhook: webhook:
timeoutSeconds: 4 timeoutSeconds: 4
enableCertificateOwnerRef: true

View File

@@ -56,14 +56,9 @@ export class CoreServices extends TerraformStack {
}); });
new CertManager(this, "cert-manager", { new CertManager(this, "cert-manager", {
certManagerApiVersion: "cert-manager.io/v1", provider: helm,
name: "cert-manager", name: "cert-manager",
namespace, namespace,
version: "1.18.2",
providers: {
kubernetes,
helm,
},
}); });
} }
} }

View File

@@ -5,7 +5,6 @@ import { Release } from "@cdktf/provider-helm/lib/release";
import { Construct } from "constructs"; import { Construct } from "constructs";
import { Manifest } from "@cdktf/provider-kubernetes/lib/manifest"; import { Manifest } from "@cdktf/provider-kubernetes/lib/manifest";
import { KubernetesProvider } from "@cdktf/provider-kubernetes/lib/provider"; import { KubernetesProvider } from "@cdktf/provider-kubernetes/lib/provider";
import { IngressRoute } from "../../utils";
type LonghornOptions = { type LonghornOptions = {
providers: { providers: {
@@ -53,16 +52,5 @@ export class Longhorn extends Construct {
}, },
}, },
}); });
new IngressRoute(this, "ingress", {
provider: kubernetes,
name: "longhorn",
namespace,
serviceName: "longhorn-frontend",
servicePort: 80,
host: "longhorn.dogar.dev",
tlsSecretName: "longhorn-tls",
entryPoints: ["websecure"],
});
} }
} }

View File

@@ -1,3 +1,5 @@
import * as fs from "fs";
import * as path from "path";
import { HelmProvider } from "@cdktf/provider-helm/lib/provider"; import { HelmProvider } from "@cdktf/provider-helm/lib/provider";
import { Release } from "@cdktf/provider-helm/lib/release"; import { Release } from "@cdktf/provider-helm/lib/release";
import { Construct } from "constructs"; import { Construct } from "constructs";
@@ -17,6 +19,7 @@ export class MetalLB extends Construct {
repository: "https://metallb.github.io/metallb", repository: "https://metallb.github.io/metallb",
chart: "metallb", chart: "metallb",
createNamespace: true, createNamespace: true,
values: [fs.readFileSync(path.join(__dirname, "values.yaml"), "utf8")],
}); });
} }
} }

View File

@@ -0,0 +1,6 @@
controller:
nodeSelector:
nodepool: worker
speaker:
nodeSelector:
nodepool: worker

View File

@@ -1,7 +1,10 @@
providers:
kubernetesCRD:
allowCrossNamespace: true
ingress: ingress:
ingressClass: ingressClass:
enabled: true enabled: false
isDefaultClass: false isDefaultClass: true
name: traefik name: traefik
deployment: deployment:
replicas: 3 replicas: 3
@@ -9,15 +12,21 @@ deployment:
app: traefik app: traefik
nodeSelector: nodeSelector:
nodepool: worker nodepool: worker
service:
spec:
externalTrafficPolicy: Local
topologySpreadConstraints: topologySpreadConstraints:
- maxSkew: 1 - maxSkew: 1
topologyKey: "kubernetes.io/hostname" topologyKey: "kubernetes.io/hostname"
whenUnsatisfiable: "ScheduleAnyway" whenUnsatisfiable: "DoNotSchedule"
labelSelector: labelSelector:
matchLabels: matchLabels:
app: traefik app: traefik
additionalArguments: additionalArguments:
- "--entryPoints.ssh.address=:22/tcp" - "--entryPoints.ssh.address=:22/tcp"
- "--entryPoints.minecraft-gtnh.address=:25566/tcp"
- "--entryPoints.minecraft-tfg.address=:25567/tcp"
- "--entryPoints.minecraft-star-technology.address=:25568/tcp"
ports: ports:
ssh: ssh:
name: ssh name: ssh
@@ -26,3 +35,24 @@ ports:
expose: expose:
default: true default: true
protocol: TCP protocol: TCP
minecraft-gtnh:
name: minecraft-gtnh
port: 25566
exposedPort: 25566
expose:
default: true
protocol: TCP
minecraft-tfg:
name: minecraft-tfg
port: 25567
exposedPort: 25567
expose:
default: true
protocol: TCP
minecraft-star-technology:
name: minecraft-star-technology
port: 25568
exposedPort: 25568
expose:
default: true
protocol: TCP

View File

@@ -1,128 +0,0 @@
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: devpi
namespace: homelab
spec:
storageClassName: longhorn
accessModes:
- ReadWriteMany
resources:
requests:
storage: 128Gi
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: devpi
namespace: homelab
spec:
replicas: 3
selector:
matchLabels:
app: devpi
template:
metadata:
labels:
app: devpi
spec:
nodeSelector:
nodepool: worker
topologySpreadConstraints:
- maxSkew: 1
topologyKey: kubernetes.io/hostname
whenUnsatisfiable: ScheduleAnyway
labelSelector:
matchLabels:
app: devpi
affinity:
podAntiAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
- labelSelector:
matchExpressions:
- key: app
operator: In
values:
- devpi
topologyKey: "kubernetes.io/hostname"
containers:
- name: devpi
image: jonasal/devpi-server:latest
env:
- name: DEVPI_PASSWORD
valueFrom:
secretKeyRef:
name: devpi-secret
key: password
ports:
- containerPort: 3141
volumeMounts:
- name: data
mountPath: /devpi
volumes:
- name: data
persistentVolumeClaim:
claimName: devpi
---
apiVersion: v1
kind: Service
metadata:
name: devpi
namespace: homelab
spec:
selector:
app: devpi
ports:
- port: 3141
targetPort: 3141
protocol: TCP
type: ClusterIP
---
apiVersion: traefik.io/v1alpha1
kind: Middleware
metadata:
name: devpi
namespace: homelab
spec:
ipAllowList:
sourceRange:
- "127.0.0.1/32"
- "10.43.0.0/16"
rateLimit:
average: 10
burst: 50
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: devpi
namespace: homelab
annotations:
nginx.ingress.kubernetes.io/proxy-body-size: "0"
cert-manager.io/cluster-issuer: "cloudflare-issuer"
cert-manager.io/acme-challenge-type: "dns01"
cert-manager.io/private-key-size: "4096"
# Traefik Middleware
traefik.io/router.middlewares: "devpi@kubernetescrd"
spec:
ingressClassName: traefik
tls:
- hosts:
- pip.dogar.dev
secretName: devpi-tls
rules:
- host: pip.dogar.dev
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: devpi
port:
number: 3141

View File

@@ -0,0 +1,77 @@
import { Construct } from "constructs";
import { KubernetesProvider } from "@cdktf/provider-kubernetes/lib/provider";
import { MinecraftServer } from "./utils";
export class GTNH extends Construct {
constructor(
scope: Construct,
id: string,
provider: KubernetesProvider,
namespace: string,
) {
super(scope, id);
new MinecraftServer(this, "gtnh", {
provider,
namespace,
image: "itzg/minecraft-server:java25",
name: "gtnh",
env: [
{
name: "EULA",
value: "TRUE",
},
{
name: "MODE",
value: "survival",
},
{
name: "DIFFICULTY",
value: "easy",
},
{
name: "TYPE",
value: "CUSTOM",
},
{
name: "GENERIC_PACKS",
value: "GT_New_Horizons_2.8.0_Server_Java_17-25",
},
{
name: "GENERIC_PACKS_SUFFIX",
value: ".zip",
},
{
name: "GENERIC_PACKS_PREFIX",
value: "https://downloads.gtnewhorizons.com/ServerPacks/",
},
{
name: "SKIP_GENERIC_PACK_UPDATE_CHECK",
value: "true",
},
{
name: "MEMORY",
value: "12G",
},
{
name: "JVM_OPTS",
value:
"-Dfml.readTimeout=180 -Dfml.queryResult=confirm @java9args.txt",
},
{
name: "CUSTOM_JAR_EXEC",
value: "-jar lwjgl3ify-forgePatches.jar nogui",
},
{
name: "ALLOW_FLIGHT",
value: "TRUE",
},
{
name: "ENABLE_ROLLING_LOGS",
value: "TRUE",
},
],
});
}
}

View File

@@ -0,0 +1,37 @@
import { Construct } from "constructs";
import { TerraformStack } from "cdktf";
import { KubernetesProvider } from "@cdktf/provider-kubernetes/lib/provider";
import { NamespaceV1 } from "@cdktf/provider-kubernetes/lib/namespace-v1";
import { OnePasswordSecret } from "../../utils";
import { TerraFirmaGreg } from "./tfg";
import { GTNH } from "./gtnh";
import { StarTechnology } from "./star-technology";
export class GamingServices extends TerraformStack {
constructor(scope: Construct, id: string) {
super(scope, id);
const provider = new KubernetesProvider(this, "kubernetes", {
configPath: "~/.kube/config",
});
const namespace = "minecraft";
new NamespaceV1(this, "namespace", {
metadata: {
name: namespace,
},
});
new OnePasswordSecret(this, "curseforge", {
provider,
namespace,
name: "curseforge",
itemPath: "vaults/Lab/items/curseforge",
});
new TerraFirmaGreg(this, "tfg", provider, namespace);
new GTNH(this, "gtnh", provider, namespace);
new StarTechnology(this, "star-technology", provider, namespace);
}
}

View File

@@ -0,0 +1,74 @@
import { Construct } from "constructs";
import { KubernetesProvider } from "@cdktf/provider-kubernetes/lib/provider";
import { MinecraftServer } from "./utils";
export class StarTechnology extends Construct {
constructor(
scope: Construct,
id: string,
provider: KubernetesProvider,
namespace: string,
) {
super(scope, id);
new MinecraftServer(this, "star-technology", {
provider,
namespace,
image: "itzg/minecraft-server:java21",
name: "star-technology",
env: [
{
name: "EULA",
value: "TRUE",
},
{
name: "MODE",
value: "survival",
},
{
name: "MODPACK_PLATFORM",
value: "AUTO_CURSEFORGE",
},
{
name: "CF_API_KEY",
valueFrom: {
secretKeyRef: {
name: "curseforge",
key: "credential",
},
},
},
{
name: "CF_PAGE_URL",
value:
"https://www.curseforge.com/minecraft/modpacks/star-technology",
},
{
name: "VERSION",
value: "1.20.1",
},
{
name: "INIT_MEMORY",
value: "2G",
},
{
name: "MAX_MEMORY",
value: "12G",
},
{
name: "ALLOW_FLIGHT",
value: "TRUE",
},
{
name: "ENABLE_ROLLING_LOGS",
value: "TRUE",
},
{
name: "USE_MEOWICE_FLAGS",
value: "TRUE",
},
],
});
}
}

View File

@@ -0,0 +1,78 @@
import { Construct } from "constructs";
import { KubernetesProvider } from "@cdktf/provider-kubernetes/lib/provider";
import { MinecraftServer } from "./utils";
export class TerraFirmaGreg extends Construct {
constructor(
scope: Construct,
id: string,
provider: KubernetesProvider,
namespace: string,
) {
super(scope, id);
new MinecraftServer(this, "tfg", {
provider,
namespace,
image: "itzg/minecraft-server:java17",
name: "tfg",
env: [
{
name: "EULA",
value: "TRUE",
},
{
name: "MODE",
value: "survival",
},
{
name: "MODPACK_PLATFORM",
value: "AUTO_CURSEFORGE",
},
{
name: "CF_API_KEY",
valueFrom: {
secretKeyRef: {
name: "curseforge",
key: "credential",
},
},
},
{
name: "CF_PAGE_URL",
value:
"https://www.curseforge.com/minecraft/modpacks/terrafirmagreg-modern/",
},
{
name: "CF_FILENAME_MATCHER",
value: "0.11.8",
},
{
name: "VERSION",
value: "1.20.1",
},
{
name: "INIT_MEMORY",
value: "2G",
},
{
name: "MAX_MEMORY",
value: "12G",
},
{
name: "ALLOW_FLIGHT",
value: "TRUE",
},
{
name: "ENABLE_ROLLING_LOGS",
value: "TRUE",
},
{
name: "USE_MEOWICE_FLAGS",
value: "TRUE",
},
],
});
}
}

View File

@@ -0,0 +1,148 @@
import { Construct } from "constructs";
import {
StatefulSetV1,
StatefulSetV1SpecTemplateSpecContainerEnv,
} from "@cdktf/provider-kubernetes/lib/stateful-set-v1";
import { KubernetesProvider } from "@cdktf/provider-kubernetes/lib/provider";
import { ServiceV1 } from "@cdktf/provider-kubernetes/lib/service-v1";
import { IngressRouteTcp } from "../../../utils";
export type MinecraftServerOptions = {
provider: KubernetesProvider;
namespace: string;
name: string;
env: StatefulSetV1SpecTemplateSpecContainerEnv[];
image: string;
size?: string;
};
export class MinecraftServer extends Construct {
constructor(scope: Construct, id: string, opts: MinecraftServerOptions) {
super(scope, id);
const { provider, namespace, name, image, env, size = "10Gi" } = opts;
new ServiceV1(this, "service", {
provider,
metadata: {
name,
namespace,
},
spec: {
selector: {
app: name,
},
port: [
{
port: 25565,
targetPort: "25565",
protocol: "TCP",
name: "minecraft",
},
],
type: "ClusterIP",
},
});
new StatefulSetV1(this, "stateful-set", {
provider,
metadata: {
name,
namespace,
},
waitForRollout: false,
spec: {
replicas: "1",
serviceName: name,
updateStrategy: [
{
type: "OnDelete",
},
],
selector: {
matchLabels: {
app: name,
},
},
persistentVolumeClaimRetentionPolicy: [
{
whenDeleted: "Retain",
whenScaled: "Retain",
},
],
volumeClaimTemplate: [
{
metadata: {
name: `${name}-data`,
labels: {
"recurring-job.longhorn.io/source": "enabled",
"recurring-job.longhorn.io/daily-backup": "enabled",
},
},
spec: {
accessModes: ["ReadWriteOnce"],
resources: {
requests: {
storage: size,
},
},
storageClassName: "longhorn",
},
},
],
template: {
metadata: {
labels: {
app: name,
},
},
spec: {
nodeSelector: {
nodepool: "worker",
},
container: [
{
name,
image,
env,
port: [
{
name,
containerPort: 25565,
},
],
volumeMount: [
{
name: `${name}-data`,
mountPath: "/data",
},
],
resources: {
requests: {
cpu: "2",
memory: "4Gi",
},
limits: {
cpu: "6",
memory: "12Gi",
},
},
},
],
},
},
},
});
new IngressRouteTcp(this, "ingress", {
provider,
namespace,
name,
serviceName: name,
servicePort: 25565,
entryPoint: `minecraft-${name}`,
match: "HostSNI(`*`)",
});
}
}

View File

@@ -1,66 +0,0 @@
---
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: action-runner
name: action-runner
namespace: homelab
spec:
replicas: 3
selector:
matchLabels:
app: action-runner
strategy: {}
template:
metadata:
creationTimestamp: null
labels:
app: action-runner
spec:
nodeSelector:
nodepool: worker
topologySpreadConstraints:
- maxSkew: 1
topologyKey: kubernetes.io/hostname
whenUnsatisfiable: DoNotSchedule
labelSelector:
matchLabels:
app: action-runner
restartPolicy: Always
volumes:
- name: runner-data
persistentVolumeClaim:
claimName: action-runner
securityContext:
fsGroup: 1000
containers:
- name: runner
image: gitea/act_runner:nightly-dind-rootless
imagePullPolicy: Always
env:
- name: DOCKER_HOST
value: unix:///run/user/1000/docker.sock
- name: GITEA_INSTANCE_URL
value: https://git.dogar.dev
- name: GITEA_RUNNER_REGISTRATION_TOKEN
valueFrom:
secretKeyRef:
name: runner-secret
key: runner-token
securityContext:
privileged: true
volumeMounts:
- name: runner-data
mountPath: /data
---
apiVersion: policy/v1
kind: PodDisruptionBudget
metadata:
name: action-runner-pdb
namespace: homelab
spec:
minAvailable: 6
selector:
matchLabels:
app: action-runner

View File

@@ -0,0 +1,48 @@
import * as fs from "fs";
import * as path from "path";
import { HelmProvider } from "@cdktf/provider-helm/lib/provider";
import { Release } from "@cdktf/provider-helm/lib/release";
import { Construct } from "constructs";
type OnePasswordOptions = {
provider: HelmProvider;
name: string;
};
export class OnePassword extends Construct {
constructor(scope: Construct, id: string, options: OnePasswordOptions) {
super(scope, id);
const { provider } = options;
new Release(this, "onepassword-operator", {
provider,
name: "onepassword-operator",
chart: "connect",
repository: "https://1password.github.io/connect-helm-charts/",
namespace: "1password",
createNamespace: true,
set: [
{
name: "operator.create",
value: "true",
},
],
setSensitive: [
{
name: "operator.token.value",
value: process.env.OP_CONNECT_TOKEN!,
},
{
name: "connect.credentials_base64",
value: btoa(
fs.readFileSync(
path.join(__dirname, "1password-credentials.json"),
"utf-8",
),
),
},
],
});
}
}

View File

@@ -1,12 +1,10 @@
import * as fs from "fs";
import * as path from "path";
import { HelmProvider } from "@cdktf/provider-helm/lib/provider"; import { HelmProvider } from "@cdktf/provider-helm/lib/provider";
import { Release } from "@cdktf/provider-helm/lib/release"; import { Release } from "@cdktf/provider-helm/lib/release";
import { TerraformStack } from "cdktf"; import { TerraformStack } from "cdktf";
import { Construct } from "constructs"; import { Construct } from "constructs";
import { BarmanCloudPluginInstall } from "./barman"; import { BarmanCloudPluginInstall } from "./barman";
import { Prometheus } from "./prometheus"; import { Prometheus } from "./prometheus";
import { KubernetesProvider } from "@cdktf/provider-kubernetes/lib/provider"; import { OnePassword } from "./1password";
export class K8SOperators extends TerraformStack { export class K8SOperators extends TerraformStack {
constructor(scope: Construct, id: string) { constructor(scope: Construct, id: string) {
@@ -18,48 +16,16 @@ export class K8SOperators extends TerraformStack {
}, },
}); });
const kubernetes = new KubernetesProvider(this, "kubernetes", {
configPath: "~/.kube/config",
});
new Prometheus(this, "prometheus", { new Prometheus(this, "prometheus", {
providers: { provider: helm,
helm,
kubernetes,
},
namespace: "monitoring", namespace: "monitoring",
name: "prometheus-operator", name: "prometheus-operator",
version: "75.10.0", version: "75.10.0",
}); });
new Release(this, "onepassword-operator", { new OnePassword(this, "onepassword", {
provider: helm, provider: helm,
name: "onepassword-operator", name: "onepassword",
chart: "connect",
repository: "https://1password.github.io/connect-helm-charts/",
namespace: "1password",
createNamespace: true,
set: [
{
name: "operator.create",
value: "true",
},
],
setSensitive: [
{
name: "operator.token.value",
value: process.env.OP_CONNECT_TOKEN!,
},
{
name: "connect.credentials_base64",
value: btoa(
fs.readFileSync(
path.join(__dirname, "1password-credentials.json"),
"utf-8",
),
),
},
],
}); });
const cnpg = new Release(this, "cnpg-operator", { const cnpg = new Release(this, "cnpg-operator", {

View File

@@ -2,11 +2,10 @@ import * as fs from "fs";
import * as path from "path"; import * as path from "path";
import { Release } from "@cdktf/provider-helm/lib/release"; import { Release } from "@cdktf/provider-helm/lib/release";
import { Construct } from "constructs"; import { Construct } from "constructs";
import { IngressRoute } from "../../utils"; import { HelmProvider } from "@cdktf/provider-helm/lib/provider";
import { Providers } from "../../types";
type PrometheusOptions = { type PrometheusOptions = {
providers: Providers; provider: HelmProvider;
name: string; name: string;
namespace: string; namespace: string;
version: string; version: string;
@@ -16,22 +15,8 @@ export class Prometheus extends Construct {
constructor(scope: Construct, id: string, options: PrometheusOptions) { constructor(scope: Construct, id: string, options: PrometheusOptions) {
super(scope, id); super(scope, id);
const { helm, kubernetes } = options.providers;
new IngressRoute(this, "ingress", {
provider: kubernetes,
name: "grafana",
namespace: options.namespace,
entryPoints: ["websecure"],
serviceName: "prometheus-operator-grafana",
servicePort: 80,
tlsSecretName: "grafana-tls",
host: "grafana.dogar.dev",
});
new Release(this, id, { new Release(this, id, {
...options, ...options,
provider: helm,
repository: "https://prometheus-community.github.io/helm-charts", repository: "https://prometheus-community.github.io/helm-charts",
chart: "kube-prometheus-stack", chart: "kube-prometheus-stack",
createNamespace: true, createNamespace: true,

30
main.ts
View File

@@ -5,6 +5,11 @@ import { CacheInfrastructure } from "./cache-infrastructure";
import { UtilityServices } from "./utility-services"; import { UtilityServices } from "./utility-services";
import { K8SOperators } from "./k8s-operators"; import { K8SOperators } from "./k8s-operators";
import { CoreServices } from "./core-services"; import { CoreServices } from "./core-services";
import { NetworkSecurity } from "./network-security";
import { GamingServices } from "./gaming-services/minecraft";
import { MediaServices } from "./media-services";
import { PKI } from "./pki";
import { Netbird } from "./netbird";
dotenv.config(); dotenv.config();
@@ -13,6 +18,7 @@ const env = cleanEnv(process.env, {
OP_CONNECT_TOKEN: str({ desc: "1Password Connect token." }), OP_CONNECT_TOKEN: str({ desc: "1Password Connect token." }),
ACCESS_KEY: str({ desc: "Access key ID for R2 storage." }), ACCESS_KEY: str({ desc: "Access key ID for R2 storage." }),
SECRET_KEY: str({ desc: "Secret access key for R2 storage." }), SECRET_KEY: str({ desc: "Secret access key for R2 storage." }),
VALKEY_PASSWORD: str({ desc: "Password for Valkey database." }),
}); });
const r2Endpoint = `https://${env.ACCOUNT_ID}.r2.cloudflarestorage.com`; const r2Endpoint = `https://${env.ACCOUNT_ID}.r2.cloudflarestorage.com`;
@@ -23,12 +29,27 @@ const coreServices = new CoreServices(app, "core-services");
const k8sOperators = new K8SOperators(app, "k8s-operators"); const k8sOperators = new K8SOperators(app, "k8s-operators");
k8sOperators.node.addDependency(coreServices); k8sOperators.node.addDependency(coreServices);
const pki = new PKI(app, "pki");
pki.node.addDependency(k8sOperators);
const networkSecurity = new NetworkSecurity(app, "network-security");
networkSecurity.node.addDependency(pki);
const utilityServices = new UtilityServices(app, "utility-services"); const utilityServices = new UtilityServices(app, "utility-services");
utilityServices.node.addDependency(k8sOperators); utilityServices.node.addDependency(networkSecurity);
const gamingServices = new GamingServices(app, "gaming-services");
gamingServices.node.addDependency(networkSecurity);
const mediaServices = new MediaServices(app, "media-services");
mediaServices.node.addDependency(networkSecurity);
const caches = new CacheInfrastructure(app, "cache-infrastructure"); const caches = new CacheInfrastructure(app, "cache-infrastructure");
caches.node.addDependency(utilityServices); caches.node.addDependency(utilityServices);
const netbird = new Netbird(app, "netbird");
netbird.node.addDependency(utilityServices);
const deploy: (stack: TerraformStack, key: string) => S3Backend = ( const deploy: (stack: TerraformStack, key: string) => S3Backend = (
stack, stack,
key, key,
@@ -51,8 +72,13 @@ const deploy: (stack: TerraformStack, key: string) => S3Backend = (
}); });
deploy(coreServices, "core-services"); deploy(coreServices, "core-services");
deploy(utilityServices, "utility-services");
deploy(k8sOperators, "k8s-operators"); deploy(k8sOperators, "k8s-operators");
deploy(pki, "pki");
deploy(networkSecurity, "network-security");
deploy(utilityServices, "utility-services");
deploy(caches, "cache-infrastructure"); deploy(caches, "cache-infrastructure");
deploy(gamingServices, "gaming-services");
deploy(mediaServices, "media-services");
deploy(netbird, "netbird");
app.synth(); app.synth();

82
media-services/index.ts Normal file
View File

@@ -0,0 +1,82 @@
import { Construct } from "constructs";
import { TerraformStack } from "cdktf";
import { KubernetesProvider } from "@cdktf/provider-kubernetes/lib/provider";
import { NamespaceV1 } from "@cdktf/provider-kubernetes/lib/namespace-v1";
import { LonghornPvc } from "../utils";
import { JellyfinServer } from "./jellyfin";
import { SonarrServer } from "./sonarr";
import { RadarrServer } from "./radarr";
import { QBittorrentServer } from "./qbittorrent";
import { ProwlarrServer } from "./prowlarr";
export class MediaServices extends TerraformStack {
constructor(scope: Construct, id: string) {
super(scope, id);
const provider = new KubernetesProvider(this, "kubernetes", {
configPath: "~/.kube/config",
});
const namespace = "media";
// Create namespace
new NamespaceV1(this, "namespace", {
metadata: {
name: namespace,
},
});
// Shared PVCs
const mediaPvc = new LonghornPvc(this, "media-pvc", {
provider,
name: "media",
namespace,
size: "1Ti",
});
const downloadsPvc = new LonghornPvc(this, "downloads-pvc", {
provider,
name: "downloads",
namespace,
size: "450Gi",
});
// Deploy media services
new JellyfinServer(this, "jellyfin", {
provider,
namespace,
mediaPvcName: mediaPvc.name,
host: "media.dogar.dev",
});
new SonarrServer(this, "sonarr", {
provider,
namespace,
mediaPvcName: mediaPvc.name,
downloadsPvcName: downloadsPvc.name,
host: "sonarr.dogar.dev",
});
new RadarrServer(this, "radarr", {
provider,
namespace,
mediaPvcName: mediaPvc.name,
downloadsPvcName: downloadsPvc.name,
host: "radarr.dogar.dev",
});
new QBittorrentServer(this, "qbittorrent", {
provider,
namespace,
downloadsPvcName: downloadsPvc.name,
host: "torrent.dogar.dev",
});
new ProwlarrServer(this, "prowlarr", {
provider,
namespace,
host: "prowlarr.dogar.dev",
});
}
}

View File

@@ -0,0 +1,163 @@
import { Construct } from "constructs";
import { DeploymentV1 } from "@cdktf/provider-kubernetes/lib/deployment-v1";
import { ServiceV1 } from "@cdktf/provider-kubernetes/lib/service-v1";
import {
CloudflareCertificate,
InternalIngressRoute,
LonghornPvc,
} from "../../utils";
import { BaseMediaServiceOptions, getAamil3NodeSelector } from "../types";
type JellyfinServerOptions = BaseMediaServiceOptions & {
/** Name of the shared media PVC */
mediaPvcName: string;
/** Hostname for the ingress */
host: string;
};
export class JellyfinServer extends Construct {
constructor(scope: Construct, id: string, options: JellyfinServerOptions) {
super(scope, id);
const { provider, namespace, mediaPvcName, host } = options;
const name = "server";
// Config PVC with backup
const configPvc = new LonghornPvc(this, "config", {
provider,
name: "jellyfin-config",
namespace,
size: "5Gi",
backup: true,
});
// Service
new ServiceV1(this, "service", {
provider,
metadata: {
name,
namespace,
},
spec: {
selector: {
app: name,
},
port: [
{
name: "http",
port: 80,
targetPort: "http",
},
{
name: "discovery",
port: 7359,
targetPort: "discovery",
},
],
type: "LoadBalancer",
},
});
// Deployment
new DeploymentV1(this, "deployment", {
provider,
metadata: {
name,
namespace,
},
spec: {
replicas: "1",
selector: {
matchLabels: {
app: name,
},
},
template: {
metadata: {
labels: {
app: name,
},
},
spec: {
nodeSelector: getAamil3NodeSelector(),
container: [
{
name,
image: "jellyfin/jellyfin:latest",
imagePullPolicy: "IfNotPresent",
port: [
{
containerPort: 8096,
name: "http",
},
{
containerPort: 7359,
name: "discovery",
},
],
env: [
{
name: "TZ",
value: "Asia/Karachi",
},
],
volumeMount: [
{
name: "config",
mountPath: "/config",
},
{
name: "cache",
mountPath: "/cache",
},
{
name: "media",
mountPath: "/media",
},
],
},
],
volume: [
{
name: "config",
persistentVolumeClaim: {
claimName: configPvc.name,
},
},
{
name: "cache",
emptyDir: {},
},
{
name: "media",
persistentVolumeClaim: {
claimName: mediaPvcName,
},
},
],
},
},
},
});
new CloudflareCertificate(this, "certificate", {
provider,
namespace,
name,
secretName: "jellyfin-tls",
dnsNames: [host],
});
// Ingress - using internal ingress for secure access
new InternalIngressRoute(this, "ingress", {
provider,
namespace,
name,
host,
serviceName: name,
servicePort: 80,
tlsSecretName: "jellyfin-tls",
});
}
}

View File

@@ -0,0 +1,134 @@
import { Construct } from "constructs";
import { DeploymentV1 } from "@cdktf/provider-kubernetes/lib/deployment-v1";
import { ServiceV1 } from "@cdktf/provider-kubernetes/lib/service-v1";
import {
InternalIngressRoute,
LonghornPvc,
PrivateCertificate,
} from "../../utils";
import {
BaseMediaServiceOptions,
getWorkerNodeSelector,
getCommonEnv,
} from "../types";
type ProwlarrOptions = BaseMediaServiceOptions & {
/** Hostname for the ingress */
host: string;
};
export class ProwlarrServer extends Construct {
constructor(scope: Construct, id: string, options: ProwlarrOptions) {
super(scope, id);
const { provider, namespace, host } = options;
const name = "prowlarr";
// Config PVC with backup
const configPvc = new LonghornPvc(this, "config", {
provider,
name: "prowlarr-config",
namespace,
size: "512Mi",
backup: true,
});
// Service
new ServiceV1(this, "service", {
provider,
metadata: {
name,
namespace,
},
spec: {
selector: {
app: name,
},
port: [
{
name: "http",
port: 80,
targetPort: "9696",
},
],
type: "ClusterIP",
},
});
// Deployment
new DeploymentV1(this, "deployment", {
provider,
metadata: {
name,
namespace,
},
spec: {
replicas: "1",
selector: {
matchLabels: {
app: name,
},
},
template: {
metadata: {
labels: {
app: name,
},
},
spec: {
nodeSelector: getWorkerNodeSelector(),
container: [
{
name,
image: "lscr.io/linuxserver/prowlarr:latest",
imagePullPolicy: "IfNotPresent",
port: [
{
containerPort: 9696,
name: "http",
},
],
env: getCommonEnv(),
volumeMount: [
{
name: "config",
mountPath: "/config",
},
],
},
],
volume: [
{
name: "config",
persistentVolumeClaim: {
claimName: configPvc.name,
},
},
],
},
},
},
});
new PrivateCertificate(this, "certificate", {
provider,
namespace,
name,
commonName: host,
dnsNames: [host],
secretName: `${name}-tls`,
});
// Ingress
new InternalIngressRoute(this, "ingress", {
provider,
namespace,
name,
host,
serviceName: name,
servicePort: 80,
tlsSecretName: `${name}-tls`,
});
}
}

View File

@@ -0,0 +1,160 @@
import { Construct } from "constructs";
import { DeploymentV1 } from "@cdktf/provider-kubernetes/lib/deployment-v1";
import { ServiceV1 } from "@cdktf/provider-kubernetes/lib/service-v1";
import {
InternalIngressRoute,
LonghornPvc,
PrivateCertificate,
} from "../../utils";
import {
BaseMediaServiceOptions,
getAamil3NodeSelector,
getCommonEnv,
} from "../types";
type QBittorrentServerOptions = BaseMediaServiceOptions & {
/** Name of the shared downloads PVC */
downloadsPvcName: string;
/** Hostname for the ingress */
host: string;
};
export class QBittorrentServer extends Construct {
constructor(scope: Construct, id: string, options: QBittorrentServerOptions) {
super(scope, id);
const { provider, namespace, downloadsPvcName, host } = options;
const name = "qbittorrent";
// Config PVC with backup
const configPvc = new LonghornPvc(this, "config", {
provider,
name: "qbittorrent-config",
namespace,
size: "512Mi",
backup: true,
});
// Service
new ServiceV1(this, "service", {
provider,
metadata: {
name,
namespace,
},
spec: {
selector: {
app: name,
},
port: [
{
name: "http",
port: 80,
targetPort: "8080",
},
],
type: "ClusterIP",
},
});
// Deployment
new DeploymentV1(this, "deployment", {
provider,
metadata: {
name,
namespace,
},
spec: {
replicas: "1",
selector: {
matchLabels: {
app: name,
},
},
template: {
metadata: {
labels: {
app: name,
},
},
spec: {
nodeSelector: getAamil3NodeSelector(),
container: [
{
name,
image: "lscr.io/linuxserver/qbittorrent:latest",
port: [
{
containerPort: 8080,
name: "http",
},
{
containerPort: 6881,
name: "bt",
},
{
containerPort: 6881,
protocol: "UDP",
name: "bt-udp",
},
],
env: [
...getCommonEnv(),
{
name: "WEBUI_PORT",
value: "8080",
},
],
volumeMount: [
{
name: "config",
mountPath: "/config",
},
{
name: "downloads",
mountPath: "/downloads",
},
],
},
],
volume: [
{
name: "config",
persistentVolumeClaim: {
claimName: configPvc.name,
},
},
{
name: "downloads",
persistentVolumeClaim: {
claimName: downloadsPvcName,
},
},
],
},
},
},
});
new PrivateCertificate(this, "certificate", {
provider,
namespace,
name,
commonName: host,
dnsNames: [host],
secretName: `${name}-tls`,
});
// Ingress
new InternalIngressRoute(this, "ingress", {
provider,
namespace,
name,
host,
serviceName: name,
servicePort: 80,
tlsSecretName: `${name}-tls`,
});
}
}

View File

@@ -0,0 +1,159 @@
import { Construct } from "constructs";
import { DeploymentV1 } from "@cdktf/provider-kubernetes/lib/deployment-v1";
import { ServiceV1 } from "@cdktf/provider-kubernetes/lib/service-v1";
import {
InternalIngressRoute,
LonghornPvc,
PrivateCertificate,
} from "../../utils";
import {
BaseMediaServiceOptions,
getAamil3NodeSelector,
getCommonEnv,
} from "../types";
type RadarrServerOptions = BaseMediaServiceOptions & {
/** Name of the shared media PVC */
mediaPvcName: string;
/** Name of the shared downloads PVC */
downloadsPvcName: string;
/** Hostname for the ingress */
host: string;
};
export class RadarrServer extends Construct {
constructor(scope: Construct, id: string, options: RadarrServerOptions) {
super(scope, id);
const { provider, namespace, mediaPvcName, downloadsPvcName, host } =
options;
const name = "radarr";
// Config PVC with backup
const configPvc = new LonghornPvc(this, "config", {
provider,
name: "radarr-config",
namespace,
size: "512Mi",
backup: true,
});
// Service
new ServiceV1(this, "service", {
provider,
metadata: {
name,
namespace,
},
spec: {
selector: {
app: name,
},
port: [
{
name: "http",
port: 80,
targetPort: "7878",
},
],
type: "ClusterIP",
},
});
// Deployment
new DeploymentV1(this, "deployment", {
provider,
metadata: {
name,
namespace,
},
spec: {
replicas: "1",
selector: {
matchLabels: {
app: name,
},
},
template: {
metadata: {
labels: {
app: name,
},
},
spec: {
nodeSelector: getAamil3NodeSelector(),
container: [
{
name,
image: "lscr.io/linuxserver/radarr:latest",
imagePullPolicy: "IfNotPresent",
port: [
{
containerPort: 7878,
name: "http",
},
],
env: getCommonEnv(),
volumeMount: [
{
name: "config",
mountPath: "/config",
},
{
name: "media",
mountPath: "/media",
},
{
name: "downloads",
mountPath: "/downloads",
},
],
},
],
volume: [
{
name: "config",
persistentVolumeClaim: {
claimName: configPvc.name,
},
},
{
name: "media",
persistentVolumeClaim: {
claimName: mediaPvcName,
},
},
{
name: "downloads",
persistentVolumeClaim: {
claimName: downloadsPvcName,
},
},
],
},
},
},
});
new PrivateCertificate(this, "certificate", {
provider,
namespace,
name,
commonName: host,
dnsNames: [host],
secretName: `${name}-tls`,
});
// Ingress
new InternalIngressRoute(this, "ingress", {
provider,
namespace,
name,
host,
serviceName: name,
servicePort: 80,
tlsSecretName: `${name}-tls`,
});
}
}

View File

@@ -0,0 +1,159 @@
import { Construct } from "constructs";
import { DeploymentV1 } from "@cdktf/provider-kubernetes/lib/deployment-v1";
import { ServiceV1 } from "@cdktf/provider-kubernetes/lib/service-v1";
import {
InternalIngressRoute,
LonghornPvc,
PrivateCertificate,
} from "../../utils";
import {
BaseMediaServiceOptions,
getAamil3NodeSelector,
getCommonEnv,
} from "../types";
type SonarrServerOptions = BaseMediaServiceOptions & {
/** Name of the shared media PVC */
mediaPvcName: string;
/** Name of the shared downloads PVC */
downloadsPvcName: string;
/** Hostname for the ingress */
host: string;
};
export class SonarrServer extends Construct {
constructor(scope: Construct, id: string, options: SonarrServerOptions) {
super(scope, id);
const { provider, namespace, mediaPvcName, downloadsPvcName, host } =
options;
const name = "sonarr";
// Config PVC with backup
const configPvc = new LonghornPvc(this, "config", {
provider,
name: "sonarr-config",
namespace,
size: "512Mi",
backup: true,
});
// Service
new ServiceV1(this, "service", {
provider,
metadata: {
name,
namespace,
},
spec: {
selector: {
app: name,
},
port: [
{
name: "http",
port: 80,
targetPort: "8989",
},
],
type: "ClusterIP",
},
});
// Deployment
new DeploymentV1(this, "deployment", {
provider,
metadata: {
name,
namespace,
},
spec: {
replicas: "1",
selector: {
matchLabels: {
app: name,
},
},
template: {
metadata: {
labels: {
app: name,
},
},
spec: {
nodeSelector: getAamil3NodeSelector(),
container: [
{
name,
image: "lscr.io/linuxserver/sonarr:latest",
imagePullPolicy: "IfNotPresent",
port: [
{
containerPort: 8989,
name: "http",
},
],
env: getCommonEnv(),
volumeMount: [
{
name: "config",
mountPath: "/config",
},
{
name: "media",
mountPath: "/media",
},
{
name: "downloads",
mountPath: "/downloads",
},
],
},
],
volume: [
{
name: "config",
persistentVolumeClaim: {
claimName: configPvc.name,
},
},
{
name: "media",
persistentVolumeClaim: {
claimName: mediaPvcName,
},
},
{
name: "downloads",
persistentVolumeClaim: {
claimName: downloadsPvcName,
},
},
],
},
},
},
});
new PrivateCertificate(this, "certificate", {
provider,
namespace,
name,
commonName: host,
dnsNames: [host],
secretName: `${name}-tls`,
});
// Ingress
new InternalIngressRoute(this, "ingress", {
provider,
namespace,
name,
host,
serviceName: name,
servicePort: 80,
tlsSecretName: `${name}-tls`,
});
}
}

32
media-services/types.ts Normal file
View File

@@ -0,0 +1,32 @@
import { KubernetesProvider } from "@cdktf/provider-kubernetes/lib/provider";
/**
* Common options shared across all media service constructs
*/
export type BaseMediaServiceOptions = {
provider: KubernetesProvider;
namespace: string;
};
/**
* Common environment variables for LinuxServer.io containers
*/
export const getCommonEnv = () => [
{ name: "TZ", value: "Asia/Karachi" },
{ name: "PUID", value: "1000" },
{ name: "PGID", value: "1000" },
];
/**
* Node selector for the aamil-3 node
*/
export const getAamil3NodeSelector = () => ({
"kubernetes.io/hostname": "aamil-3",
});
/**
* Node selector for worker nodepool
*/
export const getWorkerNodeSelector = () => ({
nodepool: "worker",
});

View File

@@ -1,231 +0,0 @@
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: server
namespace: media
spec:
replicas: 1
selector:
matchLabels:
app: server
template:
metadata:
labels:
app: server
spec:
nodeSelector:
kubernetes.io/hostname: aamil-3
containers:
- name: server
image: jellyfin/jellyfin:latest
imagePullPolicy: IfNotPresent
ports:
- containerPort: 8096
name: http
env:
- name: TZ
value: "Asia/Karachi"
volumeMounts:
- name: config
mountPath: /config
- name: cache
mountPath: /cache
- name: media
mountPath: /media
volumes:
- name: config
persistentVolumeClaim:
claimName: jellyfin-config
- name: cache
emptyDir: {}
- name: media
persistentVolumeClaim:
claimName: media
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: sonarr
namespace: media
spec:
replicas: 1
selector:
matchLabels:
app: sonarr
template:
metadata:
labels:
app: sonarr
spec:
nodeSelector:
kubernetes.io/hostname: aamil-3
containers:
- name: sonarr
image: lscr.io/linuxserver/sonarr:latest
imagePullPolicy: IfNotPresent
ports:
- containerPort: 8989
name: http
env:
- name: TZ
value: "Asia/Karachi"
- name: PUID
value: "1000"
- name: PGID
value: "1000"
volumeMounts:
- name: config
mountPath: /config
- name: media
mountPath: /media
- name: downloads
mountPath: /downloads
volumes:
- name: config
persistentVolumeClaim:
claimName: sonarr-config
- name: media
persistentVolumeClaim:
claimName: media
- name: downloads
persistentVolumeClaim:
claimName: downloads
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: qbittorrent
namespace: media
spec:
replicas: 1
selector:
matchLabels:
app: qbittorrent
template:
metadata:
labels:
app: qbittorrent
spec:
nodeSelector:
kubernetes.io/hostname: aamil-3
containers:
- name: qbittorrent
image: lscr.io/linuxserver/qbittorrent:latest
ports:
- containerPort: 8080 # web UI
name: http
- containerPort: 6881
name: bt
- containerPort: 6881
protocol: UDP
name: bt-udp
env:
- name: TZ
value: "Asia/Karachi"
- name: PUID
value: "1000"
- name: PGID
value: "1000"
- name: WEBUI_PORT
value: "8080"
volumeMounts:
- name: config
mountPath: /config
- name: downloads
mountPath: /downloads
volumes:
- name: config
persistentVolumeClaim:
claimName: qbittorrent-config
- name: downloads
persistentVolumeClaim:
claimName: downloads
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: prowlarr
namespace: media
spec:
replicas: 1
selector:
matchLabels:
app: prowlarr
template:
metadata:
labels:
app: prowlarr
spec:
nodeSelector:
nodepool: worker
containers:
- name: prowlarr
image: lscr.io/linuxserver/prowlarr:latest
imagePullPolicy: IfNotPresent
ports:
- containerPort: 9696
name: http
env:
- name: TZ
value: "Asia/Karachi"
- name: PUID
value: "1000"
- name: PGID
value: "1000"
volumeMounts:
- name: config
mountPath: /config
volumes:
- name: config
persistentVolumeClaim:
claimName: prowlarr-config
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: radarr
namespace: media
spec:
replicas: 1
selector:
matchLabels:
app: radarr
template:
metadata:
labels:
app: radarr
spec:
nodeSelector:
kubernetes.io/hostname: aamil-3
containers:
- name: radarr
image: lscr.io/linuxserver/radarr:latest
imagePullPolicy: IfNotPresent
ports:
- containerPort: 7878
name: http
env:
- name: TZ
value: "Asia/Karachi"
- name: PUID
value: "1000"
- name: PGID
value: "1000"
volumeMounts:
- name: config
mountPath: /config
- name: media
mountPath: /media
- name: downloads
mountPath: /downloads
volumes:
- name: config
persistentVolumeClaim:
claimName: radarr-config
- name: media
persistentVolumeClaim:
claimName: media
- name: downloads
persistentVolumeClaim:
claimName: downloads

View File

@@ -1,76 +0,0 @@
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: server
namespace: media
annotations:
# cert-manager (Cloudflare DNS-01)
cert-manager.io/cluster-issuer: cloudflare-issuer
cert-manager.io/acme-challenge-type: dns01
cert-manager.io/private-key-size: "4096"
# Jellyfin / streaming friendly nginx settings
nginx.ingress.kubernetes.io/proxy-body-size: "0"
nginx.ingress.kubernetes.io/proxy-buffering: "off"
nginx.ingress.kubernetes.io/proxy-read-timeout: "3600"
nginx.ingress.kubernetes.io/proxy-send-timeout: "3600"
nginx.ingress.kubernetes.io/proxy-http-version: "1.1"
nginx.ingress.kubernetes.io/use-proxy-protocol: "false"
nginx.ingress.kubernetes.io/proxy-request-buffering: "off"
spec:
ingressClassName: nginx-internal
tls:
- hosts:
- media.dogar.dev
secretName: media-tls
- hosts:
- sonarr.dogar.dev
secretName: sonarr-tls
- hosts:
- radarr.dogar.dev
secretName: radarr-tls
- hosts:
- torrent.dogar.dev
secretName: torrent-tls
rules:
- host: media.dogar.dev
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: server
port:
number: 80
- host: sonarr.dogar.dev
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: sonarr
port:
number: 80
- host: radarr.dogar.dev
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: radarr
port:
number: 80
- host: torrent.dogar.dev
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: qbittorrent
port:
number: 80

View File

@@ -1,5 +0,0 @@
---
apiVersion: v1
kind: Namespace
metadata:
name: media

View File

@@ -1,106 +0,0 @@
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: jellyfin-config
namespace: media
labels:
recurring-job.longhorn.io/source: "enabled"
recurring-job.longhorn.io/daily-backup: "enabled"
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 5Gi
storageClassName: longhorn
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: media
namespace: media
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 1Ti
storageClassName: longhorn
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: sonarr-config
namespace: media
labels:
recurring-job.longhorn.io/source: "enabled"
recurring-job.longhorn.io/daily-backup: "enabled"
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 512Mi
storageClassName: longhorn
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: qbittorrent-config
namespace: media
labels:
recurring-job.longhorn.io/source: "enabled"
recurring-job.longhorn.io/daily-backup: "enabled"
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 512Mi
storageClassName: longhorn
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: downloads
namespace: media
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 450Gi
storageClassName: longhorn
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: prowlarr-config
namespace: media
labels:
recurring-job.longhorn.io/source: "enabled"
recurring-job.longhorn.io/daily-backup: "enabled"
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 512Mi
storageClassName: longhorn
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: radarr-config
namespace: media
labels:
recurring-job.longhorn.io/source: "enabled"
recurring-job.longhorn.io/daily-backup: "enabled"
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 512Mi
storageClassName: longhorn

View File

@@ -1,69 +0,0 @@
---
apiVersion: v1
kind: Service
metadata:
name: server
namespace: media
spec:
selector:
app: server
ports:
- name: http
port: 80
targetPort: 8096
type: ClusterIP
---
apiVersion: v1
kind: Service
metadata:
name: sonarr
namespace: media
spec:
selector:
app: sonarr
ports:
- name: http
port: 80
targetPort: 8989
type: ClusterIP
---
apiVersion: v1
kind: Service
metadata:
name: qbittorrent
namespace: media
spec:
selector:
app: qbittorrent
ports:
- name: http
port: 80
targetPort: 8080
type: ClusterIP
---
apiVersion: v1
kind: Service
metadata:
name: prowlarr
namespace: media
spec:
selector:
app: prowlarr
ports:
- name: http
port: 80
targetPort: 9696
type: ClusterIP
---
apiVersion: v1
kind: Service
metadata:
name: radarr
namespace: media
spec:
selector:
app: radarr
ports:
- name: http
port: 80
targetPort: 7878

View File

@@ -1,5 +0,0 @@
---
apiVersion: v1
kind: Namespace
metadata:
name: minecraft

View File

@@ -1,45 +1,6 @@
--- ---
apiVersion: v1 apiVersion: v1
kind: PersistentVolumeClaim kind: PersistentVolumeClaim
metadata:
name: monifactory-data
namespace: minecraft
spec:
storageClassName: longhorn
accessModes:
- ReadWriteMany
resources:
requests:
storage: 10Gi
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: gtnh-data
namespace: minecraft
spec:
storageClassName: longhorn
accessModes:
- ReadWriteMany
resources:
requests:
storage: 10Gi
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: tfg-data
namespace: minecraft
spec:
storageClassName: longhorn
accessModes:
- ReadWriteMany
resources:
requests:
storage: 10Gi
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata: metadata:
name: atm10-data name: atm10-data
namespace: minecraft namespace: minecraft
@@ -53,19 +14,3 @@ spec:
resources: resources:
requests: requests:
storage: 10Gi storage: 10Gi
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: star-technology-data
namespace: minecraft
labels:
recurring-job.longhorn.io/source: "enabled"
recurring-job.longhorn.io/daily-backup: "enabled"
spec:
storageClassName: longhorn
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 10Gi

View File

@@ -1,75 +1,15 @@
--- ---
apiVersion: v1 apiVersion: v1
kind: Service kind: Service
metadata:
name: monifactory-server
namespace: minecraft
labels:
app: monifactory-server
spec:
type: ClusterIP
ports:
- name: monifactory
port: 25565
selector:
app: monifactory-server
---
apiVersion: v1
kind: Service
metadata:
name: gtnh-server
namespace: minecraft
labels:
app: gtnh-server
spec:
type: ClusterIP
ports:
- name: gtnh
port: 25565
selector:
app: gtnh-server
---
apiVersion: v1
kind: Service
metadata:
name: tfg-server
namespace: minecraft
labels:
app: tfg-server
spec:
type: ClusterIP
ports:
- name: tfg
port: 25565
selector:
app: tfg-server
---
apiVersion: v1
kind: Service
metadata: metadata:
name: atm10-server name: atm10-server
namespace: minecraft namespace: minecraft
labels: labels:
app: atm10-server app: atm10-server
spec: spec:
type: ClusterIP type: LoadBalancer
ports: ports:
- name: atm10 - name: atm10
port: 25565 port: 25565
selector: selector:
app: atm10-server app: atm10-server
---
apiVersion: v1
kind: Service
metadata:
name: star-technology-server
namespace: minecraft
labels:
app: star-technology-server
spec:
type: ClusterIP
ports:
- name: star-technology
port: 25565
selector:
app: star-technology-server

View File

@@ -1,200 +1,6 @@
--- ---
apiVersion: apps/v1 apiVersion: apps/v1
kind: StatefulSet kind: StatefulSet
metadata:
name: monifactory-server
namespace: minecraft
spec:
serviceName: monifactory-server
selector:
matchLabels:
app: monifactory-server
template:
metadata:
labels:
app: monifactory-server
spec:
nodeSelector:
nodepool: worker
containers:
- name: monifactory-server
image: itzg/minecraft-server:java17
env:
- name: EULA
value: "TRUE"
- name: MODE
value: "survival"
- name: DIFFICULTY
value: "peaceful"
- name: MODPACK_PLATFORM
value: "AUTO_CURSEFORGE"
- name: CF_API_KEY
valueFrom:
secretKeyRef:
name: curseforge
key: credential
- name: CF_PAGE_URL
value: "https://www.curseforge.com/minecraft/modpacks/monifactory/"
- name: VERSION
value: "1.20.1"
- name: INIT_MEMORY
value: 4G
- name: MAX_MEMORY
value: 12G
- name: ALLOW_FLIGHT
value: "TRUE"
- name: ENABLE_ROLLING_LOGS
value: "TRUE"
- name: USE_MEOWICE_FLAGS
value: "TRUE"
ports:
- name: minecraft
containerPort: 25565
resources:
requests:
cpu: 4
memory: "4Gi"
limits:
cpu: 8
memory: "12Gi"
volumeMounts:
- name: monifactory-data
mountPath: /data
volumes:
- name: monifactory-data
persistentVolumeClaim:
claimName: monifactory-data
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: gtnh-server
namespace: minecraft
spec:
serviceName: gtnh-server
selector:
matchLabels:
app: gtnh-server
template:
metadata:
labels:
app: gtnh-server
spec:
nodeSelector:
nodepool: worker
containers:
- name: gtnh-server
image: itzg/minecraft-server:java25
env:
- name: EULA
value: "TRUE"
- name: MODE
value: "survival"
- name: DIFFICULTY
value: "peaceful"
- name: TYPE
value: "CUSTOM"
- name: GENERIC_PACKS
value: "GT_New_Horizons_2.8.0_Server_Java_17-25"
- name: GENERIC_PACKS_SUFFIX
value: ".zip"
- name: GENERIC_PACKS_PREFIX
value: "https://downloads.gtnewhorizons.com/ServerPacks/"
- name: SKIP_GENERIC_PACK_UPDATE_CHECK
value: "true"
- name: MEMORY
value: 12G
- name: JVM_OPTS
value: "-Dfml.readTimeout=180 -Dfml.queryResult=confirm @java9args.txt"
- name: CUSTOM_JAR_EXEC
value: "-jar lwjgl3ify-forgePatches.jar nogui"
- name: ALLOW_FLIGHT
value: "TRUE"
- name: ENABLE_ROLLING_LOGS
value: "TRUE"
ports:
- name: gtnh
containerPort: 25565
resources:
limits:
cpu: 8
memory: "12Gi"
volumeMounts:
- name: gtnh-data
mountPath: /data
volumes:
- name: gtnh-data
persistentVolumeClaim:
claimName: gtnh-data
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: tfg-server
namespace: minecraft
spec:
serviceName: tfg-server
selector:
matchLabels:
app: tfg-server
template:
metadata:
labels:
app: tfg-server
spec:
nodeSelector:
nodepool: worker
containers:
- name: tfg-server
image: itzg/minecraft-server:java17
env:
- name: EULA
value: "TRUE"
- name: MODE
value: "survival"
- name: MODPACK_PLATFORM
value: "AUTO_CURSEFORGE"
- name: CF_API_KEY
valueFrom:
secretKeyRef:
name: curseforge
key: credential
- name: CF_PAGE_URL
value: "https://www.curseforge.com/minecraft/modpacks/terrafirmagreg-modern/"
- name: CF_FILENAME_MATCHER
value: "0.10.17"
- name: VERSION
value: "1.20.1"
- name: INIT_MEMORY
value: 2G
- name: MAX_MEMORY
value: 12G
- name: ALLOW_FLIGHT
value: "TRUE"
- name: ENABLE_ROLLING_LOGS
value: "TRUE"
- name: USE_MEOWICE_FLAGS
value: "TRUE"
ports:
- name: minecraft
containerPort: 25565
resources:
requests:
cpu: 2
memory: "2Gi"
limits:
cpu: 6
memory: "12Gi"
volumeMounts:
- name: tfg-data
mountPath: /data
volumes:
- name: tfg-data
persistentVolumeClaim:
claimName: tfg-data
---
apiVersion: apps/v1
kind: StatefulSet
metadata: metadata:
name: atm10-server name: atm10-server
namespace: minecraft namespace: minecraft
@@ -260,71 +66,3 @@ spec:
- name: atm10-data - name: atm10-data
persistentVolumeClaim: persistentVolumeClaim:
claimName: atm10-data claimName: atm10-data
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: star-technology-server
namespace: minecraft
spec:
serviceName: star-technology-server
selector:
matchLabels:
app: star-technology-server
template:
metadata:
labels:
app: star-technology-server
spec:
nodeSelector:
nodepool: worker
containers:
- name: star-technology-server
image: itzg/minecraft-server:java21
env:
- name: EULA
value: "TRUE"
- name: MODE
value: "survival"
- name: MODPACK_PLATFORM
value: "AUTO_CURSEFORGE"
- name: CF_API_KEY
valueFrom:
secretKeyRef:
name: curseforge
key: credential
- name: CF_PAGE_URL
value: "https://www.curseforge.com/minecraft/modpacks/star-technology"
- name: VERSION
value: "1.20.1"
- name: INIT_MEMORY
value: 2G
- name: MAX_MEMORY
value: 15G
- name: ALLOW_FLIGHT
value: "TRUE"
- name: ENABLE_ROLLING_LOGS
value: "TRUE"
- name: USE_MEOWICE_FLAGS
value: "TRUE"
- name: CF_OVERRIDES_EXCLUSIONS
value: |
# Not applicable for server side
shaderpacks/**
ports:
- name: minecraft
containerPort: 25565
resources:
requests:
cpu: 2
memory: "2Gi"
limits:
cpu: 6
memory: "16Gi"
volumeMounts:
- name: star-technology-data
mountPath: /data
volumes:
- name: star-technology-data
persistentVolumeClaim:
claimName: star-technology-data

95
netbird/index.ts Normal file
View File

@@ -0,0 +1,95 @@
import * as fs from "fs";
import * as path from "path";
import { Construct } from "constructs";
import { TerraformStack } from "cdktf";
import { KubernetesProvider } from "@cdktf/provider-kubernetes/lib/provider";
import { NamespaceV1 } from "@cdktf/provider-kubernetes/lib/namespace-v1";
import { DataKubernetesSecretV1 } from "@cdktf/provider-kubernetes/lib/data-kubernetes-secret-v1";
import { HelmProvider } from "@cdktf/provider-helm/lib/provider";
import { SecretV1 } from "@cdktf/provider-kubernetes/lib/secret-v1";
import { Release } from "@cdktf/provider-helm/lib/release";
import { CloudflareCertificate, OnePasswordSecret } from "../utils";
export class Netbird extends TerraformStack {
constructor(scope: Construct, id: string) {
super(scope, id);
const kubernetes = new KubernetesProvider(this, "kubernetes", {
configPath: "~/.kube/config",
});
const helm = new HelmProvider(this, "helm", {
kubernetes: {
configPath: "~/.kube/config",
},
});
const namespace = "netbird";
// Create namespace
new NamespaceV1(this, "namespace", {
metadata: {
name: namespace,
},
});
new OnePasswordSecret(this, "netbird-secret", {
name: "netbird",
namespace,
provider: kubernetes,
itemPath: "vaults/Lab/items/Netbird",
});
const pgClientCert = new DataKubernetesSecretV1(
this,
"netbird-client-cert",
{
provider: kubernetes,
metadata: {
name: "netbird-client-cert",
namespace: "homelab",
},
},
);
const pgCaCert = new DataKubernetesSecretV1(this, "postgres-ca-cert", {
provider: kubernetes,
metadata: {
name: "postgres-server-cert",
namespace: "homelab",
},
});
const pgSslBundle = new SecretV1(this, "netbird-postgres-ssl", {
provider: kubernetes,
metadata: {
name: "netbird-postgres-ssl-bundle",
namespace,
},
data: {
"tls.crt": pgClientCert.data.lookup("tls.crt"),
"tls.key": pgClientCert.data.lookup("tls.key"),
"ca.crt": pgCaCert.data.lookup("ca.crt"),
},
});
new CloudflareCertificate(this, "netbird-cloudflare-cert", {
provider: kubernetes,
name: "netbird",
namespace,
dnsNames: ["vpn.dogar.dev"],
secretName: "netbird-tls",
});
new Release(this, "netbird", {
dependsOn: [pgSslBundle],
provider: helm,
namespace,
createNamespace: true,
name: "netbird",
repository: "https://netbirdio.github.io/helms",
chart: "netbird",
values: [fs.readFileSync(path.join(__dirname, "values.yaml"), "utf8")],
}).importFrom("netbird/netbird");
}
}

218
netbird/values.yaml Normal file
View File

@@ -0,0 +1,218 @@
fullnameOverride: netbird
management:
configmap: |-
{
"Stuns": [
{
"Proto": "udp",
"URI": "{{ .STUN_SERVER }}",
"Username": "",
"Password": ""
}
],
"TURNConfig": {
"TimeBasedCredentials": false,
"CredentialsTTL": "12h0m0s",
"Secret": "secret",
"Turns": [
{
"Proto": "udp",
"URI": "{{ .TURN_SERVER }}",
"Username": "{{ .TURN_SERVER_USER }}",
"Password": "{{ .TURN_SERVER_PASSWORD }}"
}
]
},
"Relay": {
"Addresses": ["rels://vpn.dogar.dev:443/relay"],
"CredentialsTTL": "24h",
"Secret": "{{ .RELAY_PASSWORD }}"
},
"Signal": {
"Proto": "https",
"URI": "vpn.dogar.dev:443",
"Username": "",
"Password": ""
},
"Datadir": "/var/lib/netbird/",
"DataStoreEncryptionKey": "{{ .DATASTORE_ENCRYPTION_KEY }}",
"HttpConfig": {
"LetsEncryptDomain": "",
"CertFile": "",
"CertKey": "",
"AuthAudience": "{{ .IDP_CLIENT_ID }}",
"AuthIssuer": "https://auth.dogar.dev/application/o/netbird/",
"AuthUserIDClaim": "",
"AuthKeysLocation": "https://auth.dogar.dev/application/o/netbird/jwks/",
"OIDCConfigEndpoint": "https://auth.dogar.dev/application/o/netbird/.well-known/openid-configuration",
"IdpSignKeyRefreshEnabled": false
},
"IdpManagerConfig": {
"ManagerType": "authentik",
"ClientConfig": {
"Issuer": "https://auth.dogar.dev/application/o/netbird",
"TokenEndpoint": "https://auth.dogar.dev/application/o/token/",
"ClientID": "{{ .IDP_CLIENT_ID }}",
"ClientSecret": "",
"GrantType": "client_credentials"
},
"ExtraConfig": {
"Password": "{{ .IDP_SERVICE_ACCOUNT_PASSWORD }}",
"Username": "{{ .IDP_SERVICE_ACCOUNT_USER }}"
},
"Auth0ClientCredentials": null,
"AzureClientCredentials": null,
"KeycloakClientCredentials": null,
"ZitadelClientCredentials": null
},
"DeviceAuthorizationFlow": {
"Provider": "hosted",
"ProviderConfig": {
"ClientID": "{{ .IDP_CLIENT_ID }}",
"ClientSecret": "",
"Domain": "auth.dogar.dev",
"Audience": "{{ .IDP_CLIENT_ID }}",
"TokenEndpoint": "https://auth.dogar.dev/application/o/token/",
"DeviceAuthEndpoint": "https://auth.dogar.dev/application/o/device/",
"AuthorizationEndpoint": "",
"Scope": "openid",
"UseIDToken": false,
"RedirectURLs": null
}
},
"PKCEAuthorizationFlow": {
"ProviderConfig": {
"ClientID": "{{ .IDP_CLIENT_ID }}",
"ClientSecret": "",
"Domain": "",
"Audience": "{{ .IDP_CLIENT_ID }}",
"TokenEndpoint": "https://auth.dogar.dev/application/o/token/",
"DeviceAuthEndpoint": "",
"AuthorizationEndpoint": "https://auth.dogar.dev/application/o/authorize/",
"Scope": "openid profile email offline_access api",
"UseIDToken": false,
"RedirectURLs": ["http://localhost:53000"]
}
},
"StoreConfig": {
"Engine": "postgres"
},
"ReverseProxy": {
"TrustedHTTPProxies": null,
"TrustedHTTPProxiesCount": 0,
"TrustedPeers": null
}
}
persistentVolume:
enabled: true
storageClass: longhorn
size: 1Gi
envFromSecret:
NETBIRD_STORE_ENGINE_POSTGRES_DSN: netbird/postgresDSN
STUN_SERVER: netbird/stunServer
TURN_SERVER: netbird/turnServer
TURN_SERVER_USER: netbird/turnServerUser
TURN_SERVER_PASSWORD: netbird/turnServerPassword
RELAY_PASSWORD: netbird/relayPassword
IDP_CLIENT_ID: netbird/idpClientID
IDP_SERVICE_ACCOUNT_USER: netbird/idpServiceAccountUser
IDP_SERVICE_ACCOUNT_PASSWORD: netbird/idpServiceAccountPassword
DATASTORE_ENCRYPTION_KEY: netbird/datastoreEncryptionKey
livenessProbe:
failureThreshold: 3
initialDelaySeconds: 180
periodSeconds: 10
timeoutSeconds: 3
tcpSocket:
port: http
volumes:
- name: postgres-ssl-bundle
secret:
secretName: netbird-postgres-ssl-bundle
volumeMounts:
- name: postgres-ssl-bundle
mountPath: /etc/ssl/certs/postgres-ssl-bundle
readOnly: true
signal:
enabled: true
relay:
envFromSecret:
NB_AUTH_SECRET: netbird/relayPassword
env:
NB_LOG_LEVEL: info
NB_LISTEN_ADDRESS: ":33080"
NB_EXPOSED_ADDRESS: rels://vpn.dogar.dev:443/relay
dashboard:
enabled: true
env:
# Endpoints
NETBIRD_MGMT_API_ENDPOINT: https://vpn.dogar.dev:443
NETBIRD_MGMT_GRPC_API_ENDPOINT: https://vpn.dogar.dev:443
# OIDC
AUTH_CLIENT_SECRET:
AUTH_AUTHORITY: https://auth.dogar.dev/application/o/netbird/
USE_AUTH0: false
AUTH_SUPPORTED_SCOPES: openid profile email offline_access api
AUTH_REDIRECT_URI:
AUTH_SILENT_REDIRECT_URI:
NETBIRD_TOKEN_SOURCE: accessToken
NGINX_SSL_PORT:
LETSENCRYPT_DOMAIN:
LETSENCRYPT_EMAIL:
envFromSecret:
AUTH_CLIENT_ID: netbird/idpClientID
AUTH_AUDIENCE: netbird/idpClientID
extraManifests:
- apiVersion: traefik.io/v1alpha1
kind: IngressRoute
metadata:
name: netbird
namespace: netbird
spec:
entryPoints:
- websecure
routes:
- kind: Rule
match: Host(`vpn.dogar.dev`) && !PathPrefix(`/api`) && !PathPrefix(`/management`) && !PathPrefix(`/signalexchange`) && !PathPrefix(`/relay`)
services:
- name: netbird-dashboard
namespace: netbird
passHostHeader: true
port: 80
- kind: Rule
match: Host(`vpn.dogar.dev`) && PathPrefix(`/api`)
services:
- name: netbird-management
namespace: netbird
passHostHeader: true
port: 80
- kind: Rule
match: Host(`vpn.dogar.dev`) && PathPrefix(`/relay`)
services:
- name: netbird-relay
namespace: netbird
passHostHeader: true
port: 33080
- kind: Rule
match: Host(`vpn.dogar.dev`) && PathPrefix(`/management`)
services:
- name: netbird-management
namespace: netbird
passHostHeader: true
port: 80
scheme: h2c
- kind: Rule
match: Host(`vpn.dogar.dev`) && PathPrefix(`/signalexchange`)
services:
- name: netbird-signal
namespace: netbird
passHostHeader: true
port: 80
scheme: h2c
tls:
secretName: netbird-tls

128
network-security/index.ts Normal file
View File

@@ -0,0 +1,128 @@
import { DataKubernetesNamespaceV1 } from "@cdktf/provider-kubernetes/lib/data-kubernetes-namespace-v1";
import { KubernetesProvider } from "@cdktf/provider-kubernetes/lib/provider";
import { DataTerraformRemoteStateS3, TerraformStack } from "cdktf";
import { Construct } from "constructs";
import {
RateLimitMiddleware,
IpAllowListMiddleware,
IpAllowListMiddlewareTCP,
TLSOptions,
} from "./traefik";
import { ValkeyCluster } from "./valkey";
import { InternalIngressRoute, PrivateCertificate } from "../utils";
export class NetworkSecurity extends TerraformStack {
constructor(scope: Construct, id: string) {
super(scope, id);
const kubernetes = new KubernetesProvider(this, "kubernetes", {
configPath: "~/.kube/config",
});
const r2Endpoint = `${process.env.ACCOUNT_ID!}.r2.cloudflarestorage.com`;
const coreServicesState = new DataTerraformRemoteStateS3(
this,
"core-services-state",
{
usePathStyle: true,
skipRegionValidation: true,
skipCredentialsValidation: true,
skipRequestingAccountId: true,
skipS3Checksum: true,
encrypt: true,
bucket: "terraform-state",
key: "core-services/terraform.tfstate",
endpoints: {
s3: `https://${r2Endpoint}`,
},
region: "auto",
accessKey: process.env.ACCESS_KEY,
secretKey: process.env.SECRET_KEY,
},
);
const namespaceName = coreServicesState.getString("namespace-output");
const namespaceResource = new DataKubernetesNamespaceV1(
this,
"homelab-namespace",
{
provider: kubernetes,
metadata: {
name: namespaceName,
},
},
);
const namespace = namespaceResource.metadata.name;
new ValkeyCluster(this, "valkey-cluster", {
provider: kubernetes,
name: "valkey",
namespace,
});
new RateLimitMiddleware(this, "rate-limit", {
provider: kubernetes,
namespace,
name: "rate-limit",
});
new TLSOptions(this, "tls-options", {
provider: kubernetes,
namespace,
});
new IpAllowListMiddleware(this, "internal-ip-allow-list", {
provider: kubernetes,
namespace,
name: "ip-allow-list",
sourceRanges: ["192.168.18.0/24", "10.42.0.0/16"],
});
new IpAllowListMiddlewareTCP(this, "tcp-internal-ip-allow-list", {
provider: kubernetes,
namespace,
name: "tcp-ip-allow-list",
sourceRanges: ["192.168.18.0/24", "10.42.0.0/16"],
});
new PrivateCertificate(this, "longhorn-cert", {
provider: kubernetes,
namespace: "longhorn-system",
name: "longhorn-ui",
dnsNames: ["longhorn.dogar.dev"],
commonName: "longhorn.dogar.dev",
secretName: "longhorn-tls",
});
new InternalIngressRoute(this, "longhorn-ui", {
provider: kubernetes,
namespace: "longhorn-system",
name: "longhorn-ui",
host: "longhorn.dogar.dev",
serviceName: "longhorn-frontend",
servicePort: 80,
tlsSecretName: "longhorn-tls",
});
new PrivateCertificate(this, "grafana-cert", {
provider: kubernetes,
namespace: "monitoring",
name: "grafana-ui",
dnsNames: ["grafana.dogar.dev"],
commonName: "grafana.dogar.dev",
secretName: "grafana-tls",
});
new InternalIngressRoute(this, "grafana-ui", {
provider: kubernetes,
namespace: "monitoring",
name: "grafana-ui",
host: "grafana.dogar.dev",
serviceName: "prometheus-operator-grafana",
servicePort: 80,
tlsSecretName: "grafana-tls",
});
}
}

View File

@@ -0,0 +1,3 @@
export { RateLimitMiddleware } from "./rateLimit";
export { IpAllowListMiddleware, IpAllowListMiddlewareTCP } from "./ipAllowList";
export { TLSOptions } from "./tlsOpts";

View File

@@ -0,0 +1,64 @@
import { Construct } from "constructs";
import { Manifest } from "@cdktf/provider-kubernetes/lib/manifest";
import { KubernetesProvider } from "@cdktf/provider-kubernetes/lib/provider";
type IpAllowListMiddlewareOptions = {
provider: KubernetesProvider;
namespace: string;
name: string;
sourceRanges: string[];
};
export class IpAllowListMiddleware extends Construct {
constructor(
scope: Construct,
id: string,
opts: IpAllowListMiddlewareOptions,
) {
super(scope, id);
new Manifest(this, opts.name, {
provider: opts.provider,
manifest: {
apiVersion: "traefik.io/v1alpha1",
kind: "Middleware",
metadata: {
name: opts.name,
namespace: opts.namespace,
},
spec: {
ipAllowList: {
sourceRange: opts.sourceRanges,
},
},
},
});
}
}
export class IpAllowListMiddlewareTCP extends Construct {
constructor(
scope: Construct,
id: string,
opts: IpAllowListMiddlewareOptions,
) {
super(scope, id);
new Manifest(this, opts.name, {
provider: opts.provider,
manifest: {
apiVersion: "traefik.io/v1alpha1",
kind: "MiddlewareTCP",
metadata: {
name: opts.name,
namespace: opts.namespace,
},
spec: {
ipAllowList: {
sourceRange: opts.sourceRanges,
},
},
},
});
}
}

View File

@@ -0,0 +1,51 @@
import { Construct } from "constructs";
import { Manifest } from "@cdktf/provider-kubernetes/lib/manifest";
import { KubernetesProvider } from "@cdktf/provider-kubernetes/lib/provider";
type RateLimitMiddlewareOptions = {
provider: KubernetesProvider;
namespace: string;
name: string;
average?: number; // default 60
burst?: number; // default 120
period?: string; // default "1s"
};
export class RateLimitMiddleware extends Construct {
public readonly ref: string;
constructor(scope: Construct, id: string, opts: RateLimitMiddlewareOptions) {
super(scope, id);
const average = opts.average ?? 60;
const burst = opts.burst ?? 120;
const period = opts.period ?? "1s";
this.ref = `${opts.namespace}/${opts.name}`;
new Manifest(this, opts.name, {
provider: opts.provider,
manifest: {
apiVersion: "traefik.io/v1alpha1",
kind: "Middleware",
metadata: {
name: opts.name,
namespace: opts.namespace,
},
spec: {
rateLimit: {
average,
burst,
period,
redis: {
endpoints: [`valkey.${opts.namespace}.svc.cluster.local:6379`],
secret: "valkey",
db: 5,
},
},
},
},
});
}
}

View File

@@ -0,0 +1,31 @@
import { Construct } from "constructs";
import { Manifest } from "@cdktf/provider-kubernetes/lib/manifest";
import { KubernetesProvider } from "@cdktf/provider-kubernetes/lib/provider";
export class TLSOptions extends Construct {
constructor(
scope: Construct,
id: string,
opts: { provider: KubernetesProvider; namespace: string },
) {
super(scope, id);
const { provider, namespace } = opts;
new Manifest(this, "traefik-tls-options", {
provider,
manifest: {
apiVersion: "traefik.io/v1alpha1",
kind: "TLSOption",
metadata: {
namespace,
name: "tls-options",
},
spec: {
minVersion: "VersionTLS13",
sniStrict: true,
},
},
});
}
}

View File

@@ -18,14 +18,14 @@ export class ValkeyCluster extends Construct {
const labels = { app: "valkey" }; const labels = { app: "valkey" };
const { provider, name, namespace } = options; const { provider, name, namespace } = options;
new OnePasswordSecret(this, "valkey-secret", { new OnePasswordSecret(this, "secret", {
provider, provider,
name: "valkey", name: "valkey",
namespace, namespace,
itemPath: "vaults/Lab/items/valkey", itemPath: "vaults/Lab/items/valkey",
}); });
new DeploymentV1(this, "valkey-deployment", { new DeploymentV1(this, "deployment", {
provider, provider,
metadata: { metadata: {
name, name,
@@ -60,9 +60,22 @@ export class ValkeyCluster extends Construct {
}, },
}, },
}, },
{
name: "SHAHAB_PASSWORD",
valueFrom: {
secretKeyRef: { name: "valkey", key: "password" },
},
},
], ],
command: ["/bin/sh", "-c"], command: ["/bin/sh", "-c"],
args: ['exec valkey-server --requirepass "$PASSWORD"'], args: [
`
valkey-server --requirepass "$PASSWORD" &
sleep 2
valkey-cli -a "$PASSWORD" ACL SETUSER shahab on ">$SHAHAB_PASSWORD" allcommands allkeys
wait
`,
],
readinessProbe: { readinessProbe: {
tcpSocket: [ tcpSocket: [
{ {
@@ -107,13 +120,9 @@ export class ValkeyCluster extends Construct {
name, name,
namespace, namespace,
labels, labels,
annotations: {
"external-dns.alpha.kubernetes.io/hostname": "valkey.dogar.dev",
"metallb.io/ip-allocated-from-pool": "pool",
},
}, },
spec: { spec: {
type: "LoadBalancer", type: "ClusterIP",
selector: labels, selector: labels,
port: [ port: [
{ {

View File

@@ -1,159 +0,0 @@
---
apiVersion: v1
kind: ConfigMap
metadata:
name: verdaccio
namespace: homelab
data:
config.yaml: |
storage: /verdaccio/storage
uplinks:
npmjs:
url: https://registry.npmjs.org/
packages:
"@*/*":
access: $all
publish: never
proxy: npmjs
"**":
access: $all
publish: never
proxy: npmjs
log:
- {type: stdout, format: pretty, level: http}
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: verdaccio
namespace: homelab
spec:
accessModes:
- ReadWriteMany
storageClassName: longhorn
resources:
requests:
storage: 128Gi
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: verdaccio
namespace: homelab
spec:
replicas: 3
selector:
matchLabels:
app: verdaccio
template:
metadata:
labels:
app: verdaccio
spec:
nodeSelector:
nodepool: worker
topologySpreadConstraints:
- maxSkew: 1
topologyKey: kubernetes.io/hostname
whenUnsatisfiable: ScheduleAnyway
labelSelector:
matchLabels:
app: verdaccio
affinity:
podAntiAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
- labelSelector:
matchExpressions:
- key: app
operator: In
values:
- verdaccio
topologyKey: "kubernetes.io/hostname"
containers:
- name: verdaccio
image: verdaccio/verdaccio:latest
env:
- name: VERDACCIO_APP_CONFIG
value: /verdaccio/conf/custom.yaml
- name: VERDACCIO_PORT
value: "4873"
ports:
- containerPort: 4873
volumeMounts:
- name: storage
mountPath: /verdaccio/storage
- name: config
mountPath: /verdaccio/conf/config.yaml
subPath: config.yaml
volumes:
- name: storage
persistentVolumeClaim:
claimName: verdaccio
- name: config
configMap:
name: verdaccio
---
apiVersion: v1
kind: Service
metadata:
name: verdaccio
namespace: homelab
spec:
selector:
app: verdaccio
ports:
- port: 4873
targetPort: 4873
type: ClusterIP
---
apiVersion: traefik.io/v1alpha1
kind: Middleware
metadata:
name: verdaccio
namespace: homelab
spec:
ipAllowList:
sourceRange:
- "127.0.0.1/32"
- "10.43.0.0/16"
rateLimit:
average: 10
burst: 50
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: verdaccio
namespace: homelab
annotations:
cert-manager.io/cluster-issuer: "cloudflare-issuer"
cert-manager.io/acme-challenge-type: "dns01"
cert-manager.io/private-key-size: "4096"
# Traefik Middleware
traefik.io/router.middlewares: "verdaccio@kubernetescrd"
spec:
ingressClassName: traefik
tls:
- hosts:
- npm.dogar.dev
secretName: verdaccio-tls
rules:
- host: npm.dogar.dev
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: verdaccio
port:
number: 4873

67
pki/index.ts Normal file
View File

@@ -0,0 +1,67 @@
import { DataKubernetesNamespaceV1 } from "@cdktf/provider-kubernetes/lib/data-kubernetes-namespace-v1";
import { KubernetesProvider } from "@cdktf/provider-kubernetes/lib/provider";
import { DataTerraformRemoteStateS3, TerraformStack } from "cdktf";
import { Construct } from "constructs";
import { PrivateIssuer, PublicIssuer } from "./issuers";
export class PKI extends TerraformStack {
constructor(scope: Construct, id: string) {
super(scope, id);
const kubernetes = new KubernetesProvider(this, "kubernetes", {
configPath: "~/.kube/config",
});
const r2Endpoint = `${process.env.ACCOUNT_ID!}.r2.cloudflarestorage.com`;
const coreServicesState = new DataTerraformRemoteStateS3(
this,
"core-services-state",
{
usePathStyle: true,
skipRegionValidation: true,
skipCredentialsValidation: true,
skipRequestingAccountId: true,
skipS3Checksum: true,
encrypt: true,
bucket: "terraform-state",
key: "core-services/terraform.tfstate",
endpoints: {
s3: `https://${r2Endpoint}`,
},
region: "auto",
accessKey: process.env.ACCESS_KEY,
secretKey: process.env.SECRET_KEY,
},
);
const namespaceName = coreServicesState.getString("namespace-output");
const namespaceResource = new DataKubernetesNamespaceV1(
this,
"homelab-namespace",
{
provider: kubernetes,
metadata: {
name: namespaceName,
},
},
);
const namespace = namespaceResource.metadata.name;
new PrivateIssuer(this, "private-issuer", {
provider: kubernetes,
namespace,
apiVersion: "cert-manager.io/v1",
rootSecretName: "root-secret",
intermediateSecretName: `${namespace}-ca-secret`,
commonName: "Homelab Root CA",
});
new PublicIssuer(this, "public-issuer", {
provider: kubernetes,
namespace,
apiVersion: "cert-manager.io/v1",
server: "https://acme-v02.api.letsencrypt.org/directory",
});
}
}

2
pki/issuers/index.ts Normal file
View File

@@ -0,0 +1,2 @@
export { PrivateIssuer } from "./private";
export { PublicIssuer } from "./public";

116
pki/issuers/private.ts Normal file
View File

@@ -0,0 +1,116 @@
import { Manifest } from "@cdktf/provider-kubernetes/lib/manifest";
import { KubernetesProvider } from "@cdktf/provider-kubernetes/lib/provider";
import { Construct } from "constructs";
type PrivateIssuerOptions = {
provider: KubernetesProvider;
namespace: string;
apiVersion: string;
commonName: string;
rootSecretName: string;
intermediateSecretName: string;
};
export class PrivateIssuer extends Construct {
constructor(scope: Construct, id: string, options: PrivateIssuerOptions) {
super(scope, id);
const {
provider,
namespace,
apiVersion,
commonName,
rootSecretName,
intermediateSecretName,
} = options;
//
// 1. Root CA (self-signed)
//
new Manifest(this, "root-ca-issuer", {
provider,
manifest: {
apiVersion,
kind: "ClusterIssuer",
metadata: { name: "root-ca-selfsigned" },
spec: { selfSigned: {} },
},
});
new Manifest(this, "root-ca", {
provider,
manifest: {
apiVersion,
kind: "Certificate",
metadata: { name: "root-ca", namespace },
spec: {
isCA: true,
commonName: `${commonName} Root CA`,
secretName: rootSecretName,
privateKey: {
algorithm: "RSA",
size: 4096,
},
issuerRef: {
name: "root-ca-selfsigned",
kind: "ClusterIssuer",
group: "cert-manager.io",
},
},
},
});
//
// 2. Intermediate CA (signed by root CA)
//
new Manifest(this, "intermediate-ca-issuer", {
provider,
manifest: {
apiVersion,
kind: "ClusterIssuer",
metadata: { name: "root-ca-signer" },
spec: {
ca: { secretName: rootSecretName },
},
},
});
new Manifest(this, "intermediate-ca", {
provider,
manifest: {
apiVersion,
kind: "Certificate",
metadata: { name: "intermediate-ca", namespace },
spec: {
isCA: true,
commonName: `${commonName} Intermediate CA`,
secretName: intermediateSecretName,
privateKey: {
algorithm: "ECDSA",
size: 384,
},
issuerRef: {
name: "root-ca-signer",
kind: "ClusterIssuer",
group: "cert-manager.io",
},
},
},
});
//
// 3. Final public cluster issuer (used by your apps)
//
new Manifest(this, "cluster-issuer", {
provider,
manifest: {
apiVersion,
kind: "ClusterIssuer",
metadata: { name: "cluster-issuer" },
spec: {
ca: { secretName: intermediateSecretName },
},
},
});
}
}

59
pki/issuers/public.ts Normal file
View File

@@ -0,0 +1,59 @@
import { Manifest } from "@cdktf/provider-kubernetes/lib/manifest";
import { KubernetesProvider } from "@cdktf/provider-kubernetes/lib/provider";
import { Construct } from "constructs";
import { OnePasswordSecret } from "../../utils";
type PublicIssuerOptions = {
provider: KubernetesProvider;
apiVersion: string;
namespace: string;
server: string;
};
export class PublicIssuer extends Construct {
constructor(scope: Construct, id: string, options: PublicIssuerOptions) {
super(scope, id);
const { apiVersion, provider, namespace, server } = options;
new OnePasswordSecret(this, "cloudflare-token", {
provider,
namespace,
name: "public-issuer-cloudflare-token",
itemPath: "vaults/Lab/items/cloudflare",
});
// Cloudflare ACME ClusterIssuer
new Manifest(this, "cloudflare-issuer", {
provider,
manifest: {
apiVersion,
kind: "ClusterIssuer",
metadata: {
name: "cloudflare-issuer",
},
spec: {
acme: {
email: "shahab@dogar.dev",
server,
privateKeySecretRef: {
name: "cloudflare-cluster-issuer-account-key",
},
solvers: [
{
dns01: {
cloudflare: {
apiTokenSecretRef: {
name: "public-issuer-cloudflare-token",
key: "token",
},
},
},
},
],
},
},
},
});
}
}

View File

@@ -2,7 +2,7 @@ import * as fs from "fs";
import * as path from "path"; import * as path from "path";
import { Release } from "@cdktf/provider-helm/lib/release"; import { Release } from "@cdktf/provider-helm/lib/release";
import { Construct } from "constructs"; import { Construct } from "constructs";
import { IngressRoute, OnePasswordSecret } from "../../utils"; import { PublicIngressRoute, OnePasswordSecret } from "../../utils";
import { Providers } from "../../types"; import { Providers } from "../../types";
type AuthentikServerOptions = { type AuthentikServerOptions = {
@@ -44,14 +44,13 @@ export class AuthentikServer extends Construct {
], ],
}); });
new IngressRoute(this, "ingress", { new PublicIngressRoute(this, "ingress", {
provider: kubernetes, provider: kubernetes,
name: options.name, name: options.name,
namespace: options.namespace, namespace: options.namespace,
host: "auth.dogar.dev", host: "auth.dogar.dev",
serviceName: `authentik-server`, serviceName: `authentik-server`,
servicePort: 80, servicePort: 80,
tlsSecretName: "authentik-tls",
}); });
} }
} }

View File

@@ -87,23 +87,8 @@ authentik:
server: server:
replicas: 3 replicas: 3
ingress:
enabled: false
annotations:
cert-manager.io/cluster-issuer: cloudflare-issuer
cert-manager.io/acme-challenge-type: dns01
cert-manager.io/private-key-size: "4096"
ingressClassName: traefik
hosts:
- auth.dogar.dev
tls:
- secretName: authentik-tls
hosts:
- auth.dogar.dev
worker: worker:
replicas: 3 replicas: 3
postgresql: postgresql:
enabled: false enabled: false
redis: redis:

View File

@@ -1,10 +1,10 @@
import { Construct } from "constructs"; import { Construct } from "constructs";
import { KubernetesProvider } from "@cdktf/provider-kubernetes/lib/provider"; import { KubernetesProvider } from "@cdktf/provider-kubernetes/lib/provider";
import { OnePasswordSecret, LonghornPvc } from "../../utils";
import { DeploymentV1 } from "@cdktf/provider-kubernetes/lib/deployment-v1"; import { DeploymentV1 } from "@cdktf/provider-kubernetes/lib/deployment-v1";
import { PodDisruptionBudgetV1 } from "@cdktf/provider-kubernetes/lib/pod-disruption-budget-v1"; import { PodDisruptionBudgetV1 } from "@cdktf/provider-kubernetes/lib/pod-disruption-budget-v1";
import { OnePasswordSecret, LonghornPvc } from "../../../utils";
type GiteaRunnerOptions = { type GiteaRunnerOptions = {
provider: KubernetesProvider; provider: KubernetesProvider;
name: string; name: string;

View File

@@ -3,8 +3,12 @@ import * as path from "path";
import { Release } from "@cdktf/provider-helm/lib/release"; import { Release } from "@cdktf/provider-helm/lib/release";
import { Construct } from "constructs"; import { Construct } from "constructs";
import { OnePasswordSecret, IngressRoute, IngressRouteTcp } from "../../utils"; import {
import type { Providers } from "../../types"; OnePasswordSecret,
PublicIngressRoute,
IngressRouteTcp,
} from "../../../utils";
import type { Providers } from "../../../types";
type GiteaServerOptions = { type GiteaServerOptions = {
providers: Providers; providers: Providers;
@@ -18,32 +22,33 @@ export class GiteaServer extends Construct {
super(scope, id); super(scope, id);
const { kubernetes, helm } = options.providers; const { kubernetes, helm } = options.providers;
const { name, namespace, r2Endpoint } = options;
new OnePasswordSecret(this, "admin", { new OnePasswordSecret(this, "admin", {
provider: kubernetes, provider: kubernetes,
name: "gitea-admin", name: "gitea-admin",
namespace: options.namespace, namespace,
itemPath: "vaults/Lab/items/gitea-admin", itemPath: "vaults/Lab/items/gitea-admin",
}); });
new OnePasswordSecret(this, "oauth", { new OnePasswordSecret(this, "oauth", {
provider: kubernetes, provider: kubernetes,
name: "gitea-oauth", name: "gitea-oauth",
namespace: options.namespace, namespace,
itemPath: "vaults/Lab/items/gitea-oauth", itemPath: "vaults/Lab/items/gitea-oauth",
}); });
new OnePasswordSecret(this, "smtp", { new OnePasswordSecret(this, "smtp", {
provider: kubernetes, provider: kubernetes,
name: "gitea-smtp-token", name: "gitea-smtp-token",
namespace: options.namespace, namespace,
itemPath: "vaults/Lab/items/smtp-token", itemPath: "vaults/Lab/items/smtp-token",
}); });
new OnePasswordSecret(this, "r2", { new OnePasswordSecret(this, "r2", {
provider: kubernetes, provider: kubernetes,
name: "gitea-cloudflare-token", name: "gitea-cloudflare-token",
namespace: options.namespace, namespace,
itemPath: "vaults/Lab/items/cloudflare", itemPath: "vaults/Lab/items/cloudflare",
}); });
@@ -52,11 +57,12 @@ export class GiteaServer extends Construct {
provider: helm, provider: helm,
repository: "https://dl.gitea.com/charts", repository: "https://dl.gitea.com/charts",
chart: "gitea", chart: "gitea",
namespace,
createNamespace: true, createNamespace: true,
set: [ set: [
{ {
name: "gitea.config.storage.MINIO_ENDPOINT", name: "gitea.config.storage.MINIO_ENDPOINT",
value: options.r2Endpoint, value: r2Endpoint,
}, },
], ],
values: [ values: [
@@ -68,21 +74,22 @@ export class GiteaServer extends Construct {
new IngressRouteTcp(this, "ssh-ingress", { new IngressRouteTcp(this, "ssh-ingress", {
provider: kubernetes, provider: kubernetes,
namespace: options.namespace, namespace,
name,
match: "HostSNI(`*`)",
entryPoint: "ssh", entryPoint: "ssh",
serviceName: `${options.name}-ssh`, serviceName: `${name}-ssh`,
servicePort: 22, servicePort: 22,
}); });
new IngressRoute(this, "http-ingress", { new PublicIngressRoute(this, "http-ingress", {
provider: kubernetes, provider: kubernetes,
namespace: options.namespace, namespace,
name: options.name, name,
entryPoints: ["websecure"],
host: "git.dogar.dev", host: "git.dogar.dev",
serviceName: `${options.name}-http`, serviceName: `${name}-http`,
servicePort: 3000, servicePort: 3000,
tlsSecretName: `${options.name}-tls`, serviceProtocol: "https",
}); });
} }
} }

View File

@@ -1,5 +1,8 @@
global: global:
storageClass: longhorn storageClass: longhorn
podSecurityContext:
fsGroup: 1000
fsGroupChangePolicy: "OnRootMismatch"
image: image:
rootless: false rootless: false
service: service:
@@ -21,12 +24,23 @@ gitea:
enabled: true enabled: true
serviceMonitor: serviceMonitor:
enabled: true enabled: true
scheme: "https"
tlsConfig:
insecureSkipVerify: false
caFile: /internal-ca/ca.crt
config: config:
server: server:
ENABLE_PPROF: true ENABLE_PPROF: true
ENABLE_GZIP: true ENABLE_GZIP: true
LFS_START_SERVER: true LFS_START_SERVER: true
PROTOCOL: https
CERT_FILE: /certs/tls.crt
KEY_FILE: /certs/tls.key
ROOT_URL: https://git.dogar.dev/
SSH_DOMAIN: git.dogar.dev SSH_DOMAIN: git.dogar.dev
DISABLE_SSH: false
SSH_LISTEN_PORT: 2222
SSH_PORT: 22
database: database:
DB_TYPE: postgres DB_TYPE: postgres
HOST: postgres-cluster-rw HOST: postgres-cluster-rw
@@ -86,6 +100,27 @@ gitea:
secretKeyRef: secretKeyRef:
name: gitea-cloudflare-token name: gitea-cloudflare-token
key: secret_access_key key: secret_access_key
livenessProbe:
httpGet:
path: /api/healthz
port: 3000
scheme: HTTPS
initialDelaySeconds: 5
periodSeconds: 10
readinessProbe:
httpGet:
path: /api/healthz
port: 3000
scheme: HTTPS
initialDelaySeconds: 5
periodSeconds: 10
startupProbe:
httpGet:
path: /api/healthz
port: 3000
scheme: HTTPS
initialDelaySeconds: 5
periodSeconds: 10
persistence: persistence:
labels: labels:
recurring-job.longhorn.io/source: "enabled" recurring-job.longhorn.io/source: "enabled"
@@ -94,6 +129,20 @@ persistence:
size: 50Gi size: 50Gi
accessModes: accessModes:
- ReadWriteMany - ReadWriteMany
postExtraInitContainers:
- name: fix-gitea-ssh-perms
image: alpine:3
command:
- sh
- -c
- |
echo "Fixing /data/ssh permissions..."
mkdir -p /data/ssh
chown -R 1000:1000 /data/ssh
chmod 700 /data/ssh
volumeMounts:
- name: data
mountPath: /data
deployment: deployment:
env: env:
- name: PGSSLMODE - name: PGSSLMODE
@@ -128,6 +177,9 @@ extraVolumes:
items: items:
- key: ca.crt - key: ca.crt
path: root.crt path: root.crt
- name: gitea-tls-internal
secret:
secretName: gitea-http-tls-internal
- name: gitea-temp - name: gitea-temp
emptyDir: {} emptyDir: {}
extraInitVolumeMounts: extraInitVolumeMounts:
@@ -138,6 +190,8 @@ extraContainerVolumeMounts:
- name: ssl-bundle - name: ssl-bundle
mountPath: /opt/gitea/.postgresql mountPath: /opt/gitea/.postgresql
readOnly: true readOnly: true
- name: gitea-tls-internal
mountPath: /certs
readOnly: true readOnly: true
- name: gitea-temp - name: gitea-temp
mountPath: /tmp/gitea-uploads mountPath: /tmp/gitea-uploads

View File

@@ -4,7 +4,6 @@ import { HelmProvider } from "@cdktf/provider-helm/lib/provider";
import { DataTerraformRemoteStateS3, TerraformStack } from "cdktf"; import { DataTerraformRemoteStateS3, TerraformStack } from "cdktf";
import { Construct } from "constructs"; import { Construct } from "constructs";
import { ValkeyCluster } from "./valkey";
import { GiteaRunner, GiteaServer } from "./gitea"; import { GiteaRunner, GiteaServer } from "./gitea";
import { AuthentikServer } from "./authentik"; import { AuthentikServer } from "./authentik";
import { PostgresCluster } from "./postgres"; import { PostgresCluster } from "./postgres";
@@ -26,7 +25,10 @@ export class UtilityServices extends TerraformStack {
const r2Endpoint = `${process.env.ACCOUNT_ID!}.r2.cloudflarestorage.com`; const r2Endpoint = `${process.env.ACCOUNT_ID!}.r2.cloudflarestorage.com`;
const homelabState = new DataTerraformRemoteStateS3(this, "homelab-state", { const coreServicesState = new DataTerraformRemoteStateS3(
this,
"core-services-state",
{
usePathStyle: true, usePathStyle: true,
skipRegionValidation: true, skipRegionValidation: true,
skipCredentialsValidation: true, skipCredentialsValidation: true,
@@ -41,9 +43,10 @@ export class UtilityServices extends TerraformStack {
region: "auto", region: "auto",
accessKey: process.env.ACCESS_KEY, accessKey: process.env.ACCESS_KEY,
secretKey: process.env.SECRET_KEY, secretKey: process.env.SECRET_KEY,
}); },
);
const namespaceName = homelabState.getString("namespace-output"); const namespaceName = coreServicesState.getString("namespace-output");
const namespaceResource = new DataKubernetesNamespaceV1( const namespaceResource = new DataKubernetesNamespaceV1(
this, this,
"homelab-namespace", "homelab-namespace",
@@ -70,18 +73,12 @@ export class UtilityServices extends TerraformStack {
], ],
}); });
const valkeyCluster = new ValkeyCluster(this, "valkey-cluster", {
namespace,
provider: kubernetes,
name: "valkey",
});
const postgres = new PostgresCluster(this, "postgres-cluster", { const postgres = new PostgresCluster(this, "postgres-cluster", {
certManagerApiVersion: "cert-manager.io/v1", certManagerApiVersion: "cert-manager.io/v1",
name: "postgres-cluster", name: "postgres-cluster",
namespace, namespace,
provider: kubernetes, provider: kubernetes,
users: ["shahab", "budget-tracker", "authentik", "gitea"], users: ["shahab", "budget-tracker", "authentik", "gitea", "netbird"],
primaryUser: "shahab", primaryUser: "shahab",
initSecretName: "postgres-password", initSecretName: "postgres-password",
backupR2EndpointURL: `https://${r2Endpoint}`, backupR2EndpointURL: `https://${r2Endpoint}`,
@@ -96,7 +93,6 @@ export class UtilityServices extends TerraformStack {
namespace, namespace,
}); });
authentik.node.addDependency(valkeyCluster);
authentik.node.addDependency(postgres); authentik.node.addDependency(postgres);
const gitea = new GiteaServer(this, "gitea-server", { const gitea = new GiteaServer(this, "gitea-server", {

View File

@@ -357,6 +357,7 @@ export class PostgresCluster extends Construct {
{ {
name: "barman-cloud.cloudnative-pg.io", name: "barman-cloud.cloudnative-pg.io",
isWALArchiver: true, isWALArchiver: true,
enabled: true,
parameters: { parameters: {
barmanObjectName: barmanStoreName, barmanObjectName: barmanStoreName,
serverName: backupServerName, serverName: backupServerName,

281
utils/cert-manager/base.ts Normal file
View File

@@ -0,0 +1,281 @@
import { Construct } from "constructs";
import { Manifest } from "@cdktf/provider-kubernetes/lib/manifest";
import { KubernetesProvider } from "@cdktf/provider-kubernetes/lib/provider";
/**
* Options passed to the Certificate construct for generating
* cert-manager.io/v1 Certificate resources.
*
* This type supports both public certificates (Cloudflare/ACME)
* and private internal certificates (internal CA), making it usable
* across all cluster security contexts (Ingress TLS, internal mTLS, etc.).
*/
export type CertificateOptions = {
/**
* Kubernetes provider instance used by the underlying Manifest resource.
*
* This should typically be the cluster's primary Kubernetes provider.
*
* Required.
*/
provider: KubernetesProvider;
/**
* Kubernetes namespace where the Certificate resource and the
* corresponding Secret will be created.
*
* Required.
*/
namespace: string;
/**
* Name of the Certificate resource (metadata.name).
*
* This should be unique within the namespace.
*
* Required.
*/
name: string;
/**
* Name of the Kubernetes Secret that cert-manager will populate with
* `tls.crt`, `tls.key`, and optionally `ca.crt`.
*
* This secret is automatically created and updated by cert-manager.
*
* Required.
*/
secretName: string;
/**
* List of DNS Subject Alternative Names that the certificate must cover.
*
* cert-manager requires at least one entry.
*
* For internal certificates: service FQDNs (svc.cluster.local).
* For public certificates: external domain names.
*
* Required.
*/
dnsNames: string[];
/**
* Reference to the cert-manager Issuer or ClusterIssuer used to sign the certificate.
*
* - For public certs: Cloudflare ACME ClusterIssuer
* - For private certs: Internal CA ClusterIssuer
*
* This field is usually injected automatically by subclasses
* (e.g., PublicCertificate / PrivateCertificate).
*
* Required internally — not intended to be set by user code directly.
*/
issuerRef?: {
/**
* Name of the Issuer or ClusterIssuer.
*/
name: string;
/**
* Type of issuer ("Issuer" or "ClusterIssuer").
*
* Defaults to "ClusterIssuer" when omitted.
*/
kind?: string;
};
/**
* The certificate's validity duration (e.g. "2160h" for 90 days).
*
* If omitted, cert-manager applies its own default (90 days for ACME).
*
* Optional.
*/
duration?: string;
/**
* How long before expiry cert-manager should attempt early renewal.
*
* Example: "360h" (15 days before expiration).
*
* Optional.
*/
renewBefore?: string;
/**
* Optional Common Name for the certificate's subject.
*
* SAN-only certificates are recommended, but CN is still required for
* compatibility with some older libraries (Java, ClickHouse, OpenSSL tooling).
*
* Optional.
*/
commonName?: string;
/**
* Key Usage extension — determines what the certificate may be used for.
*
* Common values:
*
* - "digital signature"
* - "key encipherment"
* - "server auth"
* - "client auth"
*
* Example for mTLS server certificates:
* usages: ["digital signature", "key encipherment", "server auth"]
*
* Example for mTLS client certificates:
* usages: ["digital signature", "client auth"]
*
* Optional — cert-manager applies sensible defaults when omitted.
*/
usages?: string[];
/**
* Options controlling the generated private key.
*
* Useful for:
* - Choosing RSA vs ECDSA vs Ed25519
* - Increasing RSA key strength (2048 → 4096)
* - Optimizing performance for internal services (ECDSA P-256)
*
* Optional.
*/
privateKey?: {
/**
* Private key algorithm.
*
* - "RSA" (default)
* - "ECDSA" (great for internal TLS)
* - "Ed25519" (fast and modern, but not universally supported)
*
* Optional.
*/
algorithm?: "RSA" | "ECDSA" | "Ed25519";
/**
* Key size in bits.
*
* Only applies to algorithms that support length:
* - RSA: 2048, 3072, 4096
* - ECDSA: 256, 384
*
* Optional.
*/
size?: number;
};
/**
* IP address SAN entries (rarely needed, but sometimes required
* for services bound directly to cluster node IPs or StatefulSet pod IPs).
*
* Using IP SANs is generally discouraged unless explicitly required.
*
* Optional.
*/
ipAddresses?: string[];
/**
* Subject information for the certificate (Organization, OrgUnit, etc.)
*
* Example:
*
* subject: {
* organizations: ["Internal Systems"],
* organizationalUnits: ["Platform"]
* }
*
* Optional.
*/
subject?: {
organizations?: string[];
organizationalUnits?: string[];
countries?: string[];
provinces?: string[];
localities?: string[];
streetAddresses?: string[];
postalCodes?: string[];
};
};
export class Certificate extends Construct {
/** The underlying Kubernetes manifest */
public readonly manifest: Manifest;
constructor(scope: Construct, id: string, opts: CertificateOptions) {
super(scope, id);
// --- Validation ---------------------------------------------------------
if (!opts.issuerRef) {
throw new Error(
`Certificate '${opts.name}' must specify issuerRef (usually provided by a subclass).`,
);
}
if (!opts.dnsNames || opts.dnsNames.length === 0) {
throw new Error(
`Certificate '${opts.name}' must include at least one DNS name in dnsNames[].`,
);
}
// --- Base manifest ------------------------------------------------------
const manifest: any = {
apiVersion: "cert-manager.io/v1",
kind: "Certificate",
metadata: {
name: opts.name,
namespace: opts.namespace,
},
spec: {
secretName: opts.secretName,
dnsNames: opts.dnsNames,
issuerRef: {
name: opts.issuerRef.name,
kind: opts.issuerRef.kind ?? "ClusterIssuer",
},
},
};
// --- Optional: duration & renewBefore ---------------------------------
if (opts.duration) {
manifest.spec.duration = opts.duration;
}
if (opts.renewBefore) {
manifest.spec.renewBefore = opts.renewBefore;
}
// --- Optional: commonName ----------------------------------------------
if (opts.commonName) {
manifest.spec.commonName = opts.commonName;
}
// --- Optional: key usages ----------------------------------------------
if (opts.usages?.length) {
manifest.spec.usages = opts.usages;
}
// --- Optional: private key settings ------------------------------------
if (opts.privateKey) {
manifest.spec.privateKey = {
...opts.privateKey,
};
}
// --- Optional: IP SAN entries ------------------------------------------
if (opts.ipAddresses?.length) {
manifest.spec.ipAddresses = opts.ipAddresses;
}
// --- Optional: subject fields ------------------------------------------
if (opts.subject) {
manifest.spec.subject = opts.subject;
}
// --- Create manifest resource ------------------------------------------
this.manifest = new Manifest(this, id, {
provider: opts.provider,
manifest,
});
}
}

View File

@@ -0,0 +1,36 @@
import { Construct } from "constructs";
import { Certificate, CertificateOptions } from "./base";
/**
* Public certificate issued via the Cloudflare ACME ClusterIssuer.
*
* This subclass automatically injects:
*
* issuerRef:
* name: "cloudflare-issuer"
* kind: "ClusterIssuer"
*
* It is intended for generating publicly trusted HTTPS certificates
* (e.g., *.dogar.dev) using Cloudflare DNS-01 validation.
*
* Users of this class should *not* specify issuerRef manually.
*/
export class CloudflareCertificate extends Certificate {
constructor(
scope: Construct,
id: string,
opts: Omit<CertificateOptions, "issuerRef" | "privateKey">,
) {
super(scope, id, {
...opts,
issuerRef: {
name: "cloudflare-issuer",
kind: "ClusterIssuer",
},
privateKey: {
algorithm: "RSA",
size: 4096,
},
});
}
}

View File

@@ -1,85 +1,2 @@
import { Construct } from "constructs"; export { CloudflareCertificate } from "./cloudflare";
import { Manifest } from "@cdktf/provider-kubernetes/lib/manifest"; export { PrivateCertificate } from "./internal";
import { KubernetesProvider } from "@cdktf/provider-kubernetes/lib/provider";
type CertificateOptions = {
provider: KubernetesProvider;
/** Namespace to create the Certificate in */
namespace: string;
/** Required name of the certificate (and CRD name) */
name: string;
/** Secret name for storing the issued TLS cert */
secretName: string;
/** One or more DNS names the certificate should cover */
dnsNames: string[];
/** Reference to the cert-manager issuer */
issuerRef: {
name: string;
kind?: string; // ClusterIssuer or Issuer
};
/** Optional duration (default: cert-manager default) */
duration?: string;
/** Optional renewBefore (default: cert-manager default) */
renewBefore?: string;
};
class Certificate extends Construct {
public readonly manifest: Manifest;
constructor(scope: Construct, id: string, opts: CertificateOptions) {
super(scope, id);
const manifest: any = {
apiVersion: "cert-manager.io/v1",
kind: "Certificate",
metadata: {
name: opts.name,
namespace: opts.namespace,
},
spec: {
secretName: opts.secretName,
dnsNames: opts.dnsNames,
issuerRef: {
name: opts.issuerRef.name,
kind: opts.issuerRef.kind ?? "ClusterIssuer",
},
},
};
if (opts.duration) {
manifest.spec.duration = opts.duration;
}
if (opts.renewBefore) {
manifest.spec.renewBefore = opts.renewBefore;
}
this.manifest = new Manifest(this, id, {
provider: opts.provider,
manifest,
});
}
}
export class CloudflareCertificate extends Certificate {
constructor(
scope: Construct,
id: string,
opts: Omit<CertificateOptions, "issuerRef">,
) {
super(scope, id, {
...opts,
issuerRef: {
name: "cloudflare-issuer",
kind: "ClusterIssuer",
},
});
}
}

View File

@@ -0,0 +1,41 @@
import { Construct } from "constructs";
import { Certificate, CertificateOptions } from "./base";
/**
* Private TLS certificate issued by the internal cluster CA.
*
* This subclass automatically injects:
*
* issuerRef:
* name: "cluster-issuer"
* kind: "ClusterIssuer"
*
* Use this for:
* - Internal service-to-service TLS (HTTP, gRPC, Webhooks)
* - mTLS server certificates
* - mTLS client certificates
* - Internal wildcard certificates
* - Databases, queues, operators, controllers, etc.
*
* Users of this class should NOT specify issuerRef manually.
*/
export class PrivateCertificate extends Certificate {
constructor(
scope: Construct,
id: string,
opts: Omit<CertificateOptions, "issuerRef" | "privateKey">,
) {
super(scope, id, {
...opts,
issuerRef: {
name: "cluster-issuer", // internal CA
kind: "ClusterIssuer",
},
privateKey: {
algorithm: "ECDSA",
size: 384,
},
usages: ["digital signature", "key encipherment", "server auth"],
});
}
}

View File

@@ -1,4 +1,8 @@
export { CloudflareCertificate } from "./cert-manager"; export { CloudflareCertificate, PrivateCertificate } from "./cert-manager";
export { OnePasswordSecret } from "./1password-secret"; export { OnePasswordSecret } from "./1password-secret";
export { IngressRoute, IngressRouteTcp } from "./traefik"; export {
PublicIngressRoute,
InternalIngressRoute,
IngressRouteTcp,
} from "./traefik";
export { LonghornPvc } from "./longhorn"; export { LonghornPvc } from "./longhorn";

View File

@@ -1,2 +1,2 @@
export { IngressRoute } from "./ingress"; export { PublicIngressRoute, InternalIngressRoute } from "./ingress";
export { IngressRouteTcp } from "./ingress-tcp"; export { IngressRouteTcp } from "./ingressTCP";

View File

@@ -0,0 +1,2 @@
export { PublicIngressRoute } from "./publicIngress";
export { InternalIngressRoute } from "./internalIngress";

View File

@@ -2,11 +2,12 @@ import { Construct } from "constructs";
import { Manifest } from "@cdktf/provider-kubernetes/lib/manifest"; import { Manifest } from "@cdktf/provider-kubernetes/lib/manifest";
import { KubernetesProvider } from "@cdktf/provider-kubernetes/lib/provider"; import { KubernetesProvider } from "@cdktf/provider-kubernetes/lib/provider";
import { CloudflareCertificate } from "../cert-manager"; import { PrivateCertificate } from "../../cert-manager";
type IngressRouteOptions = { export type IngressRouteOptions = {
provider: KubernetesProvider; provider: KubernetesProvider;
namespace: string; namespace: string;
name: string;
/** Hostname for this route (e.g. npm.dogar.dev) */ /** Hostname for this route (e.g. npm.dogar.dev) */
host: string; host: string;
@@ -17,6 +18,7 @@ type IngressRouteOptions = {
/** Backend K8s Service */ /** Backend K8s Service */
serviceName: string; serviceName: string;
servicePort: number; servicePort: number;
serviceProtocol?: "http" | "https";
/** EntryPoints (default: ["websecure"]) */ /** EntryPoints (default: ["websecure"]) */
entryPoints?: string[]; entryPoints?: string[];
@@ -26,9 +28,6 @@ type IngressRouteOptions = {
/** Extra middlewares (traefik format: namespace/name) */ /** Extra middlewares (traefik format: namespace/name) */
middlewares?: string[]; middlewares?: string[];
/** Name override (otherwise auto) */
name?: string;
}; };
export class IngressRoute extends Construct { export class IngressRoute extends Construct {
@@ -37,17 +36,61 @@ export class IngressRoute extends Construct {
constructor(scope: Construct, id: string, opts: IngressRouteOptions) { constructor(scope: Construct, id: string, opts: IngressRouteOptions) {
super(scope, id); super(scope, id);
const name = opts.name ?? `route-${opts.host.replace(/\./g, "-")}`; const name = opts.name;
const path = opts.path ?? "/"; const path = opts.path ?? "/";
const entryPoints = opts.entryPoints ?? ["websecure"]; const entryPoints = opts.entryPoints ?? ["websecure"];
const { provider, namespace } = opts;
if (opts.serviceProtocol === "https") {
new PrivateCertificate(this, "internal-cert", {
provider,
namespace,
name: `${opts.serviceName}-tls-internal`,
secretName: `${opts.serviceName}-tls-internal`,
dnsNames: [
opts.serviceName,
`${opts.serviceName}.${opts.namespace}.svc`,
`${opts.serviceName}.${opts.namespace}.svc.cluster.local`,
],
usages: ["digital signature", "key encipherment", "server auth"],
});
new Manifest(this, `${name}-https-transport`, {
provider,
manifest: {
apiVersion: "traefik.io/v1alpha1",
kind: "ServersTransport",
metadata: {
name: `${name}-https-transport`,
namespace,
},
spec: {
serverName: `${opts.serviceName}.${opts.namespace}.svc.cluster.local`,
rootCAs: [
{
secret: "root-secret",
},
],
insecureSkipVerify: false,
},
},
});
}
const route: any = { const route: any = {
match: `Host(\`${opts.host}\`) && PathPrefix(\`${path}\`)`, match: `Host(\`${opts.host}\`) && PathPrefix(\`${path}\`)`,
kind: "Rule", kind: "Rule",
services: [ services: [
{ {
namespace,
name: opts.serviceName, name: opts.serviceName,
port: opts.servicePort, port: opts.servicePort,
scheme: opts.serviceProtocol ?? "http",
serversTransport:
opts.serviceProtocol === "https"
? `${name}-https-transport`
: undefined,
}, },
], ],
}; };
@@ -67,25 +110,21 @@ export class IngressRoute extends Construct {
if (opts.tlsSecretName) { if (opts.tlsSecretName) {
spec.tls = { spec.tls = {
secretName: opts.tlsSecretName, secretName: opts.tlsSecretName,
options: {
name: "tls-options",
namespace: "homelab",
},
}; };
new CloudflareCertificate(this, `${name}-cert`, {
provider: opts.provider,
namespace: opts.namespace,
name: opts.host,
secretName: opts.tlsSecretName,
dnsNames: [opts.host],
});
} }
this.manifest = new Manifest(this, name, { this.manifest = new Manifest(this, name, {
provider: opts.provider, provider,
manifest: { manifest: {
apiVersion: "traefik.io/v1alpha1", apiVersion: "traefik.io/v1alpha1",
kind: "IngressRoute", kind: "IngressRoute",
metadata: { metadata: {
name, name,
namespace: opts.namespace, namespace,
}, },
spec, spec,
}, },

View File

@@ -0,0 +1,16 @@
import { Construct } from "constructs";
import { IngressRoute, IngressRouteOptions } from "./ingress";
export class InternalIngressRoute extends IngressRoute {
constructor(
scope: Construct,
id: string,
opts: Omit<IngressRouteOptions, "entryPoints" | "middlewares">,
) {
super(scope, id, {
...opts,
entryPoints: ["websecure"],
middlewares: ["homelab/ip-allow-list"],
});
}
}

View File

@@ -0,0 +1,33 @@
import { Construct } from "constructs";
import { IngressRoute, IngressRouteOptions } from "./ingress";
import { CloudflareCertificate } from "../../cert-manager";
export class PublicIngressRoute extends IngressRoute {
constructor(
scope: Construct,
id: string,
opts: Omit<
IngressRouteOptions,
"entryPoints" | "tlsSecretName" | "middlewares"
>,
) {
const tlsSecretName = `${opts.name}-tls`;
super(scope, id, {
...opts,
tlsSecretName,
entryPoints: ["websecure"],
middlewares: ["homelab/rate-limit"],
});
const { provider, name, namespace, host } = opts;
new CloudflareCertificate(this, `${name}-cert`, {
provider,
namespace,
name: host,
secretName: tlsSecretName,
dnsNames: [host],
});
}
}

View File

@@ -0,0 +1 @@
export { IngressRouteTcp } from "./ingress-tcp";

View File

@@ -4,6 +4,13 @@ import { KubernetesProvider } from "@cdktf/provider-kubernetes/lib/provider";
type IngressRouteTcpOptions = { type IngressRouteTcpOptions = {
provider: KubernetesProvider; provider: KubernetesProvider;
name: string;
/**
* Match rule.
* Default is `HostSNI(\`*\`)` which is correct for most TCP services.
*/
match: string;
/** Namespace where the IngressRouteTCP will be created */ /** Namespace where the IngressRouteTCP will be created */
namespace: string; namespace: string;
@@ -16,15 +23,6 @@ type IngressRouteTcpOptions = {
/** Backend service port */ /** Backend service port */
servicePort: number; servicePort: number;
/**
* Match rule.
* Default is `HostSNI(\`*\`)` which is correct for most TCP services.
*/
match?: string;
/** Name override (CR name) */
name?: string;
}; };
export class IngressRouteTcp extends Construct { export class IngressRouteTcp extends Construct {
@@ -33,14 +31,7 @@ export class IngressRouteTcp extends Construct {
constructor(scope: Construct, id: string, opts: IngressRouteTcpOptions) { constructor(scope: Construct, id: string, opts: IngressRouteTcpOptions) {
super(scope, id); super(scope, id);
const name = const { name, match } = opts;
opts.name ??
`tcp-${opts.entryPoint}-${opts.serviceName}`.replace(
/[^a-zA-Z0-9-]/g,
"",
);
const matchRule = opts.match ?? "HostSNI(`*`)";
this.manifest = new Manifest(this, name, { this.manifest = new Manifest(this, name, {
provider: opts.provider, provider: opts.provider,
@@ -55,7 +46,7 @@ export class IngressRouteTcp extends Construct {
entryPoints: [opts.entryPoint], entryPoints: [opts.entryPoint],
routes: [ routes: [
{ {
match: matchRule, match,
services: [ services: [
{ {
name: opts.serviceName, name: opts.serviceName,