Skip to content

Commit

Permalink
add fluxcd labels post processor (#180)
Browse files Browse the repository at this point in the history
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:
- fluxcd/helm-controller#283
- fluxcd/flux2#4368

Signed-off-by: Andrei Kvapil <[email protected]>
  • Loading branch information
kvaps authored Jun 25, 2024
1 parent 91a1f49 commit cf1d9fa
Show file tree
Hide file tree
Showing 29 changed files with 94 additions and 57 deletions.
4 changes: 2 additions & 2 deletions packages/system/capi-operator/Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
NAME=capi-operator
NAMESPACE=cozy-cluster-api
export NAME=capi-operator
export NAMESPACE=cozy-cluster-api

include ../../../scripts/package-system.mk

Expand Down
4 changes: 2 additions & 2 deletions packages/system/capi-providers/Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
NAME=capi-providers
NAMESPACE=cozy-cluster-api
export NAME=capi-providers
export NAMESPACE=cozy-cluster-api

include ../../../scripts/package-system.mk
4 changes: 2 additions & 2 deletions packages/system/cert-manager-issuers/Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
NAME=cert-manager-issuers
NAMESPACE=cozy-cert-manager
export NAME=cert-manager-issuers
export NAMESPACE=cozy-cert-manager

include ../../../scripts/package-system.mk
4 changes: 2 additions & 2 deletions packages/system/cert-manager/Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
NAME=cert-manager
NAMESPACE=cozy-$(NAME)
export NAME=cert-manager
export NAMESPACE=cozy-$(NAME)

include ../../../scripts/package-system.mk

Expand Down
4 changes: 2 additions & 2 deletions packages/system/cilium/Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
CILIUM_TAG=$(shell awk '$$1 == "version:" {print $$2}' charts/cilium/Chart.yaml)

NAME=cilium
NAMESPACE=cozy-$(NAME)
export NAME=cilium
export NAMESPACE=cozy-$(NAME)

include ../../../scripts/common-envs.mk
include ../../../scripts/package-system.mk
Expand Down
4 changes: 2 additions & 2 deletions packages/system/clickhouse-operator/Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
NAME=clickhouse-operator
NAMESPACE=cozy-clickhouse-operator
export NAME=clickhouse-operator
export NAMESPACE=cozy-clickhouse-operator

include ../../../scripts/package-system.mk

Expand Down
4 changes: 2 additions & 2 deletions packages/system/dashboard/Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
NAME=dashboard
NAMESPACE=cozy-$(NAME)
export NAME=dashboard
export NAMESPACE=cozy-$(NAME)

include ../../../scripts/common-envs.mk
include ../../../scripts/package-system.mk
Expand Down
4 changes: 2 additions & 2 deletions packages/system/etcd-operator/Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
NAME=etcd-operator
NAMESPACE=cozy-${NAME}
export NAME=etcd-operator
export NAMESPACE=cozy-${NAME}

include ../../../scripts/package-system.mk

Expand Down
4 changes: 2 additions & 2 deletions packages/system/grafana-operator/Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
NAME=grafana-operator
NAMESPACE=cozy-grafana-operator
export NAME=grafana-operator
export NAMESPACE=cozy-grafana-operator

include ../../../scripts/package-system.mk

Expand Down
4 changes: 2 additions & 2 deletions packages/system/ingress-nginx/Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
NAME=ingress-nginx
NAMESPACE=cozy-$(NAME)
export NAME=ingress-nginx
export NAMESPACE=cozy-$(NAME)

include ../../../scripts/package-system.mk

Expand Down
4 changes: 2 additions & 2 deletions packages/system/kafka-operator/Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
NAME=kafka-operator
NAMESPACE=cozy-$(NAME)
export NAME=kafka-operator
export NAMESPACE=cozy-$(NAME)

include ../../../scripts/package-system.mk

Expand Down
4 changes: 2 additions & 2 deletions packages/system/kamaji/Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
NAME=kamaji
NAMESPACE=cozy-$(NAME)
export NAME=kamaji
export NAMESPACE=cozy-$(NAME)

include ../../../scripts/package-system.mk

Expand Down
4 changes: 2 additions & 2 deletions packages/system/kubeovn/Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
KUBEOVN_TAG = v1.13.0

NAME=kubeovn
NAMESPACE=cozy-$(NAME)
export NAME=kubeovn
export NAMESPACE=cozy-$(NAME)

include ../../../scripts/common-envs.mk
include ../../../scripts/package-system.mk
Expand Down
4 changes: 2 additions & 2 deletions packages/system/kubevirt-cdi-operator/Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
NAME=kubevirt-cdi-operator
NAMESPACE=cozy-kubevirt-cdi
export NAME=kubevirt-cdi-operator
export NAMESPACE=cozy-kubevirt-cdi

include ../../../scripts/package-system.mk

Expand Down
4 changes: 2 additions & 2 deletions packages/system/kubevirt-cdi/Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
NAME=kubevirt-cdi
NAMESPACE=cozy-$(NAME)
export NAME=kubevirt-cdi
export NAMESPACE=cozy-$(NAME)

include ../../../scripts/package-system.mk

Expand Down
4 changes: 2 additions & 2 deletions packages/system/kubevirt-operator/Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
NAME=kubevirt-operator
NAMESPACE=cozy-kubevirt
export NAME=kubevirt-operator
export NAMESPACE=cozy-kubevirt

include ../../../scripts/package-system.mk

Expand Down
4 changes: 2 additions & 2 deletions packages/system/kubevirt/Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
NAME=kubevirt
NAMESPACE=cozy-$(NAME)
export NAME=kubevirt
export NAMESPACE=cozy-$(NAME)

include ../../../scripts/package-system.mk

Expand Down
4 changes: 2 additions & 2 deletions packages/system/linstor/Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
NAME=linstor
NAMESPACE=cozy-$(NAME)
export NAME=linstor
export NAMESPACE=cozy-$(NAME)

include ../../../scripts/package-system.mk
4 changes: 2 additions & 2 deletions packages/system/mariadb-operator/Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
NAME=mariadb-operator
NAMESPACE=cozy-$(NAME)
export NAME=mariadb-operator
export NAMESPACE=cozy-$(NAME)

include ../../../scripts/package-system.mk

Expand Down
4 changes: 2 additions & 2 deletions packages/system/metallb/Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
NAME=metallb
NAMESPACE=cozy-$(NAME)
export NAME=metallb
export NAMESPACE=cozy-$(NAME)

include ../../../scripts/package-system.mk

Expand Down
4 changes: 2 additions & 2 deletions packages/system/monitoring/Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
NAME=monitoring
NAMESPACE=cozy-$(NAME)
export NAME=monitoring
export NAMESPACE=cozy-$(NAME)

include ../../../scripts/package-system.mk

Expand Down
4 changes: 2 additions & 2 deletions packages/system/piraeus-operator/Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
NAME=piraeus-operator
NAMESPACE=cozy-linstor
export NAME=piraeus-operator
export NAMESPACE=cozy-linstor

include ../../../scripts/package-system.mk

Expand Down
4 changes: 2 additions & 2 deletions packages/system/postgres-operator/Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
NAME=postgres-operator
NAMESPACE=cozy-$(NAME)
export NAME=postgres-operator
export NAMESPACE=cozy-$(NAME)

include ../../../scripts/package-system.mk

Expand Down
4 changes: 2 additions & 2 deletions packages/system/rabbitmq-operator/Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
NAME=rabbitmq-operator
NAMESPACE=cozy-$(NAME)
export NAME=rabbitmq-operator
export NAMESPACE=cozy-$(NAME)

include ../../../scripts/package-system.mk

Expand Down
4 changes: 2 additions & 2 deletions packages/system/redis-operator/Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
NAME=redis-operator
NAMESPACE=cozy-$(NAME)
export NAME=redis-operator
export NAMESPACE=cozy-$(NAME)

include ../../../scripts/package-system.mk

Expand Down
4 changes: 2 additions & 2 deletions packages/system/telepresence/Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
NAME=traffic-manager
NAMESPACE=cozy-telepresence
export NAME=traffic-manager
export NAMESPACE=cozy-telepresence

include ../../../scripts/package-system.mk

Expand Down
4 changes: 2 additions & 2 deletions packages/system/victoria-metrics-operator/Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
NAME=victoria-metrics-operator
NAMESPACE=cozy-$(NAME)
export NAME=victoria-metrics-operator
export NAMESPACE=cozy-$(NAME)

include ../../../scripts/package-system.mk

Expand Down
37 changes: 37 additions & 0 deletions scripts/fluxcd-kustomize.sh
Original file line number Diff line number Diff line change
@@ -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}"
6 changes: 3 additions & 3 deletions scripts/package-system.mk
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit cf1d9fa

Please sign in to comment.