apiVersion: v1
kind: ConfigMap
metadata:
  name: component-pod-view
  namespace: vela-system
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" {
              podIP:    pod.object.status.podIP
              hostIP:   pod.object.status.hostIP
              nodeName: pod.object.spec.nodeName
            }
          }
        }]
      }
    }

    if result.err != _|_ {
      status: {
        error: result.err
      }
    }
