From 77a2f28009527c0bf19fadd5e0ee250eaeec64b6 Mon Sep 17 00:00:00 2001 From: Quentin Smith Date: Mon, 13 May 2024 16:01:48 -0400 Subject: [PATCH] Script to deploy k8s resources to an arbitrary namespace. --- gcp/deploy.sh | 138 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 138 insertions(+) create mode 100755 gcp/deploy.sh diff --git a/gcp/deploy.sh b/gcp/deploy.sh new file mode 100755 index 00000000..4e60b63d --- /dev/null +++ b/gcp/deploy.sh @@ -0,0 +1,138 @@ +#!/bin/bash + +# Copyright 2024 Google LLC +# +# Licensed 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 +# +# https://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. + +set -ex + +apply="kubectl apply -k" + +# $helmfile_vars is an associative array that will be passed to helmfile --state-values-set-string +declare -A helmfile_vars +helmfile_vars["namespace"]="otel-demo" + +# Parse options +while getopts "N:p:kn" opt; do + case $opt in + N) + helmfile_vars["namespace"]="${OPTARG}" + ;; + n) + # dry-run + apply="kubectl apply --dry-run=client -o yaml -k" + ;; + k) + # Stop after kustomize + apply="kubectl kustomize" + ;; + p) + PROJECT_ID="${OPTARG}" + ;; + :) + echo "Error: ${OPTARG} requires an argument" + exit 1 + ;; + ?) + exit 1 + ;; + esac +done + +shift $((OPTIND-1)) + +# Append the helmfile_vars associative array to the HELMFILE_FLAGS indexed array of flags for helmfile + +HELMFILE_FLAGS=("--environment" "cicd") +set_values() { + local -a out + for key in "${!helmfile_vars[@]}"; do + out+=( "${key}=${helmfile_vars[${key}]}" ) + done + local IFS=, + if [ ${#out[*]} -gt 0 ]; then + HELMFILE_FLAGS+=("--state-values-set-string" "${out[*]}") + fi +} +set_values + +src=$(readlink -f "$(dirname "$0")/..") + +cd "$src" + +# Re-generate the k8s YAML with the new values + +make generate-kubernetes-manifests HELMFILE_FLAGS="${HELMFILE_FLAGS[*]}" >&2 + +kustomize_dir=$(mktemp -d) +cleanup() { + [ -n "$kustomize_dir" ] && rm -rf "$kustomize_dir" +} +trap cleanup EXIT + +# Use Kustomize to transform cluster-wide resources into namespaced resources. +# TODO: Add support for running inside a namespace to the upstream helm charts, so we can get rid of Kustomize. + +cd "$kustomize_dir" + +cp "$src/kubernetes/opentelemetry-demo.yaml" ./ + +cat > role.yaml < role_binding.yaml < prometheus_role.yaml < kustomization.yaml <