Skip to content

Commit 35051a3

Browse files
authored
Merge pull request #878 from nebius/SCHED-1167/flacky-fluxcd-install
Backport: Add retry wrapper for FluxCD kubectl provisioners
2 parents e181c54 + 16d51f3 commit 35051a3

File tree

2 files changed

+42
-7
lines changed

2 files changed

+42
-7
lines changed

soperator/modules/fluxcd/main.tf

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
resource "terraform_data" "flux_namespace" {
22
provisioner "local-exec" {
33
interpreter = ["/bin/bash", "-c"]
4-
command = join(
5-
" ",
6-
[
7-
"kubectl", "create", "namespace", "flux-system",
8-
"--context", var.k8s_cluster_context,
9-
]
10-
)
4+
# `kubectl create namespace` fails with AlreadyExists on re-runs, making it
5+
# non-retryable. Piping through `--dry-run=client -o yaml | kubectl apply`
6+
# produces an idempotent apply that succeeds whether the namespace exists or not.
7+
# The pipe requires a shell, so the inner command is passed as a bash -c string.
8+
command = join(" ", [
9+
"${path.module}/../scripts/retry.sh", "--", "bash", "-c",
10+
"'kubectl create namespace flux-system --context ${var.k8s_cluster_context} --dry-run=client -o yaml",
11+
"| kubectl apply --context ${var.k8s_cluster_context} -f -'",
12+
])
1113
}
1214
triggers_replace = {
1315
first_run = "true"
@@ -21,6 +23,7 @@ resource "terraform_data" "flux2" {
2123
command = join(
2224
" ",
2325
[
26+
"${path.module}/../scripts/retry.sh", "--",
2427
"kubectl", "--context", var.k8s_cluster_context,
2528
"apply", "-f", "https://github.com/fluxcd/flux2/releases/download/${var.flux_version}/install.yaml",
2629
]

soperator/modules/scripts/retry.sh

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#!/usr/bin/env bash
2+
set -euo pipefail
3+
4+
retries=5
5+
interval=2
6+
7+
while [[ $# -gt 0 && "$1" != "--" ]]; do
8+
case "$1" in
9+
-n) retries="$2"; shift 2 ;;
10+
-i) interval="$2"; shift 2 ;;
11+
*) echo "Usage: retry.sh [-n retries] [-i interval] -- <command>" >&2; exit 1 ;;
12+
esac
13+
done
14+
[[ "${1:-}" == "--" ]] && shift
15+
16+
if [[ $# -eq 0 ]]; then
17+
echo "Usage: retry.sh [-n retries] [-i interval] -- <command>" >&2
18+
exit 1
19+
fi
20+
21+
for i in $(seq 1 "$retries"); do
22+
if "$@"; then
23+
exit 0
24+
fi
25+
if [[ $i -lt $retries ]]; then
26+
echo "($i/$retries) Command failed, retrying in ${interval}s..."
27+
sleep "$interval"
28+
fi
29+
done
30+
31+
echo "Command failed after $retries attempts" >&2
32+
exit 1

0 commit comments

Comments
 (0)