From 4d60778c8de08268c1039404bf4e9be2b486ec84 Mon Sep 17 00:00:00 2001 From: Alper Rifat Ulucinar Date: Fri, 12 Jan 2024 04:43:29 +0300 Subject: [PATCH] Split kustomize'd CRD files for building with "up xpkg batch" Signed-off-by: Alper Rifat Ulucinar --- Makefile | 12 +++--- cluster/images/provider-aws/Makefile | 7 ++-- package/kustomize/kustomization.yaml | 3 ++ .../splittransformer/SplitTransformer | 42 +++++++++++++++++++ package/kustomize/transformer.yaml | 5 +++ 5 files changed, 61 insertions(+), 8 deletions(-) create mode 100755 package/kustomize/plugin/providers.upbound.io/splittransformer/SplitTransformer create mode 100644 package/kustomize/transformer.yaml diff --git a/Makefile b/Makefile index 74016d0317..1b2279de89 100644 --- a/Makefile +++ b/Makefile @@ -92,7 +92,7 @@ XPKG_REG_ORGS ?= xpkg.upbound.io/upbound # inferred. XPKG_REG_ORGS_NO_PROMOTE ?= xpkg.upbound.io/upbound XPKG_DIR = $(OUTPUT_DIR)/package -XPKG_IGNORE = kustomize/*,crds/* +XPKG_IGNORE = kustomize/*,crds/kustomization.yaml,crds.yaml export XPKG_REG_ORGS := $(XPKG_REG_ORGS) export XPKG_REG_ORGS_NO_PROMOTE := $(XPKG_REG_ORGS_NO_PROMOTE) @@ -315,10 +315,12 @@ go.mod.cachedir: build.init: kustomize-crds kustomize-crds: output.init - rm -fr $(OUTPUT_DIR)/package - cp -R package $(OUTPUT_DIR) && \ + @$(INFO) Kustomizing CRDs... + @rm -fr $(OUTPUT_DIR)/package || $(FAIL) + @cp -R package $(OUTPUT_DIR) && \ cd $(OUTPUT_DIR)/package/crds && \ - kustomize create --autodetect - kustomize build $(OUTPUT_DIR)/package/kustomize -o $(OUTPUT_DIR)/package/crds.yaml + 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) + @$(OK) Kustomizing CRDs. .PHONY: kustomize-crds diff --git a/cluster/images/provider-aws/Makefile b/cluster/images/provider-aws/Makefile index 843b969c8f..3da34af2db 100755 --- a/cluster/images/provider-aws/Makefile +++ b/cluster/images/provider-aws/Makefile @@ -77,10 +77,11 @@ batch-process: $(UP) --build-only=$(BUILD_ONLY) \ --examples-root $(ROOT_DIR)/examples \ --examples-group-override monolith=* --examples-group-override config=providerconfig \ - --auth-ext $(ROOT_DIR)/package/auth.yaml \ - --crd-root $(ROOT_DIR)/package/crds \ + --auth-ext $(XPKG_DIR)/auth.yaml \ + --crd-root $(XPKG_DIR)/crds \ + --ignore $(XPKG_IGNORE) \ --crd-group-override monolith=* --crd-group-override config=$(PROVIDER_NAME) \ - --package-metadata-template $(ROOT_DIR)/package/crossplane.yaml.tmpl \ + --package-metadata-template $(XPKG_DIR)/crossplane.yaml.tmpl \ --template-var XpkgRegOrg=$(XPKG_REG_ORGS) --template-var DepConstraint="$(DEP_CONSTRAINT)" --template-var ProviderName=$(PROVIDER_NAME) \ --concurrency $(CONCURRENCY) \ --push-retry 10 || $(FAIL) diff --git a/package/kustomize/kustomization.yaml b/package/kustomize/kustomization.yaml index 2c84e5ea6b..5344428145 100644 --- a/package/kustomize/kustomization.yaml +++ b/package/kustomize/kustomization.yaml @@ -9,3 +9,6 @@ patches: target: group: apiextensions.k8s.io kind: CustomResourceDefinition + +transformers: + - transformer.yaml diff --git a/package/kustomize/plugin/providers.upbound.io/splittransformer/SplitTransformer b/package/kustomize/plugin/providers.upbound.io/splittransformer/SplitTransformer new file mode 100755 index 0000000000..12795e5777 --- /dev/null +++ b/package/kustomize/plugin/providers.upbound.io/splittransformer/SplitTransformer @@ -0,0 +1,42 @@ +#!/usr/bin/env bash + +set -aeuo pipefail + +# skip the config directory argument +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 diff --git a/package/kustomize/transformer.yaml b/package/kustomize/transformer.yaml new file mode 100644 index 0000000000..92cb1330ca --- /dev/null +++ b/package/kustomize/transformer.yaml @@ -0,0 +1,5 @@ +apiVersion: providers.upbound.io +kind: SplitTransformer +metadata: + name: splitter +argsOneLiner: ../crds