Skip to content

kube-components-stack/kustomization

Repository files navigation

kustomization

This repository represents how implement a Kubernetes DevOps Workflow which simplifies evolve bricks of a DevOps infrastructure.

Who have never got the temptation to fork a helm chart repository in order to add some functionalities ? Me first ...

This workflow try bring closer the state of the art using the better of DevOps tools: Package Management (Helm) >> Config Management (Kustomize) >> GitOps (ArgoCD) >> Admission Controller (kyverno)

This implementation is based on following publictions:

Some killer features:

  • based on App Of Apps Pattern & ArgoCD Applicationset
  • ArgoCD manages itself ... Chicken or the egg ?
  • Custom Ressource Definitions management
  • Performing Async Actions using Hooks
  • Pre-installed Unified Grafana Dashboards (logging/monitoring) + ArgoCD notifications correlation
  • Backup of ingress's logs into an object storage using a low level tool (fast, memory efficient, and designed to handle the most demanding workloads)
  • Backup of app's logs into an object storage
  • ArgoCD notifications ready: slack or teams
  • Private key encryption of passwords
  • Pod Security Standards implemented as Kyverno policies
  • extra policies: Best Practices, mitigates CVE

Usage

Helm must be installed to use the charts. Please refer to Helm's documentation to get started.

Once Helm has been set up correctly, add the repo as follows:

helm repo add kustomization https://kube-components-stack.github.io/kustomization

If you had already added this repo earlier, run helm repo update to retrieve the latest versions of the packages. You can then run helm search repo kustomization to see the charts.

To install the kustomization chart:

helm install my-kustomization kustomization/kustomization

To uninstall the chart:

helm delete my-kustomization

structure

.
β”œβ”€β”€ manifests
β”‚   β”œβ”€β”€ argocd-applicationset
β”‚   β”‚   β”œβ”€β”€ addons-apps.yaml
β”‚   β”‚   β”œβ”€β”€ addons-crds.yaml
β”‚   β”‚   β”œβ”€β”€ core-apps.yaml
β”‚   β”‚   └── core-crds.yaml
β”‚   └── argocd-appproject
β”‚       β”œβ”€β”€ addons-apps.yaml
β”‚       β”œβ”€β”€ addons-crds.yaml
β”‚       β”œβ”€β”€ core-apps.yaml
β”‚       └── core-crds.yaml
β”œβ”€β”€ rendered-manifests
β”‚   β”œβ”€β”€ core
β”‚   β”‚   β”œβ”€β”€ apps
β”‚   β”‚   β”‚   β”œβ”€β”€ namespace-01
β”‚   β”‚   β”‚   β”‚   β”œβ”€β”€ base
β”‚   β”‚   β”‚   β”‚   β”‚   β”œβ”€β”€ helm-generator.yaml
β”‚   β”‚   β”‚   β”‚   β”‚   β”œβ”€β”€ kustomization.yaml
β”‚   β”‚   β”‚   β”‚   β”‚   └── namespace.yaml
β”‚   β”‚   β”‚   β”‚   └── overlays
β”‚   β”‚   β”‚   β”‚       β”œβ”€β”€ kind-dev
β”‚   β”‚   β”‚   β”‚       β”‚   β”œβ”€β”€ kustomization.yaml
β”‚   β”‚   β”‚   |       |   β”œβ”€β”€ secrets.yaml
β”‚   β”‚   β”‚   β”‚       β”‚   └── values.yaml
β”‚   β”‚   β”‚   β”‚       └── kind-prod
β”‚   β”‚   β”‚   β”‚           β”œβ”€β”€ kustomization.yaml
β”‚   β”‚   β”‚   |           β”œβ”€β”€ secrets.yaml
β”‚   β”‚   β”‚   β”‚           └── values.yaml
β”‚   β”‚   β”‚   └── namespace-x
β”‚   β”‚   β”‚       β”œβ”€β”€ base
β”‚   β”‚   β”‚       β”‚   β”œβ”€β”€ helm-generator.yaml
β”‚   β”‚   β”‚       β”‚   β”œβ”€β”€ kustomization.yaml
β”‚   β”‚   β”‚       β”‚   └── namespace.yaml
β”‚   β”‚   β”‚       └── overlays
β”‚   β”‚   β”‚           β”œβ”€β”€ kind-dev
β”‚   β”‚   β”‚           |   β”œβ”€β”€ kustomization.yaml
β”‚   β”‚   β”‚           |   β”œβ”€β”€ secrets.yaml
β”‚   β”‚   β”‚           |   └── values.yaml
β”‚   β”‚   β”‚           └── kind-prod
β”‚   β”‚   β”‚               β”œβ”€β”€ kustomization.yaml
β”‚   β”‚   β”‚               β”œβ”€β”€ secrets.yaml
β”‚   β”‚   β”‚               └── values.yaml
β”‚   β”‚   └── crds
β”‚   β”‚       β”œβ”€β”€ crds-01
β”‚   β”‚       β”‚   β”œβ”€β”€ base
β”‚   β”‚       β”‚   β”‚   └── kustomization.yaml
β”‚   β”‚       β”‚   └── overlays
β”‚   β”‚       β”‚       β”œβ”€β”€ kind-dev
β”‚   β”‚       β”‚       β”‚   └── kustomization.yaml
β”‚   β”‚       β”‚       └── kind-prod
β”‚   β”‚       β”‚           └── kustomization.yaml
β”‚   β”‚       └── crds-x
β”‚   β”‚           β”œβ”€β”€ base
β”‚   β”‚           β”‚   └── kustomization.yaml
β”‚   β”‚           └── overlays
β”‚   β”‚               β”œβ”€β”€ kind-dev
β”‚   β”‚               β”‚   └── kustomization.yaml
β”‚   β”‚               └── kind-prod
β”‚   β”‚                   └── kustomization.yaml
β”‚   └── addons
β”‚       β”œβ”€β”€ apps
β”‚       β”‚   β”œβ”€β”€ namespace-01
β”‚       β”‚   β”‚   β”œβ”€β”€ base
β”‚       β”‚   β”‚   β”‚   β”œβ”€β”€ helm-generator.yaml
β”‚       β”‚   β”‚   β”‚   β”œβ”€β”€ kustomization.yaml
β”‚       β”‚   β”‚   β”‚   └── namespace.yaml
β”‚       β”‚   β”‚   └── overlays
β”‚       β”‚   β”‚       β”œβ”€β”€ kind-dev
β”‚       β”‚   β”‚       β”‚   β”œβ”€β”€ kustomization.yaml
β”‚       β”‚   |       |   β”œβ”€β”€ secrets.yaml
β”‚       β”‚   β”‚       β”‚   └── values.yaml
β”‚       β”‚   β”‚       └── kind-prod
β”‚       β”‚   β”‚           β”œβ”€β”€ kustomization.yaml
β”‚       β”‚   |           β”œβ”€β”€ secrets.yaml
β”‚       β”‚   β”‚           └── values.yaml
β”‚       β”‚   └── namespace-x
β”‚       β”‚       β”œβ”€β”€ base
β”‚       β”‚       β”‚   β”œβ”€β”€ helm-generator.yaml
β”‚       β”‚       β”‚   β”œβ”€β”€ kustomization.yaml
β”‚       β”‚       β”‚   └── namespace.yaml
β”‚       β”‚       └── overlays
β”‚       β”‚           β”œβ”€β”€ kind-dev
β”‚       β”‚           |   β”œβ”€β”€ kustomization.yaml
β”‚       β”‚           |   β”œβ”€β”€ secrets.yaml
β”‚       β”‚           |   └── values.yaml
β”‚       β”‚           └── kind-prod
β”‚       β”‚               β”œβ”€β”€ kustomization.yaml
β”‚       β”‚               β”œβ”€β”€ secrets.yaml
β”‚       β”‚               └── values.yaml
β”‚       └── crds
β”‚           β”œβ”€β”€ crds-addon-01
β”‚           β”‚   β”œβ”€β”€ base
β”‚           β”‚   β”‚   └── kustomization.yaml
β”‚           β”‚   └── overlays
β”‚           β”‚       β”œβ”€β”€ kind-dev
β”‚           β”‚       β”‚   └── kustomization.yaml
β”‚           β”‚       └── kind-prod
β”‚           β”‚           └── kustomization.yaml
β”‚           └── crds-addon-x
β”‚               β”œβ”€β”€ base
β”‚               β”‚   └── kustomization.yaml
β”‚               └── overlays
β”‚                   β”œβ”€β”€ kind-dev
β”‚                   β”‚   └── kustomization.yaml
β”‚                   └── kind-prod
β”‚                       └── kustomization.yaml
β”œβ”€β”€ Makefile
β”œβ”€β”€ README.md
β”œβ”€β”€ scripts
β”‚Β Β  └── tools
└── secrets
    β”œβ”€β”€ core
    β”‚   β”œβ”€β”€ loki
    β”‚   β”‚   └── overlays
    β”‚   β”‚       β”œβ”€β”€ kind-dev
    β”‚   β”‚       β”‚   └── kustomization.yaml
    β”‚   β”‚       β”œβ”€β”€ kind-prod
    β”‚   β”‚       β”‚   β”œβ”€β”€ kustomization.yaml
    β”‚   β”‚       β”‚   └── secrets
    β”‚   β”‚       β”‚       └── bucket-s3.yaml
    β”‚   β”‚       └── plato-devncoargo
    β”‚   β”‚           β”œβ”€β”€ kustomization.yaml
    β”‚   β”‚           └── secrets
    β”‚   β”‚               └── bucket-s3.yaml
    β”‚   └── vector
    β”‚       └── overlays
    β”‚           β”œβ”€β”€ kind-dev
    β”‚           β”‚   └── kustomization.yaml
    β”‚           β”œβ”€β”€ kind-prod
    β”‚           β”‚   β”œβ”€β”€ kustomization.yaml
    β”‚           β”‚   └── secrets
    β”‚           β”‚       └── bucket-s3.yaml
    β”‚           └── plato-devncoargo
    β”‚               β”œβ”€β”€ kustomization.yaml
    β”‚               └── secrets
    β”‚                   └── bucket-s3.yaml
    β”œβ”€β”€ addons
    β”‚   └── argocd
    β”‚       └── overlays
    β”‚           β”œβ”€β”€ kind-dev
    β”‚           β”‚   β”œβ”€β”€ kustomization.yaml
    β”‚           β”‚   └── secrets
    β”‚           β”‚       β”œβ”€β”€ argocd-secret.yaml
    β”‚           β”‚       └── grafana-secret.yaml
    β”‚           β”œβ”€β”€ kind-prod
    β”‚           β”‚   β”œβ”€β”€ kustomization.yaml
    β”‚           β”‚   └── secrets
    β”‚           β”‚       β”œβ”€β”€ argocd-notifications-secret.yaml
    β”‚           β”‚       β”œβ”€β”€ argocd-secret.yaml
    β”‚           β”‚       β”œβ”€β”€ grafana-secret.yaml
    β”‚           β”‚       └── keycloak-ca.yaml
    β”‚           └── plato-devncoargo
    β”‚               β”œβ”€β”€ kustomization.yaml
    β”‚               └── secrets
    β”‚                   β”œβ”€β”€ argocd-notifications-secret.yaml
    β”‚                   β”œβ”€β”€ argocd-secret.yaml
    β”‚                   β”œβ”€β”€ grafana-secret.yaml
    β”‚                   └── keycloak-ca.yaml
    β”œβ”€β”€ clusters
    β”‚Β Β  β”œβ”€β”€ kind-dev
    β”‚Β Β  |    └── sealed-secrets-private-key.yaml
    β”‚Β Β  └── kind-prod
    β”‚Β Β      └── sealed-secrets-private-key.yaml
    └── README.md

declare vars and create directory structure and files

addon=aws-efs-csi-driver
declare clusters=("plato-devncoargo")
namespace=kube-system
type=core

mkdir -p rendered-manifests/$type/namespaces/$addon/base
cat << EOF > rendered-manifests/$type/namespaces/$addon/base/helm-chart-$addon.yaml
apiVersion: builtin
kind: HelmChartInflationGenerator
metadata:
  name: helm-chart-$addon
name: &name $addon
version: ""
repo: ""
releaseName: *name
namespace: $namespace
includeCRDs: false
valuesFile: values-$addon.yaml
valuesMerge: override                 # merge, override or replace
valuesInline: {}
EOF

cat << EOF > rendered-manifests/$type/namespaces/$addon/base/namespace.yaml
apiVersion: v1
kind: Namespace
metadata:
  name: $namespace
EOF

cat << EOF > rendered-manifests/$type/namespaces/$addon/base/kustomization.yaml
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
namespace: $namespace
resources:
- namespace.yaml
EOF

for c in $clusters; do 
  mkdir -p rendered-manifests/$type/namespaces/$addon/overlays/$c
  touch rendered-manifests/$type/namespaces/$addon/overlays/$c/{kustomization.yaml,values-$addon.yaml}
  cat << EOF > rendered-manifests/$type/namespaces/$addon/overlays/$c/kustomization.yaml
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
namespace: $namespace
generators:
- ../../base/helm-chart-$addon.yaml
resources:
- ../../base
EOF
done

copy an existing environment

current_path=$PWD
cluster=plato
env=devncoargo

cd rendered-manifests/addons/namespaces
for dir in $(find $PWD -type d -regex '.*/kind-prod'); do cp -ra $dir $(dirname $dir)/$cluster-$env; done
cd ../crds
for dir in $(find $PWD -type d -regex '.*/kind-prod'); do cp -ra $dir $(dirname $dir)/$cluster-$env; done
cd ../../core/namespaces
for dir in $(find $PWD -type d -regex '.*/kind-prod'); do cp -ra $dir $(dirname $dir)/$cluster-$env; done
cd ../crds
for dir in $(find $PWD -type d -regex '.*/kind-prod'); do cp -ra $dir $(dirname $dir)/$cluster-$env; done
cd $current_path
cd secrets/addons
for dir in $(find $PWD -type d -regex '.*/kind-prod'); do cp -ra $dir $(dirname $dir)/$cluster-$env; done
cd ../core
for dir in $(find $PWD -type d -regex '.*/kind-prod'); do cp -ra $dir $(dirname $dir)/$cluster-$env; done
cd ../clusters
for dir in $(find $PWD -type d -regex '.*/kind-prod'); do cp -ra $dir $(dirname $dir)/$cluster-$env; done

create a sealed-secrets certificate

openssl req -days 3650 -x509 -nodes -newkey rsa:4096 -keyout "tls.key" -out "tls.crt" -subj "/CN=sealed-secret/O=sealed-secret"

cluster=plato
env=devncoargo
mkdir -p secrets/clusters/$cluster-$env
cat > secrets/clusters/$cluster-$env/sealed-secrets-private-key.yaml << EOF
apiVersion: v1
kind: Secret
metadata:
  generateName: sealed-secrets-key
  labels:
    sealedsecrets.bitnami.com/sealed-secrets-key: active
  name: sealed-secrets-key
  namespace: kube-system
type: kubernetes.io/tls
data:
EOF
crt=`cat tls.crt|base64 -w 0`
key=`cat tls.key|base64 -w 0`
echo "  tls.crt: $crt" >> secrets/clusters/$cluster-$env/sealed-secrets-private-key.yaml
echo "  tls.key: $key" >> secrets/clusters/$cluster-$env/sealed-secrets-private-key.yaml
rm -Rf tls.crt tls.key

sign secret with certificate

edit Makefile, in target "create-secrets:" adjust cluster & env vars and use command: make create-secrets

create keycloak cert without openssl

for ingress

edit ingress values like this

ingress:
  enabled: true
  hostname: keycloak-nco.admin.plato-devncoargo.dev-sbr.com
  tls: true
  selfSigned: true

generate template, catch secret, extract datas, store them into a file and create, ca, crt and key files.

kustomize build --enable-alpha-plugins --enable-helm --load-restrictor LoadRestrictionsNone rendered-manifests/addons/namespaces/keycloak/overlays/plato-devncoargo|yq -ojson|jq -s|jq '.[]|select(.kind == "Secret")|select(.metadata.name == "keycloak-nco.admin.plato-devncoargo.dev-sbr.com-tls")'|jq -s '.[0].data|{"ca.crt": .["ca.crt"]|@base64d,"tls.crt": .["tls.crt"]|@base64d,"tls.key": .["tls.key"]|@base64d}' > secrets/addons/keycloak/overlays/plato-devncoargo/secrets/tls.json

cd secrets/addons/keycloak/overlays/plato-devncoargo/secrets
cat tls.json| jq -r '.["ca.crt"]' > ca.crt
cat tls.json| jq -r '.["tls.crt"]' > tls.crt
cat tls.json| jq -r '.["tls.key"]' > tls.key
rm tls.json
cd $OLDPWD

for service

edit ingress values like this

tls: 
  enabled: true
  autoGenerated: true
  existingSecret: ""

generate template, catch secret, extract datas, store them into a file and create, ca, crt and key files.

kustomize build --enable-alpha-plugins --enable-helm --load-restrictor LoadRestrictionsNone rendered-manifests/addons/namespaces/keycloak/overlays/plato-devncoargo|yq -ojson|jq -s|jq '.[]|select(.kind == "Secret")|select(.metadata.name == "keycloak-crt")'|jq -s '.[0].data|{"ca.crt": .["ca.crt"]|@base64d,"tls.crt": .["tls.crt"]|@base64d,"tls.key": .["tls.key"]|@base64d}' > secrets/addons/keycloak/overlays/plato-devncoargo/secrets/tls.json

cd secrets/addons/keycloak/overlays/plato-devncoargo/secrets
cat tls.json| jq -r '.["ca.crt"]' > ca.crt
cat tls.json| jq -r '.["tls.crt"]' > tls.crt
cat tls.json| jq -r '.["tls.key"]' > tls.key
rm tls.json
cd $OLDPWD