feat: MediaServices | deploy through cdktf

This commit is contained in:
2025-11-24 12:14:07 +05:00
parent d003c3f280
commit fcb087a915
13 changed files with 806 additions and 487 deletions

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

@@ -0,0 +1,81 @@
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,
},
}).importFrom("media");
// 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,
});
}
}

View File

@@ -0,0 +1,141 @@
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 } 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: "8096",
},
],
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: "jellyfin/jellyfin:latest",
imagePullPolicy: "IfNotPresent",
port: [
{
containerPort: 8096,
name: "http",
},
],
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,
},
},
],
},
},
},
});
// Ingress - using internal ingress for secure access
new InternalIngressRoute(this, "ingress", {
provider,
namespace,
name,
host,
serviceName: name,
servicePort: 80,
});
}
}

View File

@@ -0,0 +1,107 @@
import { Construct } from "constructs";
import { DeploymentV1 } from "@cdktf/provider-kubernetes/lib/deployment-v1";
import { ServiceV1 } from "@cdktf/provider-kubernetes/lib/service-v1";
import { LonghornPvc } from "../../utils";
import {
BaseMediaServiceOptions,
getWorkerNodeSelector,
getCommonEnv,
} from "../types";
export class ProwlarrServer extends Construct {
constructor(scope: Construct, id: string, options: BaseMediaServiceOptions) {
super(scope, id);
const { provider, namespace } = 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,
},
},
],
},
},
},
});
// Note: No ingress - Prowlarr is for internal use only
}
}

View File

@@ -0,0 +1,150 @@
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 } 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,
},
},
],
},
},
},
});
// Ingress
new InternalIngressRoute(this, "ingress", {
provider,
namespace,
name,
host,
serviceName: name,
servicePort: 80,
});
}
}

View File

@@ -0,0 +1,145 @@
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 } 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,
},
},
],
},
},
},
});
// Ingress
new InternalIngressRoute(this, "ingress", {
provider,
namespace,
name,
host,
serviceName: name,
servicePort: 80,
});
}
}

View File

@@ -0,0 +1,145 @@
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 } 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,
},
},
],
},
},
},
});
// Ingress
new InternalIngressRoute(this, "ingress", {
provider,
namespace,
name,
host,
serviceName: name,
servicePort: 80,
});
}
}

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",
});