{{- if .Values.internalTLS.enabled }}
{{- $ca := genCA "harbor-internal-ca" 365 }}

{{- if not .Values.core.tls.existingSecret }}
{{- $coreCN := include "harbor.core" . }}
{{- $coreCrt := genSignedCert $coreCN (list "127.0.0.1") (list "localhost" $coreCN) 365 $ca }}
apiVersion: v1
kind: Secret
metadata:
  name: {{ include "harbor.core" . }}-crt
  namespace: {{ .Release.Namespace | quote }}
  labels: {{- include "common.labels.standard" . | nindent 4 }}
    {{- if .Values.commonLabels }}
    {{- include "common.tplvalues.render" ( dict "value" .Values.commonLabels "context" $ ) | nindent 4 }}
    {{- end }}
    app.kubernetes.io/component: core
type: kubernetes.io/tls
data:
  ca.crt: {{ $ca.Cert | b64enc | quote }}
  tls.crt: {{ $coreCrt.Cert | b64enc | quote }}
  tls.key: {{ $coreCrt.Key | b64enc | quote }}
{{- end }}
{{- if not .Values.jobservice.tls.existingSecret }}
{{- $jsCN := include "harbor.jobservice" . }}
{{- $jsCrt := genSignedCert $jsCN nil (list $jsCN) 365 $ca }}
---
apiVersion: v1
kind: Secret
metadata:
  name: {{ include "harbor.jobservice" . }}-crt
  namespace: {{ .Release.Namespace | quote }}
  labels: {{- include "common.labels.standard" . | nindent 4 }}
    {{- if .Values.commonLabels }}
    {{- include "common.tplvalues.render" ( dict "value" .Values.commonLabels "context" $ ) | nindent 4 }}
    {{- end }}
    app.kubernetes.io/component: jobservice
type: kubernetes.io/tls
data:
  ca.crt: {{ $ca.Cert | b64enc | quote }}
  tls.crt: {{ $jsCrt.Cert | b64enc | quote }}
  tls.key: {{ $jsCrt.Key | b64enc | quote }}
{{- end }}
{{- if not .Values.registry.tls.existingSecret }}
{{- $regCN := include "harbor.registry" . }}
{{- $regCrt := genSignedCert $regCN nil (list $regCN) 365 $ca }}
---
apiVersion: v1
kind: Secret
metadata:
  name: {{ include "harbor.registry" . }}-crt
  namespace: {{ .Release.Namespace | quote }}
  labels: {{- include "common.labels.standard" . | nindent 4 }}
    {{- if .Values.commonLabels }}
    {{- include "common.tplvalues.render" ( dict "value" .Values.commonLabels "context" $ ) | nindent 4 }}
    {{- end }}
    app.kubernetes.io/component: registry
type: kubernetes.io/tls
data:
  ca.crt: {{ $ca.Cert | b64enc | quote }}
  tls.crt: {{ $regCrt.Cert | b64enc | quote }}
  tls.key: {{ $regCrt.Key | b64enc | quote }}
{{- end }}
{{- if not .Values.portal.tls.existingSecret }}
{{- $portalCN := include "harbor.portal" . }}
{{- $portalCrt := genSignedCert $portalCN nil (list $portalCN) 365 $ca }}
---
apiVersion: v1
kind: Secret
metadata:
  name: {{ include "harbor.portal" . }}-crt
  namespace: {{ .Release.Namespace | quote }}
  labels: {{- include "common.labels.standard" . | nindent 4 }}
    {{- if .Values.commonLabels }}
    {{- include "common.tplvalues.render" ( dict "value" .Values.commonLabels "context" $ ) | nindent 4 }}
    {{- end }}
    app.kubernetes.io/component: portal
type: kubernetes.io/tls
data:
  ca.crt: {{ $ca.Cert | b64enc | quote }}
  tls.crt: {{ $portalCrt.Cert | b64enc | quote }}
  tls.key: {{ $portalCrt.Key | b64enc | quote }}
{{- end }}
{{- if and .Values.chartmuseum.enabled (not .Values.chartmuseum.tls.existingSecret) }}
{{- $chartCN := include "harbor.chartmuseum" . }}
{{- $chartCrt := genSignedCert $chartCN nil (list $chartCN) 365 $ca }}
---
apiVersion: v1
kind: Secret
metadata:
  name: {{ include "harbor.chartmuseum" . }}-crt
  namespace: {{ .Release.Namespace | quote }}
  labels: {{- include "common.labels.standard" . | nindent 4 }}
    {{- if .Values.commonLabels }}
    {{- include "common.tplvalues.render" ( dict "value" .Values.commonLabels "context" $ ) | nindent 4 }}
    {{- end }}
    app.kubernetes.io/component: chartmuseum
type: kubernetes.io/tls
data:
  ca.crt: {{ $ca.Cert | b64enc | quote }}
  tls.crt: {{ $chartCrt.Cert | b64enc | quote }}
  tls.key: {{ $chartCrt.Key | b64enc | quote }}
{{- end }}

{{- if and .Values.trivy.enabled (not .Values.trivy.tls.existingSecret) }}
{{- $trivyCN := include "harbor.trivy" . }}
{{- $trivyCrt := genSignedCert $trivyCN nil (list $trivyCN) 365 $ca }}
---
apiVersion: v1
kind: Secret
metadata:
  name: {{ include "harbor.trivy" . }}-crt
  namespace: {{ .Release.Namespace | quote }}
  labels: {{- include "common.labels.standard" . | nindent 4 }}
    {{- if .Values.commonLabels }}
    {{- include "common.tplvalues.render" ( dict "value" .Values.commonLabels "context" $ ) | nindent 4 }}
    {{- end }}
    app.kubernetes.io/component: trivy
type: kubernetes.io/tls
data:
  ca.crt: {{ $ca.Cert | b64enc | quote }}
  tls.crt: {{ $trivyCrt.Cert | b64enc | quote }}
  tls.key: {{ $trivyCrt.Key | b64enc | quote }}
{{- end }}
{{- end }}
