Skip to content

Commit

Permalink
feat: container with web ui
Browse files Browse the repository at this point in the history
  • Loading branch information
mgoltzsche committed Oct 5, 2023
0 parents commit 469cfe4
Show file tree
Hide file tree
Showing 19 changed files with 502 additions and 0 deletions.
65 changes: 65 additions & 0 deletions .github/workflows/workflow.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# TODO: Make downstream projects apply changes within this file.
# Currently this file must be listed within .kptignore since it is not a KRM file.
# However, therefore changes within the upstream repo are not propagated down.
name: Build and release

on:
push:
branches:
- main
pull_request:
branches:
- main

concurrency: # Run release builds sequentially, cancel outdated PR builds
group: ci-${{ github.ref }}
cancel-in-progress: ${{ github.ref != 'refs/heads/main' }}

permissions: # Grant write access to github.token within non-pull_request builds
contents: write
packages: write

jobs:
build:
name: Build
runs-on: ubuntu-latest

steps:
- name: Check out code
uses: actions/checkout@v3
with:
fetch-depth: 0
persist-credentials: false

- id: release
name: Prepare release
uses: mgoltzsche/conventional-release@v0
with:
commit-files: deploy setters.yaml

- name: Set up qemu
uses: docker/setup-qemu-action@v3
with:
platforms: all

- name: Build
if: '!steps.release.outputs.publish'
run: |
make
- name: Log into GitHub Container Registry
if: steps.release.outputs.publish
run: echo "${{ github.token }}" | docker login ghcr.io -u ${{ github.actor }} --password-stdin

- name: Push container image
if: steps.release.outputs.publish
run: |
set -u
make release VERSION=$RELEASE_VERSION
- name: Replace image version within manifests
if: steps.release.outputs.publish
run: |
set -u
make manifest-image VERSION=$RELEASE_VERSION
3 changes: 3 additions & 0 deletions .krmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/.github
.releaserc.yaml
beets.yaml
18 changes: 18 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
FROM python:3-alpine3.18
RUN apk add --update --no-cache cargo
RUN python3 -m pip install \
beets==1.6.0 \
flask==3.0.0 \
flask-cors==4.0.0 \
pylast==5.2.0 \
beets-describe==0.0.4 \
beets-goingrunning==1.2.5
RUN apk add --update --no-cache beets
COPY beets.yaml /etc/beets.yaml
RUN set -eux; \
adduser -Su 1000 beets beets; \
mkdir -m750 /data; \
chown 1000:1000 /data
USER beets:beets
ENTRYPOINT ["beet", "-c", "/etc/beets.yaml"]
CMD ["web"]
26 changes: 26 additions & 0 deletions Kptfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
apiVersion: kpt.dev/v1
kind: Kptfile
metadata:
name: beets-container
annotations:
config.kubernetes.io/local-config: "true"
upstream:
type: git
git:
repo: https://github.com/mgoltzsche/kubemate-app-blueprints
directory: /packages/webapp
ref: main
updateStrategy: resource-merge
upstreamLock:
type: git
git:
repo: https://github.com/mgoltzsche/kubemate-app-blueprints
directory: /packages/webapp
ref: main
commit: 1cd29027a8ef9d091c30403b181f10fe97225612
info:
description: web application
pipeline:
mutators:
- image: gcr.io/kpt-fn/apply-setters:v0.2.0
configPath: ./setters.yaml
99 changes: 99 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
# DO NOT EDIT THIS FILE MANUALLY!
# This file is maintained within the upstream kpt package (see Kptfile).
# Committing manual changes to this file, disables blueprint updates.

KPT_IMAGE ?= mgoltzsche/kpt-docker:1.0.0-beta.32
KPT_PKG_UPDATE_STRATEGY ?= resource-merge
SKAFFOLD_IMAGE ?= gcr.io/k8s-skaffold/skaffold:v2.7.1
SKAFFOLD_OPTS ?=
KUBECONFIG ?= $$HOME/.kube/config

DOCKER ?= docker

.PHONY: all
all: image

##@ Build

.PHONY: image
image: skaffold-build ## Build the image(s) for the host architecture.

##@ Deploy

.PHONY: deploy
deploy: skaffold-run ## Deploy the application into the selected cluster.
.PHONY: undeploy
undeploy: skaffold-delete ## Undeploy the debug application.

.PHONY: debug
debug: SKAFFOLD_OPTS += --auto-build
debug: skaffold-debug ## Deploy the application in debug mode.

##@ Development

.PHONY: render
render: kpt-fn-render ## Run kpt render pipeline, applying setters.yaml.
kpt-fn-render: kpt-fn-%:
$(DOCKER) run -i --rm -u "`id -u`:`id -g`" --group-add 998 \
-v "/var/run/docker.sock:/var/run/docker.sock" \
-v "`pwd`:/workspace" \
$(KPT_IMAGE) fn $* /workspace --truncate-output=false

.PHONY: blueprint-update
blueprint-update: kpt-pkg-update render ## Apply the latest blueprint updates to this codebase.

kpt-pkg-update:
mkdir -p $$HOME/.kpt
$(DOCKER) run --rm -u "`id -u`:`id -g`" -v "$$HOME/.kpt:/.kpt" \
-v `pwd`:/data -w /data \
$(KPT_IMAGE) pkg update --strategy=$(KPT_PKG_UPDATE_STRATEGY) .

skaffold-debug skaffold-dev: DOCKER_RUN_OPTS += -ti
skaffold-debug skaffold-dev skaffold-run skaffold-stop skaffold-delete: DOCKER_RUN_OPTS += --mount "type=bind,src=$(KUBECONFIG),dst=/tmp/.kube/config,ro"
skaffold-run skaffold-stop skaffold-build skaffold-dev skaffold-delete skaffold-debug skaffold-survey skaffold-help: skaffold-%:
mkdir -p $$HOME/.docker
$(DOCKER) run $(DOCKER_RUN_OPTS) --rm -u "`id -u`:`id -g`" --group-add=998 \
-v "`pwd`:/workspace" -w /workspace --network=host \
-v "/var/run/docker.sock:/var/run/docker.sock" \
--mount "type=bind,src=$$HOME/.docker,dst=/tmp/.docker" \
-e HOME=/tmp \
$(SKAFFOLD_IMAGE) skaffold $* $(SKAFFOLD_OPTS)

.PHONY: help
help: ## Display this help.
@awk 'BEGIN {FS = ":.*##"; printf "\nUsage:\n make \033[36m<target>\033[0m\n"} /^[a-zA-Z_0-9-]+:.*?##/ { printf " \033[36m%-15s\033[0m %s\n", $$1, $$2 } /^##@/ { printf "\n\033[1m%s\033[0m\n", substr($$0, 5) } ' $(MAKEFILE_LIST)

##@ Release

.PHONY: push-image
push-image: REGISTRY ?= ghcr.io
push-image: SKAFFOLD_OPTS += --profile=release --default-repo=$(REGISTRY)
push-image: skaffold-build ## Build and push the multi-arch image(s).

.PHONY: binfmt-config
binfmt-config: ## Enable multi-arch support on the host.
$(DOCKER) run --rm --privileged multiarch/qemu-user-static:7.2.0-1 --reset -p yes

.PHONY: release ## Build and push multi-arch image
release: SKAFFOLD_OPTS += -t '$(VERSION)'
release: require-version require-clean-worktree manifest-image binfmt-config push-image
make push-image VERSION=latest REGISTRY=$(REGISTRY)

.PHONY: manifest-image
manifest-image: set-version render

.PHONY: set-version
set-version: require-version
$(DOCKER) run --rm -v "$$PWD":/workdir -u "`id -u`:`id -g`" -e VERSION mikefarah/yq:4.29.2 -i '.data.version = env(VERSION)' setters.yaml

.PHONY: require-version
require-version:
@[ ! "$(VERSION)" = '' ] || (echo no VERSION specified >&2; false)

.PHONY: require-clean-worktree
require-clean-worktree:
@[ -z "`git status --untracked-files=no --porcelain`" ] || (\
echo 'ERROR: the build changed files that are tracked by git:'; \
git status --untracked-files=no --porcelain | sed -E 's/^/ /'; \
echo 'Please run `make render` and commit the resulting changes!'; \
false) >&2
69 changes: 69 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
# webapp blueprint

kubemate web application blueprint based on the app blueprint.

## Usage
Create a new application by fetching, configuring and rendering this [kpt](https://kpt.dev/) package.

### Fetch the package
To create a new application `myapp` using this package as a blueprint, run:
```sh
kpt pkg get https://github.com/mgoltzsche/kubemate-app-blueprints.git/packages/webapp[@VERSION] myapp
```

or with `docker`:
```sh
docker run --rm -u `id -u` -e HOME=/tmp -v `pwd`:/data -w /data gcr.io/kpt-dev/kpt:v1.0.0-beta.25 pkg get https://github.com/mgoltzsche/kubemate-app-blueprints.git/packages/webapp myapp
```

See [`kpt pkg get` documentation](https://kpt.dev/reference/cli/pkg/get/).

### Customize the package
Within the fetched package's directory, edit [`setters.yaml`](./setters.yaml) to meet your requirements.
To replace the corresponding values within the manifests and skaffold.yaml, call `make render` or rather [`kpt fn render`](https://kpt.dev/reference/cli/fn/render/).

TL;DR: [Variant Constructor Pattern](https://kpt.dev/guides/variant-constructor-pattern)

## Development

To list the supported targets, run `make help`.

### Prerequisites

* git
* make
* [docker 20+](https://docs.docker.com/engine/install/)
* [kubectl](https://kubernetes.io/docs/tasks/tools/#kubectl)

### Build the application
To build the application container image using [skaffold](https://skaffold.dev), run:
```sh
make image
```

### Deploy the application
To deploy the application using [skaffold](https://skaffold.dev), run:
```sh
make deploy
```
To deploy the application in debug mode (debug ports forwarded), stream its logs and redeploy on source code changes automatically, run:
```sh
make debug
```

To undeploy the application, run:
```sh
make undeploy
```

### Apply blueprint updates
To apply blueprint updates to the application codebase, update the [kpt](https://kpt.dev/) package:
1. Before updating the package, make sure you don't have uncommitted changes in order to be able to distinguish package update changes from others.
2. Call `make blueprint-update` or rather [`kpt pkg update`](https://kpt.dev/reference/cli/pkg/update/) and [`kpt fn render`](https://kpt.dev/reference/cli/fn/render/) (applies the configuration within [`setters.yaml`](./setters.yaml) to the manifests and `skaffold.yaml`).
3. Before committing the changes, review them carefully and make manual changes if necessary.

TL;DR: [Variant Constructor Pattern](https://kpt.dev/guides/variant-constructor-pattern)

## Release

The release process is driven by [Conventional Commits](https://www.conventionalcommits.org/en/v1.0.0-beta.4/), letting the CI pipeline generate a version and publish a release depending on the [commit messages](https://semantic-release.gitbook.io/semantic-release/#commit-message-format) on the `main` branch.
22 changes: 22 additions & 0 deletions beets.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
directory: /data/music
library: /data/music.beets.db

plugins:
- web
- lastgenre
- smartplaylist
- random

web:
host: 0.0.0.0
port: 8337
reverse_proxy: true
#cors: 'http://example.com'

smartplaylist:
relative_to: /data
playlist_dir: /data/playlists
forward_slash: no
playlists:
- name: all.m3u
query: ''
18 changes: 18 additions & 0 deletions deploy/app/configschema.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
apiVersion: apps.kubemate.mgoltzsche.github.com/v1alpha1
kind: AppConfigSchema
metadata: # kpt-merge: /${APP_NAME:=app}
name: ${APP_NAME:=app}
annotations:
internal.kpt.dev/upstream-identifier: 'apps.kubemate.mgoltzsche.github.com|AppConfigSchema|default|${APP_NAME:=app}'

# TODO: define the parameters for your app here
#spec:
# params:
# - name: EXAMPLE_VAR
# type: enum # password|string|enum|number|boolean
# enum:
# - value-a
# - value-b
# title: Example config option
# category: General # tab within the GUI
# description: This is an example option.
9 changes: 9 additions & 0 deletions deploy/app/defaultconfig.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
apiVersion: v1
kind: Secret
metadata: # kpt-merge: /${APP_NAME:=app}-defaultconfig
name: ${APP_NAME:=app}-defaultconfig
annotations:
internal.kpt.dev/upstream-identifier: '|Secret|default|${APP_NAME:=app}-defaultconfig'
# TODO: specify the default configuration for your app
#stringData:
# EXAMPLE_VAR: "value-a"
16 changes: 16 additions & 0 deletions deploy/app/deployment-patch.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
apiVersion: apps/v1
kind: Deployment
metadata: # kpt-merge: /blueprint
name: beets # kpt-set: ${name}
annotations:
internal.kpt.dev/upstream-identifier: 'apps|Deployment|default|blueprint'
spec:
template:
spec:
containers:
- name: app
envFrom:
- secretRef:
name: ${APP_NAME:=app}-defaultconfig
- secretRef:
name: ${APP_CONFIG_SECRET_NAME:=app-defaultconfig}
6 changes: 6 additions & 0 deletions deploy/app/kustomization.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
resources:
- configschema.yaml
- defaultconfig.yaml
- ../default
patchesStrategicMerge:
- deployment-patch.yaml
Loading

0 comments on commit 469cfe4

Please sign in to comment.