|
| 1 | +#!/bin/bash |
| 2 | +# Builds, packages, and pushes artifacts for the Splunk OpenTelemetry Collector EKS Add-on. |
| 3 | +# |
| 4 | +# Parameters: |
| 5 | +# --dry-run : (Optional) Enables dry-run mode that skips pushing artifacts. |
| 6 | +# --chart-version VERSION : (Optional) Overrides the chart version from Chart.yaml. |
| 7 | +# |
| 8 | +# Note: This script requires OKTA_AWS_ROLE_ARN to be set in the environment. |
| 9 | + |
| 10 | +# Enable bash strict mode to fail fast |
| 11 | +set -euo pipefail |
| 12 | + |
| 13 | +# Check required tools |
| 14 | +for tool in "okta-aws-login" "aws" "yq" "helm" "docker"; do |
| 15 | + command -v "${tool}" &>/dev/null || { echo "❌ Required command '${tool}' is not installed or not in PATH"; exit 1; } |
| 16 | +done |
| 17 | + |
| 18 | +# Parse command line arguments |
| 19 | +DRY_RUN_PREFIX="" |
| 20 | +CHART_VERSION="" |
| 21 | +while [[ $# -gt 0 ]]; do |
| 22 | + case "$1" in |
| 23 | + --dry-run) |
| 24 | + DRY_RUN_PREFIX="echo 🚧 [DRY-RUN] " |
| 25 | + shift |
| 26 | + ;; |
| 27 | + --chart-version) |
| 28 | + CHART_VERSION="$2" |
| 29 | + echo "ℹ️ Chart version override provided: $CHART_VERSION" |
| 30 | + shift 2 |
| 31 | + ;; |
| 32 | + *) |
| 33 | + echo "❌ Unknown argument: $1" |
| 34 | + exit 1 |
| 35 | + ;; |
| 36 | + esac |
| 37 | +done |
| 38 | + |
| 39 | +SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" |
| 40 | +CHART_DIR="$SCRIPT_DIR/../helm-charts/splunk-otel-collector" |
| 41 | +if [[ -z "$CHART_VERSION" ]]; then |
| 42 | + CHART_VERSION=$(yq e ".version" "${CHART_DIR}/Chart.yaml") |
| 43 | +fi |
| 44 | +CHART_APPVERSION=$(yq e ".appVersion" "${CHART_DIR}/Chart.yaml") |
| 45 | +ECR_REGION="us-east-1" |
| 46 | +ECR_REGISTRY="709825985650.dkr.ecr.${ECR_REGION}.amazonaws.com" |
| 47 | +ECR_OTELCOL_REPO="${ECR_REGISTRY}/splunk/images/splunk-otel-collector" |
| 48 | +ECR_FLUENTD_REPO="${ECR_REGISTRY}/splunk/docker.io/splunk/fluentd-hec" |
| 49 | +ECR_FLUENTD_REPO_TAG="1.3.3-linux" |
| 50 | +ECR_HELM_NAMESPACE="${ECR_REGISTRY}/splunk/charts" |
| 51 | +ECR_HELM_REPO="${ECR_HELM_NAMESPACE}/splunk-otel-collector" |
| 52 | + |
| 53 | +aws_okta_auth() { |
| 54 | + echo "🌐 Authenticating with Okta (browser will open)..." |
| 55 | + okta-aws-login > /tmp/okta_creds.txt 2>&1 || { echo "❌ AWS Authentication failed"; rm -f /tmp/okta_creds.txt; exit 1; } |
| 56 | + |
| 57 | + # Extract and evaluate AWS credentials |
| 58 | + eval "$(grep -E "^(AWS_|export)" /tmp/okta_creds.txt)" |
| 59 | + rm -f /tmp/okta_creds.txt |
| 60 | + AUTH_AWS_ECR_PASSWORD=$(aws ecr get-login-password --region "$ECR_REGION") |
| 61 | + [[ -n "$AUTH_AWS_ECR_PASSWORD" ]] || { echo "❌ Failed to get ECR login password."; exit 1; } |
| 62 | + |
| 63 | + # Login to ECR for Docker and Helm |
| 64 | + echo "$AUTH_AWS_ECR_PASSWORD" | docker login --username AWS --password-stdin "$ECR_REGISTRY" |
| 65 | + echo "$AUTH_AWS_ECR_PASSWORD" | helm registry login --username AWS --password-stdin "$ECR_REGISTRY" |
| 66 | + echo "✅ AWS Authentication successful" |
| 67 | +} |
| 68 | + |
| 69 | +copy_docker_image_to_ecr() { |
| 70 | + # Get the otelcol repository from values.yaml |
| 71 | + local src_repo=$(yq e ".image.otelcol.repository" "${CHART_DIR}/values.yaml") |
| 72 | + [[ -n "$src_repo" && "$src_repo" != "null" ]] || { echo "❌ Error: Could not find otelcol repository in values.yaml"; exit 1; } |
| 73 | + |
| 74 | + local src="${src_repo}:${CHART_APPVERSION}" |
| 75 | + local dest="${ECR_OTELCOL_REPO}:${CHART_APPVERSION}" |
| 76 | + |
| 77 | + echo "⏳ Copying otelcol image from ${src} to ${dest} ..." |
| 78 | + docker pull ${src} |
| 79 | + docker tag ${src} ${dest} |
| 80 | + ${DRY_RUN_PREFIX} docker push ${dest} |
| 81 | + echo "✅ Successfully copied otelcol image: ${src} → ${dest}" |
| 82 | +} |
| 83 | + |
| 84 | +# Function to modify the Helm chart to meet EKS Add-on requirements |
| 85 | +modify_helm_chart() { |
| 86 | + local chart_dir="$1" |
| 87 | + local overrides_dir="${SCRIPT_DIR}/overrides" |
| 88 | + |
| 89 | + echo "⏳ Removing subcharts ..." |
| 90 | + rm -rf "${chart_dir}/charts" |
| 91 | + |
| 92 | + echo "⏳ Modifying Helm chart in ${chart_dir} using overrides from ${overrides_dir} ..." |
| 93 | + for override in "${overrides_dir}"/*.yaml; do |
| 94 | + if [[ -f "${override}" ]]; then |
| 95 | + local override_basename=$(basename "${override}") |
| 96 | + local target_file="${chart_dir}/${override_basename}" |
| 97 | + |
| 98 | + # Process environment variables |
| 99 | + local tmp_file="/tmp/${override_basename}.expanded" |
| 100 | + eval "cat <<EOF |
| 101 | +$(cat "${override}") |
| 102 | +EOF" > "${tmp_file}" |
| 103 | + |
| 104 | + # Merge override with the corresponding file in the chart |
| 105 | + yq eval-all 'select(fileIndex==0) * select(fileIndex==1)' -i "${target_file}" "${tmp_file}" |
| 106 | + rm -f "${tmp_file}" |
| 107 | + fi |
| 108 | + done |
| 109 | + |
| 110 | + echo "⏳ Moving values.schema.json to aws_mp_configuration_schema.json and removing unsupported properties ..." |
| 111 | + cp "${tmp_chart_dir}/values.schema.json" "${tmp_chart_dir}/aws_mp_configuration_schema.json" |
| 112 | + disabled_properties=( |
| 113 | + "enabled" |
| 114 | + "operatorcrds" |
| 115 | + "operator-crds" |
| 116 | + "operator" |
| 117 | + "opentelemetry-operator" |
| 118 | + "instrumentation" |
| 119 | + "certmanager" |
| 120 | + "cert-manager" |
| 121 | + "targetAllocator" |
| 122 | + ) |
| 123 | + for prop in "${disabled_properties[@]}"; do |
| 124 | + yq e "del(.properties.\"${prop}\")" -i "${tmp_chart_dir}/aws_mp_configuration_schema.json" |
| 125 | + done |
| 126 | + |
| 127 | + echo "✅ Successfully modified the Helm chart for EKS Add-on compliance" |
| 128 | +} |
| 129 | + |
| 130 | +package_and_push_helm_chart() { |
| 131 | + local ecr_chart_release="oci://${ECR_HELM_REPO}:${CHART_VERSION}" |
| 132 | + |
| 133 | + # Check if chart already exists in the registry |
| 134 | + if helm show chart "${ecr_chart_release}" &>/dev/null; then |
| 135 | + echo "❌ Chart already exists in registry: ${ecr_chart_release}. Use --chart-version to push another version of this chart." |
| 136 | + return 1 |
| 137 | + fi |
| 138 | + |
| 139 | + # Copy chart to a temporary build directory |
| 140 | + local build_dir="${SCRIPT_DIR}/build" |
| 141 | + rm -rf "${build_dir}" |
| 142 | + mkdir -p "${build_dir}" |
| 143 | + cp -R "${CHART_DIR}" "${build_dir}/" |
| 144 | + |
| 145 | + local tmp_chart_dir="${build_dir}/splunk-otel-collector" |
| 146 | + modify_helm_chart "${tmp_chart_dir}" |
| 147 | + |
| 148 | + echo "⏳ Packaging and pushing Helm chart ${ecr_chart_release} ..." |
| 149 | + |
| 150 | + # Package the chart |
| 151 | + helm package "${tmp_chart_dir}" -d "${build_dir}" |
| 152 | + local package_file="${build_dir}/splunk-otel-collector-${CHART_VERSION}.tgz" |
| 153 | + |
| 154 | + # Push the chart from the build location |
| 155 | + ${DRY_RUN_PREFIX} helm push ${package_file} oci://${ECR_HELM_NAMESPACE} |
| 156 | + |
| 157 | + echo "✅ Successfully pushed Helm chart to ${ecr_chart_release}" |
| 158 | +} |
| 159 | + |
| 160 | +print_summary() { |
| 161 | + echo "" |
| 162 | + echo "📋 Use these image references when creating the EKS Add-on release." |
| 163 | + echo " Helm Chart:" |
| 164 | + echo " - ${ECR_HELM_REPO}:${CHART_VERSION}" |
| 165 | + echo " Container Images:" |
| 166 | + echo " - ${ECR_OTELCOL_REPO}:${CHART_APPVERSION}" |
| 167 | + echo " - ${ECR_FLUENTD_REPO}:${ECR_FLUENTD_REPO_TAG}" |
| 168 | +} |
| 169 | + |
| 170 | +aws_okta_auth |
| 171 | +copy_docker_image_to_ecr |
| 172 | +package_and_push_helm_chart |
| 173 | +print_summary |
0 commit comments