apiVersion: core.oam.dev/v1beta1
kind: Application
metadata:
  annotations:
    helm.sh/hook: test-success
    helm.sh/hook-delete-policy: hook-succeeded
  name: helm-test-vela-app
  namespace: {{ include "systemDefinitionNamespace" . }}
spec:
  components:
    - name: helm-test-express-server
      type: webservice
      properties:
        image: {{ .Values.imageRegistry }}{{ .Values.test.app.repository }}:{{ .Values.test.app.tag }}
        port: 8000
---
apiVersion: v1
kind: Pod
metadata:
  name: "{{ .Release.Name }}-application-test"
  namespace: {{ include "systemDefinitionNamespace" . }}
  annotations:
    "helm.sh/hook": test
    helm.sh/hook-delete-policy: hook-succeeded
spec:
  serviceAccountName: {{ include "kubevela.serviceAccountName" . }}
  containers:
    - name: {{ .Release.Name }}-application-test
      image: {{ .Values.imageRegistry }}{{ .Values.test.k8s.repository }}:{{ .Values.test.k8s.tag }}
      imagePullPolicy: IfNotPresent
      command:
        - /bin/bash
        - -ec
        - |

          set -e

          echo "Waiting application is ready..."

          echo "waiting for application being Ready"
          kubectl -n {{ include "systemDefinitionNamespace" . }} wait --for=condition=Ready applications.core.oam.dev helm-test-vela-app --timeout=3m
          echo "application is Ready"

          # wait for deploy being created
          echo "waiting for deployment being available"
          kubectl -n {{ include "systemDefinitionNamespace" . }} wait --for=condition=available deployments helm-test-express-server --timeout 3m
          echo "deployment being available"

          echo "Application and its components are created"
  restartPolicy: Never
  {{- with .Values.nodeSelector }}
  nodeSelector:
  {{- toYaml . | nindent 4 }}
  {{- end }}
  {{- with .Values.affinity }}
  affinity:
  {{- toYaml . | nindent 4 }}
  {{- end }}
  {{- with .Values.tolerations }}
  tolerations:
  {{- toYaml . | nindent 4 }}
  {{- end }}
