apiVersion: v1
data:
  template: |
      import (
        "vela/ql"
      )

      parameter: {
        appName:    string
        appNs:      string
        name?:      string
        cluster?:   string
        clusterNs?: string
      }

      result: ql.#CollectPods & {
        app: {
          name:      parameter.appName
          namespace: parameter.appNs
          filter: {
            if parameter.cluster != _|_ {
              cluster: parameter.cluster
            }
            if parameter.clusterNs != _|_ {
              clusterNamespace: parameter.clusterNs
            }
            if parameter.name != _|_ {
              components: [parameter.name]
            }
          }
        }
      }

      if result.err == _|_ {
        status: {
          podList: [ for pod in result.list if pod.object != _|_ {
            cluster:   pod.cluster
            workload:  pod.workload
            component: pod.component
            metadata: {
              name:         pod.object.metadata.name
              namespace:    pod.object.metadata.namespace
              creationTime: pod.object.metadata.creationTimestamp
              labels:       pod.object.metadata.labels
              version: {
                if pod.publishVersion != _|_ {
                  publishVersion: pod.publishVersion
                }
                if pod.deployVersion != _|_ {
                  deployVersion: pod.deployVersion
                }
              }
            }
            status: {
              phase: pod.object.status.phase
              // refer to https://kubernetes.io/docs/concepts/workloads/pods/pod-lifecycle/#pod-phase
              if phase != "Pending" && phase != "Unknown" {
                if pod.object.status.podIP != _|_ {
                  podIP: pod.object.status.podIP
                }
                if pod.object.status.hostIP != _|_ {
                  hostIP: pod.object.status.hostIP
                }
                if pod.object.spec.nodeName != _|_ {
                  nodeName: pod.object.spec.nodeName
                }
              }
            }
          }]
        }
      }

      if result.err != _|_ {
        status: {
          error: result.err
        }
      }
kind: ConfigMap
metadata:
  name: component-pod-view
  namespace: {{ include "systemDefinitionNamespace" . }}
