github.com/jmccormick2001/sdk-scorecard@v0.8.2/hack/tests/e2e-helm.sh (about) 1 #!/usr/bin/env bash 2 3 source hack/lib/test_lib.sh 4 5 set -eux 6 7 DEST_IMAGE="quay.io/example/nginx-operator:v0.0.2" 8 ROOTDIR="$(pwd)" 9 GOTMP="$(mktemp -d -p $GOPATH/src)" 10 trap_add 'rm -rf $GOTMP' EXIT 11 12 deploy_operator() { 13 kubectl create -f "$OPERATORDIR/deploy/service_account.yaml" 14 kubectl create -f "$OPERATORDIR/deploy/role.yaml" 15 kubectl create -f "$OPERATORDIR/deploy/role_binding.yaml" 16 kubectl create -f "$OPERATORDIR/deploy/crds/helm_v1alpha1_nginx_crd.yaml" 17 kubectl create -f "$OPERATORDIR/deploy/operator.yaml" 18 } 19 20 remove_operator() { 21 kubectl delete --ignore-not-found=true -f "$OPERATORDIR/deploy/service_account.yaml" 22 kubectl delete --ignore-not-found=true -f "$OPERATORDIR/deploy/role.yaml" 23 kubectl delete --ignore-not-found=true -f "$OPERATORDIR/deploy/role_binding.yaml" 24 kubectl delete --ignore-not-found=true -f "$OPERATORDIR/deploy/crds/helm_v1alpha1_nginx_crd.yaml" 25 kubectl delete --ignore-not-found=true -f "$OPERATORDIR/deploy/operator.yaml" 26 } 27 28 test_operator() { 29 # wait for operator pod to run 30 if ! timeout 1m kubectl rollout status deployment/nginx-operator; 31 then 32 kubectl logs deployment/nginx-operator 33 exit 1 34 fi 35 36 # create CR 37 kubectl create -f deploy/crds/helm_v1alpha1_nginx_cr.yaml 38 trap_add 'kubectl delete --ignore-not-found -f ${OPERATORDIR}/deploy/crds/helm_v1alpha1_nginx_cr.yaml' EXIT 39 if ! timeout 1m bash -c -- 'until kubectl get nginxes.helm.example.com example-nginx -o jsonpath="{..status.deployedRelease.name}" | grep "example-nginx"; do sleep 1; done'; 40 then 41 kubectl logs deployment/nginx-operator 42 exit 1 43 fi 44 45 release_name=$(kubectl get nginxes.helm.example.com example-nginx -o jsonpath="{..status.deployedRelease.name}") 46 nginx_deployment=$(kubectl get deployment -l "app.kubernetes.io/instance=${release_name}" -o jsonpath="{..metadata.name}") 47 48 if ! timeout 1m kubectl rollout status deployment/${nginx_deployment}; 49 then 50 kubectl describe pods -l "app.kubernetes.io/instance=${release_name}" 51 kubectl describe deployments ${nginx_deployment} 52 kubectl logs deployment/nginx-operator 53 exit 1 54 fi 55 56 nginx_service=$(kubectl get service -l "app.kubernetes.io/instance=${release_name}" -o jsonpath="{..metadata.name}") 57 kubectl get service ${nginx_service} 58 59 # scale deployment replicas to 2 and verify the 60 # deployment automatically scales back down to 1. 61 kubectl scale deployment/${nginx_deployment} --replicas=2 62 if ! timeout 1m bash -c -- "until test \$(kubectl get deployment/${nginx_deployment} -o jsonpath='{..spec.replicas}') -eq 1; do sleep 1; done"; 63 then 64 kubectl describe pods -l "app.kubernetes.io/instance=${release_name}" 65 kubectl describe deployments ${nginx_deployment} 66 kubectl logs deployment/nginx-operator 67 exit 1 68 fi 69 70 # update CR to replicaCount=2 and verify the deployment 71 # automatically scales up to 2 replicas. 72 kubectl patch nginxes.helm.example.com example-nginx -p '[{"op":"replace","path":"/spec/replicaCount","value":2}]' --type=json 73 if ! timeout 1m bash -c -- "until test \$(kubectl get deployment/${nginx_deployment} -o jsonpath='{..spec.replicas}') -eq 2; do sleep 1; done"; 74 then 75 kubectl describe pods -l "app.kubernetes.io/instance=${release_name}" 76 kubectl describe deployments ${nginx_deployment} 77 kubectl logs deployment/nginx-operator 78 exit 1 79 fi 80 81 kubectl delete -f deploy/crds/helm_v1alpha1_nginx_cr.yaml --wait=true 82 kubectl logs deployment/nginx-operator | grep "Uninstalled release" | grep "${release_name}" 83 } 84 85 # if on openshift switch to the "default" namespace 86 # and allow containers to run as root (necessary for 87 # default nginx image) 88 if which oc 2>/dev/null; 89 then 90 oc project default 91 oc adm policy add-scc-to-user anyuid -z default 92 fi 93 94 95 # create and build the operator 96 pushd "$GOTMP" 97 log=$(operator-sdk new nginx-operator --api-version=helm.example.com/v1alpha1 --kind=Nginx --type=helm 2>&1) 98 echo $log 99 if echo $log | grep -q "failed to generate RBAC rules"; then 100 echo FAIL expected successful generation of RBAC rules 101 exit 1 102 fi 103 104 pushd nginx-operator 105 sed -i 's|\(FROM quay.io/operator-framework/helm-operator\)\(:.*\)\?|\1:dev|g' build/Dockerfile 106 operator-sdk build "$DEST_IMAGE" 107 sed -i "s|REPLACE_IMAGE|$DEST_IMAGE|g" deploy/operator.yaml 108 sed -i 's|Always|Never|g' deploy/operator.yaml 109 110 OPERATORDIR="$(pwd)" 111 112 deploy_operator 113 trap_add 'remove_operator' EXIT 114 test_operator 115 remove_operator 116 117 echo "###" 118 echo "### Base image testing passed" 119 echo "### Now testing migrate to hybrid operator" 120 echo "###" 121 122 export GO111MODULE=on 123 operator-sdk migrate 124 125 if [[ ! -e build/Dockerfile.sdkold ]]; 126 then 127 echo FAIL the old Dockerfile should have been renamed to Dockerfile.sdkold 128 exit 1 129 fi 130 131 add_go_mod_replace "github.com/operator-framework/operator-sdk" "$ROOTDIR" 132 go mod vendor 133 134 operator-sdk build "$DEST_IMAGE" 135 136 deploy_operator 137 test_operator 138 139 popd 140 popd