Skip to content

Commit d308612

Browse files
committed
add configuration variables
1 parent 1a39b59 commit d308612

File tree

2 files changed

+29
-8
lines changed

2 files changed

+29
-8
lines changed

README.md

+17
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,20 @@ kubectl build --context . --destination docker.io/some/image:latest --digest-fil
5656
# Build from stdin
5757
tar -cvf- . | kubectl build --destination docker.io/some/image:latest --context tar://stdin
5858
```
59+
60+
## Extra configuration
61+
62+
While standard behavior of kubectl-build plugin intend to repeat kaniko executor options. The additional configuration can be specified by setting environment variables.
63+
64+
This may be useful for both having permanent configuration and setting CI-systems.
65+
66+
| Enivroment Variable | Description | Default value |
67+
|------------------------------------|--------------------------------------------------------------------------------|----------------------------------|
68+
| `KUBECTL_BUILD_CONTEXT` | Kubernetes context for creating pod (may be overriden by `--kubecontext`) | current context |
69+
| `KUBECTL_BUILD_DOCKER_CONFIG` | Path to dockerconfig file to forward | `~/.docker/config.json` |
70+
| `KUBECTL_BUILD_IMAGE` | Kaniko-executor image | `gcr.io/kaniko-project/executor` |
71+
| `KUBECTL_BUILD_KEEP_POD` | If set to `true` do not delete pod after finising process | `false` |
72+
| `KUBECTL_BUILD_KUBECONFIG` | Path to kubeconfig file for creating pods (may be overriden by `--kubeconfig`) | kubectl defaults |
73+
| `KUBECTL_BUILD_METADATA_OVERRIDES` | Json patch to override metadata for creating pods | `{}` |
74+
| `KUBECTL_BUILD_NAME_OVERRIDE` | Name for creating pod | `kaniko-rand6n` |
75+
| `KUBECTL_BUILD_NAMESPACE` | Kubernetes namespace for creating pod (may be overriden by `--namespace`) | current namespace |

kubectl-build

+12-8
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#!/usr/bin/env sh
22
set -e
3-
DOCKER_CONFIG=${DOCKER_CONFIG:-$HOME/.docker/config.json}
3+
export DOCKER_CONFIG=${KUBECTL_BUILD_DOCKER_CONFIG:-${DOCKER_CONFIG:-$HOME/.docker/config.json}}
4+
export KUBECONFIG="${KUBECTL_BUILD_KUBECONFIG:-$KUBECONFIG}"
45
kubectl=kubectl
56
context=""
67
generator=""
@@ -79,8 +80,8 @@ while [ $# -gt 0 ]; do
7980
done
8081

8182
# Set the default context and namespace to avoid situations where the user switch them during the build process
82-
[ "$nodefaultctx" = 1 ] || kubectl="$kubectl --context=$(kubectl config current-context)"
83-
[ "$nodefaultns" = 1 ] || kubectl="$kubectl --namespace=$(kubectl config view --minify --output 'jsonpath={.contexts..namespace}')"
83+
[ "$nodefaultctx" = 1 ] || kubectl="$kubectl --context=${KUBECTL_BUILD_CONTEXT:-$(kubectl config current-context)}"
84+
[ "$nodefaultns" = 1 ] || kubectl="$kubectl --namespace=${KUBECTL_BUILD_NAMESPACE:-$(kubectl config view --minify --output 'jsonpath={.contexts..namespace}')}"
8485

8586
if [ -n "$digestfile" ] || [ -n "$imagenamewithdigestfile" ]; then
8687
args="$args\"--image-name-with-digest-file=/dev/termination-log\""
@@ -96,12 +97,13 @@ else
9697
args="$args, \"--context=$context\" ]"
9798
fi
9899

99-
image="gcr.io/kaniko-project/executor:v1.5.1"
100-
name="kaniko-$(env LC_ALL=C tr -dc a-z0-9 </dev/urandom | head -c 6)"
100+
image="${KUBECTL_BUILD_IMAGE:-gcr.io/kaniko-project/executor:v1.5.1}"
101+
name="${KUBECTL_BUILD_NAME_OVERRIDE:-kaniko-$(env LC_ALL=C tr -dc a-z0-9 </dev/urandom | head -c 6)}"
101102

102103
overrides="$(
103104
cat <<EOT
104105
{
106+
"metadata": ${KUBECTL_BUILD_METADATA_OVERRIDES:-"{}"},
105107
"spec": {
106108
"containers": [
107109
{
@@ -152,14 +154,16 @@ if [ -n "$context" ] && ([ ! -d "$context" ] && [ "$context" != "tar://stdin" ])
152154
exit 1
153155
fi
154156

155-
trap "EC=\$?; $kubectl delete pod "$name" --wait=false 2>/dev/null || true; exit \$EC" EXIT INT TERM
157+
if [ "$KUBECTL_BUILD_KEEP_POD" != "true" ]; then
158+
trap "EC=\$?; $kubectl delete pod "$name" --wait=false 2>/dev/null || true; exit \$EC" EXIT INT TERM
159+
fi
156160

157161
echo "spawning \"$name\""
158162
if [ -n "$context" ] && [ "$context" != "tar://stdin" ]; then
159163
tarf -C "$context" -czf - . |
160-
$kubectl run --image "$image" --rm --restart=Never --overrides="$overrides" -i "$name" $generator
164+
$kubectl run --image "$image" --restart=Never --overrides="$overrides" -i "$name" $generator
161165
else
162-
$kubectl run --image "$image" --rm --restart=Never --overrides="$overrides" -i "$name" $generator
166+
$kubectl run --image "$image" --restart=Never --overrides="$overrides" -i "$name" $generator
163167
fi
164168

165169
# Retrieve digest file

0 commit comments

Comments
 (0)