From f9283801241445c29d324f04f97aaae4565d05d2 Mon Sep 17 00:00:00 2001 From: Lari Hotari Date: Wed, 19 Feb 2025 00:46:24 -0800 Subject: [PATCH] Fix pulsar-cluster-initialize / pulsar-init rendering with kustomize (#572) * Fix pulsar-cluster-initialize / pulsar-init rendering with kustomize - reapply #166 changes that were reverted by #544 changes * Add validation for kustomize output in CI --- .ci/helm.sh | 72 +++++++++++++++++++ .ci/kustomization.yaml | 32 +++++++++ .github/workflows/pulsar-helm-chart-ci.yaml | 6 ++ .../templates/pulsar-cluster-initialize.yaml | 4 +- 4 files changed, 112 insertions(+), 2 deletions(-) mode change 100644 => 100755 .ci/helm.sh create mode 100644 .ci/kustomization.yaml diff --git a/.ci/helm.sh b/.ci/helm.sh old mode 100644 new mode 100755 index 516e82c..f9455a3 --- a/.ci/helm.sh +++ b/.ci/helm.sh @@ -423,3 +423,75 @@ function ci::test_pulsar_manager() { exit 1 fi } + +function ci::validate_kustomize_yaml() { + # if kustomize is not installed, install kustomize to a temp directory + if ! command -v kustomize &> /dev/null; then + KUSTOMIZE_VERSION=5.6.0 + KUSTOMIZE_DIR=$(mktemp -d) + echo "Installing kustomize ${KUSTOMIZE_VERSION} to ${KUSTOMIZE_DIR}" + curl -s "https://raw.githubusercontent.com/kubernetes-sigs/kustomize/master/hack/install_kustomize.sh" | bash -s ${KUSTOMIZE_VERSION} ${KUSTOMIZE_DIR} + export PATH=${KUSTOMIZE_DIR}:$PATH + fi + # prevent regression of https://github.com/apache/pulsar-helm-chart/issues/569 + local kustomize_yaml_dir=$(mktemp -d) + cp ${PULSAR_HOME}/.ci/kustomization.yaml ${kustomize_yaml_dir} + PULSAR_HOME=${PULSAR_HOME} yq -i '.helmGlobals.chartHome = env(PULSAR_HOME) + "/charts"' ${kustomize_yaml_dir}/kustomization.yaml + failures=0 + # validate zookeeper init + echo "Validating kustomize yaml output with zookeeper init" + _ci::validate_kustomize_yaml ${kustomize_yaml_dir} || ((failures++)) + # validate oxia init + yq -i '.helmCharts[0].valuesInline.components += {"zookeeper": false, "oxia": true}' ${kustomize_yaml_dir}/kustomization.yaml + echo "Validating kustomize yaml output with oxia init" + _ci::validate_kustomize_yaml ${kustomize_yaml_dir} || ((failures++)) + if [ $failures -gt 0 ]; then + exit 1 + fi +} + +function _ci::validate_kustomize_yaml() { + local kustomize_yaml_dir=$1 + kustomize build --enable-helm --helm-kube-version 1.23.0 --load-restrictor=LoadRestrictionsNone ${kustomize_yaml_dir} | yq 'select(.spec.template.spec.containers[0].args != null) | .spec.template.spec.containers[0].args' | \ + awk '{ + if (prev_line ~ /\\$/ && $0 ~ /^$/) { + print "Found issue: backslash at end of line followed by empty line. Must use pipe character for multiline strings to support kustomize due to kubernetes-sigs/kustomize#4201."; + print "Line: " prev_line; + has_issue = 1; + } + prev_line = $0; + } + END { + if (!has_issue) { + print "No issues found: no backslash followed by empty line"; + exit 0; + } + exit 1; + }' +} + +# lists all available functions in this tool +function ci::list_functions() { + declare -F | awk '{print $NF}' | sort | grep -E '^ci::' | sed 's/^ci:://' +} + +# Only run this section if the script is being executed directly (not sourced) +if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then + if [ -z "$1" ]; then + echo "usage: $0 [function_name]" + echo "Available functions:" + ci::list_functions + exit 1 + fi + ci_function_name="ci::$1" + shift + if [[ "$(LC_ALL=C type -t "${ci_function_name}")" == "function" ]]; then + eval "$ci_function_name" "$@" + exit $? + else + echo "Invalid ci function" + echo "Available functions:" + ci::list_functions + exit 1 + fi +fi \ No newline at end of file diff --git a/.ci/kustomization.yaml b/.ci/kustomization.yaml new file mode 100644 index 0000000..bea24b1 --- /dev/null +++ b/.ci/kustomization.yaml @@ -0,0 +1,32 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +# + +apiVersion: kustomize.config.k8s.io/v1beta1 +kind: Kustomization +helmGlobals: + chartHome: ../charts +helmCharts: + - name: pulsar + releaseName: pulsar + valuesInline: + kube-prometheus-stack: + enabled: false + components: + pulsar_manager: true + zookeeper: true \ No newline at end of file diff --git a/.github/workflows/pulsar-helm-chart-ci.yaml b/.github/workflows/pulsar-helm-chart-ci.yaml index 027647e..f4cff1c 100644 --- a/.github/workflows/pulsar-helm-chart-ci.yaml +++ b/.github/workflows/pulsar-helm-chart-ci.yaml @@ -157,6 +157,12 @@ jobs: echo "Validating with Oxia enabled" validate_helm_template_with_k8s_version $k8s_version --set components.zookeeper=false --set components.oxia=true done + + - name: Validate kustomize yaml for extra new lines in pulsar-init commands + if: ${{ steps.check_changes.outputs.docs_only != 'true' }} + run: | + ./.ci/helm.sh validate_kustomize_yaml + - name: Wait for ssh connection when build fails # ssh access is enabled for builds in own forks uses: ./.github/actions/ssh-access diff --git a/charts/pulsar/templates/pulsar-cluster-initialize.yaml b/charts/pulsar/templates/pulsar-cluster-initialize.yaml index ad3a53d..af45d3e 100755 --- a/charts/pulsar/templates/pulsar-cluster-initialize.yaml +++ b/charts/pulsar/templates/pulsar-cluster-initialize.yaml @@ -119,7 +119,7 @@ spec: command: ["timeout", "{{ .Values.pulsar_metadata.initTimeout | default 60 }}", "sh", "-c"] {{- if .Values.components.zookeeper }} args: - - >- + - | # Use the pipe character for the YAML multiline string. Workaround for kubernetes-sigs/kustomize#4201 {{- include "pulsar.toolset.zookeeper.tls.settings" . | nindent 12 }} export PULSAR_MEM="-Xmx128M"; bin/pulsar initialize-cluster-metadata \ @@ -139,7 +139,7 @@ spec: {{- end }} {{- else if .Values.components.oxia }} args: - - >- + - | # Use the pipe character for the YAML multiline string. Workaround for kubernetes-sigs/kustomize#4201 export PULSAR_MEM="-Xmx128M"; bin/pulsar initialize-cluster-metadata \ --cluster {{ template "pulsar.cluster.name" . }} \