Skip to content

Commit efcdced

Browse files
committed
Refactor CRD synchronization in Taskfile.yml by replacing inline commands with a dedicated bash script for improved maintainability and clarity.
1 parent b06133c commit efcdced

2 files changed

Lines changed: 51 additions & 73 deletions

File tree

Taskfile.yml

Lines changed: 1 addition & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -103,80 +103,8 @@ tasks:
103103
sync-crd-to-helm:
104104
desc: Sync generated CRD into Helm chart template (wraps CRD with Helm conditionals)
105105
deps: [manifests]
106-
vars:
107-
CRD_SOURCE: config/crd/bases/dataflow.dataflow.io_dataflows.yaml
108-
HELM_TARGET: ../helm-charts/charts/dataflow-operator/templates/crd.yaml
109-
CRON_CRD_SOURCE: config/crd/bases/dataflow.dataflow.io_dataflowcrons.yaml
110-
CRON_HELM_TARGET: ../helm-charts/charts/dataflow-operator/templates/crd-dataflowcrons.yaml
111106
cmds:
112-
- |
113-
HEADER='{{- if .Values.crds.install }}'
114-
FOOTER='{{- end }}'
115-
LABELS_BLOCK=' labels:
116-
{{- include "dataflow-operator.labels" . | nindent 4 }}'
117-
KEEP_BLOCK=' {{- if .Values.crds.keep }}
118-
"helm.sh/resource-policy": keep
119-
{{- end }}'
120-
121-
{
122-
echo "$HEADER"
123-
awk '
124-
/^---$/ { next }
125-
/^metadata:$/ {
126-
print
127-
getline
128-
# annotations block
129-
if ($0 ~ /^ annotations:/) {
130-
print
131-
# print existing annotations
132-
while (getline > 0 && $0 ~ /^ [^ ]/) {
133-
print
134-
}
135-
# insert keep annotation block
136-
printf "%s\n", "'"$KEEP_BLOCK"'"
137-
# insert labels block
138-
printf "%s\n", "'"$LABELS_BLOCK"'"
139-
print
140-
}
141-
next
142-
}
143-
{ print }
144-
' "{{.CRD_SOURCE}}"
145-
echo "$FOOTER"
146-
} > "{{.HELM_TARGET}}"
147-
echo "Synced CRD to {{.HELM_TARGET}}"
148-
- |
149-
HEADER='{{- if .Values.crds.install }}'
150-
FOOTER='{{- end }}'
151-
LABELS_BLOCK=' labels:
152-
{{- include "dataflow-operator.labels" . | nindent 4 }}'
153-
KEEP_BLOCK=' {{- if .Values.crds.keep }}
154-
"helm.sh/resource-policy": keep
155-
{{- end }}'
156-
157-
{
158-
echo "$HEADER"
159-
awk '
160-
/^---$/ { next }
161-
/^metadata:$/ {
162-
print
163-
getline
164-
if ($0 ~ /^ annotations:/) {
165-
print
166-
while (getline > 0 && $0 ~ /^ [^ ]/) {
167-
print
168-
}
169-
printf "%s\n", "'"$KEEP_BLOCK"'"
170-
printf "%s\n", "'"$LABELS_BLOCK"'"
171-
print
172-
}
173-
next
174-
}
175-
{ print }
176-
' "{{.CRON_CRD_SOURCE}}"
177-
echo "$FOOTER"
178-
} > "{{.CRON_HELM_TARGET}}"
179-
echo "Synced CRD to {{.CRON_HELM_TARGET}}"
107+
- bash scripts/sync-crd-to-helm.sh
180108

181109
# Deployment tasks
182110
install:

scripts/sync-crd-to-helm.sh

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#!/usr/bin/env bash
2+
# Sync controller-gen CRD manifests into Helm chart templates with install/keep conditionals.
3+
set -euo pipefail
4+
5+
ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
6+
cd "$ROOT"
7+
8+
CRD_SOURCE="config/crd/bases/dataflow.dataflow.io_dataflows.yaml"
9+
HELM_TARGET="../helm-charts/charts/dataflow-operator/templates/crd.yaml"
10+
CRON_CRD_SOURCE="config/crd/bases/dataflow.dataflow.io_dataflowcrons.yaml"
11+
CRON_HELM_TARGET="../helm-charts/charts/dataflow-operator/templates/crd-dataflowcrons.yaml"
12+
13+
HEADER='{{- if .Values.crds.install }}'
14+
FOOTER='{{- end }}'
15+
16+
sync_crd() {
17+
local source="$1"
18+
local target="$2"
19+
20+
{
21+
echo "$HEADER"
22+
awk '
23+
/^---$/ { next }
24+
/^metadata:$/ {
25+
print
26+
getline
27+
if ($0 ~ /^ annotations:/) {
28+
print
29+
while (getline > 0 && $0 ~ /^ [^ ]/) {
30+
print
31+
}
32+
print " {{- if .Values.crds.keep }}"
33+
print " \"helm.sh/resource-policy\": keep"
34+
print " {{- end }}"
35+
print " labels:"
36+
print " {{- include \"dataflow-operator.labels\" . | nindent 4 }}"
37+
print
38+
}
39+
next
40+
}
41+
{ print }
42+
' "$source"
43+
echo "$FOOTER"
44+
} > "$target"
45+
46+
echo "Synced CRD to $target"
47+
}
48+
49+
sync_crd "$CRD_SOURCE" "$HELM_TARGET"
50+
sync_crd "$CRON_CRD_SOURCE" "$CRON_HELM_TARGET"

0 commit comments

Comments
 (0)