|
| 1 | +#!/bin/bash |
| 2 | +# Purpose: Updates CRDs for the OpenTelemetry Operator in the opentelemetry-operator-crds subchart |
| 3 | +# The script compares the current CRDs with those found in the appVersion of the Opentelemetry Operator |
| 4 | +# we are using as dependency. If they differ, it updates the CRDs in the opentelemetry-operator-crds subchart. |
| 5 | + |
| 6 | +# Include the base utility functions for setting and debugging variables |
| 7 | +SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" |
| 8 | +source "${SCRIPT_DIR}/base_util.sh" |
| 9 | + |
| 10 | +# Initialize directories and paths |
| 11 | +setd "ROOT_DIR" "${SCRIPT_DIR}/../" |
| 12 | +setd "COLLECTOR_CHART_DIR" "${ROOT_DIR}/helm-charts/splunk-otel-collector" |
| 13 | +setd "CRD_CHART_DIR" "${COLLECTOR_CHART_DIR}/charts/opentelemetry-operator-crds" |
| 14 | +setd "CRD_DIR" "${CRD_CHART_DIR}/crds" |
| 15 | +setd "CRD_CHART_FILE" "${CRD_CHART_DIR}/Chart.yaml" |
| 16 | +setd "OTEL_OPERATOR_REPO" "https://github.com/open-telemetry/opentelemetry-operator.git" |
| 17 | +setd "TEMP_DIR" "$(mktemp -d)" |
| 18 | + |
| 19 | +trap "rm -rf $TEMP_DIR" EXIT |
| 20 | + |
| 21 | +# Function: get_operator_app_version |
| 22 | +# Description: Retrieves the app version of the OpenTelemetry operator dependency |
| 23 | +get_operator_app_version() { |
| 24 | + debug "Retrieving OpenTelemetry operator version" |
| 25 | + |
| 26 | + local operator_version |
| 27 | + local operator_repo |
| 28 | + |
| 29 | + operator_version=$(yq eval '.dependencies[] | select(.name == "opentelemetry-operator") | .version' "${COLLECTOR_CHART_DIR}/Chart.yaml") |
| 30 | + operator_repo=$(yq eval '.dependencies[] | select(.name == "opentelemetry-operator") | .repository' "${COLLECTOR_CHART_DIR}/Chart.yaml") |
| 31 | + |
| 32 | + debug "Pulling OpenTelemetry operator Helm chart version ${operator_version}" |
| 33 | + helm pull opentelemetry-operator --version "$operator_version" --repo "$operator_repo" --untar --untardir "$TEMP_DIR/chart" |
| 34 | + |
| 35 | + local app_version |
| 36 | + app_version=$(yq eval '.appVersion' "$TEMP_DIR/chart/opentelemetry-operator/Chart.yaml") |
| 37 | + |
| 38 | + echo "$app_version" |
| 39 | +} |
| 40 | + |
| 41 | +# Function: bump_patch_version |
| 42 | +# Description: Bumps the patch version of the CRDs chart and updates the dependency in the collector chart. |
| 43 | +bump_patch_version() { |
| 44 | + local current_version |
| 45 | + current_version=$(yq eval '.version' "$CRD_CHART_FILE") |
| 46 | + debug "Current CRDs chart version: $current_version" |
| 47 | + |
| 48 | + local major minor patch |
| 49 | + IFS='.' read -r major minor patch <<< "$current_version" |
| 50 | + patch=$((patch + 1)) |
| 51 | + local new_version="${major}.${minor}.${patch}" |
| 52 | + |
| 53 | + yq eval ".version = \"$new_version\"" -i "$CRD_CHART_FILE" |
| 54 | + echo "CRDs chart version bumped to $new_version" |
| 55 | + |
| 56 | + yq eval "(.dependencies[] | select(.name == \"opentelemetry-operator-crds\") | .version) = \"$new_version\"" -i "${COLLECTOR_CHART_DIR}/Chart.yaml" |
| 57 | + echo "Updated CRDs chart version in chart dependencies to $new_version" |
| 58 | +} |
| 59 | + |
| 60 | +# Function: update_crds |
| 61 | +# Description: Updates the OpenTelemetry CRDs if there are changes |
| 62 | +update_crds() { |
| 63 | + local latest_version |
| 64 | + latest_version=$(get_operator_app_version) |
| 65 | + setd "LATEST_VERSION" "$latest_version" |
| 66 | + echo "OpenTelemetry operator app version: $latest_version" |
| 67 | + |
| 68 | + debug "Cloning OpenTelemetry Operator repository..." |
| 69 | + git config --global advice.detachedHead false # Suppress detached HEAD warnings |
| 70 | + git clone --quiet --depth 1 --branch "v${latest_version}" ${OTEL_OPERATOR_REPO} ${TEMP_DIR}/opentelemetry-operator || { |
| 71 | + echo "Error: Failed to clone OpenTelemetry Operator repository." |
| 72 | + exit 1 |
| 73 | + } |
| 74 | + |
| 75 | + mkdir -p "${CRD_DIR}" |
| 76 | + cp ${TEMP_DIR}/opentelemetry-operator/config/crd/bases/*.yaml "${CRD_DIR}/" 2>/dev/null |
| 77 | + |
| 78 | + if git diff --quiet -- "${CRD_DIR}"; then |
| 79 | + echo "No CRD updates detected. CRDs are already up to date." |
| 80 | + setd "CRDS_NEED_UPDATE" 0 |
| 81 | + else |
| 82 | + echo "CRD updates detected for OpenTelemetry Operator." |
| 83 | + setd "CRDS_NEED_UPDATE" 1 |
| 84 | + bump_patch_version |
| 85 | + fi |
| 86 | + |
| 87 | + setd "CRDS_LATEST_VERSION" "$(yq eval '.version' "$CRD_CHART_FILE")" |
| 88 | + emit_output "CRDS_NEED_UPDATE" |
| 89 | + emit_output "CRDS_LATEST_VERSION" |
| 90 | +} |
| 91 | + |
| 92 | +update_crds |
0 commit comments