-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgenerate-image-repo-secret.sh
More file actions
executable file
·68 lines (57 loc) · 1.77 KB
/
Copy pathgenerate-image-repo-secret.sh
File metadata and controls
executable file
·68 lines (57 loc) · 1.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#!/usr/bin/env bash
set -euo pipefail
usage() {
cat <<'USAGE'
Usage: generate-image-repo-secret.sh <username> <password> <namespace> [registry] [secret-name] [kube-context]
Arguments:
username Registry username
password Registry password or token
namespace Kubernetes namespace for the secret
registry Optional registry URL (default: https://index.docker.io/v1/)
secret-name Optional secret name (default: image-repo-secret)
kube-context Optional kubectl context (default: current context)
USAGE
}
if [[ $# -lt 3 ]]; then
usage >&2
exit 1
fi
USERNAME="$1"
PASSWORD="$2"
NAMESPACE="$3"
REGISTRY="${4:-https://index.docker.io/v1/}"
SECRET_NAME="${5:-image-repo-secret}"
KUBE_CONTEXT="${6:-}"
EMAIL="${IMAGE_REGISTRY_EMAIL:-unused@example.com}"
KUBECTL_ARGS=()
if [[ -n "$KUBE_CONTEXT" ]]; then
KUBECTL_ARGS+=(--context "$KUBE_CONTEXT")
fi
KUBECTL_ARGS+=(-n "$NAMESPACE")
TMP_YAML=$(mktemp)
trap 'rm -f "$TMP_YAML"' EXIT
# Create Docker config JSON content
DOCKER_CONFIG_JSON=$(cat <<EOF
{
"auths": {
"$REGISTRY": {
"username": "$USERNAME",
"password": "$PASSWORD",
"email": "$EMAIL",
"auth": "$(echo -n "$USERNAME:$PASSWORD" | base64 | tr -d '\n')"
}
}
}
EOF
)
# Create secret with both .dockerconfigjson (for standard K8s) and config.json (for Kaniko)
kubectl "${KUBECTL_ARGS[@]}" create secret generic "$SECRET_NAME" \
--from-literal=.dockerconfigjson="$DOCKER_CONFIG_JSON" \
--from-literal=config.json="$DOCKER_CONFIG_JSON" \
--dry-run=client -o yaml >"$TMP_YAML"
# Add the docker-registry type label for compatibility
cat >>"$TMP_YAML" <<EOF
type: kubernetes.io/dockerconfigjson
EOF
kubectl "${KUBECTL_ARGS[@]}" apply -f "$TMP_YAML"
echo "Image registry secret '$SECRET_NAME' applied to namespace '$NAMESPACE'"