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

apiVersion: v1
kind: ConfigMap
metadata:
  name: {{ include "harbor.registry" . }}
  namespace: {{ include "common.names.namespace" . | quote }}
  {{- $versionLabel := dict "app.kubernetes.io/version" ( include "common.images.version" ( dict "imageRoot" .Values.registry.server.image "chart" .Chart ) ) }}
  {{- $labels := include "common.tplvalues.merge" ( dict "values" ( list .Values.commonLabels $versionLabel ) "context" . ) }}
  labels: {{- include "common.labels.standard" ( dict "customLabels" $labels "context" $ ) | nindent 4 }}
  {{- if .Values.commonAnnotations }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" .Values.commonAnnotations "context" $ ) | nindent 4 }}
  {{- end }}
data:
  config.yml: |+
    version: 0.1
    log:
      level: {{ .Values.logLevel }}
      fields:
        service: registry
    storage:
      {{- if eq .Values.persistence.imageChartStorage.type "filesystem" }}
      filesystem:
        rootdirectory: {{ .Values.persistence.imageChartStorage.filesystem.rootdirectory }}
        {{- if .Values.persistence.imageChartStorage.filesystem.maxthreads }}
        maxthreads: {{ .Values.persistence.imageChartStorage.filesystem.maxthreads }}
        {{- end }}
      {{- else if eq .Values.persistence.imageChartStorage.type "azure" }}
      azure:
        accountname: {{ .Values.persistence.imageChartStorage.azure.accountname }}
        container: {{ .Values.persistence.imageChartStorage.azure.container }}
        {{- if .Values.persistence.imageChartStorage.azure.realm }}
        realm: {{ .Values.persistence.imageChartStorage.azure.realm }}
        {{- end }}
      {{- else if eq .Values.persistence.imageChartStorage.type "gcs" }}
      gcs:
        bucket: {{ .Values.persistence.imageChartStorage.gcs.bucket }}
        {{- if .Values.persistence.imageChartStorage.gcs.encodedkey }}
        keyfile: /etc/registry/gcs-key.json
        {{- end }}
        {{- if .Values.persistence.imageChartStorage.gcs.rootdirectory }}
        rootdirectory: {{ .Values.persistence.imageChartStorage.gcs.rootdirectory }}
        {{- end }}
        {{- if .Values.persistence.imageChartStorage.gcs.chunksize }}
        chunksize: {{ .Values.persistence.imageChartStorage.gcs.chunksize }}
        {{- end }}
      {{- else if eq .Values.persistence.imageChartStorage.type "s3" }}
      s3:
        {{- if .Values.persistence.imageChartStorage.s3.region }}
        region: {{ .Values.persistence.imageChartStorage.s3.region }}
        {{- end }}
        {{- if .Values.persistence.imageChartStorage.s3.bucket }}
        bucket: {{ .Values.persistence.imageChartStorage.s3.bucket }}
        {{- end }}
        {{- if .Values.persistence.imageChartStorage.s3.regionendpoint }}
        regionendpoint: {{ .Values.persistence.imageChartStorage.s3.regionendpoint }}
        {{- end }}
        {{- if .Values.persistence.imageChartStorage.s3.encrypt }}
        encrypt: {{ .Values.persistence.imageChartStorage.s3.encrypt }}
        {{- end }}
        {{- if .Values.persistence.imageChartStorage.s3.keyid }}
        keyid: {{ .Values.persistence.imageChartStorage.s3.keyid }}
        {{- end }}
        {{- if .Values.persistence.imageChartStorage.s3.secure }}
        secure: {{ .Values.persistence.imageChartStorage.s3.secure }}
        {{- end }}
        {{- if .Values.persistence.imageChartStorage.s3.skipverify }}
        skipverify: {{ .Values.persistence.imageChartStorage.s3.skipverify }}
        {{- end }}
        {{- if .Values.persistence.imageChartStorage.s3.v4auth }}
        v4auth: {{ .Values.persistence.imageChartStorage.s3.v4auth }}
        {{- end }}
        {{- if .Values.persistence.imageChartStorage.s3.chunksize }}
        chunksize: {{ .Values.persistence.imageChartStorage.s3.chunksize }}
        {{- end }}
        {{- if .Values.persistence.imageChartStorage.s3.rootdirectory }}
        rootdirectory: {{ .Values.persistence.imageChartStorage.s3.rootdirectory }}
        {{- end }}
        {{- if .Values.persistence.imageChartStorage.s3.storageClass }}
        storageclass: {{ .Values.persistence.imageChartStorage.s3.storageClass }}
        {{- end }}
        {{- if .Values.persistence.imageChartStorage.s3.multipartcopychunksize }}
        multipartcopychunksize: {{ .Values.persistence.imageChartStorage.s3.multipartcopychunksize }}
        {{- end }}
        {{- if .Values.persistence.imageChartStorage.s3.multipartcopymaxconcurrency }}
        multipartcopymaxconcurrency: {{ .Values.persistence.imageChartStorage.s3.multipartcopymaxconcurrency }}
        {{- end }}
        {{- if .Values.persistence.imageChartStorage.s3.multipartcopythresholdsize }}
        multipartcopythresholdsize: {{ .Values.persistence.imageChartStorage.s3.multipartcopythresholdsize }}
        {{- end }}
      {{- else if eq .Values.persistence.imageChartStorage.type "swift" }}
      swift:
        authurl: {{ .Values.persistence.imageChartStorage.swift.authurl }}
        username: {{ .Values.persistence.imageChartStorage.swift.username }}
        container: {{ .Values.persistence.imageChartStorage.swift.container }}
        {{- if .Values.persistence.imageChartStorage.swift.region }}
        region: {{ .Values.persistence.imageChartStorage.swift.region }}
        {{- end }}
        {{- if .Values.persistence.imageChartStorage.swift.tenant }}
        tenant: {{ .Values.persistence.imageChartStorage.swift.tenant }}
        {{- end }}
        {{- if .Values.persistence.imageChartStorage.swift.tenantid }}
        tenantid: {{ .Values.persistence.imageChartStorage.swift.tenantid }}
        {{- end }}
        {{- if .Values.persistence.imageChartStorage.swift.domain }}
        domain: {{ .Values.persistence.imageChartStorage.swift.domain }}
        {{- end }}
        {{- if .Values.persistence.imageChartStorage.swift.domainid }}
        domainid: {{ .Values.persistence.imageChartStorage.swift.domainid }}
        {{- end }}
        {{- if .Values.persistence.imageChartStorage.swift.trustid }}
        trustid: {{ .Values.persistence.imageChartStorage.swift.trustid }}
        {{- end }}
        {{- if .Values.persistence.imageChartStorage.swift.insecureskipverify }}
        insecureskipverify: {{ .Values.persistence.imageChartStorage.swift.insecureskipverify }}
        {{- end }}
        {{- if .Values.persistence.imageChartStorage.swift.chunksize }}
        chunksize: {{ .Values.persistence.imageChartStorage.swift.chunksize }}
        {{- end }}
        {{- if .Values.persistence.imageChartStorage.swift.prefix }}
        prefix: {{ .Values.persistence.imageChartStorage.swift.prefix }}
        {{- end }}
        {{- if .Values.persistence.imageChartStorage.swift.authversion }}
        authversion: {{ .Values.persistence.imageChartStorage.swift.authversion }}
        {{- end }}
        {{- if .Values.persistence.imageChartStorage.swift.endpointtype }}
        endpointtype: {{ .Values.persistence.imageChartStorage.swift.endpointtype }}
        {{- end }}
        {{- if .Values.persistence.imageChartStorage.swift.tempurlcontainerkey }}
        tempurlcontainerkey: {{ .Values.persistence.imageChartStorage.swift.tempurlcontainerkey }}
        {{- end }}
        {{- if .Values.persistence.imageChartStorage.swift.tempurlmethods }}
        tempurlmethods: {{ .Values.persistence.imageChartStorage.swift.tempurlmethods }}
        {{- end }}
      {{- else if eq .Values.persistence.imageChartStorage.type "oss" }}
      oss:
        accesskeyid: {{ .Values.persistence.imageChartStorage.oss.accesskeyid }}
        region: {{ .Values.persistence.imageChartStorage.oss.region }}
        bucket: {{ .Values.persistence.imageChartStorage.oss.bucket }}
        {{- if .Values.persistence.imageChartStorage.oss.endpoint }}
        endpoint: {{ .Values.persistence.imageChartStorage.oss.bucket }}.{{ .Values.persistence.imageChartStorage.oss.endpoint }}
        {{- end }}
        {{- if .Values.persistence.imageChartStorage.oss.internal }}
        internal: {{ .Values.persistence.imageChartStorage.oss.internal }}
        {{- end }}
        {{- if .Values.persistence.imageChartStorage.oss.encrypt }}
        encrypt: {{ .Values.persistence.imageChartStorage.oss.encrypt }}
        {{- end }}
        {{- if .Values.persistence.imageChartStorage.oss.secure }}
        secure: {{ .Values.persistence.imageChartStorage.oss.secure }}
        {{- end }}
        {{- if .Values.persistence.imageChartStorage.oss.chunksize }}
        chunksize: {{ .Values.persistence.imageChartStorage.oss.chunksize }}
        {{- end }}
        {{- if .Values.persistence.imageChartStorage.oss.rootdirectory }}
        rootdirectory: {{ .Values.persistence.imageChartStorage.oss.rootdirectory }}
        {{- end }}
      {{- end }}
      cache:
        layerinfo: redis
      maintenance:
        uploadpurging:
          {{- if .Values.registry.uploadPurging.enabled }}
          enabled: true
          age: {{ .Values.registry.uploadPurging.age }}
          interval: {{ .Values.registry.uploadPurging.interval }}
          dryrun: {{ .Values.registry.uploadPurging.dryrun }}
          {{- else }}
          enabled: false
          {{- end }}
      delete:
        enabled: true
      redirect:
        disable: {{ .Values.persistence.imageChartStorage.disableredirect }}
    redis:
      addr: "{{ template "harbor.redis.host" ( dict "context" . "component" "registry" ) }}:{{ template "harbor.redis.port" ( dict "context" . "component" "registry" ) }}"
      {{- if ne "" (include "harbor.redis.sentinel.masterSet" ( dict "context" . "component" "registry" ) ) }}
      sentinelMasterSet: "{{ template "harbor.redis.sentinel.masterSet" ( dict "context" . "component" "registry" ) }}"
      {{- end }}
      {{- $externalRedis := ternary ( get .Values.externalRedis "registry" ) .Values.externalRedis .Values.externalRedis.instancePerComponent }}
      enableTLS: {{ or .Values.redis.tls.enabled $externalRedis.tls.enabled }}
      {{- if not (include "harbor.redis.sentinel.enabled" ( dict "context" . "component" "registry" )) }}
      db: {{ template "harbor.redis.registryDatabaseIndex" . }}
      {{- end }}
    http:
      relativeurls: {{ .Values.registry.relativeurls }}
      addr: :{{ ternary .Values.registry.server.containerPorts.https .Values.registry.server.containerPorts.http .Values.internalTLS.enabled }}
      {{- if .Values.internalTLS.enabled }}
      tls:
        certificate: /etc/harbor/ssl/registry/tls.crt
        key: /etc/harbor/ssl/registry/tls.key
        minimumtls: tls1.2
      {{- end }}
      # set via environment variable
      # secret: placeholder
      debug:
        {{- if .Values.metrics.enabled}}
        addr: :{{ .Values.registry.server.containerPorts.metrics }}
        prometheus:
          enabled: true
          path: {{ .Values.metrics.path }}
      {{- else }}
        addr: localhost:{{ .Values.registry.server.containerPorts.debug }}
      {{- end }}
    auth:
      htpasswd:
        realm: harbor-registry-basic-realm
        path: /etc/registry/passwd
    validation:
      disabled: true
    compatibility:
      schema1:
        enabled: true
    {{- if .Values.registry.middleware.enabled }}
    {{- if eq .Values.registry.middleware.type "cloudFront" }}
    middleware:
      storage:
        - name: cloudfront
          options:
            baseurl: {{ .Values.registry.middleware.cloudFront.baseurl }}
            privatekey: /etc/registry/pk.pem
            keypairid: {{ .Values.registry.middleware.cloudFront.keypairid }}
            duration: {{ .Values.registry.middleware.cloudFront.duration }}
            ipfilteredby: {{ .Values.registry.middleware.cloudFront.ipfilteredby }}
    {{- end }}
    {{- end }}
  ctl-config.yml: |+
    ---
    {{- if .Values.internalTLS.enabled }}
    protocol: "https"
    port: {{ .Values.registry.controller.containerPorts.https }}
    https_config:
      cert: "/etc/harbor/ssl/registry/tls.crt"
      key: "/etc/harbor/ssl/registry/tls.key"
    {{- else }}
    protocol: "http"
    port: {{ .Values.registry.controller.containerPorts.http }}
    {{- end }}
    log_level: {{ .Values.logLevel }}
    registry_config: "/etc/registry/config.yml"
