From 66dad81d84f787dfeac9c05ef16e103e9be858fd Mon Sep 17 00:00:00 2001 From: Vegard Hagen Date: Mon, 14 Jul 2025 13:38:30 +0200 Subject: [PATCH] feat(devcontainer): split into build and run configfiles Signed-off-by: Vegard Hagen --- .devcontainer/Containerfile | 47 +++++++++++++++++++- .devcontainer/README.md | 5 ++- .devcontainer/devcontainer-build.json | 30 +++++++++++++ .devcontainer/devcontainer.json | 52 ++++------------------- .github/workflows/build-devcontainer.yaml | 4 +- .github/workflows/tofu-format.yaml | 3 -- 6 files changed, 89 insertions(+), 52 deletions(-) create mode 100644 .devcontainer/devcontainer-build.json diff --git a/.devcontainer/Containerfile b/.devcontainer/Containerfile index 7f0fb90..22e7acd 100644 --- a/.devcontainer/Containerfile +++ b/.devcontainer/Containerfile @@ -1,2 +1,45 @@ -#FROM docker.io/ubuntu:24.04 -FROM mcr.microsoft.com/devcontainers/base:ubuntu22.04 +FROM docker.io/ubuntu:24.04 +ARG TARGETARCH + +RUN rm -rf /home/ubuntu + +RUN apt-get update && \ + apt-get install -y --no-install-recommends \ + apt-transport-https \ + ca-certificates \ + curl \ + wget \ + gnupg \ + git + +# Fetch kubectl +RUN curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/${TARGETARCH}/kubectl" && \ + curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/${TARGETARCH}/kubectl.sha256" && \ + echo "$(cat kubectl.sha256) kubectl" | sha256sum --check && \ + install -o root -g root -m 0755 kubectl /usr/local/bin/kubectl && \ + rm kubectl kubectl.sha256 + +# Install kubecolor for some colour https://kubecolor.github.io/ +RUN wget -O /tmp/kubecolor.deb https://kubecolor.github.io/packages/deb/pool/main/k/kubecolor/kubecolor_$(wget -q -O- https://kubecolor.github.io/packages/deb/version)_$(dpkg --print-architecture).deb && \ + dpkg -i /tmp/kubecolor.deb && \ + apt update && \ + rm /tmp/kubecolor.deb + +RUN adduser homelab +USER homelab + +# Install krew, a plugin manager for kubectl https://krew.sigs.k8s.io/ +RUN (set -x; cd "$(mktemp -d)" && \ + KREW="krew-linux_${TARGETARCH}" && \ + curl -fsSLO "https://github.com/kubernetes-sigs/krew/releases/latest/download/${KREW}.tar.gz" && \ + tar zxvf "${KREW}.tar.gz" && \ + ./"${KREW}" install krew ) && \ + echo "export PATH=\"${KREW_ROOT:-$HOME/.krew}/bin:$PATH\"" >> ${HOME}/.bashrc + +RUN PATH="${KREW_ROOT:-$HOME/.krew}/bin:$PATH" kubectl krew install ctx ns oidc-login + +RUN echo "alias k='kubectl'" >> ${HOME}/.bash_aliases && \ + echo "alias kubectl='kubecolor'" >> ${HOME}/.bash_aliases && \ + echo "alias tf='tofu'" >> ${HOME}/.bash_aliases + +USER root diff --git a/.devcontainer/README.md b/.devcontainer/README.md index df054fa..0a75826 100644 --- a/.devcontainer/README.md +++ b/.devcontainer/README.md @@ -1,7 +1,8 @@ # Devcontainer -A container with all the necessary tools and then some used in this homelab. -Check available features at [https://containers.dev/features](https://containers.dev/features), +The `devcontainer.json` file uses a pre-built image based on the configuration in the `devcontainer-build.json` file. + +Check available Devcontainer features at [https://containers.dev/features](https://containers.dev/features), or edit the `Containerfile`. **Disclaimer**: this is a fairly untested feature from the main author, diff --git a/.devcontainer/devcontainer-build.json b/.devcontainer/devcontainer-build.json new file mode 100644 index 0000000..a0c9752 --- /dev/null +++ b/.devcontainer/devcontainer-build.json @@ -0,0 +1,30 @@ +{ + "name": "Stonegarden Homelab Devcontainer", + "build": { + "dockerfile": "Containerfile" + }, + "features": { + "ghcr.io/devcontainers/features/common-utils:2": { + "installZsh": true, + "configureZshAsDefaultShell": true, + "installOhMyZsh": true, + "installOhMyZshConfig": true, + "upgradePackages": true, + "username": "homelab" + }, + "ghcr.io/devcontainers/features/github-cli:1": {}, + "ghcr.io/devcontainers/features/go:1": {}, + "ghcr.io/devcontainers/features/java:1": {}, + "ghcr.io/devcontainers-extra/features/argo-cd:1": {}, + "ghcr.io/devcontainers-extra/features/talosctl:1": {}, + "ghcr.io/devcontainers-extra/features/yamllint:2": {}, + "ghcr.io/dhoeric/features/k9s:1": {}, + "ghcr.io/eitsupi/devcontainer-features/jq-likes": { + "jqVersion": "latest", + "yqVersion": "latest" + }, + "ghcr.io/robbert229/devcontainer-features/postgresql-client:1": {}, + "ghcr.io/robbert229/devcontainer-features/opentofu:1": {} + }, + "remoteUser": "homelab" +} diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json index e1e931e..7473006 100644 --- a/.devcontainer/devcontainer.json +++ b/.devcontainer/devcontainer.json @@ -1,46 +1,12 @@ { - "name": "Stonegarden Homelab Devcontainer", - "build": { - "dockerfile": "Containerfile", - }, - "features": { - "ghcr.io/devcontainers/features/common-utils:2": { - "installZsh": true, - "configureZshAsDefaultShell": true, - "installOhMyZsh": true, - "installOhMyZshConfig": true, - "upgradePackages": true, - "username": "homelab" - }, - "ghcr.io/devcontainers/features/aws-cli:1": {}, - "ghcr.io/devcontainers/features/azure-cli:1": {}, - "ghcr.io/devcontainers/features/github-cli:1": {}, - "ghcr.io/devcontainers/features/git:1": {}, - "ghcr.io/devcontainers/features/go:1": {}, - "ghcr.io/devcontainers/features/java:1": {}, - "ghcr.io/devcontainers/features/python:1": {}, - "ghcr.io/devcontainers/features/hugo:1": { - "extended": "true" - }, - "ghcr.io/devcontainers-extra/features/argo-cd:1": {}, - "ghcr.io/devcontainers-extra/features/helmfile:1": {}, - "ghcr.io/devcontainers-extra/features/kubectl-asdf:2": {}, - "ghcr.io/devcontainers-extra/features/kubectx-kubens:1": {}, - "ghcr.io/devcontainers-extra/features/talosctl:1": {}, - "ghcr.io/devcontainers-extra/features/yamllint:2": {}, - "ghcr.io/dhoeric/features/k9s:1": {}, - "ghcr.io/dhoeric/features/google-cloud-cli:1": { - "installGkeGcloudAuthPlugin": true - }, - "ghcr.io/eitsupi/devcontainer-features/jq-likes": { - "jqVersion": "latest", - "yqVersion": "latest" - }, - "ghcr.io/robbert229/devcontainer-features/postgresql-client:1": {}, - "ghcr.io/robbert229/devcontainer-features/opentofu:1": {}, - "ghcr.io/schlich/devcontainer-features/powerlevel10k:1": {}, - "ghcr.io/skriptfabrik/devcontainer-features/hcloud-cli:1": {}, - "ghcr.io/skriptfabrik/devcontainer-features/infisical-cli:1.1.1": {}, - }, + "name": "Homelab Devcontainer", + "image": "ghcr.io/vehagn/homelab-devcontainer", + "mounts": [ + { + "source": "${env:HOME}${env:USERPROFILE}/.kube", + "target": "/home/homelab/.kube", + "type": "bind" + } + ], "remoteUser": "homelab" } diff --git a/.github/workflows/build-devcontainer.yaml b/.github/workflows/build-devcontainer.yaml index a83d3fe..7154669 100644 --- a/.github/workflows/build-devcontainer.yaml +++ b/.github/workflows/build-devcontainer.yaml @@ -1,7 +1,6 @@ name: Build devcontainer on: - workflow_dispatch: { } push: branches: [ main ] paths: [ ".devcontainer/**" ] @@ -12,7 +11,7 @@ jobs: build: name: Build devcontainer runs-on: ubuntu-latest - timeout-minutes: 240 + timeout-minutes: 120 permissions: contents: read packages: write @@ -37,6 +36,7 @@ jobs: - name: Pre-build dev container image uses: devcontainers/ci@8bf61b26e9c3a98f69cb6ce2f88d24ff59b785c6 #v0.3.1900000417 with: + configFile: ./.devcontainer/devcontainer-build.json platform: linux/amd64,linux/arm64 imageName: ghcr.io/vehagn/homelab-devcontainer cacheFrom: ghcr.io/vehagn/homelab-devcontainer diff --git a/.github/workflows/tofu-format.yaml b/.github/workflows/tofu-format.yaml index b4c00c6..dad38e6 100644 --- a/.github/workflows/tofu-format.yaml +++ b/.github/workflows/tofu-format.yaml @@ -39,7 +39,6 @@ jobs: - name: Tofu init uses: devcontainers/ci@8bf61b26e9c3a98f69cb6ce2f88d24ff59b785c6 #v0.3.1900000417 with: - imageName: ghcr.io/vehagn/homelab-devcontainer cacheFrom: ghcr.io/vehagn/homelab-devcontainer push: never runCmd: tofu -chdir=${{ env.TOFU_DIR }} init @@ -47,7 +46,6 @@ jobs: - name: Tofu validate uses: devcontainers/ci@8bf61b26e9c3a98f69cb6ce2f88d24ff59b785c6 #v0.3.1900000417 with: - imageName: ghcr.io/vehagn/homelab-devcontainer cacheFrom: ghcr.io/vehagn/homelab-devcontainer push: never runCmd: tofu -chdir=${{ env.TOFU_DIR }} validate @@ -55,7 +53,6 @@ jobs: - name: Tofu format uses: devcontainers/ci@8bf61b26e9c3a98f69cb6ce2f88d24ff59b785c6 #v0.3.1900000417 with: - imageName: ghcr.io/vehagn/homelab-devcontainer cacheFrom: ghcr.io/vehagn/homelab-devcontainer push: never runCmd: tofu -chdir=${{ env.TOFU_DIR }} fmt -recursive -diff -check