apiVersion: apps/v1
kind: Deployment
metadata:
  name: app10
  namespace: default
spec:
  replicas: 1
  selector:
    matchLabels:
      com.talend.application: test
      com.talend.service: test-app-svc
  template:
    metadata:
      annotations:
        sidecar.vault.talend.org/inject: "true"
        sidecar.vault.talend.org/notify: "curl localhost:8888/refresh_secrets" # To be notified each time secrets are updated
        # NOTE: 
        # For this sample to work, you cannot use default Vault docker image as curl is not installed.
        # You have to set your own custom Vault image with the tools you depend on (here we use 'grem1in/vault-curl-jq' image instead).
        sidecar.vault.talend.org/vault-image: "grem1in/vault-curl-jq"
      labels:
        com.talend.application: test
        com.talend.service: test-app-svc
    spec:
      serviceAccountName: default
      containers:
        - name: app-container
          image: node:14-buster
          command:
            - "sh"
            - "-c"
            - |
              set -e
              cat <<EOF > "/my_custom_api.js"
              const http = require("http")
              const fs = require('fs')
              const PORT = 8888
              const server = http.createServer((request, response) => {
                const { method, url, headers } = request

                if (url === "/refresh_secrets") {
                  console.log("Notification received: read secrets file to update values")

                  fs.readFile("/opt/talend/secrets/secrets.properties", "utf8", (error, data) => {
                    if (error) {
                      console.error(error)
                    } else {
                      console.log(data)
                    }
                  })

                  response.statusCode = 200
                  response.end()
                }
              })

              server.listen(PORT, error => {
                if (error) {
                  return console.error(error)
                }

                console.log(\`Server listening on port \${PORT}\`)
              })
              EOF

              while true; do
                echo "Wait for secrets file availability..."
                if [ -f "/opt/talend/secrets/secrets.properties" ]; then
                  echo "Secrets are:"
                  cat /opt/talend/secrets/secrets.properties
                  break
                fi
                sleep 2
              done

              node my_custom_api.js
