devzone 7.3.0 → 7.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (50) hide show
  1. package/.devcontainer/devcontainer.json +1 -1
  2. package/.devcontainer/devzone +13 -20
  3. package/.devcontainer/devzone-modules.sh +1 -1
  4. package/.devcontainer/devzone-repair.sh +2 -2
  5. package/.devcontainer/devzone-services.sh +7 -7
  6. package/.devcontainer/devzone-status.sh +5 -5
  7. package/.devcontainer/help.sh +3 -1
  8. package/.devcontainer/versions.env +3 -0
  9. package/.devcontainer/workspace-init.sh +8 -9
  10. package/.gitattributes +8 -0
  11. package/README.md +1 -1
  12. package/bin/SHA256SUMS +6 -6
  13. package/bin/devzone-darwin-amd64 +0 -0
  14. package/bin/devzone-darwin-arm64 +0 -0
  15. package/bin/devzone-linux-amd64 +0 -0
  16. package/bin/devzone-linux-arm64 +0 -0
  17. package/bin/devzone-windows-amd64.exe +0 -0
  18. package/bin/devzone-windows-arm64.exe +0 -0
  19. package/charts/devzone-mysql/values.yaml +1 -1
  20. package/charts/devzone-postgres/values.yaml +1 -1
  21. package/manage/devzone +2 -2
  22. package/manage/scripts/uninstall.sh +1 -1
  23. package/manage/scripts/workspace/argocd-workspace.sh +2 -2
  24. package/package.json +1 -1
  25. package/.devcontainer/agent-errors.sh +0 -156
  26. package/.devcontainer/agent-local.sh +0 -63
  27. package/.devcontainer/agent-model.sh +0 -113
  28. package/.devcontainer/agent-pod-egress.sh +0 -36
  29. package/.devcontainer/agent-pod-manifest.sh +0 -250
  30. package/.devcontainer/agent-pod.sh +0 -243
  31. package/.devcontainer/agent-session.sh +0 -242
  32. package/.devcontainer/agent-state-json.sh +0 -117
  33. package/.devcontainer/agent-workspace.sh +0 -97
  34. package/.devcontainer/agent.sh +0 -247
  35. package/.devcontainer/catalog/app.sh +0 -202
  36. package/.devcontainer/catalog/catalog-watcher.sh +0 -127
  37. package/.devcontainer/catalog.sh +0 -208
  38. package/.devcontainer/chart-direct.sh +0 -49
  39. package/.devcontainer/cluster.sh +0 -388
  40. package/.devcontainer/clusters.sh +0 -86
  41. package/.devcontainer/devzone-cluster.sh +0 -209
  42. package/.devcontainer/isolation.sh +0 -65
  43. package/.devcontainer/kube-account.sh +0 -107
  44. package/.devcontainer/kube-prompt.sh +0 -42
  45. package/.devcontainer/memory.sh +0 -113
  46. package/.devcontainer/metrics-server.sh +0 -100
  47. package/.devcontainer/packages.sh +0 -228
  48. package/.devcontainer/password.sh +0 -148
  49. package/.devcontainer/ports-json.sh +0 -31
  50. package/.devcontainer/ports.sh +0 -132
@@ -1,208 +0,0 @@
1
- #!/usr/bin/env bash
2
- # Application catalog (#414): one preconfigured application per folder
3
- # .devcontainer/catalog/<app>/ (app.conf; the chart charts/devzone-<app>, #559),
4
- # installed in the devzone services cluster under the namespace of its app.conf;
5
- # the wanted state is the line <app>=on|off of .devzone.modules.
6
- # catalog.sh sync | apply | list | resume | json | remove <app>
7
- set -euo pipefail
8
-
9
- ICI="$(cd "$(dirname "$0")" && pwd)"
10
- RACINE="${DEVZONE_ROOT:-$(dirname "$ICI")}"
11
- ETAT="${DEVZONE_STATE:-$RACINE/.devzone.modules}"
12
- # shellcheck source=SCRIPTDIR/cluster.sh
13
- # shellcheck disable=SC1091
14
- . "$ICI/cluster.sh"
15
- # shellcheck source=SCRIPTDIR/password.sh
16
- # shellcheck disable=SC1091
17
- . "$ICI/password.sh"
18
- # shellcheck source=SCRIPTDIR/isolation.sh
19
- # shellcheck disable=SC1091
20
- . "$ICI/isolation.sh"
21
- # shellcheck source=SCRIPTDIR/catalog/app.sh
22
- # shellcheck disable=SC1091
23
- . "$ICI/catalog/app.sh"
24
-
25
- refuse() { echo "devzone : catalog : $*" >&2; exit 2; }
26
-
27
- # Catalog folder (overridable by the tests)
28
- catalog_dir() { echo "${DEVZONE_CATALOG:-$ICI/catalog}"; }
29
-
30
- # Catalog applications: folders holding an app.conf, sorted
31
- catalog_apps() {
32
- local d
33
- for d in "$(catalog_dir)"/*; do
34
- [ -f "$d/app.conf" ] || continue
35
- echo "${d##*/}"
36
- done | LC_ALL=C sort
37
- }
38
-
39
- # app.conf key (name, description, image, port, memoire, taille, namespace)
40
- app_read() {
41
- grep -E "^$2=" "$(catalog_dir)/$1/app.conf" 2>/dev/null | tail -1 | cut -d= -f2- || true
42
- }
43
-
44
- # Application namespace: the one of app.conf (required)
45
- app_namespace() {
46
- local ns
47
- ns="$(app_read "$1" namespace)"
48
- [ -n "$ns" ] || refuse "missing namespace in app.conf of $1"
49
- echo "$ns"
50
- }
51
-
52
- # Failure marker of an application: same scheme as the failure file of the modules
53
- failure_file() { echo "$ETAT-failed-$1"; }
54
-
55
- # Wanted state: on if the line <app>=on exists, off by default
56
- wanted() {
57
- local v
58
- v="$(grep -E "^$1=" "$ETAT" 2>/dev/null | tail -1 | cut -d= -f2- || true)"
59
- [ "$v" = on ] && echo on || echo off
60
- }
61
-
62
- # Application present in the cluster. In the Kubernetes workspace pod: its
63
- # service resolves in the cluster DNS (no RBAC needed, #515). Elsewhere: labelled
64
- # namespace and StatefulSet of the app name; reading the namespaces is reserved
65
- # to the administrator (#430). Nothing without a chosen cluster or when absent.
66
- present() {
67
- local ctx ns
68
- ns="$(app_namespace "$1")"
69
- if [ "${DEVZONE_WORKSPACE:-}" = kubernetes ]; then
70
- # In the pod there is no kubeconfig and the limited service account
71
- # cannot list StatefulSets (#430): presence is the cluster DNS
72
- # resolving the service, no RBAC needed, the pod service account
73
- # stays limited
74
- getent hosts "$(app_read "$1" name).${ns}.svc" >/dev/null 2>&1
75
- return
76
- fi
77
- ctx="$(cluster_saved)"
78
- [ -n "$ctx" ] || return 1
79
- account_admin "$ctx" get namespace "$ns" >/dev/null 2>&1 &&
80
- kubectl --context devzone -n "$ns" get statefulset "$1" >/dev/null 2>&1
81
- }
82
-
83
- # Application ready: the StatefulSet has one ready replica
84
- app_ready() {
85
- local ns
86
- ns="$(app_namespace "$1")"
87
- [ "$(kubectl --context devzone -n "$ns" get statefulset "$1" \
88
- -o jsonpath='{.status.readyReplicas}' 2>/dev/null || true)" = 1 ]
89
- }
90
-
91
- # Adds to the state the <app>=off lines of the absent applications (once each,
92
- # an existing line is never changed), under the Go state lock (#879).
93
- catalog_sync() {
94
- DEVZONE_STATE="$ETAT" bash "$ICI/devzone" state catalog-sync
95
- }
96
-
97
- # Real state of an application: available, expected, installed, failed. These
98
- # values are the etat field of devzone status --json: keep them
99
- app_state() {
100
- local app="$1"
101
- if [ "$(wanted "$app")" != on ]; then
102
- echo available
103
- elif [ -f "$(failure_file "$app")" ]; then
104
- echo failed
105
- elif present "$app"; then
106
- echo installed
107
- else
108
- echo expected
109
- fi
110
- }
111
-
112
- # devzone catalog: name, state (off, on installed, on failed), description
113
- catalog_list() {
114
- local app state label
115
- while IFS= read -r app; do
116
- [ -n "$app" ] || continue
117
- state="$(app_state "$app")"
118
- case "$state" in
119
- available) label='off available' ;;
120
- installed) label='on installed' ;;
121
- failed) label='on failed' ;;
122
- *) label='on expected' ;;
123
- esac
124
- printf ' %-12s %-14s %s\n' "$app" "$label" "$(app_read "$app" description)"
125
- done <<<"$(catalog_apps)"
126
- }
127
-
128
- # Relay of the installed application: state of its script, else the relay pid
129
- app_relay_stopped() {
130
- local app="$1" script state
131
- if [ "${DEVZONE_WORKSPACE:-}" = kubernetes ]; then
132
- # In the pod, no relay: the access goes through the workspace ports
133
- return 1
134
- fi
135
- script="$ICI/catalog/$app/$app.sh"
136
- if [ -f "$script" ]; then
137
- state="$(bash "$script" status 2>/dev/null || true)"
138
- case "$state" in *"relay stopped"*) return 0 ;; esac
139
- fi
140
- ! relay_active "$app"
141
- }
142
-
143
- # Application lines of devzone's summary: ○ available, ✔ installed (relay
144
- # address), ✖ failed, ⚠ expected
145
- catalog_summary() {
146
- local app
147
- while IFS= read -r app; do
148
- [ -n "$app" ] || continue
149
- case "$(app_state "$app")" in
150
- available) printf ' ○ application %-12s available, %s: devzone enable %s\n' \
151
- "$app" "$(app_read "$app" description)" "$app" ;;
152
- installed)
153
- if app_relay_stopped "$app"; then
154
- printf ' ⚠ application %-12s installed, relay stopped: devzone repair\n' "$app"
155
- else
156
- if [ "${DEVZONE_WORKSPACE:-}" = kubernetes ]; then
157
- printf ' ✔ application %-12s installed: local access: devzone workspace ports\n' "$app"
158
- else
159
- printf ' ✔ application %-12s installed: 127.0.0.1:%s\n' \
160
- "$app" "$(app_read "$app" port)"
161
- fi
162
- fi ;;
163
- expected) printf ' ⚠ application %-12s expected: devzone apply\n' "$app" ;;
164
- *) printf ' ✖ application %s failed: %s: devzone apply\n' \
165
- "$app" "$(head -1 "$(failure_file "$app")")" ;;
166
- esac
167
- done <<<"$(catalog_apps)"
168
- app_unused_mysql_volumes 2>/dev/null || true
169
- }
170
-
171
- # Applications section of devzone status --json: same fields as the modules
172
- catalog_json() {
173
- local app active state failure cmd json='[]'
174
- while IFS= read -r app; do
175
- [ -n "$app" ] || continue
176
- active=false
177
- [ "$(wanted "$app")" = on ] && active=true
178
- state="$(app_state "$app")"
179
- failure=""
180
- cmd="devzone apply"
181
- case "$state" in
182
- available) cmd="devzone enable $app" ;;
183
- installed) cmd="devzone disable $app" ;;
184
- failed) failure="$(head -1 "$(failure_file "$app")")" ;;
185
- esac
186
- json="$(jq -cn --argjson before "$json" --arg name "$app" --argjson active "$active" \
187
- --arg description "$(app_read "$app" description)" \
188
- --arg state "$state" --arg port "$(app_read "$app" port)" \
189
- --arg failure "$failure" --arg command "$cmd" \
190
- '$before + [{name:$name,active:$active,description:$description,port:$port,state:$state,failed:(if $failure=="" then null else $failure end),command:(if $command=="" then null else $command end)}]')"
191
- done <<<"$(catalog_apps)"
192
- printf '%s\n' "$json"
193
- }
194
-
195
- case "${1:-}" in
196
- sync) catalog_sync ;;
197
- apply) catalog_apply ;;
198
- list) catalog_list ;;
199
- resume) catalog_summary ;;
200
- json) catalog_json ;;
201
- # Removal of an application: StatefulSet and service, volume kept (#441)
202
- remove) app_remove "$2" ;;
203
- -h | --help) sed -n '2,6p' "$0" | sed 's/^# \{0,1\}//' ;;
204
- *)
205
- echo "devzone : catalog : sync | apply | list | resume | json | remove <app>" >&2
206
- exit 1
207
- ;;
208
- esac
@@ -1,49 +0,0 @@
1
- #!/usr/bin/env bash
2
- # Helm-direct installation of a devzone service (#559, step 7): the chart
3
- # charts/devzone-<svc> of the code tree is the only definition, installed with
4
- # helm on the devzone context as the release <svc> in <ns>. Objects already
5
- # there (kubectl apply of an earlier version, or left by Argo CD when its module
6
- # went away) are adopted first: the annotations and the label helm requires, so
7
- # that the release takes them over without recreating them.
8
- # chart_install <svc> <ns> [key=value ...] adoption, then helm upgrade --install
9
- # bash 3.2 (macOS).
10
-
11
- # The charts are code: read from the code tree (this file is in .devcontainer/),
12
- # whatever the devzone folder (RACINE, the state) is
13
- CHART_CODE="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
14
-
15
- # Adopts the objects of the rendered chart that already exist in <ns>: helm
16
- # refuses an object it does not own, so the release annotations and the
17
- # managed-by label are written on them (no object is recreated)
18
- chart_adopt() {
19
- local svc="$1" ns="$2" rendu="$3" objets o
20
- objets="$(kubectl --context devzone -n "$ns" get -f "$rendu" -o name --ignore-not-found 2>/dev/null || true)"
21
- for o in $objets; do
22
- kubectl --context devzone -n "$ns" annotate "$o" \
23
- "meta.helm.sh/release-name=$svc" "meta.helm.sh/release-namespace=$ns" --overwrite >/dev/null
24
- kubectl --context devzone -n "$ns" label "$o" app.kubernetes.io/managed-by=Helm --overwrite >/dev/null
25
- done
26
- }
27
-
28
- # Installs or upgrades the release <svc> from charts/devzone-<svc>; the
29
- # key=value arguments become --set options (image, port, memory, size, claim)
30
- chart_install() {
31
- local svc="$1" ns="$2" chart rendu arg
32
- local -a sets
33
- shift 2
34
- chart="$CHART_CODE/charts/devzone-$svc"
35
- if [ ! -f "$chart/Chart.yaml" ]; then
36
- echo "✖ $svc : chart charts/devzone-$svc missing (manage/docs/reference.md#devzone-charts)" >&2
37
- return 2
38
- fi
39
- sets=()
40
- for arg in "$@"; do sets+=(--set "$arg"); done
41
- rendu="$(mktemp)"
42
- if ! helm template "$svc" "$chart" -n "$ns" ${sets[@]+"${sets[@]}"} >"$rendu"; then
43
- rm -f "$rendu"
44
- return 1
45
- fi
46
- chart_adopt "$svc" "$ns" "$rendu"
47
- rm -f "$rendu"
48
- helm --kube-context devzone upgrade --install "$svc" "$chart" -n "$ns" ${sets[@]+"${sets[@]}"} >/dev/null
49
- }
@@ -1,388 +0,0 @@
1
- #!/usr/bin/env bash
2
- # devzone cluster (#280): the cluster where the modules that provide a service
3
- # are deployed (postgres; gitlab with #279). A single choice per instance, kept
4
- # in the state (line « cluster=<kubectl context> »): the services of an instance
5
- # live in the same place and move together (devzone cluster CTX).
6
- # Sourced library: the caller defines RACINE and ETAT.
7
- # cluster_saved chosen context, nothing if there is none
8
- # cluster_save CTX keeps the choice in the state
9
- # cluster_is_local CTX 0 if local cluster (config, no network)
10
- # cluster_candidates local, then dedicated kind, then remote
11
- # cluster_choose [CTX] [--confirm CTX]
12
- # CTX validated (remote: confirmation),
13
- # otherwise kept choice, otherwise menu
14
- # (terminal only), otherwise local default
15
- # cluster_prepare CTX creates the dedicated kind cluster if missing
16
- # cluster_release CTX deletes the dedicated kind cluster once empty
17
- # relay_start NAME CTX NS TARGET PORTS… port-forward kept alive
18
- # relay_active NAME / relay_stop NAME
19
- # account_* (kube-account.sh #430) limited account and admin context
20
-
21
- # Kind and host clusters, as remoteEnv of devcontainer.json: a docker exec or a
22
- # script run outside the container terminal does not have it, and the relay
23
- # must see the host cluster
24
- : "${KUBECONFIG:=$HOME/.kube/config:$HOME/.kube/hote.config}"
25
- export KUBECONFIG
26
-
27
- # Module state: defined by the caller; default value for the tests
28
- : "${ETAT:=${DEVZONE_STATE:-$HOME/.devzone.modules}}"
29
-
30
- # Safe state write (#417): lock, temporary file in the state folder, backup copy
31
- # .prev — shared by devzone, ports
32
-
33
- # Instance name: empty or « devzone » = default instance (no suffix)
34
- : "${DEVZONE_INSTANCE:=}"
35
- INSTANCE_SUFFIX=""
36
- if [ -n "$DEVZONE_INSTANCE" ] && [ "$DEVZONE_INSTANCE" != "devzone" ]; then
37
- INSTANCE_SUFFIX="-${DEVZONE_INSTANCE}"
38
- fi
39
-
40
- # Dedicated kind cluster, created on the host Docker; name suffixed per instance
41
- CLUSTER_DEDICATED=kind-devzone${INSTANCE_SUFFIX}
42
- # Label set on every namespace created by devzone : a dedicated kind without a
43
- # labelled namespace has no use left and can be deleted
44
- CLUSTER_LABEL=app.kubernetes.io/managed-by=devzone
45
-
46
- # Limited Kubernetes account (#430): account_setup, account_allow,
47
- # account_remove, account_token, account_admin (admin context); the installers
48
- # use it, cluster_release too
49
- # shellcheck source=SCRIPTDIR/kube-account.sh
50
- . "$(dirname "${BASH_SOURCE[0]}")/kube-account.sh"
51
-
52
- cluster_saved() {
53
- grep -E '^cluster=' "$ETAT" 2>/dev/null | tail -1 | cut -d= -f2- || true
54
- }
55
-
56
- cluster_save() {
57
- DEVZONE_STATE="$ETAT" bash "${ICI:-$(dirname "${BASH_SOURCE[0]}")}/devzone" state write cluster "$1"
58
- }
59
-
60
- cluster_is_dedicated() {
61
- [ "$1" = "${CLUSTER_DEDICATED}" ]
62
- }
63
-
64
- # Local cluster: read from the KUBECONFIG configuration, without network call.
65
- # The dedicated kind is always local; a host cluster is local when its server
66
- # listens on a loopback address (127.0.0.1, localhost, ::1, host.docker.internal).
67
- cluster_is_local() {
68
- local ctx="$1" cluster server host
69
- cluster_is_dedicated "$ctx" && return 0
70
- cluster="$(kubectl config view -o jsonpath="{.contexts[?(@.name==\"$ctx\")].context.cluster}" 2>/dev/null || true)"
71
- [ -n "$cluster" ] || return 1
72
- server="$(kubectl config view -o jsonpath="{.clusters[?(@.name==\"$cluster\")].cluster.server}" 2>/dev/null || true)"
73
- [ -n "$server" ] || return 1
74
- # Rule by name (old rule of metrics-server.sh)
75
- case "$ctx" in
76
- kind-* | docker-desktop | rancher-desktop | orbstack | minikube) return 0 ;;
77
- esac
78
- # Host extraction (IPv6 addresses in brackets handled)
79
- host="${server#*://}"
80
- host="${host%%/*}"
81
- if [[ "$host" == \[* ]]; then
82
- host="${host#[}"
83
- host="${host%%]*}"
84
- else
85
- host="${host%%:*}"
86
- fi
87
- case "$host" in
88
- 127.0.0.1 | localhost | ::1 | host.docker.internal) return 0 ;;
89
- *-control-plane) return 0 ;;
90
- esac
91
- return 1
92
- }
93
-
94
- # Sets the « devzone » context (cluster + user) on the given context (default:
95
- # the current one): after an installation on a new machine, --context devzone
96
- # targets the chosen services cluster from the start (#396)
97
- devzone_context_set() {
98
- local ctx cluster user
99
- ctx="${1:-$(kubectl config current-context 2>/dev/null || true)}"
100
- if [ -z "$ctx" ]; then
101
- return 1
102
- fi
103
- cluster="$(kubectl config view -o jsonpath="{.contexts[?(@.name==\"$ctx\")].context.cluster}" 2>/dev/null || true)"
104
- user="$(kubectl config view -o jsonpath="{.contexts[?(@.name==\"$ctx\")].context.user}" 2>/dev/null || true)"
105
- if [ -n "$cluster" ]; then
106
- kubectl config set-context devzone --cluster="$cluster" --user="$user" >/dev/null 2>&1
107
- else
108
- return 1
109
- fi
110
- }
111
-
112
- cluster_reachable() {
113
- kubectl --context "$1" --request-timeout=3s get --raw /readyz >/dev/null 2>&1
114
- }
115
-
116
- # Host contexts: local ones first, then the dedicated kind, finally the remote ones
117
- cluster_candidates() {
118
- local host="${DEVZONE_KUBE_HOST:-$HOME/.kube/hote.config}" c
119
- if [ -f "$host" ]; then
120
- # 1. Local clusters of the host (server on a loopback address)
121
- for c in $(KUBECONFIG="$host" kubectl config get-contexts -o name 2>/dev/null); do
122
- cluster_is_dedicated "$c" && continue
123
- if cluster_is_local "$c" && cluster_reachable "$c"; then
124
- echo "$c"
125
- fi
126
- done
127
- fi
128
- # 2. Dedicated kind cluster
129
- echo "$CLUSTER_DEDICATED"
130
- if [ -f "$host" ]; then
131
- # 3. Remote clusters of the host
132
- for c in $(KUBECONFIG="$host" kubectl config get-contexts -o name 2>/dev/null); do
133
- cluster_is_dedicated "$c" && continue
134
- if ! cluster_is_local "$c" && cluster_reachable "$c"; then
135
- echo "$c"
136
- fi
137
- done
138
- fi
139
- return 0
140
- }
141
-
142
- # Menu on stderr (stdout carries the answer); Enter = first candidate
143
- cluster_menu() {
144
- local candidates=() c i=1 answer
145
- while IFS= read -r c; do candidates+=("$c"); done < <(cluster_candidates)
146
- {
147
- echo "devzone : on which cluster should the devzone services be installed?"
148
- for c in "${candidates[@]}"; do
149
- if cluster_is_dedicated "$c"; then
150
- echo " $i) $c dedicated kind cluster, in the container (default)"
151
- elif cluster_is_local "$c"; then
152
- echo " $i) $c local cluster"
153
- else
154
- echo " $i) $c remote cluster"
155
- fi
156
- i=$((i + 1))
157
- done
158
- [ -f "${DEVZONE_KUBE_HOST:-$HOME/.kube/hote.config}" ] ||
159
- echo " (host clusters not visible: devzone enable kubernetes)"
160
- } >&2
161
- printf 'Number [1]: ' >&2
162
- read -r answer || answer=""
163
- answer="${answer:-1}"
164
- case "$answer" in
165
- *[!0-9]* | 0) echo "devzone : invalid choice: $answer" >&2; return 2 ;;
166
- esac
167
- if [ "$answer" -gt "${#candidates[@]}" ]; then
168
- echo "devzone : invalid choice: $answer" >&2
169
- return 2
170
- fi
171
- echo "${candidates[$((answer - 1))]}"
172
- }
173
-
174
- # Interactive terminal; DEVZONE_INTERACTIVE=1 for the tests (stdin redirected)
175
- cluster_interactive() {
176
- [ "${DEVZONE_INTERACTIVE:-}" = 1 ] || [ -t 0 ]
177
- }
178
-
179
- cluster_choose() {
180
- local wanted="" confirm="" c answer arg
181
- while [ "$#" -gt 0 ]; do
182
- arg="$1"
183
- shift
184
- case "$arg" in
185
- --confirm)
186
- if [ "$#" -eq 0 ]; then
187
- echo "devzone : --confirm needs a context" >&2
188
- return 2
189
- fi
190
- confirm="$1"
191
- shift
192
- ;;
193
- --confirm=*) confirm="${arg#*=}" ;;
194
- -*)
195
- echo "devzone : unknown argument: $arg (cluster_choose [CTX] [--confirm CTX])" >&2
196
- return 2
197
- ;;
198
- *)
199
- if [ -n "$wanted" ]; then
200
- echo "devzone : unknown argument: $arg (cluster_choose [CTX] [--confirm CTX])" >&2
201
- return 2
202
- fi
203
- wanted="$arg"
204
- ;;
205
- esac
206
- done
207
- if [ -n "$wanted" ]; then
208
- while IFS= read -r c; do
209
- [ "$c" = "$wanted" ] && break
210
- done < <(cluster_candidates)
211
- if [ "$c" != "$wanted" ]; then
212
- echo "devzone : unknown or unreachable cluster: $wanted; candidates: $(cluster_candidates | tr '\n' ' ')" >&2
213
- return 2
214
- fi
215
- # Remote: confirmation required. --confirm counts only if it is exactly
216
- # the requested context; DEVZONE_CLUSTER_REMOTE is still accepted for
217
- # the tests and CI
218
- if ! cluster_is_local "$wanted"; then
219
- if [ -n "$confirm" ]; then
220
- if [ "$confirm" != "$wanted" ]; then
221
- echo "devzone : remote cluster refused: $wanted (--confirm $wanted)" >&2
222
- return 2
223
- fi
224
- elif [ -n "${DEVZONE_CLUSTER_REMOTE:-}" ]; then
225
- if [ "${DEVZONE_CLUSTER_REMOTE}" != "$wanted" ]; then
226
- echo "devzone : remote cluster refused: $wanted (DEVZONE_CLUSTER_REMOTE must be exactly $wanted)" >&2
227
- return 2
228
- fi
229
- elif cluster_interactive; then
230
- printf 'remote cluster: %s ; type its name to confirm: ' "$wanted" >&2
231
- read -r answer || answer=""
232
- if [ "$answer" != "$wanted" ]; then
233
- echo "devzone : remote cluster refused: $wanted (--confirm $wanted)" >&2
234
- return 2
235
- fi
236
- else
237
- echo "devzone : remote cluster refused: $wanted (--confirm $wanted)" >&2
238
- return 2
239
- fi
240
- fi
241
- echo "$wanted"
242
- return 0
243
- fi
244
- c="$(cluster_saved)"
245
- if [ -n "$c" ]; then
246
- echo "$c"
247
- elif cluster_interactive; then
248
- cluster_menu
249
- else
250
- # Default: local only (never remote by default)
251
- local host_choice=""
252
- if [ -f "${DEVZONE_KUBE_HOST:-$HOME/.kube/hote.config}" ]; then
253
- host_choice="$(KUBECONFIG="${DEVZONE_KUBE_HOST:-$HOME/.kube/hote.config}" kubectl config current-context 2>/dev/null || true)"
254
- fi
255
- if [ -n "$host_choice" ] && cluster_is_local "$host_choice" && cluster_reachable "$host_choice"; then
256
- echo "$host_choice"
257
- else
258
- echo "$CLUSTER_DEDICATED"
259
- fi
260
- fi
261
- }
262
-
263
- # Runs kind without switching kubectl's current context (#366): the previous
264
- # one is restored, or removed if there was none. The kind exit code is returned
265
- # even under set -e: the restore happens first.
266
- kind_keep_context() {
267
- local previous code=0
268
- previous="$(kubectl config current-context 2>/dev/null || true)"
269
- kind "$@" || code=$?
270
- if [ -n "$previous" ]; then
271
- kubectl config use-context "$previous" >/dev/null 2>&1 || true
272
- else
273
- KUBECONFIG="$HOME/.kube/config" kubectl config unset current-context >/dev/null 2>&1 || true
274
- fi
275
- return "$code"
276
- }
277
-
278
- # Creates a kind cluster without switching kubectl's current context.
279
- kind_create_keep_context() {
280
- kind_keep_context create cluster "$@"
281
- }
282
-
283
- cluster_prepare() {
284
- local ctx="$1"
285
- if [ "$ctx" = "$CLUSTER_DEDICATED" ]; then
286
- if ! kind get clusters 2>/dev/null | grep -qx "devzone${INSTANCE_SUFFIX}"; then
287
- echo "▶ Creating the kind devzone cluster (about 1 min)…"
288
- # Busy host Docker (its Kubernetes, other clusters): the node API can
289
- # restart during initialization and kind gives up within seconds. A
290
- # second attempt, on a fresh cluster.
291
- if ! kind_create_keep_context --name "devzone${INSTANCE_SUFFIX}" --wait 120s; then
292
- echo "▶ Retrying the kind cluster creation…"
293
- kind delete cluster --name "devzone${INSTANCE_SUFFIX}" >/dev/null 2>&1 || true
294
- kind_create_keep_context --name "devzone${INSTANCE_SUFFIX}" --wait 120s || return 1
295
- fi
296
- fi
297
- elif ! cluster_reachable "$ctx"; then
298
- echo "devzone : cluster $ctx unreachable (kubernetes module enabled? cluster started?)" >&2
299
- return 1
300
- fi
301
- # If it has just been created, the services cluster must carry the devzone
302
- # context at once, otherwise the installation targets the old cluster (#396)
303
- devzone_context_set "$ctx" || echo "devzone : devzone context not set on $ctx" >&2
304
- }
305
-
306
- # Dedicated kind with no devzone namespace left: deleted (it only uses memory).
307
- # Never a host cluster.
308
- cluster_release() {
309
- local ctx="$1" left
310
- cluster_is_dedicated "$ctx" || return 0
311
- kind get clusters 2>/dev/null | grep -qx "devzone${INSTANCE_SUFFIX}" || return 0
312
- # Reading the namespaces labelled by the administrator (#430): the namespace
313
- # list is refused to the limited account
314
- left="$(account_admin "$ctx" get namespaces -l "$CLUSTER_LABEL" -o name 2>/dev/null || true)"
315
- if [ -z "$left" ]; then
316
- kind delete cluster --name "devzone${INSTANCE_SUFFIX}" >/dev/null 2>&1 || true
317
- echo "▶ kind devzone cluster removed (no service left)."
318
- fi
319
- }
320
-
321
- # ---- Relay: port of a cluster service on the container's 127.0.0.1 ----
322
- # kubectl port-forward works on every cluster whose API answers (dedicated kind
323
- # or host cluster), without exposing a port on the nodes. It stops when the pod
324
- # restarts: the loop restarts it. PID and log in workspace/.<name>/ (module
325
- # folder, ignored by git).
326
- relay_dir() {
327
- echo "$RACINE/workspace/.$1"
328
- }
329
-
330
- relay_active() {
331
- local pid
332
- pid="$(cat "$(relay_dir "$1")/relais.pid" 2>/dev/null || true)"
333
- [ -n "$pid" ] && kill -0 "$pid" 2>/dev/null
334
- }
335
-
336
- relay_start() {
337
- local name="$1" ctx="$2" ns="$3" target="$4" dir
338
- shift 4
339
- dir="$(relay_dir "$name")"
340
- mkdir -p "$dir"
341
- relay_active "$name" && return 0
342
- # setsid: separate session, the relay survives the end of the script that
343
- # started it (postStartCommand, devzone workspace shell); absent on macOS (bash 3.2 tests)
344
- local detached=()
345
- command -v setsid >/dev/null 2>&1 && detached=(setsid)
346
- # port-forward targets one precise pod and sees its disappearance only at the
347
- # next connection, which fails. For a service, the loop therefore follows the
348
- # ready pod behind it (UID in its endpoints): waits until there is one, then
349
- # restarts port-forward as soon as it changes (pod restarted).
350
- # shellcheck disable=SC2016 # variables expanded by the relay's bash
351
- ${detached[@]+"${detached[@]}"} nohup bash -c '
352
- pod() {
353
- case "$2" in
354
- svc/*) kubectl --context "$0" -n "$1" get endpoints "${2#svc/}" -o jsonpath="{.subsets[*].addresses[*].targetRef.uid}" 2>/dev/null ;;
355
- *) echo fixe ;;
356
- esac
357
- }
358
- while :; do
359
- until [ -n "$(pod "$@")" ]; do sleep 2; done
360
- actuel="$(pod "$@")"
361
- kubectl --context "$0" -n "$1" port-forward --address 127.0.0.1 "$2" "${@:3}" &
362
- pf=$!
363
- while kill -0 "$pf" 2>/dev/null; do
364
- sleep 5
365
- [ "$(pod "$@")" = "$actuel" ] || { kill "$pf" 2>/dev/null; break; }
366
- done
367
- wait "$pf" 2>/dev/null
368
- sleep 1
369
- done' \
370
- "$ctx" "$ns" "$target" "$@" >"$dir/relais.log" 2>&1 &
371
- echo "$!" >"$dir/relais.pid"
372
- }
373
-
374
- relay_stop() {
375
- local file pid children
376
- file="$(relay_dir "$1")/relais.pid"
377
- pid="$(cat "$file" 2>/dev/null || true)"
378
- if [ -n "$pid" ]; then
379
- # Children (port-forward) listed before: once the loop is killed, they
380
- # are reattached to init and pgrep -P no longer sees them. The loop first,
381
- # otherwise it would restart the port-forward.
382
- children="$(pgrep -P "$pid" 2>/dev/null || true)"
383
- kill "$pid" 2>/dev/null || true
384
- # shellcheck disable=SC2086 # list of PIDs
385
- [ -z "$children" ] || kill $children 2>/dev/null || true
386
- fi
387
- rm -f "$file"
388
- }