{{ if .Values.multicluster.enabled }}
apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ .Release.Name }}-cluster-gateway
  namespace: {{ .Release.Namespace }}
  labels:
  {{- include "kubevela.labels" . | nindent 4 }}
spec:
  replicas: {{ .Values.multicluster.clusterGateway.replicaCount }}
  selector:
    matchLabels:
    {{- include "kubevela-cluster-gateway.selectorLabels" . | nindent 6 }}
  template:
    metadata:
      labels:
      {{- include "kubevela-cluster-gateway.selectorLabels" . | nindent 8 }}
    spec:
      {{- with .Values.imagePullSecrets }}
      imagePullSecrets:
      {{- toYaml . | nindent 8 }}
      {{- end }}
      serviceAccountName: {{ include "kubevela.serviceAccountName" . }}
      securityContext:
      {{- toYaml .Values.podSecurityContext | nindent 8 }}
      containers:
        - name: {{ include "kubevela.fullname" . }}-cluster-gateway
          securityContext:
          {{- toYaml .Values.securityContext | nindent 12 }}
          args:
            - "apiserver"
            - "--secure-port={{ .Values.multicluster.clusterGateway.port }}"
            - "--secret-namespace={{ .Release.Namespace }}"
            - "--feature-gates=APIPriorityAndFairness=false"
            {{ if .Values.multicluster.clusterGateway.secureTLS.enabled }}
            - "--cert-dir={{ .Values.multicluster.clusterGateway.secureTLS.certPath }}"
            {{ end }}
          image: {{ .Values.multicluster.clusterGateway.image.repository }}:{{ .Values.multicluster.clusterGateway.image.tag }}
          imagePullPolicy: {{ .Values.multicluster.clusterGateway.image.pullPolicy }}
          resources:
          {{- toYaml .Values.multicluster.clusterGateway.resources | nindent 12 }}
          ports:
            - containerPort: {{ .Values.multicluster.clusterGateway.port }}
          {{ if .Values.multicluster.clusterGateway.secureTLS.enabled }}
          volumeMounts:
            - mountPath: {{ .Values.multicluster.clusterGateway.secureTLS.certPath }}
              name: tls-cert-vol
              readOnly: true
          {{- end }}
      {{ if .Values.multicluster.clusterGateway.secureTLS.enabled }}
      volumes:
        - name: tls-cert-vol
          secret:
            defaultMode: 420
            secretName: {{ template "kubevela.fullname" . }}-cluster-gateway-tls
      {{ end }}
      {{- with .Values.nodeSelector }}
      nodeSelector:
      {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.affinity }}
      affinity:
      {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.tolerations }}
      tolerations:
      {{- toYaml . | nindent 8 }}
      {{- end }}
{{ end }}
---
{{ if .Values.multicluster.enabled }}
apiVersion: v1
kind: Service
metadata:
  name: {{ .Release.Name }}-cluster-gateway-service
  namespace: {{ .Release.Namespace }}
spec:
  selector:
  {{- include "kubevela-cluster-gateway.selectorLabels" . | nindent 4 }}
  ports:
    - protocol: TCP
      port: {{ .Values.multicluster.clusterGateway.port }}
      targetPort: {{ .Values.multicluster.clusterGateway.port }}
{{ end }}
---
{{ if .Values.multicluster.enabled }}
apiVersion: apiregistration.k8s.io/v1
kind: APIService
metadata:
  name: v1alpha1.cluster.core.oam.dev
  labels:
    api: cluster-extension-apiserver
    apiserver: "true"
spec:
  version: v1alpha1
  group: cluster.core.oam.dev
  groupPriorityMinimum: 2000
  service:
    name: {{ .Release.Name }}-cluster-gateway-service
    namespace: {{ .Release.Namespace }}
    port: {{ .Values.multicluster.clusterGateway.port }}
  versionPriority: 10
  insecureSkipTLSVerify: {{ not .Values.multicluster.clusterGateway.secureTLS.enabled }}
  {{ if .Values.multicluster.clusterGateway.secureTLS.enabled }}
  caBundle: Cg==
  {{ end }}
{{ end }}
---
{{ if and .Values.multicluster.enabled .Values.multicluster.clusterGateway.secureTLS.enabled }}
apiVersion: batch/v1
kind: Job
metadata:
  name: {{ template "kubevela.fullname" . }}-cluster-gateway-tls-secret-create
  namespace: {{ .Release.Namespace }}
  annotations:
    "helm.sh/hook": pre-install,pre-upgrade
    "helm.sh/hook-delete-policy": before-hook-creation,hook-succeeded
  labels:
    app: {{ template "kubevela.fullname" . }}-cluster-gateway-tls-secret-create
    {{- include "kubevela.labels" . | nindent 4 }}
spec:
  {{- if .Capabilities.APIVersions.Has "batch/v1alpha1" }}
  # Alpha feature since k8s 1.12
  ttlSecondsAfterFinished: 0
  {{- end }}
  template:
    metadata:
      name: {{ template "kubevela.fullname" . }}-cluster-gateway-tls-secret-create
      labels:
        app: {{ template "kubevela.fullname" . }}-cluster-gateway-tls-secret-create
        {{- include "kubevela.labels" . | nindent 8 }}
    spec:
      containers:
      - name: create
        image: {{ .Values.admissionWebhooks.patch.image.repository }}:{{ .Values.admissionWebhooks.patch.image.tag }}
        imagePullPolicy: {{ .Values.admissionWebhooks.patch.image.pullPolicy }}
        args:
          - create
          - --host={{ .Release.Name }}-cluster-gateway-service,{{ .Release.Name }}-cluster-gateway-service.{{ .Release.Namespace }}.svc
          - --namespace={{ .Release.Namespace }}
          - --secret-name={{ template "kubevela.fullname" . }}-cluster-gateway-tls
          - --key-name=apiserver.key
          - --cert-name=apiserver.crt
      restartPolicy: OnFailure
      serviceAccountName: {{ template "kubevela.fullname" . }}-admission
      securityContext:
        runAsGroup: 2000
        runAsNonRoot: true
        runAsUser: 2000
{{ end }}
---
{{ if and .Values.multicluster.enabled .Values.multicluster.clusterGateway.secureTLS.enabled }}
apiVersion: batch/v1
kind: Job
metadata:
  name: {{ template "kubevela.fullname" . }}-cluster-gateway-tls-secret-patch
  namespace: {{ .Release.Namespace }}
  annotations:
    "helm.sh/hook": post-install,post-upgrade
    "helm.sh/hook-delete-policy": before-hook-creation,hook-succeeded
  labels:
    app: {{ template "kubevela.fullname" . }}-cluster-gateway-tls-secret-patch
    {{- include "kubevela.labels" . | nindent 4 }}
spec:
  {{- if .Capabilities.APIVersions.Has "batch/v1alpha1" }}
  # Alpha feature since k8s 1.12
  ttlSecondsAfterFinished: 0
  {{- end }}
  template:
    metadata:
      name: {{ template "kubevela.fullname" . }}-cluster-gateway-tls-secret-patch
      labels:
        app: {{ template "kubevela.fullname" . }}-cluster-gateway-tls-secret-patch
        {{- include "kubevela.labels" . | nindent 8 }}
    spec:
      containers:
      - name: patch
        image: {{ .Values.multicluster.clusterGateway.image.repository }}:{{ .Values.multicluster.clusterGateway.image.tag }}
        imagePullPolicy: {{ .Values.multicluster.clusterGateway.image.pullPolicy }}
        command:
          - /patch
        args:
          - --secret-namespace={{ .Release.Namespace }}
          - --secret-name={{ template "kubevela.fullname" . }}-cluster-gateway-tls
      restartPolicy: OnFailure
      serviceAccountName: {{ include "kubevela.serviceAccountName" . }}
      securityContext:
        runAsGroup: 2000
        runAsNonRoot: true
        runAsUser: 2000
{{ end }}