-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkdeploy
More file actions
executable file
·92 lines (77 loc) · 3.06 KB
/
kdeploy
File metadata and controls
executable file
·92 lines (77 loc) · 3.06 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#!/usr/bin/env bash
set -euo pipefail
# Deploy an image: push to k8s node, update deployment image, verify pods
# Usage: kdeploy [-q] [-r host] [-n namespace] [-t timeout] <image:tag> [deployment]
# Examples:
# kdeploy myapp:1.0 cx-app
# kdeploy -r bigfish myapp:1.0 cx-app
# kdeploy -n prod -t 180 myapp:1.0 cx-app
# Kill child processes on exit
trap 'pkill -P $$ 2>/dev/null || true' EXIT
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
readonly SCRIPT_DIR
source "$SCRIPT_DIR/lib.sh"
usage() {
sed -n '4,8p' "$0"
echo
echo "If deployment is omitted, it defaults to the final repository path segment."
exit 0
}
host=""
namespace=""
timeout=60
quiet=false
while [[ $# -gt 0 && "$1" == -* ]]; do
case "$1" in
-h|--help) usage ;;
-q|--quiet) quiet=true; shift ;;
-r|--remote) require_optarg "$1" "${2:-}"; host="$2"; shift 2 ;;
-n|--namespace) require_optarg "$1" "${2:-}"; namespace="$2"; shift 2 ;;
-t|--timeout) require_optarg "$1" "${2:-}"; timeout="$2"; shift 2 ;;
*) echo "Unknown option: $1" >&2; exit 1 ;;
esac
done
readonly IMAGE="${1:?Usage: kdeploy [-r host] [-n namespace] <image:tag> [deployment]}"
if [[ -n "${2:-}" ]]; then
DEPLOY="$2"
else
DEPLOY=$(image_default_deploy_name "$IMAGE")
fi
[[ -z "$host" ]] && host=$(resolve_host)
require_host "$host" || exit 1
require_namespace namespace
declare -a q_args=()
$quiet && q_args=(-q)
rollout_timeout="$timeout"
[[ "$rollout_timeout" =~ [[:alpha:]]$ ]] || rollout_timeout="${rollout_timeout}s"
# Push image to remote k8s node
"$SCRIPT_DIR/kpush" "${q_args[@]}" -r "$host" "$IMAGE"
# Match the deployment container by image repo so tag changes like
# 1.12 -> 1.13-SNAPSHOT still resolve to the same workload container.
target_container=$(resolve_deployment_container "$namespace" "$DEPLOY" "$IMAGE")
current_image=$(kubectl get deployment "$DEPLOY" -n "$namespace" -o json | \
jq -r --arg cn "$target_container" \
'.spec.template.spec.containers[] | select(.name == $cn) | .image')
if [[ "$current_image" != "$IMAGE" ]]; then
$quiet || echo " Updating $target_container image: $current_image -> $IMAGE"
kubectl set image "deployment/$DEPLOY" "$target_container=$IMAGE" -n "$namespace" >/dev/null
else
$quiet || echo -n " Restarting..."
kubectl rollout restart deployment "$DEPLOY" -n "$namespace" >/dev/null
fi
# Wait for rollout after the image update or restart
if $quiet; then
kubectl rollout status deployment "$DEPLOY" -n "$namespace" --timeout="$rollout_timeout" >/dev/null
else
echo -n " Rolling out..."
kubectl rollout status deployment "$DEPLOY" -n "$namespace" --timeout="$rollout_timeout" 2>&1 | while read -r line; do
printf "\r Rollout: %-60s" "${line:0:60}"
done
printf "\r Rollout: done%-56s\n" ""
fi
# Verify pods are running the correct image
# Pass the resolved container through an internal env var so verification
# checks the same container we updated or restarted.
KVERIFY_CONTAINER="$target_container" \
"$SCRIPT_DIR/kverify" "${q_args[@]}" -n "$namespace" -r "$host" -t "$timeout" "$IMAGE" "$DEPLOY"
$quiet || echo "==> Deploy complete"