From c1c9ee3ba4322b0b068749106128495cd505adb4 Mon Sep 17 00:00:00 2001 From: kksat <22549266+kksat@users.noreply.github.com> Date: Mon, 26 Aug 2024 10:23:46 +0200 Subject: [PATCH 1/2] add code to error if variables are not defined Signed-off-by: kksat <22549266+kksat@users.noreply.github.com> --- Makefile | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/Makefile b/Makefile index ef13eb0..3b61a10 100644 --- a/Makefile +++ b/Makefile @@ -9,6 +9,20 @@ ifneq (,$(wildcard ./.env)) export endif +defined = $(strip $(filter-out undefined,$(flavor $1))) + +required-environment-variables = \ + $(eval .required-environment-variables :=) \ + $(foreach V,$(sort $1), \ + $(if $(call defined,$V),,$(eval .required-environment-variables += $V)) \ + ) \ + $(if $(strip ${.required-environment-variables}), \ + $(foreach V,${.required-environment-variables}, \ + $(info Variable is not defined but required: $$$V) \ + ) \ + $(error Not all required variables are defined) \ + ) + PYTHON?=python3.10 export PYTHON TOX?=tox From ebecce0b14adb8fab7758e3ecd0758450b3c7369 Mon Sep 17 00:00:00 2001 From: kksat <22549266+kksat@users.noreply.github.com> Date: Mon, 26 Aug 2024 10:44:47 +0200 Subject: [PATCH 2/2] add required environment variables checks Signed-off-by: kksat <22549266+kksat@users.noreply.github.com> --- bicep.makefile | 11 +++++++++++ rosa.makefile | 9 +++++++++ 2 files changed, 20 insertions(+) diff --git a/bicep.makefile b/bicep.makefile index 1cee752..205b764 100644 --- a/bicep.makefile +++ b/bicep.makefile @@ -7,10 +7,12 @@ ARO_VERSION?=4.14.16 .PHONY: aro-remove aro-remove: ## Remove ARO + $(call required-environment-variables,ARO_RESOURCE_GROUP) az deployment group create --resource-group ${ARO_RESOURCE_GROUP} --template-file bicep/empty.bicep --mode Complete .PHONY: aro-deploy aro-deploy: domain-zone-exists network-deploy ## Deploy ARO + $(call required-environment-variables,ARO_RESOURCE_GROUP ARO_CLUSTER_NAME ARO_DOMAIN ARO_VERSION CLIENT_ID CLIENT_SECRET) @az deployment group create --resource-group ${ARO_RESOURCE_GROUP} \ --template-file bicep/aro.bicep \ --parameters \ @@ -24,6 +26,7 @@ aro-deploy: domain-zone-exists network-deploy ## Deploy ARO .PHONY: domain-records .ONESHELL: domain-records: ## Create domain records for ARO + $(call required-environment-variables,ARO_RESOURCE_GROUP ARO_CLUSTER_NAME ARO_DOMAIN) hack/domain-records.sh \ --domain ${ARO_DOMAIN} \ --aro-name ${ARO_CLUSTER_NAME} \ @@ -31,16 +34,19 @@ domain-records: ## Create domain records for ARO .PHONY: network-deploy network-deploy: ## Deploy network + $(call required-environment-variables,ARO_RESOURCE_GROUP) az deployment group create --resource-group ${ARO_RESOURCE_GROUP} \ --template-file bicep/network.bicep .PHONY: resource-group resource-group: ## Create resource group + $(call required-environment-variables,ARO_RESOURCE_GROUP ARO_LOCATION) az group create --name ${ARO_RESOURCE_GROUP} --location ${ARO_LOCATION} --query name -o tsv .PHONY: service-principal .ONESHELL: service-principal: ## Create sevice principal for ARO deployment + $(call required-environment-variables,ARO_RESOURCE_GROUP) az ad sp create-for-rbac \ --name "aro-service-principal" \ --role Contributor \ @@ -51,22 +57,27 @@ service-principal: ## Create sevice principal for ARO deployment .PHONY: arorp-service-principal .ONESHELL: arorp-service-principal: ## Assign required roles to "Azure Red Hat Openshift" RP service principal + $(call required-environment-variables,ARO_RESOURCE_GROUP) az role assignment create --assignee $$(az ad sp list --display-name "Azure Red Hat OpenShift RP" --query "[0].id" -o tsv) \ --role Contributor \ --scope "/subscriptions/$$(az account show --query id -o tsv)/resourceGroups/${ARO_RESOURCE_GROUP}" aro-credentials: ## Get ARO credentials + $(call required-environment-variables,ARO_RESOURCE_GROUP ARO_CLUSTER_NAME) @az aro list-credentials --name ${ARO_CLUSTER_NAME} --resource-group ${ARO_RESOURCE_GROUP} aro-url: ## Get ARO URL + $(call required-environment-variables,ARO_RESOURCE_GROUP ARO_CLUSTER_NAME) @az aro show --name ${ARO_CLUSTER_NAME} --resource-group ${ARO_RESOURCE_GROUP} --query "apiserverProfile.url" -o tsv .PHONY: domain-zone-exists domain-zone-exists: ## Fail if DNS domain zone does not exists + $(call required-environment-variables,ARO_DOMAIN) ARO_DOMAIN=${ARO_DOMAIN} hack/domain-zone-exists.sh .PHONY: oc-login oc-login: ## Login with oc to existing ARO cluster + $(call required-environment-variables,ARO_CLUSTER_NAME ARO_RESOURCE_GROUP) oc login "$(shell az aro show --name ${ARO_CLUSTER_NAME} --resource-group ${ARO_RESOURCE_GROUP} --query "apiserverProfile.url" -o tsv)" \ -u "$(shell az aro list-credentials --name ${ARO_CLUSTER_NAME} --resource-group ${ARO_RESOURCE_GROUP} --query 'kubeadminUsername' -o tsv)" \ -p "$(shell az aro list-credentials --name ${ARO_CLUSTER_NAME} --resource-group ${ARO_RESOURCE_GROUP} --query 'kubeadminPassword' -o tsv)" diff --git a/rosa.makefile b/rosa.makefile index 923327a..e9cbb94 100644 --- a/rosa.makefile +++ b/rosa.makefile @@ -2,6 +2,7 @@ CLUSTER_NAME?=sapeic .PHONY: rosa-login rosa-login: ## Login using ROSA token + $(call required-environment-variables,ROSA_TOKEN) @rosa login --token="${ROSA_TOKEN}" .PHONY: rosa-init @@ -14,31 +15,38 @@ rosa-account-roles: ## Login using ROSA token .PHONY: rosa-cluster rosa-cluster: ## Create ROSA cluster + $(call required-environment-variables,ROSA_TOKEN CLUSTER_NAME) rosa create cluster --cluster-name "${CLUSTER_NAME}" .PHONY: rosa-cluster-status rosa-cluster-status: ## Get ROSA cluster status + $(call required-environment-variables,ROSA_TOKEN CLUSTER_NAME) rosa describe cluster --cluster "${CLUSTER_NAME}" .PHONY: rosa-cluster-hibernate rosa-cluster-hibernate: ## Hibernate ROSA cluster + $(call required-environment-variables,ROSA_TOKEN CLUSTER_NAME) rosa hibernate cluster --cluster "${CLUSTER_NAME}" .PHONY: rosa-cluster-resume rosa-cluster-resume: ## Resume ROSA cluster + $(call required-environment-variables,ROSA_TOKEN CLUSTER_NAME) rosa resume cluster --cluster "${CLUSTER_NAME}" .PHONY: rosa-cluster-delete rosa-cluster-delete: ## Delete ROSA cluster + $(call required-environment-variables,ROSA_TOKEN CLUSTER_NAME) rosa delete cluster --cluster "${CLUSTER_NAME}" .PHONY: rosa-cluster-admin rosa-cluster-admin: ## Create cluster admin + $(call required-environment-variables,ROSA_TOKEN CLUSTER_NAME CLUSTER_ADMIN_PASSWORD) @rosa create admin --cluster ${CLUSTER_NAME} --password ${CLUSTER_ADMIN_PASSWORD} >/dev/null $(info cluster admin created) .PHONY: rosa-cluster-admin-reset rosa-cluster-admin-reset: ## Reset cluster admin password + $(call required-environment-variables,ROSA_TOKEN CLUSTER_NAME CLUSTER_ADMIN_PASSWORD) @rosa delete admin --cluster ${CLUSTER_NAME} --yes >/dev/null $(info cluster admin deleted) @rosa create admin --cluster ${CLUSTER_NAME} --password ${CLUSTER_ADMIN_PASSWORD} >/dev/null @@ -46,4 +54,5 @@ rosa-cluster-admin-reset: ## Reset cluster admin password .PHONY: rosa-cluster-oc-login rosa-cluster-oc-login: ## OC cli login to existing cluster (cluster-admin should already exist) + $(call required-environment-variables,ROSA_TOKEN CLUSTER_NAME) @rosa describe admin --cluster=${CLUSTER_NAME} | grep -v 'INFO'