|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +set -eux |
| 4 | + |
| 5 | +source hack/lib/test_lib.sh |
| 6 | +source hack/lib/image_lib.sh |
| 7 | + |
| 8 | +DEST_IMAGE="quay.io/example/memcached-operator:v0.0.2" |
| 9 | +ROOTDIR="$(pwd)" |
| 10 | +TMPDIR="$(mktemp -d)" |
| 11 | +trap_add 'rm -rf $TMPDIR' EXIT |
| 12 | + |
| 13 | +deploy_operator() { |
| 14 | + kubectl create -f "$OPERATORDIR/deploy/service_account.yaml" |
| 15 | + kubectl create -f "$OPERATORDIR/deploy/role.yaml" |
| 16 | + kubectl create -f "$OPERATORDIR/deploy/role_binding.yaml" |
| 17 | + kubectl create -f "$OPERATORDIR/deploy/crds/helm.example.com_memcacheds_crd.yaml" |
| 18 | + kubectl create -f "$OPERATORDIR/deploy/operator.yaml" |
| 19 | +} |
| 20 | + |
| 21 | +remove_operator() { |
| 22 | + kubectl delete --ignore-not-found=true -f "$OPERATORDIR/deploy/service_account.yaml" |
| 23 | + kubectl delete --ignore-not-found=true -f "$OPERATORDIR/deploy/role.yaml" |
| 24 | + kubectl delete --ignore-not-found=true -f "$OPERATORDIR/deploy/role_binding.yaml" |
| 25 | + kubectl delete --ignore-not-found=true -f "$OPERATORDIR/deploy/crds/helm.example.com_memcacheds_crd.yaml" |
| 26 | + kubectl delete --ignore-not-found=true -f "$OPERATORDIR/deploy/operator.yaml" |
| 27 | +} |
| 28 | + |
| 29 | +test_operator() { |
| 30 | + # kind has an issue with certain image registries (ex. redhat's), so use a |
| 31 | + # different test pod image. |
| 32 | + local metrics_test_image="fedora:latest" |
| 33 | + |
| 34 | + # wait for operator pod to run |
| 35 | + if ! timeout 1m kubectl rollout status deployment/memcached-operator; |
| 36 | + then |
| 37 | + kubectl logs deployment/memcached-operator |
| 38 | + exit 1 |
| 39 | + fi |
| 40 | + |
| 41 | + # verify that metrics service was created |
| 42 | + if ! timeout 20s bash -c -- "until kubectl get service/memcached-operator-metrics > /dev/null 2>&1; do sleep 1; done"; |
| 43 | + then |
| 44 | + echo "Failed to get metrics service" |
| 45 | + kubectl logs deployment/memcached-operator |
| 46 | + exit 1 |
| 47 | + fi |
| 48 | + |
| 49 | + # verify that the metrics endpoint exists |
| 50 | + if ! timeout 1m bash -c -- "until kubectl run --attach --rm --restart=Never test-metrics --image=$metrics_test_image -- curl -sfo /dev/null http://memcached-operator-metrics:8383/metrics; do sleep 1; done"; |
| 51 | + then |
| 52 | + echo "Failed to verify that metrics endpoint exists" |
| 53 | + kubectl logs deployment/memcached-operator |
| 54 | + exit 1 |
| 55 | + fi |
| 56 | + |
| 57 | + # create CR |
| 58 | + kubectl create -f deploy/crds/helm.example.com_v1alpha1_memcached_cr.yaml |
| 59 | + trap_add 'kubectl delete --ignore-not-found -f ${OPERATORDIR}/deploy/crds/helm.example.com_v1alpha1_memcached_cr.yaml' EXIT |
| 60 | + if ! timeout 1m bash -c -- 'until kubectl get memcachedes.helm.example.com example-memcached -o jsonpath="{..status.deployedRelease.name}" | grep "example-memcached"; do sleep 1; done'; |
| 61 | + then |
| 62 | + kubectl logs deployment/memcached-operator |
| 63 | + exit 1 |
| 64 | + fi |
| 65 | + |
| 66 | + # verify that the custom resource metrics endpoint exists |
| 67 | + if ! timeout 1m bash -c -- "until kubectl run --attach --rm --restart=Never test-cr-metrics --image=$metrics_test_image -- curl -sfo /dev/null http://memcached-operator-metrics:8686/metrics; do sleep 1; done"; |
| 68 | + then |
| 69 | + echo "Failed to verify that custom resource metrics endpoint exists" |
| 70 | + kubectl logs deployment/memcached-operator |
| 71 | + exit 1 |
| 72 | + fi |
| 73 | + |
| 74 | + release_name=$(kubectl get memcachedes.helm.example.com example-memcached -o jsonpath="{..status.deployedRelease.name}") |
| 75 | + memcached_deployment=$(kubectl get deployment -l "app.kubernetes.io/instance=${release_name}" -o jsonpath="{..metadata.name}") |
| 76 | + |
| 77 | + if ! timeout 1m kubectl rollout status deployment/${memcached_deployment}; |
| 78 | + then |
| 79 | + kubectl describe pods -l "app.kubernetes.io/instance=${release_name}" |
| 80 | + kubectl describe deployments ${memcached_deployment} |
| 81 | + kubectl logs deployment/memcached-operator |
| 82 | + exit 1 |
| 83 | + fi |
| 84 | + |
| 85 | + memcached_service=$(kubectl get service -l "app.kubernetes.io/instance=${release_name}" -o jsonpath="{..metadata.name}") |
| 86 | + kubectl get service ${memcached_service} |
| 87 | + |
| 88 | + # scale deployment replicas to 2 and verify the |
| 89 | + # deployment automatically scales back down to 1. |
| 90 | + kubectl scale deployment/${memcached_deployment} --replicas=2 |
| 91 | + if ! timeout 1m bash -c -- "until test \$(kubectl get deployment/${memcached_deployment} -o jsonpath='{..spec.replicas}') -eq 1; do sleep 1; done"; |
| 92 | + then |
| 93 | + kubectl describe pods -l "app.kubernetes.io/instance=${release_name}" |
| 94 | + kubectl describe deployments ${memcached_deployment} |
| 95 | + kubectl logs deployment/memcached-operator |
| 96 | + exit 1 |
| 97 | + fi |
| 98 | + |
| 99 | + # update CR to replicaCount=2 and verify the deployment |
| 100 | + # automatically scales up to 2 replicas. |
| 101 | + kubectl patch memcachedes.helm.example.com example-memcached -p '[{"op":"replace","path":"/spec/replicaCount","value":2}]' --type=json |
| 102 | + if ! timeout 1m bash -c -- "until test \$(kubectl get deployment/${memcached_deployment} -o jsonpath='{..spec.replicas}') -eq 2; do sleep 1; done"; |
| 103 | + then |
| 104 | + kubectl describe pods -l "app.kubernetes.io/instance=${release_name}" |
| 105 | + kubectl describe deployments ${memcached_deployment} |
| 106 | + kubectl logs deployment/memcached-operator |
| 107 | + exit 1 |
| 108 | + fi |
| 109 | + |
| 110 | + kubectl delete -f deploy/crds/helm.example.com_v1alpha1_memcached_cr.yaml --wait=true |
| 111 | + kubectl logs deployment/memcached-operator | grep "Uninstalled release" | grep "${release_name}" |
| 112 | +} |
| 113 | + |
| 114 | +# create and build the operator |
| 115 | +pushd "$TMPDIR" |
| 116 | +log=$(operator-sdk new memcached-operator \ |
| 117 | + --api-version=helm.example.com/v1alpha1 \ |
| 118 | + --kind=Nginx \ |
| 119 | + --type=helm \ |
| 120 | + --helm-chart=stable/memcached \ |
| 121 | + 2>&1) |
| 122 | +echo $log |
| 123 | +if echo $log | grep -q "failed to generate RBAC rules"; then |
| 124 | + echo FAIL expected successful generation of RBAC rules |
| 125 | + exit 1 |
| 126 | +fi |
| 127 | + |
| 128 | +pushd memcached-operator |
| 129 | +sed -i 's|\(FROM quay.io/operator-framework/helm-operator\)\(:.*\)\?|\1:dev|g' build/Dockerfile |
| 130 | +operator-sdk build "$DEST_IMAGE" |
| 131 | +# If using a kind cluster, load the image into all nodes. |
| 132 | +load_image_if_kind "$DEST_IMAGE" |
| 133 | +sed -i "s|REPLACE_IMAGE|$DEST_IMAGE|g" deploy/operator.yaml |
| 134 | +sed -i 's|Always|Never|g' deploy/operator.yaml |
| 135 | +sed -i 's|hard|soft|g' helm-charts/memcached/values.yaml |
| 136 | +sed -i 's|hard|soft|g' deploy/crds/helm.example.com_v1alpha1_memcached_cr.yaml |
| 137 | +# kind has an issue with certain image registries (ex. redhat's), so use a |
| 138 | +# different test pod image. |
| 139 | +METRICS_TEST_IMAGE="fedora:latest" |
| 140 | +docker pull "$METRICS_TEST_IMAGE" |
| 141 | +# If using a kind cluster, load the metrics test image into all nodes. |
| 142 | +load_image_if_kind "$METRICS_TEST_IMAGE" |
| 143 | + |
| 144 | +OPERATORDIR="$(pwd)" |
| 145 | + |
| 146 | +deploy_operator |
| 147 | +trap_add 'remove_operator' EXIT |
| 148 | +test_operator |
| 149 | +remove_operator |
| 150 | + |
| 151 | +echo "###" |
| 152 | +echo "### Base image testing passed" |
| 153 | +echo "### Now testing migrate to hybrid operator" |
| 154 | +echo "###" |
| 155 | + |
| 156 | +export GO111MODULE=on |
| 157 | +operator-sdk migrate --repo=github.com/example-inc/memcached-operator |
| 158 | + |
| 159 | +if [[ ! -e build/Dockerfile.sdkold ]]; |
| 160 | +then |
| 161 | + echo FAIL the old Dockerfile should have been renamed to Dockerfile.sdkold |
| 162 | + exit 1 |
| 163 | +fi |
| 164 | + |
| 165 | +add_go_mod_replace "github.com/operator-framework/operator-sdk" "$ROOTDIR" |
| 166 | +# Build the project to resolve dependency versions in the modfile. |
| 167 | +go build ./... |
| 168 | + |
| 169 | +operator-sdk build "$DEST_IMAGE" |
| 170 | +# If using a kind cluster, load the image into all nodes. |
| 171 | +load_image_if_kind "$DEST_IMAGE" |
| 172 | + |
| 173 | +deploy_operator |
| 174 | +test_operator |
| 175 | + |
| 176 | +popd |
| 177 | +popd |
0 commit comments