From 94d67e4982ae51f7fadb9a82495939192de81bbd Mon Sep 17 00:00:00 2001 From: Ari Sosnovsky Date: Wed, 30 Oct 2024 10:31:46 -0400 Subject: [PATCH] metallb --- bin/dev | 87 +++++++++++++++++++ bin/{dryrun-app.sh => dryrun-app} | 0 bin/extra/common.sh | 62 +++++++++++++ bin/install-app.sh | 45 ---------- bin/install-argo.sh | 40 --------- bin/make-new-chart.sh | 45 ---------- .../templates/_helpers/application.yaml | 29 +++++++ .../templates/argoappset-infrastructure.yaml | 25 ++++++ k8s/argoapps/values.yaml | 11 ++- k8s/bootstrap/metallb.yml | 15 ---- k8s/infrastructure/metallb/Chart.yaml | 10 +++ .../metallb/templates/metallb.yml | 15 ++++ k8s/infrastructure/metallb/values-dev.yaml | 0 k8s/infrastructure/metallb/values-prod.yaml | 0 k8s/infrastructure/metallb/values.yaml | 4 + shell.nix | 2 + test.jsonnet | 0 17 files changed, 244 insertions(+), 146 deletions(-) create mode 100755 bin/dev rename bin/{dryrun-app.sh => dryrun-app} (100%) create mode 100644 bin/extra/common.sh delete mode 100755 bin/install-app.sh delete mode 100755 bin/install-argo.sh delete mode 100755 bin/make-new-chart.sh create mode 100644 k8s/argoapps/templates/_helpers/application.yaml create mode 100644 k8s/argoapps/templates/argoappset-infrastructure.yaml delete mode 100644 k8s/bootstrap/metallb.yml create mode 100644 k8s/infrastructure/metallb/Chart.yaml create mode 100644 k8s/infrastructure/metallb/templates/metallb.yml create mode 100644 k8s/infrastructure/metallb/values-dev.yaml create mode 100644 k8s/infrastructure/metallb/values-prod.yaml create mode 100644 k8s/infrastructure/metallb/values.yaml create mode 100644 test.jsonnet diff --git a/bin/dev b/bin/dev new file mode 100755 index 0000000..bb02f2a --- /dev/null +++ b/bin/dev @@ -0,0 +1,87 @@ +#!/usr/bin/env bash +set -e + + +# Bootstrap +set -e +ROOT_FOLDER=$(realpath $0 | xargs dirname | xargs dirname) +. $ROOT_FOLDER/bin/extra/common.sh +# Bootstrap + +pushd $ROOT_FOLDER > /dev/null + + + +function run-bootstrap-argo() { + : Initial setup for argocd + : + deployment=${1} + check-deploy-name $deployment + echo "Installing initial Chart..." + helm install argocd k8s/argocd \ + --namespace argocd \ + --create-namespace \ + --values k8s/argocd/values.yaml \ + --set selfapp.deployment="${DEPLOYMENT}" \ + --set selfapp.sshkey="$(cat ~/.ssh/github_ed)" + + while ! check-argo-alive; do + echo "Waiting on argo to get started" + kubectl -n argocd get all + sleep 1 + done + + echo "Login to argo" + argocd login cd.argoproj.io --core + kubectl config set-context --current --namespace argocd + argocd admin initial-password +} + +function run-upgrade-argo() { + : Manually upgrade argocd from disk + : + deployment=${1} + check-deploy-name $deployment + + pushd $ROOT_FOLDER/k8s/argocd + helm dependency update + popd + + helm upgrade argocd k8s/argocd \ + --namespace argocd \ + --create-namespace \ + --values k8s/argocd/values.yaml \ + --set selfapp.enabled="true" \ + --set selfapp.deployment="${deployment}" \ + --set selfapp.sshkey="$(cat ~/.ssh/github_ed)" +} + +function run-port-forward-argo() { + : Port forward argocd to local + : + local_port=${1:-8080} + kubectl port-forward services/argocd-server $local_port:80 -n argocd +} + +function run-new-chart() { + : Create a new helm chart + : dev new-chart {path-to-chart} + + full_path=${1} + app=$(basename $full_path) + location=$(dirname $full_path) + typeofapp=$(basename $location) + + mkdir -p $full_path/templates + touch $full_path/values.yaml + cat <> $full_path/Chart.yaml +apiVersion: v2 +name: $app +description: An $typeofapp service for homelab +type: application +version: 0.1.0 +dependencies: [] +EOT +} + +start_cli $@ \ No newline at end of file diff --git a/bin/dryrun-app.sh b/bin/dryrun-app similarity index 100% rename from bin/dryrun-app.sh rename to bin/dryrun-app diff --git a/bin/extra/common.sh b/bin/extra/common.sh new file mode 100644 index 0000000..9b10f06 --- /dev/null +++ b/bin/extra/common.sh @@ -0,0 +1,62 @@ +#!/bin/sh + +## Colors +YELLOW='\033[0;33m' +BLUE='\033[0;34m' +NC='\033[0m' +BOLD='\033[1m' +BLUE_BOLD='\033[1;34m' +YELLOW_BOLD='\033[1;33m' + + +function list_run_functions() { + declare -F | awk '$3 ~ /^run-/{sub("run-", "", $3); print $3}' +} + +function start_cli() { + MENAME=$(basename $0) + all_functions=$(list_run_functions) + + for f in ${all_functions[@]}; do + if [ "$1" == "$f" ]; then + echo -e "Running ${BLUE_BOLD}$1${NC}" + run-$1 ${@:2} 2>&1 | sed "s/^/\t│ /" + ecode=$(echo $?) + echo -e "\t└──${NC}DONE ${BLUE_BOLD}$1${NC}" + exit $ecode + fi + done + + echo -e "Invalid Command '$1' please select one of${NC}" + for function_name in $all_functions; do + start_line=$(grep -n -m 1 "function run-$function_name" "${BASH_SOURCE[1]}" | cut -d: -f1) + end_line=$(awk "NR > $start_line && /^}/ {print NR; exit}" "${BASH_SOURCE[1]}") + function_body=$(sed -n "${start_line},${end_line}p" "${BASH_SOURCE[1]}") + description=$(echo "$function_body" | sed -n '/^\s*:/,/^\s*[^:]/p' | sed 's/^\s*:\s*/ │ /') + echo -e "$BOLD\r$MENAME \`${function_name}\`$NC\n${description}\n └──────────\n" + done + +} + + +function check-deploy-name() { + deploy=${1} + if [[ "$deploy" == "prod" || "$deploy" == "dev" ]]; then + echo "Deploy environment is set to $deploy." + else + echo "Invalid deploy environment. Must be 'dev' or 'prod', but got '${deploy}'." + exit 1 + fi +} + +function check-argo-alive() { + pod_statuses=$(kubectl -n argocd get pod -o json | jq -r '.items[].status.phase') + + # Check if all pod statuses are "Running" + for pod_status in $pod_statuses; do + if [[ "$status" != "Running" ]]; then + return 0 + fi + done + return 1 +} \ No newline at end of file diff --git a/bin/install-app.sh b/bin/install-app.sh deleted file mode 100755 index 866adcf..0000000 --- a/bin/install-app.sh +++ /dev/null @@ -1,45 +0,0 @@ -#!/usr/bin/env bash -mkdir -p .tmp - -deployment=${1:-dev} -keep_old=${2:-no} -keep_old=$(echo "${keep_old:0:1}" | tr '[:upper:]' '[:lower:]') -app=argocd -location=k8s/$app -full_path=$(realpath $location) -tmp_path=".tmp/$location/$deployment-$app.yaml" -mkdir -p $(dirname $tmp_path) - -pushd $full_path &> /dev/null -helm dependency update &> /dev/null -popd &> /dev/null - -if [ -f "$tmp_path" ]; then - echo "Found existing file" - if [[ -f "$tmp_path".old && "${keep_old:0:1}" == "y" ]]; then - num_files=$(ls -1q "${tmp_path}.old"* 2> /dev/null | wc -l | tr -d '[:space:]') - echo " -> Renaming old file to "$tmp_path".old.${num_files}" - mv $tmp_path $tmp_path".old.${num_files}" - else - rm $tmp_path".old"* - echo " -> Renaming old file to "$tmp_path".old" - mv $tmp_path $tmp_path".old" - fi -fi -helm template $app $full_path --namespace $app \ ---debug \ ---create-namespace \ ---values k8s/argocd/values.yaml \ ---values k8s/argocd/values-${deployment}.yaml \ ---set selfapp.deployment="${deployment}" \ ---set selfapp.enabled="true" \ ---set selfapp.sshkey="$(cat ~/.ssh/id_rsa)" \ -> $tmp_path - -echo "Wrote Template! --> $tmp_path" -echo "===============" - -if [ -f "$tmp_path.old" ]; then - echo " -> creating diff -- $tmp_path.diff" - diff -u $tmp_path".old" $tmp_path > "$tmp_path.diff" -fi \ No newline at end of file diff --git a/bin/install-argo.sh b/bin/install-argo.sh deleted file mode 100755 index 1e7fbd8..0000000 --- a/bin/install-argo.sh +++ /dev/null @@ -1,40 +0,0 @@ -#!/usr/bin/env bash -set -e - -pushd k8s/argocd - -echo "Installing dependencies..." -helm dependency update - -popd - -DEPLOYMENT=${1:-dev} - -echo "Installing initial Chart..." -helm install argocd k8s/argocd \ ---namespace argocd \ ---create-namespace \ ---values k8s/argocd/values.yaml \ ---set selfapp.deployment="${DEPLOYMENT}" \ ---set selfapp.sshkey="$(cat ~/.ssh/github_ed)" - -echo "Waiting on argo to get started" -watch -n1 "kubectl -n argocd get all" - -echo "Login to argo" -argocd login cd.argoproj.io --core -kubectl config set-context --current --namespace argocd -argocd admin initial-password - -echo "Enabling selfapp" -helm upgrade argocd k8s/argocd \ ---namespace argocd \ ---create-namespace \ ---values k8s/argocd/values.yaml \ ---set selfapp.enabled="true" \ ---set selfapp.deployment="${DEPLOYMENT}" \ ---set selfapp.sshkey="$(cat ~/.ssh/github_ed)" - -echo "Final sync" -argocd app sync argocd/ - diff --git a/bin/make-new-chart.sh b/bin/make-new-chart.sh deleted file mode 100755 index 866adcf..0000000 --- a/bin/make-new-chart.sh +++ /dev/null @@ -1,45 +0,0 @@ -#!/usr/bin/env bash -mkdir -p .tmp - -deployment=${1:-dev} -keep_old=${2:-no} -keep_old=$(echo "${keep_old:0:1}" | tr '[:upper:]' '[:lower:]') -app=argocd -location=k8s/$app -full_path=$(realpath $location) -tmp_path=".tmp/$location/$deployment-$app.yaml" -mkdir -p $(dirname $tmp_path) - -pushd $full_path &> /dev/null -helm dependency update &> /dev/null -popd &> /dev/null - -if [ -f "$tmp_path" ]; then - echo "Found existing file" - if [[ -f "$tmp_path".old && "${keep_old:0:1}" == "y" ]]; then - num_files=$(ls -1q "${tmp_path}.old"* 2> /dev/null | wc -l | tr -d '[:space:]') - echo " -> Renaming old file to "$tmp_path".old.${num_files}" - mv $tmp_path $tmp_path".old.${num_files}" - else - rm $tmp_path".old"* - echo " -> Renaming old file to "$tmp_path".old" - mv $tmp_path $tmp_path".old" - fi -fi -helm template $app $full_path --namespace $app \ ---debug \ ---create-namespace \ ---values k8s/argocd/values.yaml \ ---values k8s/argocd/values-${deployment}.yaml \ ---set selfapp.deployment="${deployment}" \ ---set selfapp.enabled="true" \ ---set selfapp.sshkey="$(cat ~/.ssh/id_rsa)" \ -> $tmp_path - -echo "Wrote Template! --> $tmp_path" -echo "===============" - -if [ -f "$tmp_path.old" ]; then - echo " -> creating diff -- $tmp_path.diff" - diff -u $tmp_path".old" $tmp_path > "$tmp_path.diff" -fi \ No newline at end of file diff --git a/k8s/argoapps/templates/_helpers/application.yaml b/k8s/argoapps/templates/_helpers/application.yaml new file mode 100644 index 0000000..5f7f688 --- /dev/null +++ b/k8s/argoapps/templates/_helpers/application.yaml @@ -0,0 +1,29 @@ +{{ define "homelab.argo.applicaton" }} +--- +apiVersion: argoproj.io/v1alpha1 +kind: Application +metadata: + name: "{{ .name }}" +spec: + project: {{ .project }} + source: + repoURL: "git@github.com:{{ .Values.gitops.repo }}.git" + targetRevision: "{{ .Values.gitops.commit }}" + path: 'k8s/apps/{{ .name }}' + helm: + valueFiles: + - values.yaml + - values-{{.Values.global.deployment}}.yaml + values: |- + global: + {{- .Values.global | toYaml | nindent 12 }} + releaseName: {{ .name }} + destination: + server: https://kubernetes.default.svc + namespace: {{ .namespace }} + {{- if .syncPolicy }} + syncPolicy: + {{- .syncPolicy | toYaml | nindent 4 }} + {{ end }} + +{{ end }} \ No newline at end of file diff --git a/k8s/argoapps/templates/argoappset-infrastructure.yaml b/k8s/argoapps/templates/argoappset-infrastructure.yaml new file mode 100644 index 0000000..6e5ff1f --- /dev/null +++ b/k8s/argoapps/templates/argoappset-infrastructure.yaml @@ -0,0 +1,25 @@ +{{- $argoappsetInfra := "infrastructure" -}} + +{{- include "homelab.argo.project" (dict "name" $argoappsetInfra "description" "Infrastructure related charts") -}} + + +{{- range $appName, $appProps := .Values.infrastructure -}} + {{- $_ := set $appProps "Values" $.Values -}} + {{- $_ := set $appProps "name" (default $appProps.name (printf "infra-%s" $appName)) -}} + {{- $_ := set $appProps "project" (default $appProps.project $argoappsetInfra) -}} + {{- $_ := set $appProps "namespace" (default $appProps.namespace $appName) -}} + + {{- if (not $appProps.disabled) -}} + {{- if $appProps.createNamespace -}} +--- +apiVersion: v1 +kind: Namespace +metadata: + name: "{{ $appProps.namespace }}" + {{ end }} + + {{- include "homelab.argo.applicaton" $appProps -}} + + {{- end -}} + +{{- end -}} diff --git a/k8s/argoapps/values.yaml b/k8s/argoapps/values.yaml index 07f2efa..3bdfcbb 100644 --- a/k8s/argoapps/values.yaml +++ b/k8s/argoapps/values.yaml @@ -8,4 +8,13 @@ global: infrastructureCharts: {} -apps: {} \ No newline at end of file +apps: {} +infrastructure: + metallb: + createNamespace: true + namespace: metallb-system + syncPolicy: + automated: + selfHeal: true + syncOptions: + - ServerSideApply=true \ No newline at end of file diff --git a/k8s/bootstrap/metallb.yml b/k8s/bootstrap/metallb.yml deleted file mode 100644 index 78af09d..0000000 --- a/k8s/bootstrap/metallb.yml +++ /dev/null @@ -1,15 +0,0 @@ ---- -apiVersion: metallb.io/v1beta1 -kind: IPAddressPool -metadata: - name: first-pool - namespace: metallb-system -spec: - addresses: - - 10.0.100.1-10.0.100.255 ---- -apiVersion: metallb.io/v1beta1 -kind: L2Advertisement -metadata: - name: l2ad - namespace: metallb-system diff --git a/k8s/infrastructure/metallb/Chart.yaml b/k8s/infrastructure/metallb/Chart.yaml new file mode 100644 index 0000000..61945e9 --- /dev/null +++ b/k8s/infrastructure/metallb/Chart.yaml @@ -0,0 +1,10 @@ +apiVersion: v2 +name: cluster +description: An infrastructure collection of manifests for homelab +type: application +version: 0.1.0 +dependencies: + - name: metallb + version: 0.14.8 + repository: https://metallb.github.io/metallb + diff --git a/k8s/infrastructure/metallb/templates/metallb.yml b/k8s/infrastructure/metallb/templates/metallb.yml new file mode 100644 index 0000000..5d55e6b --- /dev/null +++ b/k8s/infrastructure/metallb/templates/metallb.yml @@ -0,0 +1,15 @@ +--- +apiVersion: metallb.io/v1beta1 +kind: IPAddressPool +metadata: + name: homelab-pool + namespace: "{{ .Release.Namespace }}" +spec: + addresses: + {{- .Values.homelab.addresses | toYaml | nindent 4 }} +--- +apiVersion: metallb.io/v1beta1 +kind: L2Advertisement +metadata: + name: l2ad + namespace: "{{ .Release.Namespace }}" diff --git a/k8s/infrastructure/metallb/values-dev.yaml b/k8s/infrastructure/metallb/values-dev.yaml new file mode 100644 index 0000000..e69de29 diff --git a/k8s/infrastructure/metallb/values-prod.yaml b/k8s/infrastructure/metallb/values-prod.yaml new file mode 100644 index 0000000..e69de29 diff --git a/k8s/infrastructure/metallb/values.yaml b/k8s/infrastructure/metallb/values.yaml new file mode 100644 index 0000000..18b11cd --- /dev/null +++ b/k8s/infrastructure/metallb/values.yaml @@ -0,0 +1,4 @@ + +homelab: + addresses: + - 10.0.100.1-10.0.100.255 \ No newline at end of file diff --git a/shell.nix b/shell.nix index f4c159d..cc9232b 100644 --- a/shell.nix +++ b/shell.nix @@ -5,6 +5,8 @@ pkgs.mkShell { git kubectl kubernetes-helm + jsonnet + jsonnet-language-server argocd yq jq diff --git a/test.jsonnet b/test.jsonnet new file mode 100644 index 0000000..e69de29