{{- if include "kubeovn.ovn.versionCompatibility" . -}}
---
apiVersion: v1
kind: ServiceAccount
metadata:
  name: ovs-ovn-upgrade
  namespace: {{ .Values.namespace }}
  annotations:
    # This is what defines this resource as a hook. Without this line, the
    # job is considered part of the release.
    "helm.sh/hook": post-upgrade
    "helm.sh/hook-weight": "1"
    "helm.sh/hook-delete-policy": hook-succeeded
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
  annotations:
    rbac.authorization.k8s.io/system-only: "true"
    # This is what defines this resource as a hook. Without this line, the
    # job is considered part of the release.
    "helm.sh/hook": post-upgrade
    "helm.sh/hook-weight": "2"
    "helm.sh/hook-delete-policy": hook-succeeded
  name: system:ovs-ovn-upgrade
rules:
  - apiGroups:
      - apps
    resources:
      - daemonsets
    verbs:
      - list
  - apiGroups:
      - apps
    resources:
      - daemonsets
    resourceNames:
      - ovs-ovn
    verbs:
      - get
  - apiGroups:
      - apps
    resources:
      - deployments
    resourceNames:
      - ovn-central
    verbs:
      - get
      - list
      - watch
  - apiGroups:
      - ""
    resources:
      - nodes
    verbs:
      - list
  - apiGroups:
      - ""
    resources:
      - pods
    verbs:
      - list
      - get
      - watch
      - delete
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  name: ovs-ovn-upgrade
  annotations:
    # This is what defines this resource as a hook. Without this line, the
    # job is considered part of the release.
    "helm.sh/hook": post-upgrade
    "helm.sh/hook-weight": "3"
    "helm.sh/hook-delete-policy": hook-succeeded
roleRef:
  name: system:ovs-ovn-upgrade
  kind: ClusterRole
  apiGroup: rbac.authorization.k8s.io
subjects:
  - kind: ServiceAccount
    name: ovs-ovn-upgrade
    namespace: {{ .Values.namespace }}
---
apiVersion: batch/v1
kind: Job
metadata:
  name: "{{ .Chart.Name }}-post-upgrade-hook"
  namespace: {{ .Values.namespace }}
  labels:
    app.kubernetes.io/managed-by: {{ .Release.Service | quote }}
    app.kubernetes.io/instance: {{ .Release.Name | quote }}
    app.kubernetes.io/version: {{ .Chart.AppVersion }}
    helm.sh/chart: "{{ .Chart.Name }}-{{ .Chart.Version }}"
  annotations:
    # This is what defines this resource as a hook. Without this line, the
    # job is considered part of the release.
    "helm.sh/hook": post-upgrade
    "helm.sh/hook-weight": "4"
    "helm.sh/hook-delete-policy": hook-succeeded
spec:
  completions: 1
  template:
    metadata:
      name: "{{ .Release.Name }}"
      labels:
        app.kubernetes.io/managed-by: {{ .Release.Service | quote }}
        app.kubernetes.io/instance: {{ .Release.Name | quote }}
        helm.sh/chart: "{{ .Chart.Name }}-{{ .Chart.Version }}"
        app: post-upgrade
        component: job
    spec:
      tolerations:
        - key: ""
          operator: "Exists"
          effect: "NoSchedule"
      affinity:
        podAntiAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
            - topologyKey: kubernetes.io/hostname
              labelSelector:
                matchExpressions:
                  - key: app
                    operator: In
                    values:
                      - post-upgrade
                  - key: component
                    operator: In
                    values:
                      - job
      restartPolicy: Never
      hostNetwork: true
      nodeSelector:
        kubernetes.io/os: "linux"
      serviceAccount: ovs-ovn-upgrade
      serviceAccountName: ovs-ovn-upgrade
      containers:
        - name: ovs-ovn-upgrade
          image: "{{ .Values.global.registry.address}}/{{ .Values.global.images.kubeovn.repository }}:{{ .Values.global.images.kubeovn.tag }}"
          env:
            - name: POD_NAMESPACE
              valueFrom:
                fieldRef:
                  fieldPath: metadata.namespace
            - name: ENABLE_SSL
              value: "{{ .Values.networking.ENABLE_SSL }}"
            - name: OVN_DB_IPS
              value: "{{ .Values.MASTER_NODES | default (include "kubeovn.nodeIPs" .) }}"
            - name: OVN_VERSION_COMPATIBILITY
              value: '{{ include "kubeovn.ovn.versionCompatibility" . }}'
          command:
            - bash
            - -eo
            - pipefail
            - -c
            - /kube-ovn/upgrade-ovs.sh 2>&1 | tee -a /var/log/kube-ovn/upgrade-ovs.log
          volumeMounts:
            - mountPath: /var/log/kube-ovn
              name: kube-ovn-log
            - mountPath: /var/run/tls
              name: kube-ovn-tls
      volumes:
        - name: kube-ovn-log
          hostPath:
            path: {{ .Values.log_conf.LOG_DIR }}/kube-ovn
        - name: kube-ovn-tls
          secret:
            optional: true
            secretName: kube-ovn-tls
{{- end -}}
