diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 241d6a5..d1a3a7a 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -32,6 +32,7 @@ jobs: - aws-ssm-session-manager-plugin - openapi-generator-cli - opentofu + - kubernetes-tools baseImage: - debian:latest steps: @@ -52,6 +53,7 @@ jobs: - aws-ssm-session-manager-plugin - openapi-generator-cli - opentofu + - kubernetes-tools steps: - uses: actions/checkout@v4 diff --git a/src/kubernetes-tools/devcontainer-feature.json b/src/kubernetes-tools/devcontainer-feature.json new file mode 100644 index 0000000..b487cfc --- /dev/null +++ b/src/kubernetes-tools/devcontainer-feature.json @@ -0,0 +1,48 @@ +{ + "name": "Kubernetes Tools", + "id": "kubernetes-tools", + "version": "0.1.0", + "description": "A feature to install K8s tools.", + "options": { + "kubectl-version": { + "type": "string", + "default": "latest", + "description": "The version of kubectl to install, in format of 'v*'." + }, + "install-krew": { + "type": "string", + "proposals": [ + "latest", + "none" + ], + "default": "none", + "description": "Whether to install Krew. Use 'latest' to install the latest version. Use 'none' to skip installation." + }, + "install-k9s": { + "type": "string", + "proposals": [ + "latest", + "none" + ], + "default": "none", + "description": "Whether to install k9s. Use 'latest' to install the latest version. Use 'none' to skip installation." + }, + "install-helm": { + "type": "string", + "proposals": [ + "latest", + "none" + ], + "default": "none", + "description": "Whether to install Helm. Use 'latest' to install the latest version. Use 'none' to skip installation." + } + }, + "customizations": { + "vscode": { + "extensions": [ + "ms-kubernetes-tools.vscode-kubernetes-tools", + "Tim-Koehler.helm-intellisense" + ] + } + } +} diff --git a/src/kubernetes-tools/install.sh b/src/kubernetes-tools/install.sh new file mode 100644 index 0000000..83a9c25 --- /dev/null +++ b/src/kubernetes-tools/install.sh @@ -0,0 +1,72 @@ +#!/bin/sh + +set -e + +ARCH=$(dpkg --print-architecture) + +if ! command -v curl || ! command -v git; then + echo "Installing curl, and git" + apt-get update && apt-get install -y curl git +fi + +# kubectl +# ---------------------------------------------------------------------------- +echo "Installing kubectl" + +if [ "$KUBECTL_VERSION" = "latest" ]; then + KUBECTL_VERSION="$(curl -L -s https://dl.k8s.io/release/stable.txt)" +fi + +curl -L --output /usr/bin/kubectl "https://dl.k8s.io/release/${KUBECTL_VERSION}/bin/linux/${ARCH}/kubectl" && + chmod +x /usr/bin/kubectl + +# Krew +# ---------------------------------------------------------------------------- +if [ "$INSTALL_KREW" != "none" ]; then + if [ "$INSTALL_KREW" = "latest" ]; then + krew_dl_url="$( + curl -s https://api.github.com/repos/kubernetes-sigs/krew/releases/latest | + grep -Eo -m 1 "https://.+krew-linux_${ARCH}\.tar\.gz" + )" + else + krew_dl_url="https://github.com/kubernetes-sigs/krew/releases/download/${INSTALL_KREW}/krew-linux_${ARCH}.tar.gz" + fi + + echo "Installing Krew from $krew_dl_url" + ( + set -x + cd "$(mktemp -d)" && + KREW="krew-linux_${ARCH}" && + curl -fsSLO "$krew_dl_url" && + tar zxvf "${KREW}.tar.gz" && + mv ./"${KREW}" /usr/local/bin/krew + ) +fi + +# K9s +# ---------------------------------------------------------------------------- +if [ "$INSTALL_K9S" != "none" ]; then + if [ "$INSTALL_K9S" = "latest" ]; then + k9s_dl_url="$( + curl -s https://api.github.com/repos/derailed/k9s/releases/latest | + grep -Eo -m 1 "https://.+k9s_linux_${ARCH}\.deb" + )" + else + k9s_dl_url="https://github.com/derailed/k9s/releases/download/${INSTALL_K9S}/k9s_linux_${ARCH}.deb" + fi + + echo "Downloading K9s from $k9s_dl_url" + curl -L --output /tmp/k9s.deb "$k9s_dl_url" && + dpkg --install /tmp/k9s.deb && + rm -f /tmp/k9s.deb +fi + +# Helm +# ---------------------------------------------------------------------------- +if [ "$INSTALL_HELM" != "none" ]; then + helm_version=$([ "$INSTALL_HELM" = "latest" ] && echo '' || echo "$INSTALL_HELM") + + echo "Installing Helm $helm_version" + curl -fsSL https://raw.githubusercontent.com/helm/helm/main/scripts/get-helm-3 | + DESIRED_VERSION="$helm_version" bash +fi diff --git a/test/kubernetes-tools/full.sh b/test/kubernetes-tools/full.sh new file mode 100644 index 0000000..3660987 --- /dev/null +++ b/test/kubernetes-tools/full.sh @@ -0,0 +1,13 @@ +#!/bin/bash + +set -e + +# shellcheck source=/dev/null +source dev-container-features-test-lib + +check "verify kubectl installation" kubectl version --client | grep -E 'Client Version: v.+' +check "verify Krew installation" krew version | grep -E 'GitTag\s+v.+' +check "verify k9s installation" k9s version --short | grep -E 'Version\s+v.+' +check "verify Helm installation" helm version --short | grep -E 'v.+' + +reportResults diff --git a/test/kubernetes-tools/scenarios.json b/test/kubernetes-tools/scenarios.json new file mode 100644 index 0000000..1d1099f --- /dev/null +++ b/test/kubernetes-tools/scenarios.json @@ -0,0 +1,24 @@ +{ + "full": { + "image": "mcr.microsoft.com/devcontainers/base:bookworm", + "features": { + "kubernetes-tools": { + "kubectl-version": "latest", + "install-krew": "latest", + "install-k9s": "latest", + "install-helm": "latest" + } + } + }, + "specific-version": { + "image": "mcr.microsoft.com/devcontainers/base:bookworm", + "features": { + "kubernetes-tools": { + "kubectl-version": "v1.31.1", + "install-krew": "v0.4.3", + "install-k9s": "v0.32.4", + "install-helm": "v3.16.2" + } + } + } +} diff --git a/test/kubernetes-tools/specific-version.sh b/test/kubernetes-tools/specific-version.sh new file mode 100644 index 0000000..7c41b2c --- /dev/null +++ b/test/kubernetes-tools/specific-version.sh @@ -0,0 +1,13 @@ +#!/bin/bash + +set -e + +# shellcheck source=/dev/null +source dev-container-features-test-lib + +check "verify kubectl installation" kubectl version --client | grep 'Client Version: v1.31.1' +check "verify Krew installation" krew version | grep -E 'GitTag\s+v0\.4\.3' +check "verify k9s installation" k9s version --short | grep -E 'Version\s+v0\.32\.4' +check "verify Helm installation" helm version --short | grep 'v3.16.2' + +reportResults diff --git a/test/kubernetes-tools/test.sh b/test/kubernetes-tools/test.sh new file mode 100644 index 0000000..d98bf42 --- /dev/null +++ b/test/kubernetes-tools/test.sh @@ -0,0 +1,13 @@ +#!/bin/bash + +set -e + +# shellcheck source=/dev/null +source dev-container-features-test-lib + +check "verify kubectl installation" kubectl version --client | grep -E 'Client Version: v.+' +check "do not install Krew" command krew 2>&1 | grep 'krew: command not found' +check "do not install k9s" command k9s 2>&1 | grep 'k9s: command not found' +check "do not install Helm" command helm 2>&1 | grep 'helm: command not found' + +reportResults