Skip to content

Commit b069f22

Browse files
committed
Refactor
1 parent 9850f12 commit b069f22

File tree

4 files changed

+127
-19
lines changed

4 files changed

+127
-19
lines changed

.editorconfig

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
root = true
2+
3+
[*]
4+
indent_style = space
5+
indent_size = 2
6+
charset = utf-8
7+
trim_trailing_whitespace = true
8+
insert_final_newline = true
9+
10+
[*.sh]
11+
indent_style = tab
12+
indent_size = 4

Dockerfile

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,17 @@
11
FROM busybox:1.31 AS installer
2-
ADD https://storage.googleapis.com/kubernetes-release/release/v1.18.2/bin/linux/amd64/kubectl /kubectl
3-
RUN chmod 0755 /kubectl
42

5-
FROM scratch
6-
COPY --from=installer /kubectl /kubectl
3+
ARG VERSION="1.18.2"
4+
5+
ADD https://storage.googleapis.com/kubernetes-release/release/v${VERSION}/bin/linux/amd64/kubectl /install/kubectl
6+
COPY --chown=root:root entrypoint.sh /install/entrypoint
7+
RUN chmod 0755 /install/kubectl /install/entrypoint
8+
9+
10+
FROM busybox:1.31
11+
12+
RUN mkdir -p /config && chown -R 1042 /config
13+
COPY --from=installer /install /usr/bin
714
USER 1042
8-
ENTRYPOINT ["/kubectl"]
15+
VOLUME ["/config"]
16+
ENTRYPOINT ["entrypoint"]
17+
CMD ["version", "--short", "--client"]

README.md

Lines changed: 49 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,29 @@
11
# Kubectl docker image
22

3-
Minimal docker image with kubectl only.
3+
Minimal docker image based on busybox with kubeconfig automation.
44

55
# Components
66

7-
## kubectl
7+
## Kubectl
88

99
Kubectl is the Kubernetes cli version of a swiss army knife, and can do many things.
1010

11-
[Project link](https://kubectl.docs.kubernetes.io)
11+
[Project link](https://kubectl.docs.kubernetes.io/)
1212

13-
# Usage
13+
## BusyBox
14+
15+
BusyBox provides a fairly complete environment for any small or embedded system.
1416

15-
## Console
17+
[Project link](https://busybox.net/)
18+
19+
# Usage
1620

17-
Docker:
21+
## Run in Docker
1822

1923
```shell
2024
$ docker run \
2125
--name="docker-kubectl-example" \
22-
--volume="$HOME/.kube/config:/config:ro" \
23-
--env="KUBECONFIG=/config" \
26+
--volume="$HOME/.kube/config:/config/kubectl.conf:ro" \
2427
--network="host" \
2528
--rm \
2629
--interactive \
@@ -29,21 +32,53 @@ $ docker run \
2932
get pods
3033
```
3134

32-
Kubernetes:
35+
## Run in Kubernetes
36+
37+
From command line:
3338

3439
```shell
35-
$ TOKEN=$(kubectl -n test-jobs get secrets -o jsonpath="{.items[?(@.metadata.annotations['kubernetes\.io/service-account\.name']=='executor')].data.token}" | base64 --decode)
3640
$ kubectl run "get-pods-example" \
3741
--rm="true" \
3842
--restart="Never" \
3943
--image="4ops/kubectl:1.18.2" \
40-
--image-pull-policy="Always" \
4144
--stdin \
4245
--tty \
43-
--namespace="test-jobs" \
44-
--env="TOKEN=$TOKEN" \
4546
-- \
46-
--token='$(TOKEN)' \
4747
get \
4848
pods
4949
```
50+
51+
Pod manifest example:
52+
53+
```YAML
54+
apiVersion: v1
55+
kind: Pod
56+
metadata:
57+
name: get-pods-example
58+
spec:
59+
containers:
60+
- name: "kubectl"
61+
image: "4ops/kubectl:1.18.2"
62+
args: ["get", "pods"]
63+
```
64+
65+
# Credentials
66+
67+
## Using existing kubeconfig
68+
69+
- Mount volume with kubeconfig file
70+
- Setup path to kubeconfig using environment variable `KUBECONFIG`
71+
72+
## Using ServiceAccount token
73+
74+
- Setup token as environment variable `KUBE_TOKEN`
75+
- If no `KUBECONFIG` or `KUBE_TOKEN` set, entrypoint script will try to discover ServiceAccount secrets from `/var/run/secrets/kubernetes.io/serviceaccount` directory
76+
77+
# Environment variables
78+
79+
- `KUBECONFIG` - path to kubeconfig file (default: `/config/kubectl.conf`)
80+
- `KUBERNETES_SERVICE_HOST`, `KUBERNETES_SERVICE_PORT` - Kubernetes API native service discovery [variables](https://kubernetes.io/docs/concepts/services-networking/service/#environment-variables) (default: `kubernetes.default.svc`, `443`)
81+
- `KUBE_URL` - custom Kubernetes API URL
82+
- `KUBE_CA_PEM` - PEM-encoded certificate (or path to cert file) for TLS verification
83+
- `KUBE_NAMESPACE` - default namespace for kubeconfig context (default: `default`)
84+
- `KUBE_TOKEN` - auth token

entrypoint.sh

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#!/bin/sh
2+
3+
set -eu
4+
5+
readonly kubectl="/usr/bin/kubectl"
6+
readonly sa="/var/run/secrets/kubernetes.io/serviceaccount"
7+
readonly debug="${DEBUG:-}"
8+
readonly kubeconfig="${KUBECONFIG:-/config/kubectl.conf}"
9+
readonly url="${KUBE_URL:-https://${KUBERNETES_SERVICE_HOST:-kubernetes.default.svc}:${KUBERNETES_SERVICE_PORT:-443}}"
10+
11+
token="${KUBE_TOKEN:-}"
12+
ca="${KUBE_CA_PEM:-}"
13+
namespace="${KUBE_NAMESPACE:-}"
14+
15+
[ -n "$token" ] && [ -r $sa/token ] && token="$(cat $sa/token)"
16+
[ -n "$token" ] && secret="$token" && token="@token"
17+
[ -n "$debug" ] && set -x && printenv | sed 's eyJhbGci.* @secret g'
18+
19+
unset KUBE_TOKEN KUBE_URL KUBE_NAMESPACE KUBE_CA_PEM
20+
21+
# try to get certificate authority
22+
if [ -n "$ca" ] && [ ! -r "$ca" ]; then
23+
echo "$ca" > /tmp/ca.crt
24+
ca=/tmp/ca.crt
25+
elif [ -z "$ca" ] && [ -r $sa/ca.crt ]; then
26+
ca=$sa/ca.crt
27+
fi
28+
29+
# set namespace
30+
if [ -z "$namespace" ] && [ -r $sa/namespace ]; then
31+
namespace="$(cat $sa/namespace)"
32+
else
33+
namespace="default"
34+
fi
35+
36+
# create kubeconfig
37+
if [ -n "$ca" ] && [ -n "$token" ] && [ ! -r "$kubeconfig" ]; then
38+
kcfg="$kubectl --kubeconfig=$kubeconfig config"
39+
$kcfg set-credentials token --token="$secret"
40+
$kcfg set-cluster kube --server="$url" --certificate-authority="$ca" --embed-certs=true
41+
$kcfg set-context kube-token --cluster=kube --user=token
42+
$kcfg use-context kube-token
43+
fi
44+
45+
# setup arguments
46+
if [ -r "$kubeconfig" ]; then
47+
set -- --kubeconfig="$kubeconfig" "$@"
48+
else
49+
[ -n "$token" ] && set -- --server="$url" --token="$secret" "$@"
50+
fi
51+
52+
exec $kubectl "$@" || exit $?

0 commit comments

Comments
 (0)