Skip to content

Commit 2cf9619

Browse files
chore(go/v4): Add autocomplete support in .devcontainer/post-install.sh
Add bash autocomplete for development tools (kind, kubebuilder, kubectl, docker) in the DevContainer post-install script to improve CLI usability during development. This change also refactors the post-install script to be more robust and explicit about its root user requirement.
1 parent d0c8fc7 commit 2cf9619

File tree

14 files changed

+390
-77
lines changed

14 files changed

+390
-77
lines changed

.github/workflows/test-devcontainer.yml

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ jobs:
2424
with:
2525
subFolder: testdata/project-v4
2626
runCmd: |
27+
# Source bash-completion for all tests
28+
source /usr/share/bash-completion/bash_completion
29+
2730
# Verify Tools Installation
2831
echo "Verifying installed tools..."
2932
docker --version
@@ -32,6 +35,43 @@ jobs:
3235
kubectl version --client
3336
go version
3437
echo "All required tools are installed"
38+
39+
# Verify common-utils feature is installed
40+
echo "Verifying common-utils feature..."
41+
if ! command -v zsh &> /dev/null; then
42+
echo "ERROR: common-utils feature not installed (zsh missing)"
43+
exit 1
44+
fi
45+
echo "common-utils feature is installed"
46+
47+
# Verify bash-completion setup
48+
echo "Verifying bash-completion configuration..."
49+
if ! grep -q "source /usr/share/bash-completion/bash_completion" ~/.bashrc; then
50+
echo "ERROR: bash-completion not configured in .bashrc"
51+
exit 1
52+
fi
53+
echo "bash-completion is configured in .bashrc"
54+
55+
# Verify completion files exist
56+
echo "Verifying completion files..."
57+
for cmd in kubectl kind kubebuilder docker; do
58+
if [ ! -f "/usr/share/bash-completion/completions/${cmd}" ]; then
59+
echo "ERROR: Completion file for ${cmd} not found"
60+
exit 1
61+
fi
62+
echo "${cmd} completion file exists"
63+
done
64+
echo "All completion files are present"
65+
66+
# Test bash-completion works
67+
echo "Testing bash-completion functionality..."
68+
# Bash-completion uses lazy-loading, so manually source completion to test
69+
source /usr/share/bash-completion/completions/kubectl
70+
if ! complete -p kubectl &> /dev/null; then
71+
echo "ERROR: kubectl completions not loaded"
72+
exit 1
73+
fi
74+
echo "Bash completions are working"
3575
3676
# Test Docker-in-Docker
3777
echo "Testing Docker-in-Docker functionality..."

docs/book/src/cronjob-tutorial/testdata/project/.devcontainer/devcontainer.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@
66
"moby": false,
77
"dockerDefaultAddressPool": "base=172.30.0.0/16,size=24"
88
},
9-
"ghcr.io/devcontainers/features/git:1": {}
9+
"ghcr.io/devcontainers/features/git:1": {},
10+
"ghcr.io/devcontainers/features/common-utils:2": {
11+
"upgradePackages": true
12+
}
1013
},
1114

1215
"runArgs": ["--privileged", "--init"],

docs/book/src/cronjob-tutorial/testdata/project/.devcontainer/post-install.sh

Lines changed: 46 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,65 @@ set -euo pipefail
33

44
echo "Installing Kubebuilder development tools..."
55

6-
ARCH=$(go env GOARCH)
6+
# Verify running as root (required for installing to /usr/local/bin and /etc)
7+
if [ "$(id -u)" -ne 0 ]; then
8+
echo "ERROR: This script must be run as root"
9+
exit 1
10+
fi
11+
12+
# Detect architecture using uname (more reliable than 'go env' during container init)
13+
MACHINE=$(uname -m)
14+
case "${MACHINE}" in
15+
x86_64)
16+
ARCH="amd64"
17+
;;
18+
aarch64|arm64)
19+
ARCH="arm64"
20+
;;
21+
*)
22+
echo "WARNING: Unsupported architecture ${MACHINE}, defaulting to amd64"
23+
ARCH="amd64"
24+
;;
25+
esac
26+
27+
BASH_COMPLETIONS_DIR="/usr/share/bash-completion/completions"
28+
29+
# Enable bash-completion in .bashrc
30+
if ! grep -q "source /usr/share/bash-completion/bash_completion" ~/.bashrc 2>/dev/null; then
31+
echo 'source /usr/share/bash-completion/bash_completion' >> ~/.bashrc
32+
echo "Added bash-completion to .bashrc"
33+
fi
734

835
# Install kind
936
if ! command -v kind &> /dev/null; then
10-
curl -Lo ./kind "https://kind.sigs.k8s.io/dl/latest/kind-linux-${ARCH}"
11-
chmod +x ./kind
12-
mv ./kind /usr/local/bin/kind
37+
TMP_KIND=$(mktemp)
38+
curl -Lo "${TMP_KIND}" "https://kind.sigs.k8s.io/dl/latest/kind-linux-${ARCH}"
39+
chmod +x "${TMP_KIND}"
40+
mv "${TMP_KIND}" /usr/local/bin/kind
1341
fi
42+
kind completion bash > "${BASH_COMPLETIONS_DIR}/kind" 2>/dev/null || true
1443

1544
# Install kubebuilder
1645
if ! command -v kubebuilder &> /dev/null; then
17-
curl -L -o kubebuilder "https://go.kubebuilder.io/dl/latest/linux/${ARCH}"
18-
chmod +x kubebuilder
19-
mv kubebuilder /usr/local/bin/
46+
TMP_KB=$(mktemp)
47+
curl -L -o "${TMP_KB}" "https://go.kubebuilder.io/dl/latest/linux/${ARCH}"
48+
chmod +x "${TMP_KB}"
49+
mv "${TMP_KB}" /usr/local/bin/kubebuilder
2050
fi
51+
kubebuilder completion bash > "${BASH_COMPLETIONS_DIR}/kubebuilder" 2>/dev/null || true
2152

2253
# Install kubectl
2354
if ! command -v kubectl &> /dev/null; then
2455
KUBECTL_VERSION=$(curl -L -s https://dl.k8s.io/release/stable.txt)
25-
curl -LO "https://dl.k8s.io/release/${KUBECTL_VERSION}/bin/linux/${ARCH}/kubectl"
26-
chmod +x kubectl
27-
mv kubectl /usr/local/bin/kubectl
56+
TMP_KUBECTL=$(mktemp)
57+
curl -Lo "${TMP_KUBECTL}" "https://dl.k8s.io/release/${KUBECTL_VERSION}/bin/linux/${ARCH}/kubectl"
58+
chmod +x "${TMP_KUBECTL}"
59+
mv "${TMP_KUBECTL}" /usr/local/bin/kubectl
2860
fi
61+
kubectl completion bash > "${BASH_COMPLETIONS_DIR}/kubectl" 2>/dev/null || true
62+
63+
# Docker completion
64+
docker completion bash > "${BASH_COMPLETIONS_DIR}/docker" 2>/dev/null || true
2965

3066
# Wait for Docker to be ready
3167
for i in {1..30}; do

docs/book/src/getting-started/testdata/project/.devcontainer/devcontainer.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@
66
"moby": false,
77
"dockerDefaultAddressPool": "base=172.30.0.0/16,size=24"
88
},
9-
"ghcr.io/devcontainers/features/git:1": {}
9+
"ghcr.io/devcontainers/features/git:1": {},
10+
"ghcr.io/devcontainers/features/common-utils:2": {
11+
"upgradePackages": true
12+
}
1013
},
1114

1215
"runArgs": ["--privileged", "--init"],

docs/book/src/getting-started/testdata/project/.devcontainer/post-install.sh

Lines changed: 46 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,65 @@ set -euo pipefail
33

44
echo "Installing Kubebuilder development tools..."
55

6-
ARCH=$(go env GOARCH)
6+
# Verify running as root (required for installing to /usr/local/bin and /etc)
7+
if [ "$(id -u)" -ne 0 ]; then
8+
echo "ERROR: This script must be run as root"
9+
exit 1
10+
fi
11+
12+
# Detect architecture using uname (more reliable than 'go env' during container init)
13+
MACHINE=$(uname -m)
14+
case "${MACHINE}" in
15+
x86_64)
16+
ARCH="amd64"
17+
;;
18+
aarch64|arm64)
19+
ARCH="arm64"
20+
;;
21+
*)
22+
echo "WARNING: Unsupported architecture ${MACHINE}, defaulting to amd64"
23+
ARCH="amd64"
24+
;;
25+
esac
26+
27+
BASH_COMPLETIONS_DIR="/usr/share/bash-completion/completions"
28+
29+
# Enable bash-completion in .bashrc
30+
if ! grep -q "source /usr/share/bash-completion/bash_completion" ~/.bashrc 2>/dev/null; then
31+
echo 'source /usr/share/bash-completion/bash_completion' >> ~/.bashrc
32+
echo "Added bash-completion to .bashrc"
33+
fi
734

835
# Install kind
936
if ! command -v kind &> /dev/null; then
10-
curl -Lo ./kind "https://kind.sigs.k8s.io/dl/latest/kind-linux-${ARCH}"
11-
chmod +x ./kind
12-
mv ./kind /usr/local/bin/kind
37+
TMP_KIND=$(mktemp)
38+
curl -Lo "${TMP_KIND}" "https://kind.sigs.k8s.io/dl/latest/kind-linux-${ARCH}"
39+
chmod +x "${TMP_KIND}"
40+
mv "${TMP_KIND}" /usr/local/bin/kind
1341
fi
42+
kind completion bash > "${BASH_COMPLETIONS_DIR}/kind" 2>/dev/null || true
1443

1544
# Install kubebuilder
1645
if ! command -v kubebuilder &> /dev/null; then
17-
curl -L -o kubebuilder "https://go.kubebuilder.io/dl/latest/linux/${ARCH}"
18-
chmod +x kubebuilder
19-
mv kubebuilder /usr/local/bin/
46+
TMP_KB=$(mktemp)
47+
curl -L -o "${TMP_KB}" "https://go.kubebuilder.io/dl/latest/linux/${ARCH}"
48+
chmod +x "${TMP_KB}"
49+
mv "${TMP_KB}" /usr/local/bin/kubebuilder
2050
fi
51+
kubebuilder completion bash > "${BASH_COMPLETIONS_DIR}/kubebuilder" 2>/dev/null || true
2152

2253
# Install kubectl
2354
if ! command -v kubectl &> /dev/null; then
2455
KUBECTL_VERSION=$(curl -L -s https://dl.k8s.io/release/stable.txt)
25-
curl -LO "https://dl.k8s.io/release/${KUBECTL_VERSION}/bin/linux/${ARCH}/kubectl"
26-
chmod +x kubectl
27-
mv kubectl /usr/local/bin/kubectl
56+
TMP_KUBECTL=$(mktemp)
57+
curl -Lo "${TMP_KUBECTL}" "https://dl.k8s.io/release/${KUBECTL_VERSION}/bin/linux/${ARCH}/kubectl"
58+
chmod +x "${TMP_KUBECTL}"
59+
mv "${TMP_KUBECTL}" /usr/local/bin/kubectl
2860
fi
61+
kubectl completion bash > "${BASH_COMPLETIONS_DIR}/kubectl" 2>/dev/null || true
62+
63+
# Docker completion
64+
docker completion bash > "${BASH_COMPLETIONS_DIR}/docker" 2>/dev/null || true
2965

3066
# Wait for Docker to be ready
3167
for i in {1..30}; do

docs/book/src/multiversion-tutorial/testdata/project/.devcontainer/devcontainer.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@
66
"moby": false,
77
"dockerDefaultAddressPool": "base=172.30.0.0/16,size=24"
88
},
9-
"ghcr.io/devcontainers/features/git:1": {}
9+
"ghcr.io/devcontainers/features/git:1": {},
10+
"ghcr.io/devcontainers/features/common-utils:2": {
11+
"upgradePackages": true
12+
}
1013
},
1114

1215
"runArgs": ["--privileged", "--init"],

docs/book/src/multiversion-tutorial/testdata/project/.devcontainer/post-install.sh

Lines changed: 46 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,65 @@ set -euo pipefail
33

44
echo "Installing Kubebuilder development tools..."
55

6-
ARCH=$(go env GOARCH)
6+
# Verify running as root (required for installing to /usr/local/bin and /etc)
7+
if [ "$(id -u)" -ne 0 ]; then
8+
echo "ERROR: This script must be run as root"
9+
exit 1
10+
fi
11+
12+
# Detect architecture using uname (more reliable than 'go env' during container init)
13+
MACHINE=$(uname -m)
14+
case "${MACHINE}" in
15+
x86_64)
16+
ARCH="amd64"
17+
;;
18+
aarch64|arm64)
19+
ARCH="arm64"
20+
;;
21+
*)
22+
echo "WARNING: Unsupported architecture ${MACHINE}, defaulting to amd64"
23+
ARCH="amd64"
24+
;;
25+
esac
26+
27+
BASH_COMPLETIONS_DIR="/usr/share/bash-completion/completions"
28+
29+
# Enable bash-completion in .bashrc
30+
if ! grep -q "source /usr/share/bash-completion/bash_completion" ~/.bashrc 2>/dev/null; then
31+
echo 'source /usr/share/bash-completion/bash_completion' >> ~/.bashrc
32+
echo "Added bash-completion to .bashrc"
33+
fi
734

835
# Install kind
936
if ! command -v kind &> /dev/null; then
10-
curl -Lo ./kind "https://kind.sigs.k8s.io/dl/latest/kind-linux-${ARCH}"
11-
chmod +x ./kind
12-
mv ./kind /usr/local/bin/kind
37+
TMP_KIND=$(mktemp)
38+
curl -Lo "${TMP_KIND}" "https://kind.sigs.k8s.io/dl/latest/kind-linux-${ARCH}"
39+
chmod +x "${TMP_KIND}"
40+
mv "${TMP_KIND}" /usr/local/bin/kind
1341
fi
42+
kind completion bash > "${BASH_COMPLETIONS_DIR}/kind" 2>/dev/null || true
1443

1544
# Install kubebuilder
1645
if ! command -v kubebuilder &> /dev/null; then
17-
curl -L -o kubebuilder "https://go.kubebuilder.io/dl/latest/linux/${ARCH}"
18-
chmod +x kubebuilder
19-
mv kubebuilder /usr/local/bin/
46+
TMP_KB=$(mktemp)
47+
curl -L -o "${TMP_KB}" "https://go.kubebuilder.io/dl/latest/linux/${ARCH}"
48+
chmod +x "${TMP_KB}"
49+
mv "${TMP_KB}" /usr/local/bin/kubebuilder
2050
fi
51+
kubebuilder completion bash > "${BASH_COMPLETIONS_DIR}/kubebuilder" 2>/dev/null || true
2152

2253
# Install kubectl
2354
if ! command -v kubectl &> /dev/null; then
2455
KUBECTL_VERSION=$(curl -L -s https://dl.k8s.io/release/stable.txt)
25-
curl -LO "https://dl.k8s.io/release/${KUBECTL_VERSION}/bin/linux/${ARCH}/kubectl"
26-
chmod +x kubectl
27-
mv kubectl /usr/local/bin/kubectl
56+
TMP_KUBECTL=$(mktemp)
57+
curl -Lo "${TMP_KUBECTL}" "https://dl.k8s.io/release/${KUBECTL_VERSION}/bin/linux/${ARCH}/kubectl"
58+
chmod +x "${TMP_KUBECTL}"
59+
mv "${TMP_KUBECTL}" /usr/local/bin/kubectl
2860
fi
61+
kubectl completion bash > "${BASH_COMPLETIONS_DIR}/kubectl" 2>/dev/null || true
62+
63+
# Docker completion
64+
docker completion bash > "${BASH_COMPLETIONS_DIR}/docker" 2>/dev/null || true
2965

3066
# Wait for Docker to be ready
3167
for i in {1..30}; do

0 commit comments

Comments
 (0)