Skip to content

Commit

Permalink
Use yq in the kustomize transformer for splitting the CRD documents
Browse files Browse the repository at this point in the history
Signed-off-by: Alper Rifat Ulucinar <[email protected]>
  • Loading branch information
ulucinar committed Jan 15, 2024
1 parent dcd72c8 commit 9a684ab
Show file tree
Hide file tree
Showing 5 changed files with 8 additions and 38 deletions.
6 changes: 4 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ UP_VERSION = v0.20.0
UP_CHANNEL = stable
UPTEST_VERSION = v0.8.0
KUSTOMIZE_VERSION = v5.3.0
YQ_VERSION = v4.40.5

export UP_VERSION := $(UP_VERSION)
export UP_CHANNEL := $(UP_CHANNEL)
Expand Down Expand Up @@ -315,13 +316,14 @@ go.mod.cachedir:

build.init: kustomize-crds

kustomize-crds: output.init $(KUSTOMIZE)
kustomize-crds: output.init $(KUSTOMIZE) $(YQ)
@$(INFO) Kustomizing CRDs...
@rm -fr $(OUTPUT_DIR)/package || $(FAIL)
@cp -R package $(OUTPUT_DIR) && \
cd $(OUTPUT_DIR)/package/crds && \
kustomize create --autodetect || $(FAIL)
@XDG_CONFIG_HOME=$(PWD)/package $(KUSTOMIZE) build --enable-alpha-plugins $(OUTPUT_DIR)/package/kustomize -o $(OUTPUT_DIR)/package/crds.yaml || $(FAIL)
@export YQ=$(YQ) && \
XDG_CONFIG_HOME=$(PWD)/package $(KUSTOMIZE) build --enable-alpha-plugins $(OUTPUT_DIR)/package/kustomize -o $(OUTPUT_DIR)/package/crds.yaml || $(FAIL)
@$(OK) Kustomizing CRDs.

.PHONY: kustomize-crds
2 changes: 1 addition & 1 deletion build
2 changes: 1 addition & 1 deletion package/kustomize/kustomization.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ patches:
kind: CustomResourceDefinition

transformers:
- transformer.yaml
- splitter.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,37 +6,5 @@ set -aeuo pipefail
shift
output_dir="$1"

temp_file="$(mktemp)"

function save_crd {
group="$(sed -nr 's/^[[:space:]]*group:[[:space:]]*(.+)/\1/p' "${temp_file}")"
plural="$(sed -nr 's/^[[:space:]]*plural:[[:space:]]*(.+)/\1/p' "${temp_file}")"
tee < "${temp_file}" "${output_dir}/${group//apiextensions.k8s.io?/}_${plural}.yaml"

# Reset the temporary file
: > "${temp_file}"
}

while IFS= read -r line || [[ -n "${line}" ]]; do
if [[ "$line" == "---" && -s "${temp_file}" ]]; then
save_crd
else
if [[ ! -s "${temp_file}" ]]; then
echo "---" >> "${temp_file}"
fi

echo "${line}" >> "${temp_file}"
fi
done

# Save the last document if there is one
if [[ -s "${temp_file}" ]]; then
save_crd
fi

function cleanup {
# Clean up the temporary file
rm "${temp_file}"
}

trap cleanup EXIT
cd "${output_dir}"
${YQ} --split-exp '.spec.group + "_" + .spec.names.plural + ".yaml"' -
File renamed without changes.

0 comments on commit 9a684ab

Please sign in to comment.