{{- /*
Copyright Broadcom, Inc. All Rights Reserved.
SPDX-License-Identifier: APACHE-2.0
*/}}

{{- $ca := genCA "apisix-ca" 365 }}
{{- if and .Values.controlPlane.enabled .Values.controlPlane.tls.enabled (not .Values.controlPlane.tls.existingSecret) }}
{{/* For the control plane, it is mandatory to have TLS for the config server */}}
apiVersion: v1
kind: Secret
metadata:
  name: {{ printf "%s-tls" (include "apisix.control-plane.fullname" .) }}
  namespace: {{ include "common.names.namespace" . | quote }}
  labels: {{- include "common.labels.standard" ( dict "customLabels" .Values.commonLabels "context" $ ) | nindent 4 }}
    app.kubernetes.io/part-of: apisix
    app.kubernetes.io/component: control-plane
  {{- if .Values.commonAnnotations }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" .Values.commonAnnotations "context" $ ) | nindent 4 }}
  {{- end }}
type: kubernetes.io/tls
data:
  {{- if .Values.controlPlane.tls.autoGenerated }}
  {{- $cert := genSignedCert (include "apisix.control-plane.fullname" .) nil (list (include "apisix.control-plane.fullname" .) (printf "%s.%s" (include "apisix.control-plane.fullname" .) (include "common.names.namespace" .)) (printf "%s.%s.svc" (include "apisix.control-plane.fullname" .) (include "common.names.namespace" .)) (printf "%s.%s.svc.%s" (include "apisix.control-plane.fullname" .) (include "common.names.namespace" .) .Values.clusterDomain)) 365 $ca }}
  {{ .Values.controlPlane.tls.certFilename }}: {{ include "common.secrets.lookup" (dict "secret" (printf "%s-tls" (include "apisix.control-plane.fullname" .)) "key" .Values.controlPlane.tls.certFilename "defaultValue" $cert.Cert "context" $) }}
  {{ .Values.controlPlane.tls.certKeyFilename }}: {{ include "common.secrets.lookup" (dict "secret" (printf "%s-tls" (include "apisix.control-plane.fullname" .)) "key" .Values.controlPlane.tls.certKeyFilename "defaultValue" $cert.Key "context" $) }}
  {{ .Values.controlPlane.tls.certCAFilename }}: {{ include "common.secrets.lookup" (dict "secret" (printf "%s-tls" (include "apisix.control-plane.fullname" .)) "key" .Values.controlPlane.tls.certCAFilename "defaultValue" $ca.Cert "context" $) }}
  {{- else }}
  {{- if .Values.controlPlane.tls.cert }}
  {{ .Values.controlPlane.tls.certFilename }}: {{ .Values.controlPlane.tls.cert | b64enc }}
  {{- end }}
  {{- if .Values.controlPlane.tls.key }}
  {{ .Values.controlPlane.tls.certKeyFilename }}: {{ .Values.controlPlane.tls.key | b64enc }}
  {{- end }}
  {{- if .Values.controlPlane.tls.ca }}
  {{ .Values.controlPlane.tls.certCAFilename }}: {{ .Values.controlPlane.tls.ca | b64enc }}
  {{- end }}
  {{- end }}
---
{{- end }}
{{- if and .Values.dataPlane.enabled .Values.dataPlane.tls.enabled (not .Values.dataPlane.tls.existingSecret) }}
apiVersion: v1
kind: Secret
metadata:
  name: {{ printf "%s-tls" (include "apisix.data-plane.fullname" .) }}
  namespace: {{ include "common.names.namespace" . | quote }}
  labels: {{- include "common.labels.standard" ( dict "customLabels" .Values.commonLabels "context" $ ) | nindent 4 }}
    app.kubernetes.io/part-of: apisix
    app.kubernetes.io/component: data-plane
  {{- if .Values.commonAnnotations }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" .Values.commonAnnotations "context" $ ) | nindent 4 }}
  {{- end }}
type: kubernetes.io/tls
data:
  {{- if .Values.dataPlane.tls.autoGenerated }}
  {{- $cert := genSignedCert (include "apisix.data-plane.fullname" .) nil (list (include "apisix.data-plane.fullname" .) (printf "%s.%s" (include "apisix.data-plane.fullname" .) (include "common.names.namespace" .)) (printf "%s.%s.svc" (include "apisix.data-plane.fullname" .) (include "common.names.namespace" .)) (printf "%s.%s.svc.%s" (include "apisix.data-plane.fullname" .) (include "common.names.namespace" .) .Values.clusterDomain)) 365 $ca }}
  {{ .Values.dataPlane.tls.certFilename }}: {{ include "common.secrets.lookup" (dict "secret" (printf "%s-tls" (include "apisix.data-plane.fullname" .)) "key" .Values.dataPlane.tls.certFilename "defaultValue" $cert.Cert "context" $) }}
  {{ .Values.dataPlane.tls.certKeyFilename }}: {{ include "common.secrets.lookup" (dict "secret" (printf "%s-tls" (include "apisix.data-plane.fullname" .)) "key" .Values.dataPlane.tls.certKeyFilename "defaultValue" $cert.Key "context" $) }}
  {{ .Values.dataPlane.tls.certCAFilename }}: {{ include "common.secrets.lookup" (dict "secret" (printf "%s-tls" (include "apisix.data-plane.fullname" .)) "key" .Values.dataPlane.tls.certCAFilename "defaultValue" $ca.Cert "context" $) }}
  {{- else }}
  {{- if .Values.dataPlane.tls.cert }}
  {{ .Values.dataPlane.tls.certFilename }}: {{ .Values.dataPlane.tls.cert | b64enc }}
  {{- end }}
  {{- if .Values.dataPlane.tls.key }}
  {{ .Values.dataPlane.tls.certKeyFilename }}: {{ .Values.dataPlane.tls.key | b64enc }}
  {{- end }}
  {{- if .Values.dataPlane.tls.ca }}
  {{ .Values.dataPlane.tls.certCAFilename }}: {{ .Values.dataPlane.tls.ca | b64enc }}
  {{- end }}
  {{- end }}
---
{{- end }}
{{- if and .Values.ingressController.enabled .Values.ingressController.tls.enabled (not .Values.ingressController.tls.existingSecret) }}
apiVersion: v1
kind: Secret
metadata:
  name: {{ printf "%s-tls" (include "apisix.ingress-controller.fullname" .) }}
  namespace: {{ include "common.names.namespace" . | quote }}
  labels: {{- include "common.labels.standard" ( dict "customLabels" .Values.commonLabels "context" $ ) | nindent 4 }}
    app.kubernetes.io/part-of: apisix
    app.kubernetes.io/component: ingress-controller
  {{- if .Values.commonAnnotations }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" .Values.commonAnnotations "context" $ ) | nindent 4 }}
  {{- end }}
type: kubernetes.io/tls
data:
  {{- if .Values.ingressController.tls.autoGenerated }}
  {{- $cert := genSignedCert (include "apisix.ingress-controller.fullname" .) nil (list (include "apisix.ingress-controller.fullname" .) (printf "%s.%s" (include "apisix.ingress-controller.fullname" .) (include "common.names.namespace" .)) (printf "%s.%s.svc" (include "apisix.ingress-controller.fullname" .) (include "common.names.namespace" .)) (printf "%s.%s.svc.%s" (include "apisix.ingress-controller.fullname" .) (include "common.names.namespace" .) .Values.clusterDomain)) 365 $ca }}
  {{ .Values.ingressController.tls.certFilename }}: {{ include "common.secrets.lookup" (dict "secret" (printf "%s-tls" (include "apisix.ingress-controller.fullname" .)) "key" .Values.ingressController.tls.certFilename "defaultValue" $cert.Cert "context" $) }}
  {{ .Values.ingressController.tls.certKeyFilename }}: {{ include "common.secrets.lookup" (dict "secret" (printf "%s-tls" (include "apisix.ingress-controller.fullname" .)) "key" .Values.ingressController.tls.certKeyFilename "defaultValue" $cert.Key "context" $) }}
  {{ .Values.ingressController.tls.certCAFilename }}: {{ include "common.secrets.lookup" (dict "secret" (printf "%s-tls" (include "apisix.ingress-controller.fullname" .)) "key" .Values.ingressController.tls.certCAFilename "defaultValue" $ca.Cert "context" $) }}
  {{- else }}
  {{- if .Values.ingressController.tls.cert }}
  {{ .Values.ingressController.tls.certFilename }}: {{ .Values.ingressController.tls.cert | b64enc }}
  {{- end }}
  {{- if .Values.ingressController.tls.key }}
  {{ .Values.ingressController.tls.certKeyFilename }}: {{ .Values.ingressController.tls.key | b64enc }}
  {{- end }}
  {{- if .Values.ingressController.tls.ca }}
  {{ .Values.ingressController.tls.certCAFilename }}: {{ .Values.ingressController.tls.ca | b64enc }}
  {{- end }}
  {{- end }}
---
{{- end }}
