Skip to content

Commit

Permalink
New feature: Kubernetes Tools (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
lasuillard authored Nov 17, 2024
1 parent 328f265 commit 3c226b9
Show file tree
Hide file tree
Showing 7 changed files with 185 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ jobs:
- aws-ssm-session-manager-plugin
- openapi-generator-cli
- opentofu
- kubernetes-tools
baseImage:
- debian:latest
steps:
Expand All @@ -52,6 +53,7 @@ jobs:
- aws-ssm-session-manager-plugin
- openapi-generator-cli
- opentofu
- kubernetes-tools
steps:
- uses: actions/checkout@v4

Expand Down
48 changes: 48 additions & 0 deletions src/kubernetes-tools/devcontainer-feature.json
Original file line number Diff line number Diff line change
@@ -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"
]
}
}
}
72 changes: 72 additions & 0 deletions src/kubernetes-tools/install.sh
Original file line number Diff line number Diff line change
@@ -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
13 changes: 13 additions & 0 deletions test/kubernetes-tools/full.sh
Original file line number Diff line number Diff line change
@@ -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
24 changes: 24 additions & 0 deletions test/kubernetes-tools/scenarios.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
}
}
13 changes: 13 additions & 0 deletions test/kubernetes-tools/specific-version.sh
Original file line number Diff line number Diff line change
@@ -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
13 changes: 13 additions & 0 deletions test/kubernetes-tools/test.sh
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 3c226b9

Please sign in to comment.