Skip to content

Commit

Permalink
RHIDP-512 - adapt new helm chart (janus-idp#753)
Browse files Browse the repository at this point in the history
* adapt to use new helm chart

Signed-off-by: Joseph Kim <joskim@redhat.com>

* adapt to use new helm chart

Signed-off-by: Joseph Kim <joskim@redhat.com>

* adapt to use new helm chart

Signed-off-by: Joseph Kim <joskim@redhat.com>

* adapt to use new helm chart

Signed-off-by: Joseph Kim <joskim@redhat.com>

---------

Signed-off-by: Joseph Kim <joskim@redhat.com>
Co-authored-by: Tomas Kral <tkral@redhat.com>
  • Loading branch information
josephca and kadel committed Nov 13, 2023
1 parent 79a7160 commit 28464d2
Show file tree
Hide file tree
Showing 3 changed files with 280 additions and 235 deletions.
243 changes: 119 additions & 124 deletions .ibm/pipelines/openshift-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,51 +2,39 @@

set -e

function cleanup {
echo "Cleaning up before exiting"
helm uninstall ${RELEASE_NAME} -n ${NAME_SPACE}
oc delete namespace ${NAME_SPACE}
rm -rf ~/tmpbin
# Variáveis globais
LOGFILE="pr-${GIT_PR_NUMBER}-openshift-tests-${BUILD_NUMBER}"
TEST_NAME="backstage-showcase Tests"
DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"

cleanup() {
echo "Cleaning up before exiting"
helm uninstall ${RELEASE_NAME} -n ${NAME_SPACE}
oc delete namespace ${NAME_SPACE}
rm -rf ~/tmpbin
}

trap cleanup EXIT

add_helm_repos() {
# check installed helm version
helm version
helm version

# Check if the bitnami repository already exists
if ! helm repo list | grep -q "^bitnami"; then
helm repo add bitnami https://charts.bitnami.com/bitnami
else
echo "Repository bitnami already exists - updating repository instead."
fi
declare -a repos=("bitnami=https://charts.bitnami.com/bitnami" "backstage=https://backstage.github.io/charts" "janus-idp=https://janus-idp.github.io/helm-backstage" "${HELM_REPO_NAME}=${HELM_REPO_URL}")

# Check if the backstage repository already exists
if ! helm repo list | grep -q "^backstage"; then
helm repo add backstage https://backstage.github.io/charts
else
echo "Repository backstage already exists - updating repository instead."
fi

# Check if the backstage repository already exists
if ! helm repo list | grep -q "^janus-idp"; then
helm repo add janus-idp https://janus-idp.github.io/helm-backstage
else
echo "Repository janus-idp already exists - updating repository instead."
fi

# Check if the repository already exists
if ! helm repo list | grep -q "^${HELM_REPO_NAME}"; then
helm repo add "${HELM_REPO_NAME}" "${HELM_REPO_URL}"
for repo in "${repos[@]}"; do
key="${repo%%=*}"
value="${repo##*=}"

if ! helm repo list | grep -q "^$key"; then
helm repo add "$key" "$value"
else
echo "Repository ${HELM_REPO_NAME} already exists - updating repository instead."
echo "Repository $key already exists - updating repository instead."
fi
done

helm repo update
helm repo update
}

# install the latest ibmcloud cli on Linux
install_ibmcloud() {
if [[ -x "$(command -v ibmcloud)" ]]; then
echo "ibmcloud is already installed."
Expand Down Expand Up @@ -82,129 +70,136 @@ install_helm() {
fi
}

LOGFILE="pr-${GIT_PR_NUMBER}-openshift-tests-${BUILD_NUMBER}"
echo "Log file: ${LOGFILE}"
TEST_NAME="backstage-showcase Tests"

source ./.ibm/pipelines/functions.sh

skip_if_only

# install ibmcloud
install_ibmcloud

ibmcloud version
ibmcloud config --check-version=false
ibmcloud plugin install -f container-registry
ibmcloud plugin install -f kubernetes-service
configure_namespace() {
if oc get namespace ${NAME_SPACE} >/dev/null 2>&1; then
echo "Namespace ${NAME_SPACE} already exists! refreshing namespace"
oc delete namespace ${NAME_SPACE}
fi
oc create namespace ${NAME_SPACE}
oc config set-context --current --namespace=${NAME_SPACE}
}

# Using pipeline configuration - environment properties
ibmcloud login -r "${IBM_REGION}" -g "${IBM_RSC_GROUP}" --apikey "${SERVICE_ID_API_KEY}"
ibmcloud oc cluster config --cluster "${OPENSHIFT_CLUSTER_ID}"
apply_yaml_files() {
local dir=$1

install_oc
# Update namespace and other configurations in YAML files
local files=("$dir/resources/service_account/service-account-rhdh.yaml"
"$dir/resources/cluster_role_binding/cluster-role-binding-k8s.yaml"
"$dir/resources/cluster_role_binding/cluster-role-binding-ocm.yaml")

for file in "${files[@]}"; do
sed -i "s/namespace:.*/namespace: $NAME_SPACE/g" "$file"
done

oc version --client
# oc login -u apikey -p "${SERVICE_ID_API_KEY}" --server="${IBM_OPENSHIFT_ENDPOINT}"
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
echo "$DIR"
# Add additional configurations
sed -i "s/backstage.io\/kubernetes-id:.*/backstage.io\/kubernetes-id: $K8S_PLUGIN_ANNOTATION/g" "$dir/resources/deployment/deployment-test-app-component.yaml"

oc login --token=${K8S_CLUSTER_TOKEN} --server=${K8S_CLUSTER_URL}
for key in GITHUB_APP_APP_ID GITHUB_APP_CLIENT_ID GITHUB_APP_PRIVATE_KEY GITHUB_APP_CLIENT_SECRET GITHUB_APP_WEBHOOK_URL GITHUB_APP_WEBHOOK_SECRET KEYCLOAK_URL KEYCLOAK_REALM KEYCLOAK_CLIENT_SECRET KEYCLOAK_CLIENT_ID; do
sed -i "s/$key:.*/$key: ${!key}/g" "$dir/auth/secrets-rhdh-secrets.yaml"
done

# refresh the name space if exists
if oc get namespace ${NAME_SPACE} > /dev/null 2>&1; then
echo "Namespace ${NAME_SPACE} already exists! refreshing namespace"
oc delete namespace ${NAME_SPACE}
fi
oc create namespace ${NAME_SPACE}
oc apply -f $dir/resources/service_account/service-account-rhdh.yaml --namespace=${NAME_SPACE}
oc apply -f $dir/auth/service-account-rhdh-secret.yaml --namespace=${NAME_SPACE}
oc apply -f $dir/auth/secrets-rhdh-secrets.yaml --namespace=${NAME_SPACE}
oc apply -f $dir/resources/deployment/deployment-test-app-component.yaml --namespace=${NAME_SPACE}
oc new-app https://github.com/janus-qe/test-backstage-customization-provider --namespace=${NAME_SPACE}
oc expose svc/test-backstage-customization-provider --namespace=${NAME_SPACE}
oc apply -f $dir/resources/cluster_role/cluster-role-k8s.yaml
oc apply -f $dir/resources/cluster_role_binding/cluster-role-binding-k8s.yaml
oc apply -f $dir/resources/cluster_role/cluster-role-ocm.yaml
oc apply -f $dir/resources/cluster_role_binding/cluster-role-binding-ocm.yaml

oc config set-context --current --namespace=${NAME_SPACE}
# obtain K8S_CLUSTER_NAME, K8S_CLUSTER_API_SERVER_URL and add them to secrets-rhdh-secrets.yaml
# K8S_SERVICE_ACCOUNT_TOKEN will be replaced
oc get secret rhdh-k8s-plugin-secret -o yaml >$dir/auth/service-account-rhdh-token.yaml

install_helm
TOKEN=$(grep 'token:' $dir/auth/service-account-rhdh-token.yaml | awk '{print $2}')

cd $DIR
sed -i "s/K8S_SERVICE_ACCOUNT_TOKEN:.*/K8S_SERVICE_ACCOUNT_TOKEN: $TOKEN/g" $dir/auth/secrets-rhdh-secrets.yaml

# Change the namespace of the resources to the one namespace set above
sed -i "s/namespace:.*/namespace: $NAME_SPACE/g" $DIR/resources/service_account/service-account-rhdh.yaml
sed -i "s/namespace:.*/namespace: $NAME_SPACE/g" $DIR/resources/cluster_role_binding/cluster-role-binding-k8s.yaml
sed -i "s/namespace:.*/namespace: $NAME_SPACE/g" $DIR/resources/cluster_role_binding/cluster-role-binding-ocm.yaml
# Cleanup temp file
rm $dir/auth/service-account-rhdh-token.yaml

sed -i "s/backstage.io\/kubernetes-id:.*/backstage.io\/kubernetes-id: $K8S_PLUGIN_ANNOTATION/g" $DIR/resources/deployment/deployment-test-app-component.yaml
# oc apply -f $dir/auth/rhdh-quay-pull-secret.yaml --namespace=${NAME_SPACE}

sed -i "s/GITHUB_APP_APP_ID:.*/GITHUB_APP_APP_ID: $GITHUB_APP_APP_ID/g" $DIR/auth/secrets-rhdh-secrets.yaml
sed -i "s/GITHUB_APP_CLIENT_ID:.*/GITHUB_APP_CLIENT_ID: $GITHUB_APP_CLIENT_ID/g" $DIR/auth/secrets-rhdh-secrets.yaml
sed -i "s/GITHUB_APP_PRIVATE_KEY:.*/GITHUB_APP_PRIVATE_KEY: $GITHUB_APP_PRIVATE_KEY/g" $DIR/auth/secrets-rhdh-secrets.yaml
sed -i "s/GITHUB_APP_CLIENT_SECRET:.*/GITHUB_APP_CLIENT_SECRET: $GITHUB_APP_CLIENT_SECRET/g" $DIR/auth/secrets-rhdh-secrets.yaml
sed -i "s/GITHUB_APP_WEBHOOK_URL:.*/GITHUB_APP_WEBHOOK_URL: $GITHUB_APP_WEBHOOK_URL/g" $DIR/auth/secrets-rhdh-secrets.yaml
sed -i "s/GITHUB_APP_WEBHOOK_SECRET:.*/GITHUB_APP_WEBHOOK_SECRET: $GITHUB_APP_WEBHOOK_SECRET/g" $DIR/auth/secrets-rhdh-secrets.yaml
# re-apply with the updated cluster service account token
oc apply -f $dir/auth/secrets-rhdh-secrets.yaml --namespace=${NAME_SPACE}
oc apply -f $dir/resources/config_map/configmap-app-config-rhdh.yaml --namespace=${NAME_SPACE}
}

oc apply -f $DIR/resources/service_account/service-account-rhdh.yaml --namespace=${NAME_SPACE}
oc apply -f $DIR/auth/service-account-rhdh-secret.yaml --namespace=${NAME_SPACE}
oc apply -f $DIR/auth/secrets-rhdh-secrets.yaml --namespace=${NAME_SPACE}
run_tests() {
cd $DIR/../../e2e-test
yarn install

oc apply -f $DIR/resources/deployment/deployment-test-app-component.yaml --namespace=${NAME_SPACE}
Xvfb :99 &
export DISPLAY=:99

oc new-app https://github.com/janus-qe/test-backstage-customization-provider --namespace=${NAME_SPACE}
oc expose svc/test-backstage-customization-provider --namespace=${NAME_SPACE}
(
set -e
echo Using PR container image: pr-${GIT_PR_NUMBER}-${SHORT_SHA}
yarn run cypress:run --config baseUrl="https://${RELEASE_NAME}-${NAME_SPACE}.${K8S_CLUSTER_ROUTER_BASE}"
) |& tee "/tmp/${LOGFILE}"

oc apply -f $DIR/resources/cluster_role/cluster-role-k8s.yaml
oc apply -f $DIR/resources/cluster_role_binding/cluster-role-binding-k8s.yaml
oc apply -f $DIR/resources/cluster_role/cluster-role-ocm.yaml
oc apply -f $DIR/resources/cluster_role_binding/cluster-role-binding-ocm.yaml
RESULT=${PIPESTATUS[0]}

# obtain K8S_CLUSTER_NAME, K8S_CLUSTER_API_SERVER_URL and add them to secrets-rhdh-secrets.yaml
# K8S_SERVICE_ACCOUNT_TOKEN will be replaced
oc get secret rhdh-k8s-plugin-secret -o yaml > $DIR/auth/service-account-rhdh-token.yaml
pkill Xvfb

TOKEN=$(grep 'token:' $DIR/auth/service-account-rhdh-token.yaml | awk '{print $2}')
save_logs "${LOGFILE}" "${TEST_NAME}" ${RESULT}

sed -i "s/K8S_SERVICE_ACCOUNT_TOKEN:.*/K8S_SERVICE_ACCOUNT_TOKEN: $TOKEN/g" $DIR/auth/secrets-rhdh-secrets.yaml
exit ${RESULT}
}

# Cleanup temp file
rm $DIR/auth/service-account-rhdh-token.yaml
check_backstage_running() {
# Check if Backstage is up and running
BACKSTAGE_URL_RESPONSE=$(curl -Is "https://${RELEASE_NAME}-${NAME_SPACE}.${K8S_CLUSTER_ROUTER_BASE}" | head -n 1)
echo "$BACKSTAGE_URL_RESPONSE"
}

# oc apply -f $DIR/auth/rhdh-quay-pull-secret.yaml --namespace=${NAME_SPACE}
main() {
echo "Log file: ${LOGFILE}"

# re-apply with the updated cluster service account token
oc apply -f $DIR/auth/secrets-rhdh-secrets.yaml --namespace=${NAME_SPACE}
oc apply -f $DIR/resources/config_map/configmap-app-config-rhdh.yaml --namespace=${NAME_SPACE}
source ./.ibm/pipelines/functions.sh
skip_if_only

add_helm_repos
DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"

GIT_PR_RESPONSE=$(curl -s "https://api.github.com/repos/${GITHUB_ORG_NAME}/${GITHUB_REPOSITORY_NAME}/pulls/${GIT_PR_NUMBER}")
LONG_SHA=$(echo "$GIT_PR_RESPONSE" | jq -r '.head.sha')
SHORT_SHA=$(git rev-parse --short ${LONG_SHA})
install_ibmcloud
ibmcloud version
ibmcloud config --check-version=false
ibmcloud plugin install -f container-registry
ibmcloud plugin install -f kubernetes-service
ibmcloud login -r "${IBM_REGION}" -g "${IBM_RSC_GROUP}" --apikey "${SERVICE_ID_API_KEY}"
ibmcloud oc cluster config --cluster "${OPENSHIFT_CLUSTER_ID}"

echo "Tag name with short SHA: pr-${GIT_PR_NUMBER}-${SHORT_SHA}"
install_oc
oc version --client
oc login --token=${K8S_CLUSTER_TOKEN} --server=${K8S_CLUSTER_URL}

helm upgrade -i ${RELEASE_NAME} -n ${NAME_SPACE} ${HELM_REPO_NAME}/${HELM_IMAGE_NAME} -f $DIR/value_files/${HELM_CHART_VALUE_FILE_NAME} --set global.clusterRouterBase=${K8S_CLUSTER_ROUTER_BASE} --set upstream.backstage.image.tag=pr-${GIT_PR_NUMBER}-${SHORT_SHA}
configure_namespace
install_helm

echo "Waiting for backstage deployment..."
sleep 120
cd $DIR
apply_yaml_files $DIR

echo "Display pods for verification..."
oc get pods -n ${NAME_SPACE}
add_helm_repos

# Check if Backstage is up and running
BACKSTAGE_URL_RESPONSE=$(curl -Is "https://${RELEASE_NAME}-${NAME_SPACE}.${K8S_CLUSTER_ROUTER_BASE}" | head -n 1)
echo "$BACKSTAGE_URL_RESPONSE"
GIT_PR_RESPONSE=$(curl -s "https://api.github.com/repos/${GITHUB_ORG_NAME}/${GITHUB_REPOSITORY_NAME}/pulls/${GIT_PR_NUMBER}")
LONG_SHA=$(echo "$GIT_PR_RESPONSE" | jq -r '.head.sha')
SHORT_SHA=$(git rev-parse --short ${LONG_SHA})

cd $DIR/../../e2e-test
yarn install
echo "Tag name with short SHA: pr-${GIT_PR_NUMBER}-${SHORT_SHA}"

Xvfb :99 &
export DISPLAY=:99
helm upgrade -i ${RELEASE_NAME} -n ${NAME_SPACE} ${HELM_REPO_NAME}/${HELM_IMAGE_NAME} --version ${CHART_VERSION} -f $DIR/value_files/${HELM_CHART_VALUE_FILE_NAME} --set global.clusterRouterBase=${K8S_CLUSTER_ROUTER_BASE} --set upstream.backstage.image.tag=pr-${GIT_PR_NUMBER}-${SHORT_SHA}

(
set -e
echo Using PR container image: pr-${GIT_PR_NUMBER}
yarn run cypress:run --config baseUrl="https://${RELEASE_NAME}-${NAME_SPACE}.${K8S_CLUSTER_ROUTER_BASE}"
) |& tee "/tmp/${LOGFILE}"
echo "Waiting for backstage deployment..."
sleep 500

RESULT=${PIPESTATUS[0]}
echo "Display pods for verification..."
oc get pods -n ${NAME_SPACE}

pkill Xvfb
check_backstage_running

save_logs "${LOGFILE}" "${TEST_NAME}" ${RESULT}
run_tests
}

exit ${RESULT}
main
Loading

0 comments on commit 28464d2

Please sign in to comment.