cyberark/secretless-broker

View on GitHub
demos/k8s-demo/02_app_developer_steps

Summary

Maintainability
Test Coverage
#!/bin/bash -e

# Generic utilities
. ./utils.sh

# Generic variables about the environment
. ./app_constants.sh

# Application url accessible to local machine
#
# NOTE: This file does not have access to the secrets stored in
# security_admin_secrets.sh.  It knows only this application url:
##################################################
step "Start application"

kubectl --namespace "${APP_NAMESPACE}" \
  apply --filename "etc/quick-start-application.yml"

if [ ! "${SECRETLESS_IMAGE:-}" == "" ]; then
  image_name="${SECRETLESS_IMAGE}"

  step "Patching deployment with ${image_name}"

  kubectl patch deployment "quick-start-application" \
    --namespace="${APP_NAMESPACE}" \
    --patch """
  spec:
    template:
      spec:
        containers:
          - name: secretless
            image: ${image_name}
"""
fi

APPLICATION_URL="http://quick-start-application.${APP_NAMESPACE}.svc.cluster.local:8080"
echo "Using app URL: $APPLICATION_URL"

# Since wait_for_app and kubectl rollout status are both not working...
# We revert to simply pinging the endpoint
echo 'Waiting for application to boot up'
echo '(This may take more than 1 minute)...'

kubectl run --rm -i \
  --generator=run-pod/v1 \
  --namespace "${APP_NAMESPACE}" \
  --wait \
  --image "alpine:3.9" \
  alpine-curl << EOF
  set -e
  
  attempt_count=0
  max_attempts=10
  until wget --tries=1 \
             --timeout=1 \
             --server-response \
             --quiet \
             --output-document=/dev/stdout \
             "${APPLICATION_URL}/pets" &>/dev/null; do
    attempt_count=\$(( attempt_count + 1 ))
    if [[ "\$attempt_count" -ge "\$max_attempts" ]]; then
      echo ""
      echo "Application failed to boot up after \$attempt_count pings at 2 second intervals." >&2
      exit 1
    fi

    echo -n "."
    sleep 2
  done
  echo ""
  

  echo "OK"
  echo

  echo "Adding a sample pet..."
  wget --tries=1 \
       --timeout=3 \
       --server-response \
       --quiet \
       --output-document=/dev/stdout \
       --header "Content-Type: application/json" \
       --post-data '{"name": "Mr. Snuggles"}' \
       "${APPLICATION_URL}/pet"

  echo "OK"
  echo

  echo "Retrieving all pets..."
  wget --tries=1 \
       --timeout=3 \
       --server-response \
       --quiet \
       --output-document=/dev/stdout \
       "${APPLICATION_URL}/pets"
  echo
  echo
EOF

step "Cleaning up"
./stop || true