Skip to content

Commit 97376f5

Browse files
authored
Merge pull request #52 from jsburckhardt/copilot/fix-51
feat: Add k3d feature to devcontainer features
2 parents 6aa02f6 + 37ddbaf commit 97376f5

7 files changed

Lines changed: 146 additions & 0 deletions

File tree

.github/workflows/test.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ jobs:
2020
- gic
2121
- gitleaks
2222
- jnv
23+
- k3d
2324
- kyverno
2425
- notation
2526
- ruff

README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ This repository contains a _collection_ of Features.
1313
| crane | https://github.com/google/go-containerregistry/blob/main/cmd/crane/README.md | crane is a tool for interacting with remote images and registries.|
1414
| skopeo | https://github.com/containers/skopeo | skopeo is a command line utility that performs various operations on container images and image repositories. It is install through package managers |
1515
| kyverno | https://kyverno.io/docs/introduction/ | Kyverno (Greek for “govern”) is a policy engine designed specifically for Kubernetes. |
16+
| k3d | https://k3d.io/ | K3d is a lightweight wrapper to run k3s (Rancher Lab's minimal Kubernetes distribution) in docker. |
1617
| cyclonedx | https://cyclonedx.org/ | cyclonedx is a command-line tool for working with Software Bill of Materials (SBOM). |
1718
| Copacelic | https://project-copacetic.github.io/copacetic/website/ | Project Copacetic: Directly patch container image vulnerabilities. Copa is a CLI tool written in Go and based on buildkit that can be used to directly patch container images given the vulnerability scanning results from popular tools like Trivy. |
1819
| Gic | https://github.com/jsburckhardt/gic | Reducing cognitive load by automating commit message generation, allowing developers to focus on coding instead of crafting messages. |
@@ -246,6 +247,23 @@ Running `ruff` inside the built container will print the help menu of ruff.
246247
ruff --version
247248
```
248249

250+
251+
### `k3d`
252+
253+
Running `k3d` inside the built container will print the help menu of k3d.
254+
255+
```jsonc
256+
{
257+
"image": "mcr.microsoft.com/devcontainers/base:ubuntu",
258+
"features": {
259+
"ghcr.io/jsburckhardt/devcontainer-features/k3d:1": {}
260+
}
261+
}
262+
```
263+
264+
```bash
265+
k3d --version
266+
```
249267
### `Codex-CLI`
250268

251269
Running `codex` inside the built container will print the help menu of codex.

src/k3d/devcontainer-feature.json

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"name": "K3D",
3+
"id": "k3d",
4+
"version": "1.0.0",
5+
"description": "K3d is a lightweight wrapper to run k3s (Rancher Lab's minimal Kubernetes distribution) in docker.",
6+
"options": {
7+
"version": {
8+
"type": "string",
9+
"default": "latest",
10+
"description": "Version of k3d to install."
11+
}
12+
}
13+
}

src/k3d/install.sh

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
#!/usr/bin/env bash
2+
3+
K3D_VERSION="${VERSION:-"latest"}"
4+
GITHUB_API_REPO_URL="https://api.github.com/repos/k3d-io/k3d/releases"
5+
URL_RELEASES="https://github.com/k3d-io/k3d/releases"
6+
7+
set -e
8+
9+
# Clean up
10+
rm -rf /var/lib/apt/lists/*
11+
12+
ARCH="$(uname -m)"
13+
# Map the architecture to the format used by k3d
14+
case ${ARCH} in
15+
x86_64) ARCH="amd64" ;;
16+
i686) ARCH="386" ;;
17+
armv7*) ARCH="arm" ;;
18+
aarch64) ARCH="arm64" ;;
19+
*)
20+
echo "(!) Architecture ${ARCH} unsupported"
21+
exit 1
22+
;;
23+
esac
24+
25+
# Check if linux/windows/macOS
26+
OS="$(uname -s)"
27+
case ${OS} in
28+
Linux) OS="linux" ;;
29+
Darwin) OS="darwin" ;;
30+
*)
31+
echo "(!) Platform ${OS} unsupported"
32+
exit 1
33+
;;
34+
esac
35+
36+
if [ "$(id -u)" -ne 0 ]; then
37+
echo -e 'Script must be run as root. Use sudo, su, or add "USER root" to your Dockerfile before running this script.'
38+
exit 1
39+
fi
40+
41+
# Checks if packages are installed and installs them if not
42+
check_packages() {
43+
if ! dpkg -s "$@" >/dev/null 2>&1; then
44+
if [ "$(find /var/lib/apt/lists/* | wc -l)" = "0" ]; then
45+
echo "Running apt-get update..."
46+
apt-get update -y
47+
fi
48+
apt-get -y install --no-install-recommends "$@"
49+
fi
50+
}
51+
52+
# Figure out correct version of a three part version number is not passed
53+
validate_version_exists() {
54+
local variable_name=$1
55+
local requested_version=${2}
56+
if [ "${requested_version}" = "latest" ]; then
57+
requested_version=$(curl -sL ${GITHUB_API_REPO_URL}/latest | jq -r ".tag_name")
58+
echo "Latest version is ${requested_version}"
59+
fi
60+
local version_list
61+
version_list=$(curl -sL ${GITHUB_API_REPO_URL} | jq -r ".[].tag_name")
62+
if ! echo "${version_list}" | grep "${requested_version}" >/dev/null 2>&1; then
63+
echo -e "Invalid ${variable_name} value: ${requested_version}\nValid values:\n${version_list}" >&2
64+
exit 1
65+
fi
66+
echo "${variable_name}=${requested_version}"
67+
}
68+
69+
# Make sure we have curl and jq
70+
check_packages curl jq ca-certificates
71+
72+
# Make sure version is available
73+
if [ "${K3D_VERSION}" = "latest" ]; then
74+
K3D_VERSION=$(curl -sL ${GITHUB_API_REPO_URL}/latest | jq -r ".tag_name")
75+
echo "Latest version is ${K3D_VERSION}"
76+
fi
77+
validate_version_exists K3D_VERSION "${K3D_VERSION}"
78+
79+
# Download and install binary
80+
K3D_DIST="k3d-${OS}-${ARCH}"
81+
echo "Downloading ${K3D_DIST} version ${K3D_VERSION}..."
82+
83+
url="${URL_RELEASES}/download/${K3D_VERSION}/${K3D_DIST}"
84+
echo "Downloading ${url}..."
85+
curl -sSL --insecure "$url" -o /usr/local/bin/k3d
86+
chmod +x /usr/local/bin/k3d
87+
88+
# Clean up
89+
rm -rf /var/lib/apt/lists/*
90+
91+
echo "Done!"
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/bin/bash
2+
3+
set -e
4+
source dev-container-features-test-lib
5+
check "k3d with specific version" /bin/bash -c "k3d version | grep '5.6.0'"
6+
7+
reportResults

test/_global/scenarios.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
"gic": {},
1010
"gitleaks": {},
1111
"jnv": {},
12+
"k3d": {},
1213
"kyverno": {},
1314
"notation": {},
1415
"ruff": {},
@@ -111,5 +112,13 @@
111112
"version": "v0.41.0"
112113
}
113114
}
115+
},
116+
"k3d-specific-version": {
117+
"image": "mcr.microsoft.com/devcontainers/base:ubuntu",
118+
"features": {
119+
"k3d": {
120+
"version": "v5.6.0"
121+
}
122+
}
114123
}
115124
}

test/k3d/test.sh

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/bin/bash
2+
3+
set -e
4+
5+
source dev-container-features-test-lib
6+
check "k3d" k3d version
7+
reportResults

0 commit comments

Comments
 (0)