-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Closes kairos-io/kairos#638 Signed-off-by: tyzbit <[email protected]>
- Loading branch information
Showing
3 changed files
with
187 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
FROM alpine as build | ||
ENV VERSION=2.1.1 | ||
|
||
ADD https://github.com/fluxcd/flux2/releases/download/v${VERSION}/flux_${VERSION}_linux_amd64.tar.gz /tmp | ||
RUN tar xzf /tmp/flux_${VERSION}_linux_amd64.tar.gz -C / && \ | ||
rm /tmp/flux_${VERSION}_linux_amd64.tar.gz | ||
|
||
FROM scratch | ||
COPY --from=build /flux /flux | ||
COPY ./bootstrap.sh /bootstrap.sh |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
#!/bin/bash | ||
|
||
set -x | ||
# Override with the `env` key if necessary | ||
export KUBECONFIG=/etc/rancher/k3s/k3s.yaml | ||
|
||
scriptdir="$( cd -- "$(dirname "$0")" || return >/dev/null 2>&1 ; pwd -P )" | ||
flux="$scriptdir/flux" | ||
short=2 | ||
long=900 | ||
|
||
cleanup() { | ||
timeout $short kubectl delete configmap -n default flux-boostrap | ||
} | ||
|
||
# Don't bootstrap if the cluster is already bootstrapped | ||
if [[ $($flux version 2>/dev/null; echo $?) -eq 0 ]]; then | ||
echo "Flux is already bootstrapped, exiting..." | ||
exit 0 | ||
fi | ||
|
||
# Determine what VCS we need to bootstrap | ||
for vcs in bitbucket_server git github gitlab; do | ||
if [[ $(kairos-agent config get flux.$vcs) != "null" ]]; then | ||
version_control=$vcs | ||
break | ||
fi | ||
done | ||
|
||
if [[ "${version_control}x" == "x" ]]; then | ||
echo "Unable to determine what version control provider to use, exiting..." | ||
exit 1 | ||
fi | ||
|
||
# Get flux envs and settings for our VCS | ||
mapfile -t envs < <(kairos-agent config get "flux.env") | ||
mapfile -t args < <(kairos-agent config get "flux.$version_control") | ||
declare -a cmdline | ||
|
||
# Set environment variables | ||
# These are likely sensitive | ||
set +x | ||
for setting in "${envs[@]}"; do | ||
env=$(echo "$setting" | cut -d: -f1) | ||
value=$(echo "$setting" | cut -d':' -f2 | sed -E 's/^ //g') | ||
if [[ "${value}x" != "x" ]]; then | ||
export "$env"="$value" | ||
fi | ||
done | ||
set -x | ||
|
||
# Set commandline args | ||
for setting in "${args[@]}"; do | ||
arg=$(echo "$setting" | cut -d: -f1) | ||
value=$(echo "$setting" | cut -d':' -f2 | sed -E 's/^ //g') | ||
if [[ "${value}x" != "x" ]]; then | ||
cmdline+=("--$arg" "$value") | ||
fi | ||
done | ||
|
||
# Try to bootstrap Flux for 30 minutes, sleep 15 seconds between attempts | ||
minutes=30 | ||
sleep=15 | ||
retry_attempt=1 | ||
active="false" | ||
|
||
if [[ "${#cmdline[@]}" -eq 0 ]]; then | ||
echo "Flux was not configured, not running any Flux commands" | ||
exit 2 | ||
else | ||
while [[ $retry_attempt -le "$(( minutes * 60 / sleep ))" ]]; do | ||
if [[ "$active" != "true" ]]; then | ||
# Ensure only one host tries to bootstrap, whichever makes the configmap first | ||
if ! timeout $short kubectl version &> /dev/null; then | ||
echo "Kubernetes API not ready yet, sleeping" | ||
else | ||
if ! timeout $short kubectl create configmap flux-bootstrap --from-literal=hostname="$(hostname)"; then | ||
echo "Unable to create configmap, another node may be active" | ||
fi | ||
|
||
# The configmap exists but we must finally check if the hostname matches | ||
if [[ "$(timeout $short kubectl get configmap -n default flux-bootstrap -o jsonpath='{.data.hostname}')" != "$(hostname)" ]]; then | ||
echo "Flux bootstrap ConfigMap exists but another node is active, exiting..." | ||
exit 3 | ||
fi | ||
|
||
# We must be the active node | ||
active="true" | ||
fi | ||
else | ||
if timeout $long $flux bootstrap "${version_control/_/-}" "${cmdline[@]}"; then | ||
cleanup | ||
exit 0 | ||
fi | ||
fi | ||
|
||
echo "Install attempt $retry_attempt failed, retrying in $sleep seconds" | ||
(( retry_attempt = retry_attempt + 1 )) | ||
sleep $sleep | ||
done | ||
fi | ||
|
||
echo "Failed to bootstrap with Flux, timed out ($minutes minutes)" | ||
exit 4 |