From 954986dfabe8d8f55a0ba7794727248b69fbadb0 Mon Sep 17 00:00:00 2001 From: Gerd Oberlechner Date: Wed, 20 Nov 2024 09:52:39 +0100 Subject: [PATCH] poc makefile Signed-off-by: Gerd Oberlechner --- backend/Makefile | 39 ++++++++++++++++++++----- backend/Makefile.deploy | 26 ----------------- dev-infrastructure/region-pipeline.yaml | 2 ++ dev-infrastructure/svc-pipeline.yaml | 2 ++ setup-env.mk | 21 +++++++++++++ templatize.sh | 32 ++++++++++++++++++-- tooling/templatize/gerd-param.json | 24 --------------- tooling/templatize/pkg/aks/admin.go | 6 ++-- tooling/templatize/test.mk | 6 ---- 9 files changed, 90 insertions(+), 68 deletions(-) delete mode 100644 backend/Makefile.deploy create mode 100644 setup-env.mk delete mode 100644 tooling/templatize/gerd-param.json delete mode 100644 tooling/templatize/test.mk diff --git a/backend/Makefile b/backend/Makefile index 0c55a9507..51934ea30 100644 --- a/backend/Makefile +++ b/backend/Makefile @@ -1,29 +1,54 @@ -SHELL = /bin/bash -DEPLOY_ENV ?= personal-dev -$(shell ../templatize.sh $(DEPLOY_ENV) config.tmpl.mk config.mk) -include config.mk -include Makefile.deploy +include ../setup-env.mk + +COMMIT ?= $(shell git rev-parse --short=7 HEAD) +ARO_HCP_BASE_IMAGE ?= ${ARO_HCP_IMAGE_ACR}.azurecr.io +ARO_HCP_BACKEND_IMAGE ?= $(ARO_HCP_BASE_IMAGE)/arohcpbackend:$(COMMIT) backend: go build -o aro-hcp-backend . +.PHONY: backend run: ./aro-hcp-backend --location ${LOCATION} \ --clusters-service-url http://localhost:8000 +.PHONY: run clean: rm -f aro-hcp-backend +.PHONY: clean image: pushd .. && git archive --output backend/archive.tar.gz HEAD && popd docker build -f "./Dockerfile" -t ${ARO_HCP_BACKEND_IMAGE} . rm -f archive.tar.gz +.PHONY: image push: image az acr login --name ${ARO_HCP_IMAGE_ACR} docker push ${ARO_HCP_BACKEND_IMAGE} +.PHONY: push + +deploy: + BACKEND_MI_CLIENT_ID=$$(az identity show \ + -g ${RESOURCEGROUP} \ + -n backend \ + --query clientId -o tsv) && \ + ISTO_VERSION=$$(az aks show -n ${AKS_NAME} -g ${RESOURCEGROUP} --query serviceMeshProfile.istio.revisions[-1] -o tsv) && \ + DB_URL=$$(az cosmosdb show -n ${DB_NAME} -g ${RESOURCEGROUP} --query documentEndpoint -o tsv) && \ + kubectl create namespace aro-hcp --dry-run=client -o json | kubectl apply -f - && \ + kubectl label namespace aro-hcp "istio.io/rev=$${ISTO_VERSION}" --overwrite=true && \ + helm upgrade --install aro-hcp-backend-dev \ + deploy/helm/backend/ \ + --set configMap.databaseName=${DB_NAME} \ + --set configMap.databaseUrl="$${DB_URL}" \ + --set configMap.backendMiClientId="$${BACKEND_MI_CLIENT_ID}" \ + --set serviceAccount.workloadIdentityClientId="$${BACKEND_MI_CLIENT_ID}" \ + --set configMap.currentVersion=${ARO_HCP_BACKEND_IMAGE} \ + --set configMap.location=${LOCATION} \ + --set deployment.imageName=${ARO_HCP_BACKEND_IMAGE} \ + --namespace aro-hcp +.PHONY: deploy undeploy: helm uninstall aro-hcp-backend-dev --namespace aro-hcp - -.PHONY: backend run clean image push undeploy +.PHONY: undeploy diff --git a/backend/Makefile.deploy b/backend/Makefile.deploy deleted file mode 100644 index 45aec1c3b..000000000 --- a/backend/Makefile.deploy +++ /dev/null @@ -1,26 +0,0 @@ -SHELL = /bin/bash - -COMMIT = $(shell git rev-parse --short=7 HEAD) -ARO_HCP_BASE_IMAGE ?= ${ARO_HCP_IMAGE_ACR}.azurecr.io -ARO_HCP_BACKEND_IMAGE ?= $(ARO_HCP_BASE_IMAGE)/arohcpbackend:$(COMMIT) - -deploy: - BACKEND_MI_CLIENT_ID=$(shell az identity show \ - -g ${RESOURCEGROUP} \ - -n backend \ - --query clientId);\ - ISTO_VERSION=$(shell az aks show -n ${AKS_NAME} -g ${RESOURCEGROUP} --query serviceMeshProfile.istio.revisions[-1] -o tsv) && \ - DB_URL=$(shell az cosmosdb show -n ${DB_NAME} -g ${RESOURCEGROUP} --query documentEndpoint -o tsv) && \ - kubectl create namespace aro-hcp --dry-run=client -o json | kubectl apply -f - && \ - kubectl label namespace aro-hcp "istio.io/rev=$${ISTO_VERSION}" --overwrite=true && \ - helm upgrade --install aro-hcp-backend-dev \ - deploy/helm/backend/ \ - --set configMap.databaseName=${DB_NAME} \ - --set configMap.databaseUrl="$${DB_URL}" \ - --set configMap.backendMiClientId="$${BACKEND_MI_CLIENT_ID}" \ - --set serviceAccount.workloadIdentityClientId="$${BACKEND_MI_CLIENT_ID}" \ - --set configMap.currentVersion=${ARO_HCP_BACKEND_IMAGE} \ - --set configMap.location=${LOCATION} \ - --set deployment.imageName=${ARO_HCP_BACKEND_IMAGE} \ - --namespace aro-hcp -.PHONY: deploy diff --git a/dev-infrastructure/region-pipeline.yaml b/dev-infrastructure/region-pipeline.yaml index d55a00e7e..e355c5a03 100644 --- a/dev-infrastructure/region-pipeline.yaml +++ b/dev-infrastructure/region-pipeline.yaml @@ -12,3 +12,5 @@ resourceGroups: action: ARM template: modules/modules/metrics.bicep parameters: configurations/metrics.tmpl.bicepparam + dependsOn: + - region diff --git a/dev-infrastructure/svc-pipeline.yaml b/dev-infrastructure/svc-pipeline.yaml index 3222d8fd6..7a79b4c8b 100644 --- a/dev-infrastructure/svc-pipeline.yaml +++ b/dev-infrastructure/svc-pipeline.yaml @@ -23,3 +23,5 @@ resourceGroups: configRef: monitoringWorkspaceName - name: GRAFANA_NAME configRef: grafanaName + dependsOn: + - region diff --git a/setup-env.mk b/setup-env.mk new file mode 100644 index 000000000..dab2c7654 --- /dev/null +++ b/setup-env.mk @@ -0,0 +1,21 @@ +SHELL = /bin/bash +SHELLFLAGS = -eu -o pipefail -c +PROJECT_ROOT_DIR := $(dir $(abspath $(lastword $(MAKEFILE_LIST)))) + +DEPLOY_ENV ?= personal-dev +PIPELINE ?= pipeline.yaml +PIPELINE_STEP ?= deploy +HASH = $(shell echo -n "$(DEPLOY_ENV)$(PIPELINE)$(PIPELINE_STEP)" | md5) +ENV_VARS_FILE ?= /tmp/deploy.${HASH}.cfg + +# Target to generate the environment variables file +$(ENV_VARS_FILE): ${PROJECT_ROOT_DIR}/config/config.yaml ${PIPELINE} ${PROJECT_ROOT_DIR}/templatize.sh ${MAKEFILE_LIST} + @echo "generate env vars" + @${PROJECT_ROOT_DIR}/templatize.sh ${DEPLOY_ENV} \ + -p ${PIPELINE} \ + -s ${PIPELINE_STEP} > $@ + +# Include the environment variables file if it exists +ifndef EV2 +-include ${ENV_VARS_FILE} +endif diff --git a/templatize.sh b/templatize.sh index 6137fe2ba..7f2730029 100755 --- a/templatize.sh +++ b/templatize.sh @@ -14,10 +14,14 @@ usage() { echo " deploy_env Deployment environment" echo " input Optional input file" echo " output Optional output file" + echo " -i Set the input file same as second arg" + echo " -o Set the output file same as third arg" echo " -c Set the cloud (default: public)" echo " -r Set the region (default: westus3)" echo " -x Set the cxstamp (default: 1)" echo " -e Extra args for config interpolation" + echo " -p Pipeline to inspect" + echo " -s Pipeline step to inspect" exit 1 } @@ -41,7 +45,7 @@ if [ "$#" -ge 1 ] && [[ ! "$1" =~ ^- ]]; then fi # Parse optional flags -while getopts "c:r:x:e:" opt; do +while getopts "c:r:x:e:i:o:p:s:" opt; do case ${opt} in c) CLOUD=${OPTARG} @@ -55,6 +59,18 @@ while getopts "c:r:x:e:" opt; do e) EXTRA_ARGS="--extra-args ${OPTARG}" ;; + i) + INPUT=${OPTARG} + ;; + o) + OUTPUT=${OPTARG} + ;; + p) + PIPELINE=${OPTARG} + ;; + s) + PIPELINE_STEP=${OPTARG} + ;; *) usage ;; @@ -96,7 +112,8 @@ else REGION_STAMP="${CLEAN_DEPLOY_ENV}" fi -TEMPLATIZE="go run ${PROJECT_ROOT_DIR}/tooling/templatize" +make -s -C ${PROJECT_ROOT_DIR}/tooling/templatize templatize +TEMPLATIZE="${PROJECT_ROOT_DIR}/tooling/templatize/templatize" CONFIG_FILE=${PROJECT_ROOT_DIR}/config/config.yaml if [ -n "$INPUT" ] && [ -n "$OUTPUT" ]; then @@ -110,6 +127,17 @@ if [ -n "$INPUT" ] && [ -n "$OUTPUT" ]; then --input=${INPUT} \ --output=${OUTPUT} \ ${EXTRA_ARGS} +elif [ -n "$PIPELINE" ] && [ -n "$PIPELINE_STEP" ]; then + $TEMPLATIZE run-pipeline \ + --config-file=${CONFIG_FILE} \ + --cloud=${CLOUD} \ + --deploy-env=${DEPLOY_ENV} \ + --region=${REGION} \ + --region-short=${REGION_STAMP} \ + --stamp=${CXSTAMP} \ + --pipeline-file=${PIPELINE} \ + --step=${PIPELINE_STEP} \ + --dump-step-vars else $TEMPLATIZE inspect \ --config-file=${CONFIG_FILE} \ diff --git a/tooling/templatize/gerd-param.json b/tooling/templatize/gerd-param.json deleted file mode 100644 index f585b397b..000000000 --- a/tooling/templatize/gerd-param.json +++ /dev/null @@ -1,24 +0,0 @@ -{ - "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentParameters.json#", - "contentVersion": "1.0.0.0", - "parameters": { - "baseDNSZoneName": { - "value": "hcp.osadev.cloud" - }, - "baseDNSZoneResourceGroup": { - "value": "global" - }, - "regionalDNSSubdomain": { - "value": "usw3sjan" - }, - "maestroKeyVaultName": { - "value": "arohcp-maestro-usw3sjan" - }, - "maestroEventGridNamespacesName": { - "value": "arohcp-maestro-usw3sjan" - }, - "maestroEventGridMaxClientSessionsPerAuthName": { - "value": 4 - } - } -} \ No newline at end of file diff --git a/tooling/templatize/pkg/aks/admin.go b/tooling/templatize/pkg/aks/admin.go index fc119321d..0b5d73e30 100644 --- a/tooling/templatize/pkg/aks/admin.go +++ b/tooling/templatize/pkg/aks/admin.go @@ -48,7 +48,7 @@ func EnsureClusterAdmin(ctx context.Context, kubeconfigPath, subscriptionID, res } // Validate assignment - err = TestClusterAdminPermissions(ctx, kubeconfigPath) + err = CheckClusterAdminPermissions(ctx, kubeconfigPath) if err == nil { return nil } @@ -66,7 +66,7 @@ func EnsureClusterAdmin(ctx context.Context, kubeconfigPath, subscriptionID, res case <-timeout: return fmt.Errorf("timed out waiting for role assignment to be effective") case <-ticker.C: - err = TestClusterAdminPermissions(ctx, kubeconfigPath) + err = CheckClusterAdminPermissions(ctx, kubeconfigPath) if err == nil { fmt.Println("Cluster admin permissions are now effective") return nil @@ -76,7 +76,7 @@ func EnsureClusterAdmin(ctx context.Context, kubeconfigPath, subscriptionID, res } } -func TestClusterAdminPermissions(ctx context.Context, kubeconfigPath string) error { +func CheckClusterAdminPermissions(ctx context.Context, kubeconfigPath string) error { clientset, err := createKubeClient(kubeconfigPath) if err != nil { return fmt.Errorf("failed to create Kubernetes client: %w", err) diff --git a/tooling/templatize/test.mk b/tooling/templatize/test.mk deleted file mode 100644 index 9e12764f3..000000000 --- a/tooling/templatize/test.mk +++ /dev/null @@ -1,6 +0,0 @@ -ARO_HCP_IMAGE_ACR ?= __SVCACRNAME__ -HO_IMAGE_TAG ?= __HYPERSHIFTOPERATORIMAGETAG__ -ED_IMAGE_TAG ?= __EXTERNALDNSIMAGETAG__ -RESOURCEGROUP ?= __MANAGEMENTCLUSTERRG__ -REGIONAL_RESOURCEGROUP ?= __REGIONRG__ -ZONE_NAME ?= __REGIONALDNSSUBDOMAIN__.__BASEDNSZONENAME__