Skip to content

Instantly share code, notes, and snippets.

@log2
Forked from fragolinux/k3d_local_git.sh
Last active March 12, 2022 11:42
Show Gist options
  • Save log2/f2fd0fa040509d3b6829ce813e5470dc to your computer and use it in GitHub Desktop.
Save log2/f2fd0fa040509d3b6829ce813e5470dc to your computer and use it in GitHub Desktop.
complete setup of a local k3d cluster on macos with flux enabled on local git server
#!/usr/bin/env bash
set -m # enable job control
## change your LAN ip address in the variable "PUBLICIP" before run
## run with "-d" command line switch to debug each step, script will wait for keypress to go on
## integrated mods by KingdonB in his fork: https://gist.github.com/kingdonb/dec74f3b74ffbb83b54d53d5c033e508
## added proper coredns patching via "coredns-custom" configmap
## added automatic /etc/hosts file modification if needed, sudo password will be asked in case
## added (commented out) lines to add Flux extra controllers to the setup
## deployed podinfo as in Flux Get Started guide
## added kustomize patch to change color in podinfo deployment
## issue: https://github.com/fluxcd/flux/issues/3594
CONFIG_HOME=~/testgit
CONFIG_REPO=testrepo
CLONE_PATH=${CONFIG_HOME}/${CONFIG_REPO}
BARE_REPO_PATH=${CONFIG_HOME}/gitsrv/${CONFIG_REPO}.git
CLUSTER_NAME=testclu
_is_good_local_ip() {
local ip="$1"
[ -n "$ip" ] && ping "$ip" -c 1 -W 50 &>/dev/null
}
if ! _is_good_local_ip "$PUBLICIP"; then
echo "PUBLICIP defined or not reachable, searching for it among defined interfaces (en0 through en9)"
PUBLICIP=
for intf in $(seq 0 9)
do
tempIP="$(ipconfig getifaddr en$intf)"
echo -n "Probing local IP $tempIP from interface en$intf ..."
if _is_good_local_ip "$tempIP" ; then
echo " IP is reachable, will use it for the rest of this script"
PUBLICIP="$tempIP"
break
else
echo " IP is not reachable"
fi
done
[ -z "$PUBLICIP" ] && echo "Can't find local IP, please set it accordingly via PUBLICIP variable before starting this script" && exit 1
fi
PUBLICDNS=e4t.example.com
GITSRV_IMG=jkarlos/git-server-docker
echo "creating folder structure and ssh keys"
mkdir -p ${CONFIG_HOME}/{${CONFIG_REPO},sshkeys,gitsrv}
ssh-keygen -b 521 -o -t ecdsa -N "" -f ${CONFIG_HOME}/sshkeys/identity
echo "creating a test repo for the gitsrv"
cd ${CLONE_PATH}
git init --shared=true
echo Nothing > README.md
git add .
git commit -m "1st commit"
git clone --bare ${CLONE_PATH} ${BARE_REPO_PATH}
[ "$1" == "-d" ] && read -p "Press [Enter] key to continue and create k3d cluster..."
echo "creating a k3d local test cluster"
k3d cluster create ${CLUSTER_NAME}
echo "Waiting for CoreDNS deploying"
ATTEMPTS=0
ROLLOUT_STATUS_CMD="kubectl rollout status deployment/coredns -n kube-system"
until $ROLLOUT_STATUS_CMD || [ $ATTEMPTS -eq 120 ]; do
$ROLLOUT_STATUS_CMD
ATTEMPTS=$((ATTEMPTS + 1))
sleep 1
done
echo "CoreDNS deployed, waiting for its ConfigMap"
while true ; do
result=$(kubectl -n kube-system get configmap coredns 2>&1)
if [[ $result == *NotFound* ]] || [[ $result == *refused* ]]; then
sleep 1
else
break
fi
done
[ "$1" == "-d" ] && read -p "Press [Enter] key to continue and patch coredns..."
echo "checking /etc/hosts line presence, or adding it if missing"
if grep -q "$PUBLICDNS" /etc/hosts ; then
echo "/etc/hosts file already modded"
else
echo "127.0.0.1 $PUBLICDNS" | sudo tee -a /etc/hosts > /dev/null
echo "added missing '127.0.0.1 $PUBLICDNS' line to your /etc/hosts file"
fi
echo "CoreDNS ConfigMap ready, patching it"
read -rd '' coredns_custom << EOF
apiVersion: v1
kind: ConfigMap
metadata:
name: coredns-custom
namespace: kube-system
data:
example.server: |
example.com {
hosts {
${PUBLICIP} ${PUBLICDNS}
fallthrough
}
whoami
}
EOF
echo "$coredns_custom" > ${CONFIG_HOME}/coredns-custom.yaml
kubectl apply -f ${CONFIG_HOME}/coredns-custom.yaml
kubectl get pods -n kube-system|grep coredns|cut -d\ -f1|xargs kubectl delete pod -n kube-system
# to test new dns record working, use: kubectl apply -f https://k8s.io/examples/admin/dns/dnsutils.yaml
[ "$1" == "-d" ] && read -p "Press [Enter] key to continue and create local git server..."
echo "creating the local gitserver pointing to the folders created above"
docker run -d -p 2222:22 \
-v ${CONFIG_HOME}/sshkeys:/git-server/keys \
-v ${CONFIG_HOME}/gitsrv:/git-server/repos \
--name gitsrv $GITSRV_IMG
echo "wait for port 2222 to be opened"
while ! nc -z localhost 2222; do
sleep 0.1
done
echo "adding ssh key and testing ssh"
ssh-add -D ${CONFIG_HOME}/sshkeys/identity
ssh-add ${CONFIG_HOME}/sshkeys/identity
sleep 1
ssh git@${PUBLICDNS} -p 2222
[ "$1" == "-d" ] && read -p "Press [Enter] key to continue and bootstrap Flux on local git server and cluster..."
echo "bootstrapping flux on test cluster, using SAME private key already used for the git server"
flux bootstrap git --branch=develop --path=. \
--url=ssh://git@${PUBLICDNS}:2222/git-server/repos/${CONFIG_REPO}.git \
--private-key-file=../sshkeys/identity
[ "$1" == "-d" ] && read -p "Press [Enter] key to continue cloning the git repo and adding coredns ..."
echo "cloning created git repo and adding coredns patch"
cd ${CONFIG_HOME}
rm -rf ${CLONE_PATH}
git clone ssh://git@${PUBLICDNS}:2222/git-server/repos/${CONFIG_REPO}.git -b develop
cd ${CLONE_PATH}
mv ${CONFIG_HOME}/coredns-custom.yaml ${CLONE_PATH}
git add -A && git commit -m "Add coredns patch"
git push
[ "$1" == "-d" ] && read -p "Press [Enter] key to continue and add podinfo git repo..."
echo "Add podinfo repository to Flux"
flux create source git podinfo \
--url=https://github.com/stefanprodan/podinfo \
--branch=master \
--interval=30s \
--export > podinfo-source.yaml
git add -A && git commit -m "Add podinfo GitRepository"
git push
[ "$1" == "-d" ] && read -p "Press [Enter] key to continue and add podinfo application..."
echo "Deploy podinfo application"
flux create kustomization podinfo \
--target-namespace=default \
--source=podinfo \
--path="./kustomize" \
--prune=true \
--wait=true \
--interval=5m \
--export > podinfo-kustomization.yaml
git add -A && git commit -m "Add podinfo Kustomization"
git push
echo "Waiting for PodInfo deploying"
ATTEMPTS=0
ROLLOUT_STATUS_CMD="kubectl rollout status deployment/podinfo -n default"
FLUX_RECONCILE_CMD="flux reconcile kustomization flux-system --with-source"
$FLUX_RECONCILE_CMD &
until $ROLLOUT_STATUS_CMD || [ $ATTEMPTS -eq 120 ]; do
$ROLLOUT_STATUS_CMD
ATTEMPTS=$((ATTEMPTS + 1))
sleep 1
done
echo "opening http://localhost:9898 in your browser (only on Mac - do it manually on other systems)"
kubectl port-forward deployment/podinfo 9898:9898 &
PID=$!
open http://localhost:9898
[ "$1" == "-d" ] && read -p "Press [Enter] key to continue and change podinfo background via kustomize patch..."
kill -9 $PID
echo "adding kustomize patch to alter podinfo background"
cd ${CLONE_PATH}
IFS= read -rd '' podinfopatch << EOF
patches:
- patch: |-
apiVersion: apps/v1
kind: Deployment
metadata:
name: podinfo
spec:
template:
spec:
containers:
- name: podinfod
env:
- name: PODINFO_UI_COLOR
value: '#ffff00'
target:
name: podinfo
kind: Deployment
EOF
echo -e "$podinfopatch" >> podinfo-kustomization.yaml
git add -A
git commit -m "changed color in podinfo deployment via kustomize patch"
git push
$FLUX_RECONCILE_CMD # flux will wait for the new deploy to become ready, but we cannot create a new
$ROLLOUT_STATUS_CMD # port-forward on the same port before it finished terminating! wait for that,
fg %2 # then also wait for port-forward to exit, to be sure it closes its port here.
# we can now also get away with opening the browser just once!
# echo "open http://localhost:9898 in your browser"
exec kubectl port-forward deployment/podinfo 9898:9898 &
PID=$!
# open http://localhost:9898
echo; echo
# [ "$1" == "-d" ] &&
read -p "Press [Enter] key to continue and close portforward, ending script (or ^C to leave it running)"
kill -9 $PID
echo "to remove everything, run the following lines"
echo "docker stop gitsrv"
echo "docker rm gitsrv"
echo "k3d cluster delete ${CLUSTER_NAME}"
echo "rm -rf ${CONFIG_HOME}"
# to add flux extra controllers, uncomment and run next lines:
# echo "adding image-reflector-controller,image-automation-controller Flux extra controllers"
# cd ${CLONE_PATH}/flux-system
# flux install --components-extra=image-reflector-controller,image-automation-controller --export > gotk-components.yaml
# git add .
# git commit -m "adding Flux image-reflector-controller and image-automation-controller"
# git push
# $FLUX_RECONCILE_CMD &
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment