From 25331ec0e1a0784aad1efa67a13a504efcd16916 Mon Sep 17 00:00:00 2001 From: TapTap Date: Fri, 13 Nov 2026 12:56:30 +0000 Subject: [PATCH] feat: add Docker image, Helm chart and Gitea CI - multi-stage Dockerfile with ffmpeg on Alpine JRE 21 - Helm chart for k3s: library ConfigMap, Vault users secret, data PVC, read-only hostPath media, ingress, probes and security context - Gitea Actions runs tests, smoke test and chart lint, then builds and pushes the image on main and version tags --- .dockerignore | 15 +++ .gitea/workflows/ci.yaml | 87 +++++++++++++ Dockerfile | 31 +++++ README.md | 50 ++++++-- charts/musicmanager/Chart.yaml | 6 + charts/musicmanager/templates/_helpers.tpl | 33 +++++ .../templates/configmap-library.yaml | 16 +++ charts/musicmanager/templates/deployment.yaml | 118 ++++++++++++++++++ charts/musicmanager/templates/ingress.yaml | 29 +++++ charts/musicmanager/templates/pvc.yaml | 17 +++ charts/musicmanager/templates/service.yaml | 15 +++ charts/musicmanager/values.yaml | 88 +++++++++++++ 12 files changed, 494 insertions(+), 11 deletions(-) create mode 100644 .dockerignore create mode 100644 .gitea/workflows/ci.yaml create mode 100644 Dockerfile create mode 100644 charts/musicmanager/Chart.yaml create mode 100644 charts/musicmanager/templates/_helpers.tpl create mode 100644 charts/musicmanager/templates/configmap-library.yaml create mode 100644 charts/musicmanager/templates/deployment.yaml create mode 100644 charts/musicmanager/templates/ingress.yaml create mode 100644 charts/musicmanager/templates/pvc.yaml create mode 100644 charts/musicmanager/templates/service.yaml create mode 100644 charts/musicmanager/values.yaml diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..17f7715 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,15 @@ +.git +.github +.gitea +.gradle +build +data +media +test-media +charts +config/library.yaml +config/users.yaml +*.db +*.db-shm +*.db-wal +*.log diff --git a/.gitea/workflows/ci.yaml b/.gitea/workflows/ci.yaml new file mode 100644 index 0000000..358e981 --- /dev/null +++ b/.gitea/workflows/ci.yaml @@ -0,0 +1,87 @@ +name: CI + +on: + push: + branches: + - main + tags: + - "v*" + pull_request: + +env: + REGISTRY: gitea.tap-tap.win + IMAGE_NAME: tap-tap/audiomanager + +jobs: + test: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - uses: actions/setup-java@v4 + with: + distribution: temurin + java-version: "21" + - name: Install ffmpeg + run: sudo apt-get update && sudo apt-get install -y ffmpeg + - name: Test + run: ./gradlew test --console=plain + + smoke: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - uses: actions/setup-java@v4 + with: + distribution: temurin + java-version: "21" + - name: Install ffmpeg and sqlite + run: sudo apt-get update && sudo apt-get install -y ffmpeg sqlite3 + - name: Smoke test + run: ./scripts/smoke.sh + + chart: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - uses: azure/setup-helm@v4 + - name: Lint and render the chart + run: | + helm lint charts/musicmanager + helm template musicmanager charts/musicmanager --set users.existingSecret=test > /dev/null + + image: + needs: + - test + - smoke + - chart + if: github.event_name == 'push' + runs-on: ubuntu-latest + permissions: + contents: read + packages: write + steps: + - uses: actions/checkout@v4 + - uses: docker/setup-buildx-action@v3 + - name: Log in to the registry + uses: docker/login-action@v3 + with: + registry: ${{ env.REGISTRY }} + username: ${{ secrets.REGISTRY_USERNAME || gitea.actor }} + password: ${{ secrets.REGISTRY_PASSWORD || secrets.GITEA_TOKEN }} + - name: Image metadata + id: meta + uses: docker/metadata-action@v5 + with: + images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} + tags: | + type=ref,event=branch + type=ref,event=tag + type=sha + type=raw,value=latest,enable={{is_default_branch}} + - name: Build and push + uses: docker/build-push-action@v6 + with: + context: . + push: true + tags: ${{ steps.meta.outputs.tags }} + labels: ${{ steps.meta.outputs.labels }} diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..9d5679c --- /dev/null +++ b/Dockerfile @@ -0,0 +1,31 @@ +# syntax=docker/dockerfile:1 + +FROM gradle:8.14.3-jdk21 AS build +WORKDIR /src + +COPY gradle ./gradle +COPY gradlew settings.gradle.kts build.gradle.kts gradle.properties ./ +RUN ./gradlew --no-daemon dependencies > /dev/null + +COPY src ./src +RUN ./gradlew --no-daemon shadowJar + +FROM eclipse-temurin:21-jre-alpine + +RUN apk add --no-cache ffmpeg \ + && addgroup -g 10001 -S app \ + && adduser -u 10001 -S -G app app + +WORKDIR /app +COPY --from=build /src/build/libs/*-all.jar /app/musicmanager.jar + +ENV DATA_DIR=/data \ + LIBRARY_CONFIG=/etc/musicmanager/library.yaml \ + USERS_FILE=/etc/musicmanager/users.yaml \ + FFMPEG_PATH=ffmpeg \ + PORT=8080 + +USER 10001:10001 +EXPOSE 8080 + +ENTRYPOINT ["java", "-XX:+UseSerialGC", "-Xmx256m", "-jar", "/app/musicmanager.jar"] diff --git a/README.md b/README.md index 3602f8e..b5cdb7f 100644 --- a/README.md +++ b/README.md @@ -632,21 +632,49 @@ Cover extraction order: embedded APIC frame -> `cover.jpg`/`folder.jpg`/`front.j - Image: `gitea.tap-tap.win/tap-tap/audiomanager:` (registry paths must be lowercase). - Gitea Actions builds and pushes on tags; `imagePullSecret` is provided by the cluster. +Build the image locally: + +```bash +docker build -t gitea.tap-tap.win/tap-tap/audiomanager:dev . +``` + +`.gitea/workflows/ci.yaml` runs tests and the smoke script, then builds and pushes +`:latest` for `main` and the tag name for `v*` tags. Set `REGISTRY_USERNAME` / +`REGISTRY_PASSWORD` repository secrets if the automatically provided token cannot +push to the registry. + ### 9.2 Helm chart -`charts/musicmanager` with values for: +`charts/musicmanager` renders: -- image repository/tag, `imagePullSecrets` -- `library` config (folders) rendered into a ConfigMap +- image repository/tag and `imagePullSecrets` +- `library.folders` into a ConfigMap (`/etc/musicmanager/library.yaml`) - `users.existingSecret` + key (Vault-managed; never put passwords in values) -- ingress host `subsonic.tap-tap.win`, ingress class, TLS via cluster issuer +- ingress host `subsonic.tap-tap.win`, ingress class and TLS secret - `/data` PVC (size, storage class) -- `hostPath` mounts: `/mnt/media/music` and `/mnt/media/longform`, read-only -- no node pinning: every schedulable node must expose the same mounts (documented) -- resource requests/limits, security context, liveness/readiness probes (`/health`) -- optional `nodeSelector`/`tolerations` for future use +- `hostPath` mounts for music and long-form roots, both read-only +- resource requests/limits, non-root security context, probes on `/health` +- optional `nodeSelector`, `tolerations` and `affinity` -The server fails its readiness probe if a configured root path is missing or unreadable. +The secret is not created by the chart. Create it from the Vault-managed file: + +```bash +kubectl create namespace musicmanager +kubectl -n musicmanager create secret generic musicmanager-users \ + --from-file=users.yaml=config/users.yaml + +helm upgrade --install musicmanager charts/musicmanager \ + --namespace musicmanager \ + --set image.tag=dev \ + --set users.existingSecret=musicmanager-users +``` + +Adjust `library.folders`, `media.music.hostPath` and `media.longform.hostPath` for +the cluster. Every schedulable node must expose the same host paths; the chart +deliberately does not pin the pod to a node. + +The server exits at startup if a configured root path is missing or unreadable, so +the pod will not pass its readiness probe in that case. ### 9.3 Operations @@ -696,8 +724,8 @@ Verify each step with `./gradlew test` and/or `curl`: - [x] 4. **Browsing & media:** system, browsing, genre and media routes (`stream`, `download`, `getCoverArt`). - [x] 5. **Transcoding:** `FfmpegTranscoder.kt`, `maxBitRate`/`format`/`timeOffset`, extension declaration. - [x] 6. **Interaction & playlists:** `getAlbumList2`, `getRandomSongs`, `getStarred2`, `search3`, `scrobble`, `star`/`unstar`, `setRating`, bookmarks, playlists. -- [ ] 7. **Packaging:** multi-stage Dockerfile (with ffmpeg), Helm chart, Gitea Actions workflow. -- [ ] 8. **Tests:** integration suite as described above, plus a `curl` smoke script. +- [x] 7. **Packaging:** multi-stage Dockerfile (with ffmpeg), Helm chart, Gitea Actions workflow. +- [x] 8. **Tests:** integration suite as described above, plus a `curl` smoke script. --- diff --git a/charts/musicmanager/Chart.yaml b/charts/musicmanager/Chart.yaml new file mode 100644 index 0000000..402572a --- /dev/null +++ b/charts/musicmanager/Chart.yaml @@ -0,0 +1,6 @@ +apiVersion: v2 +name: musicmanager +description: Self-hosted OpenSubsonic music server +type: application +version: 0.1.0 +appVersion: "0.1.0" diff --git a/charts/musicmanager/templates/_helpers.tpl b/charts/musicmanager/templates/_helpers.tpl new file mode 100644 index 0000000..efc2171 --- /dev/null +++ b/charts/musicmanager/templates/_helpers.tpl @@ -0,0 +1,33 @@ +{{- define "musicmanager.name" -}} +{{- default .Chart.Name .Values.nameOverride | trunc 63 | trimSuffix "-" -}} +{{- end -}} + +{{- define "musicmanager.fullname" -}} +{{- if .Values.fullnameOverride -}} +{{- .Values.fullnameOverride | trunc 63 | trimSuffix "-" -}} +{{- else -}} +{{- $name := default .Chart.Name .Values.nameOverride -}} +{{- if contains $name .Release.Name -}} +{{- .Release.Name | trunc 63 | trimSuffix "-" -}} +{{- else -}} +{{- printf "%s-%s" .Release.Name $name | trunc 63 | trimSuffix "-" -}} +{{- end -}} +{{- end -}} +{{- end -}} + +{{- define "musicmanager.labels" -}} +helm.sh/chart: {{ printf "%s-%s" .Chart.Name .Chart.Version | replace "+" "_" }} +app.kubernetes.io/name: {{ include "musicmanager.name" . }} +app.kubernetes.io/instance: {{ .Release.Name }} +app.kubernetes.io/version: {{ .Chart.AppVersion | quote }} +app.kubernetes.io/managed-by: {{ .Release.Service }} +{{- end -}} + +{{- define "musicmanager.selectorLabels" -}} +app.kubernetes.io/name: {{ include "musicmanager.name" . }} +app.kubernetes.io/instance: {{ .Release.Name }} +{{- end -}} + +{{- define "musicmanager.image" -}} +{{- printf "%s:%s" .Values.image.repository (default .Chart.AppVersion .Values.image.tag) -}} +{{- end -}} diff --git a/charts/musicmanager/templates/configmap-library.yaml b/charts/musicmanager/templates/configmap-library.yaml new file mode 100644 index 0000000..94465cc --- /dev/null +++ b/charts/musicmanager/templates/configmap-library.yaml @@ -0,0 +1,16 @@ +apiVersion: v1 +kind: ConfigMap +metadata: + name: {{ include "musicmanager.fullname" . }}-library + labels: + {{- include "musicmanager.labels" . | nindent 4 }} +data: + library.yaml: | + library: + folders: + {{- range .Values.library.folders }} + - id: {{ .id }} + name: {{ .name | quote }} + type: {{ .type }} + path: {{ .path | quote }} + {{- end }} diff --git a/charts/musicmanager/templates/deployment.yaml b/charts/musicmanager/templates/deployment.yaml new file mode 100644 index 0000000..ba3f297 --- /dev/null +++ b/charts/musicmanager/templates/deployment.yaml @@ -0,0 +1,118 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + name: {{ include "musicmanager.fullname" . }} + labels: + {{- include "musicmanager.labels" . | nindent 4 }} +spec: + replicas: {{ .Values.replicaCount }} + strategy: + type: Recreate + selector: + matchLabels: + {{- include "musicmanager.selectorLabels" . | nindent 6 }} + template: + metadata: + labels: + {{- include "musicmanager.selectorLabels" . | nindent 8 }} + annotations: + checksum/library: {{ include (print $.Template.BasePath "/configmap-library.yaml") . | sha256sum }} + spec: + {{- with .Values.imagePullSecrets }} + imagePullSecrets: + {{- toYaml . | nindent 8 }} + {{- end }} + securityContext: + {{- toYaml .Values.podSecurityContext | nindent 8 }} + containers: + - name: musicmanager + image: "{{ include "musicmanager.image" . }}" + imagePullPolicy: {{ .Values.image.pullPolicy }} + securityContext: + {{- toYaml .Values.securityContext | nindent 12 }} + env: + - name: PORT + value: "8080" + - name: DATA_DIR + value: /data + - name: LIBRARY_CONFIG + value: /etc/musicmanager/library.yaml + - name: USERS_FILE + value: /etc/musicmanager/users.yaml + - name: FFMPEG_PATH + value: ffmpeg + {{- with .Values.env }} + {{- toYaml . | nindent 12 }} + {{- end }} + ports: + - name: http + containerPort: 8080 + livenessProbe: + httpGet: + path: /health + port: http + initialDelaySeconds: 10 + periodSeconds: 20 + readinessProbe: + httpGet: + path: /health + port: http + initialDelaySeconds: 5 + periodSeconds: 10 + resources: + {{- toYaml .Values.resources | nindent 12 }} + volumeMounts: + - name: library + mountPath: /etc/musicmanager/library.yaml + subPath: library.yaml + readOnly: true + - name: users + mountPath: /etc/musicmanager/users.yaml + subPath: {{ .Values.users.key }} + readOnly: true + - name: data + mountPath: /data + - name: tmp + mountPath: /tmp + - name: music + mountPath: {{ .Values.media.music.mountPath }} + readOnly: true + - name: longform + mountPath: {{ .Values.media.longform.mountPath }} + readOnly: true + volumes: + - name: library + configMap: + name: {{ include "musicmanager.fullname" . }}-library + - name: users + secret: + secretName: {{ required "users.existingSecret is required" .Values.users.existingSecret }} + - name: data + {{- if .Values.data.persistence.enabled }} + persistentVolumeClaim: + claimName: {{ include "musicmanager.fullname" . }}-data + {{- else }} + emptyDir: {} + {{- end }} + - name: tmp + emptyDir: {} + - name: music + hostPath: + path: {{ .Values.media.music.hostPath }} + type: Directory + - name: longform + hostPath: + path: {{ .Values.media.longform.hostPath }} + type: Directory + {{- with .Values.nodeSelector }} + nodeSelector: + {{- toYaml . | nindent 8 }} + {{- end }} + {{- with .Values.affinity }} + affinity: + {{- toYaml . | nindent 8 }} + {{- end }} + {{- with .Values.tolerations }} + tolerations: + {{- toYaml . | nindent 8 }} + {{- end }} diff --git a/charts/musicmanager/templates/ingress.yaml b/charts/musicmanager/templates/ingress.yaml new file mode 100644 index 0000000..8783b65 --- /dev/null +++ b/charts/musicmanager/templates/ingress.yaml @@ -0,0 +1,29 @@ +{{- if .Values.ingress.enabled }} +apiVersion: networking.k8s.io/v1 +kind: Ingress +metadata: + name: {{ include "musicmanager.fullname" . }} + labels: + {{- include "musicmanager.labels" . | nindent 4 }} +spec: + {{- with .Values.ingress.className }} + ingressClassName: {{ . }} + {{- end }} + {{- if .Values.ingress.tls.enabled }} + tls: + - hosts: + - {{ .Values.ingress.host | quote }} + secretName: {{ .Values.ingress.tls.secretName }} + {{- end }} + rules: + - host: {{ .Values.ingress.host | quote }} + http: + paths: + - path: {{ .Values.ingress.path }} + pathType: {{ .Values.ingress.pathType }} + backend: + service: + name: {{ include "musicmanager.fullname" . }} + port: + name: http +{{- end }} diff --git a/charts/musicmanager/templates/pvc.yaml b/charts/musicmanager/templates/pvc.yaml new file mode 100644 index 0000000..af8def7 --- /dev/null +++ b/charts/musicmanager/templates/pvc.yaml @@ -0,0 +1,17 @@ +{{- if .Values.data.persistence.enabled }} +apiVersion: v1 +kind: PersistentVolumeClaim +metadata: + name: {{ include "musicmanager.fullname" . }}-data + labels: + {{- include "musicmanager.labels" . | nindent 4 }} +spec: + accessModes: + {{- toYaml .Values.data.persistence.accessModes | nindent 4 }} + {{- with .Values.data.persistence.storageClass }} + storageClassName: {{ . | quote }} + {{- end }} + resources: + requests: + storage: {{ .Values.data.persistence.size }} +{{- end }} diff --git a/charts/musicmanager/templates/service.yaml b/charts/musicmanager/templates/service.yaml new file mode 100644 index 0000000..da1e268 --- /dev/null +++ b/charts/musicmanager/templates/service.yaml @@ -0,0 +1,15 @@ +apiVersion: v1 +kind: Service +metadata: + name: {{ include "musicmanager.fullname" . }} + labels: + {{- include "musicmanager.labels" . | nindent 4 }} +spec: + type: {{ .Values.service.type }} + selector: + {{- include "musicmanager.selectorLabels" . | nindent 4 }} + ports: + - name: http + port: {{ .Values.service.port }} + targetPort: http + protocol: TCP diff --git a/charts/musicmanager/values.yaml b/charts/musicmanager/values.yaml new file mode 100644 index 0000000..e0b2e25 --- /dev/null +++ b/charts/musicmanager/values.yaml @@ -0,0 +1,88 @@ +replicaCount: 1 + +image: + repository: gitea.tap-tap.win/tap-tap/audiomanager + tag: "" # defaults to appVersion + pullPolicy: IfNotPresent + +imagePullSecrets: [] +nameOverride: "" +fullnameOverride: "" + +# Rendered into a ConfigMap as /etc/musicmanager/library.yaml. +# Keep folder IDs stable: they are part of song IDs. +library: + folders: + - id: 1 + name: "Music" + type: music + path: /mnt/media/music + - id: 10 + name: "Audiobooks & Podcasts" + type: longform + path: /mnt/media/longform + +# Vault-managed users.yaml. The secret must already exist and contain the key below. +users: + existingSecret: musicmanager-users + key: users.yaml + +data: + persistence: + enabled: true + size: 5Gi + storageClass: "" + accessModes: + - ReadWriteOnce + +# hostPath mounts shared by every schedulable node. Read-only in the pod. +media: + music: + hostPath: /mnt/media/music + mountPath: /mnt/media/music + longform: + hostPath: /mnt/media/longform + mountPath: /mnt/media/longform + +service: + type: ClusterIP + port: 8080 + +ingress: + enabled: true + className: traefik + host: subsonic.tap-tap.win + path: / + pathType: Prefix + tls: + enabled: true + secretName: subsonic-tap-tap-win-tls + +resources: + requests: + cpu: 100m + memory: 256Mi + limits: + cpu: 1000m + memory: 512Mi + +podSecurityContext: + fsGroup: 10001 + +securityContext: + runAsNonRoot: true + runAsUser: 10001 + runAsGroup: 10001 + readOnlyRootFilesystem: true + allowPrivilegeEscalation: false + capabilities: + drop: + - ALL + +env: + - name: WEB_UI + value: "true" + +nodeSelector: {} +tolerations: [] +affinity: {}