add fluxcd labels post processor (#180)

This PR introduces a new fluxcd-kustomize.sh script that can be used as
post-processor for helm for adding a common fluxcd labels.

This is very useful for `make diff`, so it will not include diff between
these labels anymore

Also for debugging specific kustomize cases, eg:
- https://github.com/fluxcd/helm-controller/issues/283
- https://github.com/fluxcd/flux2/issues/4368

Signed-off-by: Andrei Kvapil <kvapss@gmail.com>
This commit is contained in:
Andrei Kvapil
2024-06-25 19:23:20 +02:00
committed by GitHub
parent 91a1f4917c
commit cf1d9fabf4
29 changed files with 94 additions and 57 deletions

37
scripts/fluxcd-kustomize.sh Executable file
View File

@@ -0,0 +1,37 @@
#!/bin/sh
# This scripts adds common fluxcd labels to all objects
if [ -z "$NAME" ]; then
echo 'Variable $NAME is not set!' >&2
exit 1
fi
if [ -z "$NAMESPACE" ]; then
echo 'Variable $NAMESPACE is not set!' >&2
exit 1
fi
TMP_DIR=$(mktemp -d)
cat - > "${TMP_DIR}/helm-generated-output.yaml"
cat > "${TMP_DIR}/global-labels.yaml" <<EOT
apiVersion: builtin
kind: LabelTransformer
metadata:
name: global-labels
labels:
helm.toolkit.fluxcd.io/name: ${NAME}
helm.toolkit.fluxcd.io/namespace: ${NAMESPACE:-$HELM_NAMESPACE}
fieldSpecs:
- path: metadata/labels
create: true
EOT
cat > "${TMP_DIR}/kustomization.yaml" <<EOT
resources:
- helm-generated-output.yaml
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
transformers:
- global-labels.yaml
EOT
kubectl kustomize "${TMP_DIR}"
rm -rf "${TMP_DIR}"

View File

@@ -5,13 +5,13 @@ help: ## Show this help.
@awk 'BEGIN {FS = ":.*?## "} /^[a-zA-Z_-]+:.*?## / {sub("\\\\n",sprintf("\n%22c"," "), $$2);printf "\033[36m%-20s\033[0m %s\n", $$1, $$2}' $(MAKEFILE_LIST)
show: ## Show output of rendered templates
kubectl get hr -n $(NAMESPACE) $(NAME) -o jsonpath='{.spec.values}' | helm template --dry-run=server -n $(NAMESPACE) $(NAME) . -f -
kubectl get hr -n $(NAMESPACE) $(NAME) -o jsonpath='{.spec.values}' | helm template --dry-run=server --post-renderer ../../../scripts/fluxcd-kustomize.sh -n $(NAMESPACE) $(NAME) . -f -
apply: suspend ## Apply Helm release to a Kubernetes cluster
kubectl get hr -n $(NAMESPACE) $(NAME) -o jsonpath='{.spec.values}' | helm upgrade -i -n $(NAMESPACE) $(NAME) . -f -
kubectl get hr -n $(NAMESPACE) $(NAME) -o jsonpath='{.spec.values}' | helm upgrade -i --post-renderer ../../../scripts/fluxcd-kustomize.sh -n $(NAMESPACE) $(NAME) . -f -
diff: ## Diff Helm release against objects in a Kubernetes cluster
kubectl get hr -n $(NAMESPACE) $(NAME) -o jsonpath='{.spec.values}' | helm diff upgrade --allow-unreleased --normalize-manifests -n $(NAMESPACE) $(NAME) . -f -
kubectl get hr -n $(NAMESPACE) $(NAME) -o jsonpath='{.spec.values}' | helm diff upgrade --allow-unreleased --post-renderer ../../../scripts/fluxcd-kustomize.sh -n $(NAMESPACE) $(NAME) . -f -
suspend: ## Suspend reconciliation for an existing Helm release
kubectl patch hr -n $(NAMESPACE) $(NAME) -p '{"spec": {"suspend": true}}' --type=merge --field-manager=flux-client-side-apply