From d3ba3115ebdcc8ceb913ba0b98b2e08ffaa8e052 Mon Sep 17 00:00:00 2001 From: Andrei Kartashov Date: Mon, 4 Sep 2023 13:24:24 +0700 Subject: [PATCH] refactor state dir structure --- scripts/bootstrap.sh | 38 ++++++++++++------- state/gullfaxi/apps.yaml | 4 +- state/gullfaxi/system.yaml | 4 +- .../{argocd.yaml => argocd/application.yaml} | 0 .../values.yaml} | 0 .../application.yaml} | 17 +++++---- .../cert-manager/manifests/clusterissuer.yaml | 14 +++++++ 7 files changed, 53 insertions(+), 24 deletions(-) rename state/gullfaxi/system/{argocd.yaml => argocd/application.yaml} (100%) rename state/gullfaxi/system/{argocd-values.yaml => argocd/values.yaml} (100%) rename state/gullfaxi/system/{cert-manager.yaml => cert-manager/application.yaml} (50%) create mode 100644 state/gullfaxi/system/cert-manager/manifests/clusterissuer.yaml diff --git a/scripts/bootstrap.sh b/scripts/bootstrap.sh index 50af0a7..ef6e5cc 100755 --- a/scripts/bootstrap.sh +++ b/scripts/bootstrap.sh @@ -24,13 +24,15 @@ function main() { local state_path="$(git rev-parse --show-toplevel)/state/${env_name}" # apply manifests for crucial applications - for app_file in \ - ${state_path}/system/argocd.yaml \ + for app in \ + argocd \ ; do + app_dir=${state_path}/system/${app} + app_file=${state_path}/system/${app}/application.yaml namespace=$(yq -e '.spec.destination.namespace' ${app_file}) ${kubectl_cmd} create ns ${namespace} || true - helm_render ${app_file} | ${kubectl_cmd} -n ${namespace} apply -f - + helm_render ${app_dir} | ${kubectl_cmd} -n ${namespace} apply -f - done ${kubectl_cmd} apply -n argocd -R \ @@ -61,10 +63,10 @@ function help() { } function helm_render() { - local app_file=$1 - local values_file=${app_file%.yaml}-values.yaml + local app_dir=$1 + local app_file=${app_dir}/application.yaml - if [ -f ${values_file} ]; then + if yq -e '.spec | has("sources")' ${app_file} >/dev/null; then helm_render_from_sources "$@" else helm_render_from_source "$@" @@ -72,7 +74,8 @@ function helm_render() { } function helm_render_from_source() { - local app_file=$1 + local app_dir=$1 + local app_file=${app_dir}/application.yaml local repo=$(yq -e '.spec.source.repoURL' ${app_file}) local chart=$(yq -e '.spec.source.chart' ${app_file}) local chart_version=$(yq -e '.spec.source.targetRevision' ${app_file}) @@ -81,17 +84,22 @@ function helm_render_from_source() { release=$(yq -e '.spec.source.helm.releaseName' ${app_file}) \ || release=$(yq -e '.metadata.name' ${app_file}) local namespace=$(yq -e '.spec.destination.namespace' ${app_file}) - local values_file=$(mktemp /tmp/${release}_${chart}_${chart_version}.yaml.XXXX) - yq '.spec.source.helm.values // ""' ${app_file} > ${values_file} + local values_arg="" + if yq -e '.spec.source.helm | has("values")' ${app_file}; then + local values_file=$(mktemp /tmp/${release}_${chart}_${chart_version}.yaml.XXXX) + values_arg="--values ${values_file}" + yq '.spec.source.helm.values // ""' ${app_file} > ${values_file} + fi helm template ${release} ${chart} \ --version ${chart_version} --repo ${repo} \ --include-crds \ - --namespace ${namespace} --values ${values_file} + --namespace ${namespace} ${values_arg} } function helm_render_from_sources() { - local app_file=$1 + local app_dir=$1 + local app_file=${app_dir}/application.yaml local repo=$(yq -e '.spec.sources[0].repoURL' ${app_file}) local chart=$(yq -e '.spec.sources[0].chart' ${app_file}) local chart_version=$(yq -e '.spec.sources[0].targetRevision' ${app_file}) @@ -99,12 +107,16 @@ function helm_render_from_sources() { release=$(yq -e '.spec.sources[0].helm.releaseName' ${app_file}) \ || release=$(yq -e '.metadata.name' ${app_file}) local namespace=$(yq -e '.spec.destination.namespace' ${app_file}) - local values_file=${app_file%.yaml}-values.yaml + local values_file=${app_dir}/values.yaml + local values_arg="" + if [ -f ${values_file} ]; then + values_arg="--values ${values_file}" + fi helm template ${release} ${chart} \ --version ${chart_version} --repo ${repo} \ --include-crds \ - --namespace ${namespace} --values ${values_file} + --namespace ${namespace} ${values_arg} } main "${@}" diff --git a/state/gullfaxi/apps.yaml b/state/gullfaxi/apps.yaml index d7a442a..5585579 100644 --- a/state/gullfaxi/apps.yaml +++ b/state/gullfaxi/apps.yaml @@ -34,8 +34,8 @@ spec: targetRevision: main directory: # https://argo-cd.readthedocs.io/en/stable/user-guide/directory/ - recurse: false - exclude: '{*-values.yaml}' + recurse: true + exclude: '{values.yaml,*/manifests/*}' syncPolicy: automated: prune: true diff --git a/state/gullfaxi/system.yaml b/state/gullfaxi/system.yaml index aa76a63..f0c18a6 100644 --- a/state/gullfaxi/system.yaml +++ b/state/gullfaxi/system.yaml @@ -36,8 +36,8 @@ spec: targetRevision: main directory: # https://argo-cd.readthedocs.io/en/stable/user-guide/directory/ - recurse: false - exclude: '{*-values.yaml}' + recurse: true + exclude: '{values.yaml,*/manifests/*}' syncPolicy: automated: prune: true diff --git a/state/gullfaxi/system/argocd.yaml b/state/gullfaxi/system/argocd/application.yaml similarity index 100% rename from state/gullfaxi/system/argocd.yaml rename to state/gullfaxi/system/argocd/application.yaml diff --git a/state/gullfaxi/system/argocd-values.yaml b/state/gullfaxi/system/argocd/values.yaml similarity index 100% rename from state/gullfaxi/system/argocd-values.yaml rename to state/gullfaxi/system/argocd/values.yaml diff --git a/state/gullfaxi/system/cert-manager.yaml b/state/gullfaxi/system/cert-manager/application.yaml similarity index 50% rename from state/gullfaxi/system/cert-manager.yaml rename to state/gullfaxi/system/cert-manager/application.yaml index 0506290..fca03eb 100644 --- a/state/gullfaxi/system/cert-manager.yaml +++ b/state/gullfaxi/system/cert-manager/application.yaml @@ -13,10 +13,13 @@ spec: selfHeal: true syncOptions: - CreateNamespace=true - source: - repoURL: https://charts.jetstack.io - chart: cert-manager - targetRevision: 1.12.4 - helm: - values: | - # nothing + sources: + - repoURL: https://charts.jetstack.io + chart: cert-manager + targetRevision: 1.12.4 + helm: + values: | + installCRDs: true + - repoURL: git@github.com:amkartashov/gf-k8s.git + targetRevision: main + path: state/gullfaxi/system/cert-manager/manifests/ diff --git a/state/gullfaxi/system/cert-manager/manifests/clusterissuer.yaml b/state/gullfaxi/system/cert-manager/manifests/clusterissuer.yaml new file mode 100644 index 0000000..5180c8e --- /dev/null +++ b/state/gullfaxi/system/cert-manager/manifests/clusterissuer.yaml @@ -0,0 +1,14 @@ +apiVersion: cert-manager.io/v1 +kind: ClusterIssuer +metadata: + name: letsencrypt +spec: + acme: + server: https://acme-v02.api.letsencrypt.org/directory + email: a@ioot.xyz + privateKeySecretRef: + name: letsencrypt + solvers: + - http01: + ingress: + class: nginx