apiVersion: v1
kind: ConfigMap
metadata:
  labels:
    app: {{ .Values.name }}
  name: {{ .Values.name }}
  namespace: {{ .Release.Namespace }}
data:
  # NOTE: https://github.com/helm/helm/issues/2798#issuecomment-470435015
  kwok-config.yaml: |-{{`
    kind: Stage
    apiVersion: kwok.x-k8s.io/v1alpha1
    metadata:
      name: pod-create
    spec:
      resourceRef:
        apiGroup: v1
        kind: Pod
      selector:
        matchExpressions:
          - key: '.metadata.deletionTimestamp'
            operator: 'DoesNotExist'
          - key: '.status.podIP'
            operator: 'DoesNotExist'
      weight: 1
      delay:
        durationMilliseconds: 1000
        jitterDurationMilliseconds: 5000
      next:
        event:
          type: Normal
          reason: Created
          message: Created container
        statusTemplate: |
          {{ $now := Now }}

          conditions:
          {{ if .spec.initContainers }}
            - lastProbeTime: null
              lastTransitionTime: '{{ $now }}'
              message: 'containers with incomplete status: [{{ range .spec.initContainers }} {{ .name }} {{ end }}]'
              reason: ContainersNotInitialized
              status: "False"
              type: Initialized
          {{ else }}
            - lastProbeTime: null
              lastTransitionTime: '{{ $now }}'
              status: "True"
              type: Initialized
          {{ end }}
            - lastProbeTime: null
              lastTransitionTime: '{{ $now }}'
              message: 'containers with unready status: [{{ range .spec.containers }} {{ .name }} {{ end }}]'
              reason: ContainersNotReady
              status: "False"
              type: Ready
            - lastProbeTime: null
              lastTransitionTime: '{{ $now }}'
              message: 'containers with unready status: [{{ range .spec.containers }} {{ .name }} {{ end }}]'
              reason: ContainersNotReady
              status: "False"
              type: ContainersReady
          {{ range .spec.readinessGates }}
            - lastTransitionTime: {{ $now }}
              status: "True"
              type: {{ .conditionType }}
          {{ end }}

          {{ if .spec.initContainers }}
          initContainerStatuses:
            {{ range .spec.initContainers }}
            - image: {{ .image }}
              name: {{ .name }}
              ready: false
              restartCount: 0
              started: false
              state:
                waiting:
                  reason: PodInitializing
            {{ end }}
          containerStatuses:
            {{ range .spec.containers }}
            - image: {{ .image }}
              name: {{ .name }}
              ready: false
              restartCount: 0
              started: false
              state:
                waiting:
                  reason: PodInitializing
            {{ end }}
          {{ else }}
          containerStatuses:
            {{ range .spec.containers }}
            - image: {{ .image }}
              name: {{ .name }}
              ready: false
              restartCount: 0
              started: false
              state:
                waiting:
                  reason: ContainerCreating
            {{ end }}
          {{ end }}

          hostIP: {{ NodeIPWith .spec.nodeName }}
          podIP: {{ PodIPWith .spec.nodeName ( or .spec.hostNetwork false ) ( or .metadata.uid "" ) ( or .metadata.name "" ) ( or .metadata.namespace "" ) }}
          phase: Pending
    ---
    kind: Stage
    apiVersion: kwok.x-k8s.io/v1alpha1
    metadata:
      name: pod-init-container-running
    spec:
      resourceRef:
        apiGroup: v1
        kind: Pod
      selector:
        matchExpressions:
          - key: '.metadata.deletionTimestamp'
            operator: 'DoesNotExist'
          - key: '.status.phase'
            operator: 'In'
            values:
              - 'Pending'
          - key: '.status.conditions.[] | select( .type == "Initialized" ) | .status'
            operator: 'NotIn'
            values:
              - 'True'
          - key: '.status.initContainerStatuses.[].state.waiting.reason'
            operator: 'Exists'
      weight: 1
      delay:
        durationMilliseconds: 1000
        jitterDurationMilliseconds: 5000
      next:
        statusTemplate: |
          {{ $now := Now }}
          {{ $root := . }}
          initContainerStatuses:
            {{ range $index, $item := .spec.initContainers }}
            {{ $origin := index $root.status.initContainerStatuses $index }}
            - image: {{ $item.image }}
              name: {{ $item.name }}
              ready: true
              restartCount: 0
              started: true
              state:
                running:
                  startedAt: '{{ $now }}'
            {{ end }}
    ---
    kind: Stage
    apiVersion: kwok.x-k8s.io/v1alpha1
    metadata:
      name: pod-init-container-completed
    spec:
      resourceRef:
        apiGroup: v1
        kind: Pod
      selector:
        matchExpressions:
          - key: '.metadata.deletionTimestamp'
            operator: 'DoesNotExist'
          - key: '.status.phase'
            operator: 'In'
            values:
              - 'Pending'
          - key: '.status.conditions.[] | select( .type == "Initialized" ) | .status'
            operator: 'NotIn'
            values:
              - 'True'
          - key: '.status.initContainerStatuses.[].state.running.startedAt'
            operator: 'Exists'
      weight: 1
      delay:
        durationMilliseconds: 1000
        jitterDurationMilliseconds: 5000
      next:
        statusTemplate: |
          {{ $now := Now }}
          {{ $root := . }}
          conditions:
            - lastProbeTime: null
              lastTransitionTime: '{{ $now }}'
              status: "True"
              type: Initialized
          initContainerStatuses:
            {{ range $index, $item := .spec.initContainers }}
            {{ $origin := index $root.status.initContainerStatuses $index }}
            - image: {{ $item.image }}
              name: {{ $item.name }}
              ready: true
              restartCount: 0
              started: false
              state:
                terminated:
                  exitCode: 0
                  finishedAt: '{{ $now }}'
                  reason: Completed
                  startedAt: '{{ $now }}'
            {{ end }}
          containerStatuses:
            {{ range .spec.containers }}
            - image: {{ .image }}
              name: {{ .name }}
              ready: false
              restartCount: 0
              started: false
              state:
                waiting:
                  reason: ContainerCreating
            {{ end }}
    ---
    kind: Stage
    apiVersion: kwok.x-k8s.io/v1alpha1
    metadata:
      name: pod-ready
    spec:
      resourceRef:
        apiGroup: v1
        kind: Pod
      selector:
        matchExpressions:
          - key: '.metadata.deletionTimestamp'
            operator: 'DoesNotExist'
          - key: '.status.phase'
            operator: 'In'
            values:
              - 'Pending'
          - key: '.status.conditions.[] | select( .type == "Initialized" ) | .status'
            operator: 'In'
            values:
              - 'True'
          - key: '.status.conditions.[] | select( .type == "ContainersReady" ) | .status'
            operator: 'NotIn'
            values:
              - 'True'
      weight: 1
      delay:
        durationMilliseconds: 1000
        jitterDurationMilliseconds: 5000
      next:
        delete: false
        statusTemplate: |
          {{ $now := Now }}
          {{ $root := . }}
          conditions:
            - lastProbeTime: null
              lastTransitionTime: '{{ $now }}'
              message: ''
              reason: ''
              status: "True"
              type: Ready
            - lastProbeTime: null
              lastTransitionTime: '{{ $now }}'
              message: ''
              reason: ''
              status: "True"
              type: ContainersReady
          containerStatuses:
            {{ range $index, $item := .spec.containers }}
            {{ $origin := index $root.status.containerStatuses $index }}
            - image: {{ $item.image }}
              name: {{ $item.name }}
              ready: true
              restartCount: 0
              started: true
              state:
                running:
                  startedAt: '{{ $now }}'
            {{ end }}
          phase: Running
    ---
    kind: Stage
    apiVersion: kwok.x-k8s.io/v1alpha1
    metadata:
      name: pod-complete
    spec:
      resourceRef:
        apiGroup: v1
        kind: Pod
      selector:
        matchExpressions:
          - key: '.metadata.deletionTimestamp'
            operator: 'DoesNotExist'
          - key: '.status.phase'
            operator: 'In'
            values:
              - 'Running'
          - key: '.status.conditions.[] | select( .type == "Ready" ) | .status'
            operator: 'In'
            values:
              - 'True'
          - key: '.metadata.ownerReferences.[].kind'
            operator: 'In'
            values:
              - 'Job'
      weight: 1
      delay:
        durationMilliseconds: 1000
        jitterDurationMilliseconds: 5000
      next:
        delete: false
        statusTemplate: |
          {{ $now := Now }}
          {{ $root := . }}
          containerStatuses:
            {{ range $index, $item := .spec.containers }}
            {{ $origin := index $root.status.containerStatuses $index }}
            - image: {{ $item.image }}
              name: {{ $item.name }}
              ready: true
              restartCount: 0
              started: false
              state:
                terminated:
                  exitCode: 0
                  finishedAt: '{{ $now }}'
                  reason: Completed
                  startedAt: '{{ $now }}'
            {{ end }}
          phase: Succeeded
    ---
    kind: Stage
    apiVersion: kwok.x-k8s.io/v1alpha1
    metadata:
      name: pod-remove-finalizer
    spec:
      resourceRef:
        apiGroup: v1
        kind: Pod
      selector:
        matchExpressions:
          - key: '.metadata.deletionTimestamp'
            operator: 'Exists'
          - key: '.metadata.finalizers.[]'
            operator: 'In'
            values:
              - 'kwok.x-k8s.io/fake'
      weight: 1
      delay:
        durationMilliseconds: 1000
        jitterDurationMilliseconds: 5000
      next:
        finalizers:
          remove:
            - value: 'kwok.x-k8s.io/fake'
        event:
          type: Normal
          reason: Killing
          message: Stopping container
    ---
    kind: Stage
    apiVersion: kwok.x-k8s.io/v1alpha1
    metadata:
      name: pod-delete
    spec:
      resourceRef:
        apiGroup: v1
        kind: Pod
      selector:
        matchExpressions:
          - key: '.metadata.deletionTimestamp'
            operator: 'Exists'
          - key: '.metadata.finalizers'
            operator: 'DoesNotExist'
      weight: 1
      delay:
        durationMilliseconds: 1000
        jitterDurationFrom:
          expressionFrom: '.metadata.deletionTimestamp'
      next:
        delete: true`}}
