|  | 
|  | 1 | +#!/usr/bin/env bash | 
|  | 2 | +# https://github.com/olivergondza/bash-strict-mode | 
|  | 3 | +set -eEuo pipefail | 
|  | 4 | +trap 's=$?; echo >&2 "$0: Error on line "$LINENO": $BASH_COMMAND"; exit $s' ERR | 
|  | 5 | + | 
|  | 6 | +DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null && pwd)" | 
|  | 7 | + | 
|  | 8 | +SUBSCRIPTION_NAME="gitops-operator" | 
|  | 9 | +OPERATOR_NAMESPACE="openshift-gitops-operator" | 
|  | 10 | +VERSION="$(git describe --tags --dirty | sed 's/^v//')-$(date '+%Y%m%d-%H%M%S')" | 
|  | 11 | + | 
|  | 12 | +function build_bundles() { | 
|  | 13 | +    export VERSION | 
|  | 14 | +    make build docker-build docker-push bundle bundle-build bundle-push catalog-build catalog-push | 
|  | 15 | +} | 
|  | 16 | + | 
|  | 17 | +function install_catalog_source() { | 
|  | 18 | +    oc apply -f - <<EOF | 
|  | 19 | +apiVersion: operators.coreos.com/v1alpha1 | 
|  | 20 | +kind: CatalogSource | 
|  | 21 | +metadata: | 
|  | 22 | +    name: devel-gitops-service-source | 
|  | 23 | +    namespace: openshift-marketplace | 
|  | 24 | +spec: | 
|  | 25 | +    displayName: 'GITOPS DEVEL' | 
|  | 26 | +    publisher: 'GITOPS DEVEL' | 
|  | 27 | +    sourceType: grpc | 
|  | 28 | +    image: '${IMAGE}-catalog:v${VERSION}' | 
|  | 29 | +EOF | 
|  | 30 | + | 
|  | 31 | +    local yaml='' | 
|  | 32 | +    for i in $(seq 1 10); do | 
|  | 33 | +        echo >&2 "Waiting for catalog source to be ready... ($i)" | 
|  | 34 | +        yaml=$(oc get -o yaml CatalogSource devel-gitops-service-source -n openshift-marketplace --ignore-not-found) | 
|  | 35 | +        if [ "$(yq '.status.connectionState.lastObservedState' <<< "$yaml")" == "READY" ]; then | 
|  | 36 | +            echo >&2  "Catalog source is ready" | 
|  | 37 | +            return 0 | 
|  | 38 | +        fi | 
|  | 39 | + | 
|  | 40 | +        sleep 6 | 
|  | 41 | +    done | 
|  | 42 | + | 
|  | 43 | +    echo >&2 "Timeout waiting for catalog source to be ready. Current state:" | 
|  | 44 | +    echo >&2 "$yaml" | 
|  | 45 | +    return 1 | 
|  | 46 | +} | 
|  | 47 | + | 
|  | 48 | +function delete_operator() { | 
|  | 49 | +    oc delete subscription "$SUBSCRIPTION_NAME" -n "$OPERATOR_NAMESPACE" --ignore-not-found | 
|  | 50 | +    readarray -t ogs < <(oc get operatorgroup -n "$OPERATOR_NAMESPACE" -o name --ignore-not-found) | 
|  | 51 | +    if [[ ${#ogs[@]} -gt 0 ]]; then | 
|  | 52 | +        oc delete -n "$OPERATOR_NAMESPACE" "${ogs[@]}" | 
|  | 53 | +    fi | 
|  | 54 | +    oc delete csv -n "$OPERATOR_NAMESPACE" -l "operators.coreos.com/${SUBSCRIPTION_NAME}.${OPERATOR_NAMESPACE}" --ignore-not-found | 
|  | 55 | + | 
|  | 56 | +    readarray -t pods < <(oc get pod -n "$OPERATOR_NAMESPACE" -o name --ignore-not-found) | 
|  | 57 | +    if [[ ${#pods[@]} -gt 0 ]]; then | 
|  | 58 | +        # Pods might stop existing before query and delete, so ignoring not found ones | 
|  | 59 | +        oc delete -n "$OPERATOR_NAMESPACE" "${pods[@]}" --ignore-not-found | 
|  | 60 | +    fi | 
|  | 61 | +    oc delete ns "$OPERATOR_NAMESPACE" --ignore-not-found | 
|  | 62 | +} | 
|  | 63 | + | 
|  | 64 | +function install_operator() { | 
|  | 65 | +    oc create ns "$OPERATOR_NAMESPACE" | 
|  | 66 | +    oc apply -f - <<EOF | 
|  | 67 | +apiVersion: operators.coreos.com/v1 | 
|  | 68 | +kind: OperatorGroup | 
|  | 69 | +metadata: | 
|  | 70 | +  name: openshift-gitops-operator-devel | 
|  | 71 | +  namespace: $OPERATOR_NAMESPACE | 
|  | 72 | +spec: | 
|  | 73 | +  upgradeStrategy: Default | 
|  | 74 | +--- | 
|  | 75 | +apiVersion: operators.coreos.com/v1alpha1 | 
|  | 76 | +kind: Subscription | 
|  | 77 | +metadata: | 
|  | 78 | +  name: $SUBSCRIPTION_NAME | 
|  | 79 | +  namespace: $OPERATOR_NAMESPACE | 
|  | 80 | +spec: | 
|  | 81 | +  channel: latest | 
|  | 82 | +  name: $SUBSCRIPTION_NAME | 
|  | 83 | +  installPlanApproval: Automatic | 
|  | 84 | +  source: devel-gitops-service-source | 
|  | 85 | +  sourceNamespace: openshift-marketplace | 
|  | 86 | +EOF | 
|  | 87 | + | 
|  | 88 | +    local out='' | 
|  | 89 | +    for i in $(seq 1 10); do | 
|  | 90 | +        echo >&2 "Waiting for operator to start... ($i)" | 
|  | 91 | +        out=$(oc get pods -n "$OPERATOR_NAMESPACE" --ignore-not-found --no-headers) | 
|  | 92 | +        echo "$out" | 
|  | 93 | +        if [[ "$out" =~ "Running" ]]; then | 
|  | 94 | +            echo >&2 "Operator is ready" | 
|  | 95 | +            return 0 | 
|  | 96 | +        fi | 
|  | 97 | +        sleep 6 | 
|  | 98 | +    done | 
|  | 99 | + | 
|  | 100 | +    echo >&2 "Timeout waiting for operator to start. Current state:" | 
|  | 101 | +    echo >&2 "$out" | 
|  | 102 | +    oc events -n "$OPERATOR_NAMESPACE" >&2 | 
|  | 103 | +    return 1 | 
|  | 104 | +} | 
|  | 105 | + | 
|  | 106 | +function main() { | 
|  | 107 | +    if [ ! -v IMAGE ]; then | 
|  | 108 | +        echo >&2 "Variable IMAGE not specified" | 
|  | 109 | +        exit 1 | 
|  | 110 | +    fi | 
|  | 111 | + | 
|  | 112 | +    echo >&2 "Deploying version $VERSION" | 
|  | 113 | + | 
|  | 114 | +    build_bundles | 
|  | 115 | +    install_catalog_source | 
|  | 116 | +    delete_operator | 
|  | 117 | +    install_operator | 
|  | 118 | +} | 
|  | 119 | + | 
|  | 120 | +main "$@" | 
0 commit comments