Skip to content

Commit

Permalink
Add process to create legacy Helm chart CRDs and autogenerate role.ya…
Browse files Browse the repository at this point in the history
…ml, fixes k8ssandra#680
  • Loading branch information
burmanm committed Jul 19, 2024
1 parent 72af057 commit 6880ef0
Showing 1 changed file with 58 additions and 20 deletions.
78 changes: 58 additions & 20 deletions scripts/release-helm-chart.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,45 +16,83 @@ fi
# Includes here to get all the updates even if we swap to an older branch
. scripts/lib.sh

LEGACY=false
if [[ $2 == "legacy" ]]; then
LEGACY=true
fi

# This should work with BSD/MacOS mktemp and GNU one
CRD_TMP_DIR=$(mktemp -d 2>/dev/null || mktemp -d -t 'crd')

VERSION=$1
CHART_HOME=../k8ssandra/charts/cass-operator
CRD_TARGET_PATH=$CRD_TMP_DIR
TEMPLATE_HOME=$CHART_HOME/templates
CRD_TARGET_PATH=$TEMPLATE_HOME

# Checkout tag
git checkout v$VERSION

# Create CRDs
kustomize build config/crd --output $CRD_TARGET_PATH
kustomize build config/crd --output $CRD_TMP_DIR

# Rename generated CRDs to shorter format
for f in $CRD_TARGET_PATH/*; do
for f in $CRD_TMP_DIR/*; do
TARGET_FILENAME=$(yq '.spec.names.plural' $f).yaml
mv $f $CRD_TARGET_PATH/$TARGET_FILENAME
mv $f $CRD_TMP_DIR/$TARGET_FILENAME
done

# Add Helm conditionals to the end and beginning of CRDs before applying them to the templates path
echo "Updating CRDs in" $TEMPLATE_HOME
CRD_FILE_NAME=$TEMPLATE_HOME/crds.yaml
echo '{{- if .Values.manageCrds }}' > $CRD_FILE_NAME

declare -a files
files=($CRD_TARGET_PATH/*)
for i in ${!files[@]}; do
echo "Processing " ${files[$i]}
yq -i '.metadata.annotations."helm.sh/resource-policy" = "keep"' ${files[$i]}
cat ${files[$i]} >> $CRD_FILE_NAME
if [[ $i -lt ${#files[@]}-1 ]]; then
echo "---" >> $CRD_FILE_NAME
fi
done
echo '{{- end }}' >> $CRD_FILE_NAME
if [ "$LEGACY" == true ]; then
echo "Updating CRDs for legacy CRD handling in Helm chart"

# Update CRDs for legacy Helm chart
CRD_TARGET_PATH=$CHART_HOME/crds
cp -r $CRD_TMP_DIR/* $CRD_TARGET_PATH
else
# Add Helm conditionals to the end and beginning of CRDs before applying them to the templates path
echo "Updating CRDs in" $TEMPLATE_HOME
CRD_FILE_NAME=$CRD_TARGET_PATH/crds.yaml
echo '{{- if .Values.manageCrds }}' > $CRD_FILE_NAME

declare -a files
files=($CRD_TMP_DIR/*)
for i in ${!files[@]}; do
echo "Processing " ${files[$i]}
yq -i '.metadata.annotations."helm.sh/resource-policy" = "keep"' ${files[$i]}
cat ${files[$i]} >> $CRD_FILE_NAME
if [[ $i -lt ${#files[@]}-1 ]]; then
echo "---" >> $CRD_FILE_NAME
fi
done
echo '{{- end }}' >> $CRD_FILE_NAME
fi

rm -fr $CRD_TMP_DIR

# Update role.yaml
echo "Updating role.yaml"
ROLE_FILE_NAME=$TEMPLATE_HOME/role.yaml
cat <<'EOF' >> $ROLE_FILE_NAME
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
name: {{ include "k8ssandra-common.fullname" . }}
labels: {{ include "k8ssandra-common.labels" . | indent 4 }}
{{- if .Values.global.clusterScoped }}
EOF
yq -N eval-all '.rules = (.rules as $item ireduce ([]; . *+ $item)) | select(di == 0) | with_entries(select(.key | test("rules")))' config/rbac/role.yaml >> $ROLE_FILE_NAME
echo '{{- else }}' >> $ROLE_FILE_NAME
yq -N 'select(di == 0) | with_entries(select(.key | test("rules")))' config/rbac/role.yaml >> $ROLE_FILE_NAME
cat <<'EOF' >> $ROLE_FILE_NAME
---
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
name: {{ template "k8ssandra-common.fullname" . }}
labels: {{ include "k8ssandra-common.labels" . | indent 4 }}
EOF
yq -N 'select(di == 1) | with_entries(select(.key | test("rules")))' config/rbac/role.yaml >> $ROLE_FILE_NAME
echo '{{- end }}' >> $ROLE_FILE_NAME

# Update version of the Chart.yaml automatically (to next minor one)
CURRENT_VERSION=$(yq '.version' $CHART_HOME/Chart.yaml)
next_minor_version
Expand Down

0 comments on commit 6880ef0

Please sign in to comment.