rancher-partner-charts/scripts/prepare-crds

111 lines
3.8 KiB
Bash
Executable File

#!/usr/bin/env bash
set -e
# Split the provided package into a charts and charts-crd package
# This script is split up into the following sections
# - Initial checks to make sure that the current state of the directory is suitable for running this script
# - Collecting information from the upstream chart that will be used to fill out a set of templates
# - Copy templates stored in ./scripts/chart-templates into ${f}/charts-crd based on flags
# - Apply templates based on information collected from the upstream chart
# - Move anything from ${f}/charts-crd/overlay-upstream to the upstream chart
# - Move any CRDs from the upstream chart into the appropriate directory for CRDs based on flags
# - Apply any additional necessary annotation logic on the Chart.yaml of both the upstream and CRD charts
if [[ -z $1 ]]; then
echo "No directory provided to initialize charts-crd within"
exit 1
fi
f=$1
if ! [[ -f ${f}/package.yaml ]]; then
echo "Could not find ${f}/package.yaml"
exit 1
fi
if [[ -d ${f}/charts-crd ]]; then
rm -rf ${f}/charts-crd
fi
if ! [[ -d ${f}/charts ]]; then
echo "Could not find ${f}/charts"
exit 1
fi
if ! [[ -f ${f}/charts/Chart.yaml ]]; then
echo "Could not find ${f}/charts/Chart.yaml"
exit 1
fi
if ! [[ -d ${f}/charts/crds ]] || [[ $(ls ${f}/charts/crds | wc -l) -eq 0 ]]; then
echo "Chart does not have any crds within a crd/ directory"
exit 1
fi
# Collect information on chart
name=$(cat ${f}/charts/Chart.yaml | yq r - 'name')
api_version=$(cat ${f}/charts/Chart.yaml | yq r - 'apiVersion')
chart_version=$(cat ${f}/charts/Chart.yaml | yq r - 'version')
# Collect information on CRDs
crd_apis=()
for crd_yaml in ${f}/charts/crds/*; do
crd_group=$(yq r ${crd_yaml} 'spec.group')
crd_kind=$(yq r ${crd_yaml} 'spec.names.kind')
crd_version=$(yq r ${crd_yaml} 'spec.version')
if [[ -z "$crd_version" ]]; then
crd_version=$(yq r ${crd_yaml} 'spec.versions[0].name')
fi
crd_apis+=("${crd_group}/${crd_version}/${crd_kind}")
done
# Set environment variable to be used in validate-install-crd.yaml template
set_found_crd=$(
for crd in ${crd_apis[@]}; do
echo "# {{- set \$found \"${crd}\" false -}}"
done
)
# Copy base template and apply variables to the template
mkdir -p ${f}/charts-crd
cp -R ./scripts/chart-templates/crd-base/* ${f}/charts-crd
assume_ownership=$(cat ${f}/package.yaml | yq r - generateCRDChart.assumeOwnershipOfCRDs)
if [[ ${assume_ownership} == "true" ]]; then
cp -fR ./scripts/chart-templates/crd-assume-ownership/* ${f}/charts-crd
fi
for template_file in $(find ${f}/charts-crd -type f); do
# Applies any environment variables currently set onto your template file
eval "echo \"$(sed 's/"/\\"/g' ${template_file})\"" > ${template_file}
done
# Move anything from ${f}/charts-crd/overlay-upstream to the main chart
cp -R ${f}/charts-crd/overlay-upstream/* ${f}/charts
rm -rf ${f}/charts-crd/overlay-upstream
# Move CRDs from main chart to CRD chart
if [[ ${assume_ownership} == "true" ]]; then
mkdir -p ${f}/charts-crd/crd-manifest
mv ${f}/charts/crds/* ${f}/charts-crd/crd-manifest
else
mkdir -p ${f}/charts-crd/templates
mv ${f}/charts/crds/* ${f}/charts-crd/templates
fi
rm -rf ${f}/charts/crds
# Make the primary chart auto-install the CRD chart
if [[ -z "$(yq r ${f}/charts/Chart.yaml 'annotations[catalog.cattle.io/auto-install]')" ]]; then
yq w -i ${f}/charts/Chart.yaml "annotations[catalog.cattle.io/auto-install]" "${name}-crd=match"
fi
# Add annotations to ${f}/charts-crd/Chart.yaml
copyAnnotations=(catalog.cattle.io/release-name catalog.cattle.io/certified catalog.cattle.io/experimental catalog.cattle.io/namespace)
for a in ${copyAnnotations[@]}; do
v=$(yq r ${f}/charts/Chart.yaml "annotations[${a}]")
if ! [[ -z ${v} ]]; then
if [[ ${a} == "catalog.cattle.io/release-name" ]]; then
v="${v}-crd"
fi
yq w -i ${f}/charts-crd/Chart.yaml "annotations[${a}]" "${v}"
fi
done