github.com/jmrodri/operator-sdk@v0.5.0/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.conditions[1].release.info.status.code}" | grep 1; 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.conditions[1].release.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  operator-sdk new nginx-operator --api-version=helm.example.com/v1alpha1 --kind=Nginx --type=helm
    98  
    99  pushd nginx-operator
   100  sed -i 's|\(FROM quay.io/operator-framework/helm-operator\)\(:.*\)\?|\1:dev|g' build/Dockerfile
   101  operator-sdk build "$DEST_IMAGE"
   102  sed -i "s|REPLACE_IMAGE|$DEST_IMAGE|g" deploy/operator.yaml
   103  sed -i 's|Always|Never|g' deploy/operator.yaml
   104  
   105  OPERATORDIR="$(pwd)"
   106  
   107  deploy_operator
   108  trap_add 'remove_operator' EXIT
   109  test_operator
   110  remove_operator
   111  
   112  echo "###"
   113  echo "### Base image testing passed"
   114  echo "### Now testing migrate to hybrid operator"
   115  echo "###"
   116  
   117  operator-sdk migrate
   118  
   119  if [[ ! -e build/Dockerfile.sdkold ]];
   120  then
   121      echo FAIL the old Dockerfile should have been renamed to Dockerfile.sdkold
   122      exit 1
   123  fi
   124  
   125  # We can't reliably run `dep ensure` because when there are changes to
   126  # operator-sdk itself, and those changes are not merged upstream, we hit this
   127  # bug: https://github.com/golang/dep/issues/1747
   128  # Instead, this re-uses operator-sdk's own vendor directory.
   129  cp -a "$ROOTDIR"/vendor ./
   130  mkdir -p vendor/github.com/operator-framework/operator-sdk/
   131  # We cannot just use operator-sdk from $GOPATH because compilation tries to use
   132  # its vendor directory, which can conflict with the local one.
   133  cp -a "$ROOTDIR"/{internal,pkg,version,LICENSE} vendor/github.com/operator-framework/operator-sdk/
   134  
   135  operator-sdk build "$DEST_IMAGE"
   136  
   137  deploy_operator
   138  test_operator
   139  
   140  popd
   141  popd