Skip to content

Instantly share code, notes, and snippets.

@praveen4g0
Created January 27, 2023 06:57
Show Gist options
  • Save praveen4g0/13266fba5635c6af7ddc3406e88d13cf to your computer and use it in GitHub Desktop.
Save praveen4g0/13266fba5635c6af7ddc3406e88d13cf to your computer and use it in GitHub Desktop.
#!/bin/bash
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd)"
NIGHTLY_BUILD_TAG=${NIGHTLY_BUILD_TAG:-""}
BUNDLE_IMAGE=brew.registry.redhat.io/rh-osbs/openshift-gitops-1-gitops-operator-bundle:v$NIGHTLY_BUILD_TAG
echo -e "Bundle image: $BUNDLE_IMAGE"
MANAGED_GITOPS_COMPONENT_IMAGE=brew.registry.redhat.io/rh-osbs/openshift-gitops-1-managed-service-rhel8:v$NIGHTLY_BUILD_TAG
echo -e "Managed service component image: $MANAGED_GITOPS_COMPONENT_IMAGE"
USERNAME=${USERNAME:-"|20b80f70-70f6-11e9-8733-001a4a0b004e.kvcr.e09cd7"}
PASSWORD=${PASSWORD:-""}
test -z "${PASSWORD}" && {
echo "PASSWORD for mirror registry env variable is required"
exit 1
}
test -z "${NIGHTLY_BUILD_TAG}" && {
echo "NIGHTLY_BUILD_TAG env variable is required, cannot be empty"
exit 1
}
function make_banner() {
local msg="$1$1$1$1 $2 $1$1$1$1"
local border="${msg//[-0-9A-Za-z _.,\/()]/$1}"
echo -e "${border}\n${msg}\n${border}"
}
# Simple header for logging purposes.
function header() {
local upper="$(echo $1 | tr a-z A-Z)"
make_banner "=" "${upper}"
}
function wait_until_pods_running() {
echo -n "Waiting until all pods in namespace $1 are up"
for i in {1..150}; do # timeout after 5 minutes
local pods="$(kubectl get pods --no-headers -n $1 2>/dev/null)"
# All pods must be running
local not_running=$(echo "${pods}" | grep -v Running | grep -v Completed | wc -l)
if [[ -n "${pods}" && ${not_running} -eq 0 ]]; then
local all_ready=1
while read pod ; do
local status=(`echo -n ${pod} | cut -f2 -d' ' | tr '/' ' '`)
# All containers must be ready
[[ -z ${status[0]} ]] && all_ready=0 && break
[[ -z ${status[1]} ]] && all_ready=0 && break
[[ ${status[0]} -lt 1 ]] && all_ready=0 && break
[[ ${status[1]} -lt 1 ]] && all_ready=0 && break
[[ ${status[0]} -ne ${status[1]} ]] && all_ready=0 && break
done <<< $(echo "${pods}" | grep -v Completed)
if (( all_ready )); then
echo -e "\nAll pods are up:\n${pods}"
return 0
fi
fi
echo -n "."
sleep 2
done
echo -e "\n\nERROR: timeout waiting for pods to come up\n${pods}"
return 1
}
function wait_until_object_exist() {
local KUBECTL_ARGS="get $1 $2"
local DESCRIPTION="$1 $2"
if [[ -n $3 ]]; then
KUBECTL_ARGS="get -n $3 $1 $2"
DESCRIPTION="$1 $3/$2"
fi
echo -n "Waiting until ${DESCRIPTION} exist"
for i in {1..150}; do # timeout after 5 minutes
if kubectl ${KUBECTL_ARGS} > /dev/null 2>&1; then
echo -e "\n${DESCRIPTION} exist"
return 0
fi
echo -n "."
sleep 2
done
echo -e "\n\nERROR: timeout waiting for ${DESCRIPTION} to exist"
kubectl ${KUBECTL_ARGS}
return 1
}
function wait_until_object_doesnt_exist() {
local KUBECTL_ARGS="get $1 $2"
local DESCRIPTION="$1 $2"
if [[ -n $3 ]]; then
KUBECTL_ARGS="get -n $3 $1 $2"
DESCRIPTION="$1 $3/$2"
fi
echo -n "Waiting until ${DESCRIPTION} doesn't exist"
for i in {1..150}; do # timeout after 5 minutes
if ! kubectl ${KUBECTL_ARGS} > /dev/null 2>&1; then
echo -e "\n${DESCRIPTION} dosen't exist"
return 0
fi
echo -n "."
sleep 2
done
echo -e "\n\nERROR: timeout waiting for ${DESCRIPTION} to doesn't exist"
kubectl ${KUBECTL_ARGS}
return 1
}
function fail_test() {
set_test_return_code 1
[[ -n $1 ]] && echo "ERROR: $1"
exit 1
}
function set_test_return_code() {
echo -n "$1"
}
function success() {
set_test_return_code 0
echo "**************************************"
echo "*** E2E TESTS PASSED ***"
echo "**************************************"
exit 0
}
function reset() {
rm -rf authfile
}
trap reset ERR EXIT
oc get secret/pull-secret -n openshift-config -o json | jq -r '.data.".dockerconfigjson"' |
base64 -d > authfile
podman login --authfile authfile --username $USERNAME --password $PASSWORD brew.registry.redhat.io
oc set data secret/pull-secret -n openshift-config --from-file=.dockerconfigjson=authfile
echo -e "Creating image content source policy"
cat <<EOF | oc apply -f -
apiVersion: operator.openshift.io/v1alpha1
kind: ImageContentSourcePolicy
metadata:
name: brew-registry
spec:
repositoryDigestMirrors:
- mirrors:
- brew.registry.redhat.io
source: registry.redhat.io
- mirrors:
- brew.registry.redhat.io
source: registry.stage.redhat.io
- mirrors:
- brew.registry.redhat.io
source: registry-proxy.engineering.redhat.com
EOF
echo -e "Waiting for nodes to get restarted"
machines=$(oc get machineconfigpool -o=jsonpath='{.items[*].metadata.name}{" "}')
sleep 60
for machine in ${machines}; do
echo -e "Waiting for machineconfigpool \"$machine\" to be in state Updated=true && Updating=false"
sleep 5
oc wait --for=condition=Updated=True -n openshift-operators machineconfigpool $machine --timeout=5m && \
oc wait --for=condition=Updating=False -n openshift-operators machineconfigpool $machine --timeout=5m
done
echo -e "Disabling default catalog sources"
oc patch operatorhub.config.openshift.io/cluster -p='{"spec":{"disableAllDefaultSources":true}}' --type=merge
sleep 5
#Run operator-bundle in background
operator-sdk run bundle -n openshift-operators $BUNDLE_IMAGE
#wait for gitops operator to install successfully
wait_until_pods_running "openshift-operators" || fail_test "openshift gitops Operator controller did not come up"
echo ">> Wait for GitopsService creation"
wait_until_object_exist "gitopsservices.pipelines.openshift.io" "cluster" "openshift-gitops" || fail_test "gitops service haven't created yet"
wait_until_pods_running "openshift-gitops" || fail_test "argocd controller did not come up"
#Make sure that everything is cleaned up in the current namespace.
for res in applications applicationsets appprojects appprojects; do
oc delete --ignore-not-found=true ${res}.argoproj.io --all
done
# install managed gitops
git clone https://github.com/redhat-appstudio/infra-deployments.git $PWD/temp
kustomize build $PWD/temp/components/gitops | COMMON_IMAGE=$MANAGED_GITOPS_COMPONENT_IMAGE envsubst | kubectl apply -f -
# install postgress password
if ! kubectl get secret -n gitops gitops-postgresql-staging &>/dev/null; then
kubectl create secret generic gitops-postgresql-staging \
--namespace=gitops \
--from-literal=postgresql-password=$(openssl rand -base64 20)
fi
sleep 2
wait_until_pods_running "gitops" || fail_test "openshift managed gitops service pods did not come up"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment