Skip to content

Instantly share code, notes, and snippets.

@Zaperex
Created August 6, 2024 13:51
Show Gist options
  • Save Zaperex/4186721e8ca80fd1c84bc7405674b3d8 to your computer and use it in GitHub Desktop.
Save Zaperex/4186721e8ca80fd1c84bc7405674b3d8 to your computer and use it in GitHub Desktop.
#!/bin/sh
# Static variables
export NAME_SPACE=rhdh
export CLUSTER_USERNAME=kubeadmin
# export RELEASE_NAME=redhat-developer-hub
export RELEASE_NAME=backstage
# export HELM_CHART_VALUE_FILE_NAME='./helm-values/rhdh-1.2-68-values.yaml'
# export HELM_CHART_VALUE_FILE_NAME='./values-rhdh.yaml'
# export HELM_CHART_VALUE_FILE_NAME='./audit-log-values.yaml'
# export HELM_CHART_VALUE_FILE_NAME='./intern-expo-demo.yaml'
export HELM_CHART_VALUE_FILE_NAME='./testing-audit-log-and-rbac.yaml'
export HELM_REPO_NAME=openshift-helm-charts
export HELM_REPO_URL=https://charts.openshift.io/
# Dynamic variables
export CLUSTER_PASSWORD=IJryK-x5A3H-mZPzR-KrbAz
export CLUSTER_API=https://api.crc.testing:6443
export CLUSTER_ROUTER_BASE=apps-crc.testing
add_helm_repo() {
helm version
# helm repo add bitnami https://charts.bitnami.com/bitnami
# helm repo add backstage https://backstage.github.io/charts
# helm repo add janus-idp https://janus-idp.github.io/helm-backstage
# helm repo add ingress-nginx https://kubernetes.github.io/ingress-nginx
# helm repo add openshift-helm-charts https://charts.openshift.io/
# Check if the repository already exists
if ! helm repo list | grep -q "^${HELM_REPO_NAME}"; then
helm repo add "$REPO_NAME" "${HELM_REPO_URL}"
else
echo "Repository $REPO_NAME already exists - updating repository instead."
helm repo update
fi
}
# PREREQ #0: install oc, helm if you don't have them installed
install_oc() {
if [[ -x "$(command -v oc)" ]]; then
echo "oc is already installed."
else
curl -LO https://mirror.openshift.com/pub/openshift-v4/clients/oc/latest/linux/oc.tar.gz
tar -xf oc.tar.gz
mv oc /usr/local/bin/
rm oc.tar.gz
echo "oc installed successfully."
fi
}
install_helm() {
if [[ -x "$(command -v helm)" ]]; then
echo "Helm is already installed."
else
echo "Installing Helm 3 client"
WORKING_DIR=$(pwd)
mkdir ~/tmpbin && cd ~/tmpbin
HELM_INSTALL_DIR=$(pwd)
curl -sL https://raw.githubusercontent.com/helm/helm/master/scripts/get-helm-3 | bash -f
export PATH=${HELM_INSTALL_DIR}:$PATH
cd $WORKING_DIR
echo "helm client installed successfully."
fi
}
uninstall_all_resources() {
# ConfigMaps and Secrets
oc delete configmap app-config-rhdh --namespace=${NAME_SPACE}
oc delete secret rhdh-pull-secret --namespace=${NAME_SPACE}
oc delete secret keycloak-secrets --namespace=${NAME_SPACE}
oc delete secret rhdh-secrets --namespace=${NAME_SPACE}
oc delete configmap rbac-policy --namespace=${NAME_SPACE}
# Deployments
oc delete deployment backstage-app --namespace=${NAME_SPACE}
# Pipelines to test Tekton plugin
oc delete pipeline hello-world-pipeline --namespace=${NAME_SPACE}
oc delete pipelinerun hello-world-pipeline-run --namespace=${NAME_SPACE}
# Cluster Service Account
oc delete serviceaccount rhdh-k8s-plugin --namespace=${NAME_SPACE}
oc delete secret rhdh-k8s-plugin-secret --namespace=${NAME_SPACE}
# ClusterRoles and ClusterRoleBindings
oc delete clusterrole rhdh-k8s-plugin --namespace=${NAME_SPACE}
oc delete clusterrole rhdh-k8s-plugin-ocm --namespace=${NAME_SPACE}
oc delete clusterrolebinding rhdh-k8s-plugin
oc delete clusterrolebinding rhdh-k8s-plugin-ocm
# oc delete namespace ${NAME_SPACE}
# Pipelines to test Tekton plugin
oc apply -f $PWD/resources/pipelines/hello-world-pipeline.yaml --namespace=${NAME_SPACE}
oc apply -f $PWD/resources/pipelines/hello-world-pipeline-run.yaml --namespace=${NAME_SPACE}
# Upload Jobs and Cronjobs
oc delete cronjob say-hello --namespace=${NAME_SPACE}
oc delete job print-pi --namespace=${NAME_SPACE}
# Daemon Set
oc delete daemonset test-daemonset --namespace=${NAME_SPACE}
# Stateful Set along with it's corresponding service resource
oc delete statefulset example-statefulset --namespace=${NAME_SPACE}
oc delete service example-service --namespace=${NAME_SPACE}
}
# Check for a specific argument to call the function
if [ "$1" == "uninstall" ]; then
helm uninstall ${RELEASE_NAME} -n ${NAME_SPACE}
# Remove the Persistant Volume Claim and it's associated Volume Claim for the PSQL Database
oc delete pvc data-redhat-developer-hub-postgresql-0 --namespace=${NAME_SPACE}
oc delete pvc data-backstage-postgresql-0 --namespace=${NAME_SPACE}
if [ "$2" == "--all" ]; then
uninstall_all_resources
fi
exit 0
fi
# install_oc
# install_helm
# oc login ${CLUSTER_API} --username ${CLUSTER_USERNAME} --password ${CLUSTER_PASSWORD}
PWD="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
echo "$PWD"
# Create Namespace and switch to it
oc new-project ${NAME_SPACE}
if [ $? -ne 0 ]; then
# Switch to it if it already exists
oc project ${NAME_SPACE}
fi
# # Change the namespace of the resources to the one namespace set above
sed -i "s/namespace:.*/namespace: $NAME_SPACE/g" $PWD/resources/service-account-rhdh.yaml
sed -i "s/namespace:.*/namespace: $NAME_SPACE/g" $PWD/resources/cluster-roles/cluster-role-binding-k8s.yaml
sed -i "s/namespace:.*/namespace: $NAME_SPACE/g" $PWD/resources/cluster-roles/cluster-role-binding-ocm.yaml
# Cluster Service Account
oc apply -f $PWD/resources/service-account-rhdh.yaml --namespace=${NAME_SPACE}
oc apply -f $PWD/auth/service-account-rhdh-secret.yaml --namespace=${NAME_SPACE}
# ClusterRoles and ClusterRoleBindings
oc apply -f $PWD/resources/cluster-roles/cluster-role-k8s.yaml --namespace=${NAME_SPACE}
oc apply -f $PWD/resources/cluster-roles/cluster-role-binding-k8s.yaml --namespace=${NAME_SPACE}
oc apply -f $PWD/resources/cluster-roles/cluster-role-ocm.yaml --namespace=${NAME_SPACE}
oc apply -f $PWD/resources/cluster-roles/cluster-role-binding-ocm.yaml --namespace=${NAME_SPACE}
# Pipelines to test Tekton plugin
oc apply -f $PWD/resources/pipelines/hello-world-pipeline.yaml --namespace=${NAME_SPACE}
oc apply -f $PWD/resources/pipelines/hello-world-pipeline-run.yaml --namespace=${NAME_SPACE}
# Upload Jobs and Cronjobs
oc apply -f $PWD/resources/jobs/cron-job.yaml --namespace=${NAME_SPACE}
oc apply -f $PWD/resources/jobs/pi-job.yaml --namespace=${NAME_SPACE}
# Upload Daemon Set
oc apply -f $PWD/resources/daemon-sets/daemon-set.yaml --namespace=${NAME_SPACE}
# # Upload Deployment
oc apply -f $PWD/resources/deployments/backstage-test.yaml --namespace=${NAME_SPACE}
# Upload Stateful Set along with it's corresponding service resource
oc apply -f $PWD/resources/stateful-sets/stateful-set.yaml --namespace=${NAME_SPACE}
# RBAC policies
oc apply -f $PWD/resources/rbac-policies.yaml --namespace=${NAME_SPACE}
# # obtain K8S_CLUSTER_NAME, K8S_CLUSTER_URL, K8S_CLUSTER_TOKEN and add them to secrets-rhdh-secrets.yaml
oc get secret rhdh-k8s-plugin-secret --namespace=${NAME_SPACE} -o yaml > $PWD/auth/service-account-rhdh-token.yaml
TOKEN=$(grep 'token:' $PWD/auth/service-account-rhdh-token.yaml | awk '{print $2}')
sed -i "s/K8S_CLUSTER_TOKEN:.*/K8S_CLUSTER_TOKEN: $TOKEN/g" $PWD/auth/secrets-rhdh-secrets.yaml
oc apply -f $PWD/auth/quay-pull-secret.yaml --namespace=${NAME_SPACE}
oc apply -f $PWD/auth/secrets-rhdh-secrets.yaml --namespace=${NAME_SPACE}
oc apply -f $PWD/resources/rhdh-configmap.yaml --namespace=${NAME_SPACE}
oc apply -f $PWD/auth/secrets-rhdh-secrets.yaml --namespace=${NAME_SPACE}
# Keycloak secrets
oc apply -f $PWD/auth/keycloak-secrets.yaml --namespace=${NAME_SPACE}
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
echo "$DIR"
# Note the quay-pull-secret.yaml has metadata.name = rhdh-pull-secret
oc create -f $DIR/auth/quay-pull-secret.yaml --namespace=${NAME_SPACE}
# PREREQ #2 : Add required helm repos if not added yet
add_helm_repo
# helm show values openshift-helm-charts/redhat-developer-hub > ${HELM_CHART_VALUE_FILE_NAME}
# helm upgrade -i ${RELEASE_NAME} -n ${NAME_SPACE} -f ${HELM_CHART_VALUE_FILE_NAME} openshift-helm-charts/redhat-developer-hub --set global.clusterRouterBase=${HELM_CHART_VALUE_FILE_NAME}
# helm upgrade -i ${RELEASE_NAME} -n ${NAME_SPACE} openshift-helm-charts/redhat-developer-hub --set global.clusterRouterBase=${CLUSTER_ROUTER_BASE}
# Alternatively, helm install by updating 'clusterRouterBase' values in values-rhdh.yaml
# update values-rhdh.yaml before helm chart install with values-rhdh.yaml
# NOTE: sed command on macOS requires to pass '.bak' with -i option
# sed -i '.bak' 's|clusterRouterBase: apps.example.com|clusterRouterBase: '"${CLUSTER_ROUTER_BASE}"'|' values-rhdh.yaml
# sed -i 's|clusterRouterBase: apps.example.com|clusterRouterBase: '"${CLUSTER_ROUTER_BASE}"'|' values-rhdh.yaml
# helm upgrade -i ${RELEASE_NAME} -n ${NAME_SPACE} -f ${HELM_CHART_VALUE_FILE_NAME} openshift-helm-charts/redhat-developer-hub
# if [ -f ${HELM_CHART_VALUE_FILE_NAME} ]; then
# echo "Deleting ${HELM_CHART_VALUE_FILE_NAME}"
# rm ${HELM_CHART_VALUE_FILE_NAME}
# fi
# helm show values openshift-helm-charts/redhat-developer-hub > ${HELM_CHART_VALUE_FILE_NAME}
# helm upgrade -i ${RELEASE_NAME} -n ${NAME_SPACE} -f ${HELM_CHART_VALUE_FILE_NAME} openshift-helm-charts/redhat-developer-hub --set global.clusterRouterBase=${CLUSTER_ROUTER_BASE}
# uninstall 'rhdh' helm chart from 'backstage' namespace
# uninstall_helm_chart
# helm upgrade -i ${RELEASE_NAME} -n ${NAME_SPACE} -f ${HELM_CHART_VALUE_FILE_NAME} openshift-helm-charts/redhat-developer-hub
helm upgrade -i ${RELEASE_NAME} -n ${NAME_SPACE} -f ${HELM_CHART_VALUE_FILE_NAME} rhdh-chart/backstage
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment