# _helpers.tpl
{{/* vim: set filetype=mustache: */}}
{{/*
Expand the name of the chart.
*/}}
{{- define "nexus.name" -}}
{{- default .Chart.Name .Values.nameOverride | trunc 63 | trimSuffix "-" -}}
{{- end -}}

{{/*
Create a default fully qualified app name.
We truncate at 63 chars because some Kubernetes name fields are limited to this (by the DNS naming spec).
If release name contains chart name it will be used as a full name.
*/}}
{{- define "nexus.fullname" -}}
{{- if .Values.fullnameOverride -}}
{{- .Values.fullnameOverride | trunc 63 | trimSuffix "-" -}}
{{- else -}}
{{- $name := default .Chart.Name .Values.nameOverride -}}
{{- if contains $name .Release.Name -}}
{{- .Release.Name | trunc 63 | trimSuffix "-" -}}
{{- else -}}
{{- printf "%s-%s" .Release.Name $name | trunc 63 | trimSuffix "-" -}}
{{- end -}}
{{- end -}}
{{- end -}}

{{/*
Create chart name and version as used by the chart label.
*/}}
{{- define "nexus.chart" -}}
{{- printf "%s-%s" .Chart.Name .Chart.Version | replace "+" "_" | trunc 63 | trimSuffix "-" -}}
{{- end -}}

{{/*
Create a default fully qualified name for proxy keystore secret.
We truncate at 63 chars because some Kubernetes name fields are limited to this (by the DNS naming spec).
*/}}
{{- define "nexus.proxy-ks.name" -}}
{{- printf "%s-%s" (include "nexus.fullname" .) "proxy-ks" | trunc 63 | trimSuffix "-" -}}
{{- end -}}

{{/*  Manage the labels for each entity  */}}
{{- define "nexus.labels" -}}
app: {{ template "nexus.name" . }}
fullname: {{ template "nexus.fullname" . }}
chart: {{ template "nexus.chart" . }}
release: {{ .Release.Name }}
heritage: {{ .Release.Service }}
{{- end -}}
 # backup-pv.yaml
{{- if not .Values.statefulset.enabled }}
{{- if .Values.nexusBackup.persistence.pdName -}}
apiVersion: v1
kind: PersistentVolume
metadata:
  name: {{ .Values.nexusBackup.persistence.pdName }}
  labels:
{{ include "nexus.labels" . | indent 4 }}
spec:
  capacity:
    storage: {{ .Values.nexusBackup.persistence.storageSize }}
  accessModes:
    - ReadWriteOnce
  claimRef:
    name: {{ template "nexus.fullname" . }}-backup
    namespace: {{ .Release.Namespace }}
  gcePersistentDisk:
    pdName: {{ .Values.nexusBackup.persistence.pdName }}
    fsType: {{ .Values.nexusBackup.persistence.fsType }}
{{- end }}
{{- end }}
 # backup-pvc.yaml
{{- if not .Values.statefulset.enabled }}
{{- if and .Values.nexusBackup.enabled (and .Values.nexusBackup.persistence.enabled (not .Values.nexusBackup.persistence.existingClaim)) }}
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: {{ template "nexus.fullname" . }}-backup
  labels:
{{ include "nexus.labels" . | indent 4 }}
{{- if .Values.nexusBackup.persistence.annotations }}
  annotations:
{{ toYaml .Values.nexusBackup.persistence.annotations | indent 4 }}
{{- end }}
spec:
  accessModes:
    - {{ .Values.nexusBackup.persistence.accessMode }}
  resources:
    requests:
      storage: {{ .Values.nexusBackup.persistence.storageSize | quote }}
{{- if .Values.nexusBackup.persistence.storageClass }}
{{- if (eq "-" .Values.nexusBackup.persistence.storageClass) }}
  storageClassName: ""
{{- else }}
  storageClassName: "{{ .Values.nexusBackup.persistence.storageClass }}"
{{- end }}
{{- end }}
{{- end }}
{{- end }}
 # backup-secret.yaml
{{- if .Values.nexusBackup.enabled }}
apiVersion: v1
kind: Secret
metadata:
  name: {{ template "nexus.fullname" . }}
  labels:
{{ include "nexus.labels" . | indent 4 }}
type: Opaque
data:
  nexus.nexusAdminPassword: {{ printf "%s%s" "Basic " (printf "%s%s" "admin:" .Values.nexusBackup.nexusAdminPassword | b64enc) | cat | b64enc | quote }}
{{- end }}
 # configmap.yaml
{{- if .Values.config.enabled -}}
apiVersion: v1
kind: ConfigMap
metadata:
  name: {{ template "nexus.name" . }}-conf
  labels:
{{ include "nexus.labels" . | indent 4 }}
{{- if .Values.nexus.labels }}
{{ toYaml .Values.nexus.labels | indent 4 }}
{{- end }}
data:
{{ toYaml .Values.config.data | indent 2 }}
{{- end }} # deployment-statefulset.yaml
{{- if .Values.statefulset.enabled }}
apiVersion: apps/v1
kind: StatefulSet
{{- else }}
apiVersion: apps/v1
kind: Deployment
{{- end }}
metadata:
  name: {{ template "nexus.fullname" . }}
  labels:
{{ include "nexus.labels" . | indent 4 }}
{{- if .Values.nexus.labels }}
{{ toYaml .Values.nexus.labels | indent 4 }}
{{- end }}
{{- if .Values.deployment.annotations }}
  annotations:
{{ toYaml .Values.deployment.annotations | indent 4 }}
{{- end }}
spec:
  replicas: {{ .Values.replicaCount }}
  {{- if .Values.statefulset.enabled }}
  {{- if .Values.nexusProxy.svcName }}
  serviceName: {{ .Values.nexusProxy.svcName }}
  {{- else }}
  serviceName: {{ template "nexus.fullname" . }}
  {{- end }}
  {{- end }}
  {{- if .Values.deploymentStrategy }}
  strategy:
{{ toYaml .Values.deploymentStrategy | indent 4 }}
  {{- end }}
  selector:
    matchLabels:
      app: {{ template "nexus.name" . }}
      release: {{ .Release.Name }}
  template:
    metadata:
    {{- if .Values.nexus.podAnnotations }}
      annotations:
{{ toYaml .Values.nexus.podAnnotations | indent 8}}
    {{- end }}
      labels:
        app: {{ template "nexus.name" . }}
        release: {{ .Release.Name }}
    spec:
      {{- if .Values.deployment.initContainers }}
      initContainers:
{{ toYaml .Values.deployment.initContainers | indent 6 }}
      {{- end }}
      {{- if .Values.nexus.nodeSelector }}
      nodeSelector:
{{ toYaml .Values.nexus.nodeSelector | indent 8 }}
      {{- end }}
      {{- if .Values.nexus.hostAliases }}
      hostAliases:
{{ toYaml .Values.nexus.hostAliases | indent 8 }}
      {{- end }}
      {{- if .Values.nexus.imagePullSecret }}
      imagePullSecrets:
        - name: {{ .Values.nexus.imagePullSecret }}
      {{- end }}
      {{- if .Values.serviceAccount.create }}
      serviceAccountName: {{ template "nexus.fullname" . }}
      {{- else }}
      serviceAccountName: {{ .Values.serviceAccount.name | quote }}
      {{- end }}
      containers:
        - name: nexus
          image: {{ .Values.nexus.imageName }}:{{ .Values.nexus.imageTag }}
          imagePullPolicy: {{ .Values.nexus.imagePullPolicy }}
          {{- if .Values.deployment.postStart.command }}
          lifecycle:
            postStart:
              exec:
                command: {{ .Values.deployment.postStart.command }}
          {{- end }}
          env:
{{ toYaml .Values.nexus.env | indent 12 }}
          resources:
{{ toYaml .Values.nexus.resources | indent 12 }}
          ports:
            - containerPort: {{ .Values.nexus.dockerPort }}
              name: nexus-docker-g
            - containerPort: {{ .Values.nexus.nexusPort }}
              name: nexus-http
          livenessProbe:
            httpGet:
              path: {{ .Values.nexus.livenessProbe.path }}
              port: {{ .Values.nexus.nexusPort }}
            initialDelaySeconds: {{ .Values.nexus.livenessProbe.initialDelaySeconds }}
            periodSeconds: {{ .Values.nexus.livenessProbe.periodSeconds }}
            failureThreshold: {{ .Values.nexus.livenessProbe.failureThreshold }}
            {{- if .Values.nexus.livenessProbe.timeoutSeconds }}
            timeoutSeconds: {{ .Values.nexus.livenessProbe.timeoutSeconds }}
            {{- end }}
          readinessProbe:
            httpGet:
              path: {{ .Values.nexus.readinessProbe.path }}
              port: {{ .Values.nexus.nexusPort }}
            initialDelaySeconds: {{ .Values.nexus.readinessProbe.initialDelaySeconds }}
            periodSeconds: {{ .Values.nexus.readinessProbe.periodSeconds }}
            failureThreshold: {{ .Values.nexus.readinessProbe.failureThreshold }}
            {{- if .Values.nexus.readinessProbe.timeoutSeconds }}
            timeoutSeconds: {{ .Values.nexus.readinessProbe.timeoutSeconds }}
            {{- end }}
          volumeMounts:
            - mountPath: /nexus-data
              name: {{ template "nexus.fullname" . }}-data
            - mountPath: /nexus-data/backup
              name: {{ template "nexus.fullname" . }}-backup
            {{- if .Values.config.enabled }}
            - mountPath: {{ .Values.config.mountPath }}
              name: {{ template "nexus.name" . }}-conf
            {{- end }}
            {{- if .Values.secret.enabled }}
            - mountPath: {{ .Values.secret.mountPath }}
              name: {{ template "nexus.name" . }}-secret
              readOnly: {{ .Values.secret.readOnly }}
            {{- end }}
            {{- if .Values.deployment.additionalVolumeMounts}}
{{ toYaml .Values.deployment.additionalVolumeMounts | indent 12 }}
            {{- end }}
          {{- if .Values.nexusProxy.enabled }}
        - name: nexus-proxy
          image: {{ .Values.nexusProxy.imageName }}:{{ .Values.nexusProxy.imageTag }}
          resources:
{{ toYaml .Values.nexusProxy.resources | indent 12 }}
          imagePullPolicy: {{ .Values.nexusProxy.imagePullPolicy }}
          env:
            - name: ALLOWED_USER_AGENTS_ON_ROOT_REGEX
              value: "GoogleHC"
            - name: CLOUD_IAM_AUTH_ENABLED
              value: {{ .Values.nexusProxy.env.cloudIamAuthEnabled | quote }}
            - name: BIND_PORT
              value: {{ .Values.nexusProxy.targetPort | quote }}
            - name: ENFORCE_HTTPS
              value: {{ .Values.nexusProxy.env.enforceHttps | quote }}
            - name: NEXUS_DOCKER_HOST
              value: {{ .Values.nexusProxy.env.nexusDockerHost | quote }}
            - name: NEXUS_HTTP_HOST
              value: {{ .Values.nexusProxy.env.nexusHttpHost | quote }}
            - name: UPSTREAM_DOCKER_PORT
              value: {{ .Values.nexus.dockerPort | quote }}
            - name: UPSTREAM_HTTP_PORT
              value: {{ .Values.nexus.nexusPort | quote }}
            - name: UPSTREAM_HOST
              value: "localhost"
            {{- if .Values.nexusProxy.env.cloudIamAuthEnabled }}
            - name: NEXUS_RUT_HEADER
              value: "X-Forwarded-User"
            - name: CLIENT_ID
              value: {{ .Values.nexusProxy.env.clientId | quote }}
            - name: CLIENT_SECRET
              value: {{ .Values.nexusProxy.env.clientSecret | quote }}
            - name: ORGANIZATION_ID
              value: {{ .Values.nexusProxy.env.organizationId | quote }}
            - name: REDIRECT_URL
              value: {{ .Values.nexusProxy.env.redirectUrl | quote }}
            - name: KEYSTORE_PASS
              valueFrom:
                secretKeyRef:
                  name: {{ template "nexus.proxy-ks.name" . }}
                  key: password
            - name: KEYSTORE_PATH
              value: "/nexus-proxy-ks/keystore"
            - name: AUTH_CACHE_TTL
              value: "60000"
            - name: SESSION_TTL
              value: "86400000"
            - name: JWT_REQUIRES_MEMBERSHIP_VERIFICATION
              value: {{ .Values.nexusProxy.env.requiredMembershipVerification | quote }}
            {{- end }}
          ports:
            - containerPort: {{ .Values.nexusProxy.targetPort }}
              name: nexus-proxy
          {{- if .Values.nexusProxy.env.cloudIamAuthEnabled }}
          volumeMounts:
            - mountPath: /nexus-proxy-ks
              name: {{ template "nexus.proxy-ks.name" . }}
              readOnly: true
          {{- end }}
          {{- end }}
        {{- if .Values.nexusBackup.enabled }}
        - name: nexus-backup
          image: {{ .Values.nexusBackup.imageName }}:{{ .Values.nexusBackup.imageTag }}
          imagePullPolicy: {{ .Values.nexusBackup.imagePullPolicy }}
          resources:
{{ toYaml .Values.nexusBackup.resources | indent 12 }}
          env:
            - name: NEXUS_AUTHORIZATION
              valueFrom:
                secretKeyRef:
                  key: nexus.nexusAdminPassword
                  name: {{ template "nexus.fullname" . }}
            - name: NEXUS_BACKUP_DIRECTORY
              value: /nexus-data/backup
            - name: NEXUS_DATA_DIRECTORY
              value: /nexus-data
            - name: NEXUS_LOCAL_HOST_PORT
              value: "localhost:{{ .Values.nexus.nexusPort }}"
            - name: OFFLINE_REPOS
              value: "maven-central maven-public maven-releases maven-snapshots"
            - name: TARGET_BUCKET
              value: {{ .Values.nexusBackup.env.targetBucket | quote }}
            - name: GRACE_PERIOD
              value: "60"
            - name: TRIGGER_FILE
              value: .backup
          volumeMounts:
            - mountPath: /nexus-data
              name: {{ template "nexus.fullname" . }}-data
            - mountPath: /nexus-data/backup
              name: {{ template "nexus.fullname" . }}-backup
        {{- end }}
        {{- if .Values.deployment.additionalContainers }}
{{ toYaml .Values.deployment.additionalContainers | indent 8 }}
        {{- end }}
      {{- if .Values.nexus.securityContext }}
      securityContext:
{{ toYaml .Values.nexus.securityContext | indent 8 }}
      {{- end }}
      volumes:
        {{- if .Values.nexusProxy.env.cloudIamAuthEnabled }}
        - name: {{ template "nexus.proxy-ks.name" . }}
          secret:
            secretName: {{ template "nexus.proxy-ks.name" . }}
        {{- end }}

        {{- if .Values.statefulset.enabled }}
        {{- if not .Values.persistence.enabled }}
        - name: {{ template "nexus.fullname" . }}-data
          emptyDir: {}
        {{- end }}
        {{- if not (and .Values.nexusBackup.enabled .Values.nexusBackup.persistence.enabled) }}
        - name: {{ template "nexus.fullname" . }}-backup
          emptyDir: {}
        {{- end }}
        {{- else }}
        - name: {{ template "nexus.fullname" . }}-data
          {{- if .Values.persistence.enabled }}
          persistentVolumeClaim:
            claimName: {{ .Values.persistence.existingClaim | default (printf "%s-%s" (include "nexus.fullname" .) "data") }}
          {{- else }}
          emptyDir: {}
          {{- end }}
        - name: {{ template "nexus.fullname" . }}-backup
          {{- if and .Values.nexusBackup.enabled (.Values.nexusBackup.persistence.enabled) }}
          persistentVolumeClaim:
            claimName: {{ .Values.nexusBackup.persistence.existingClaim | default (printf "%s-%s" (include "nexus.fullname" .) "backup") }}
          {{- else }}
          emptyDir: {}
          {{- end }}
        {{- end }}
        {{- if .Values.config.enabled }}
        - name: {{ template "nexus.name" . }}-conf
          configMap:
            name: {{ template "nexus.name" . }}-conf
        {{- end }}
        {{- if .Values.secret.enabled }}
        - name: {{ template "nexus.name" . }}-secret
          secret:
            secretName: {{ template "nexus.name" . }}-secret
        {{- end }}
        {{- if .Values.deployment.additionalVolumes }}
{{ toYaml .Values.deployment.additionalVolumes | indent 8 }}
        {{- end }}
    {{- with .Values.tolerations }}
      tolerations:
{{ toYaml . | indent 8 }}
    {{- end }}


## create pvc in case of statefulsets
  {{- if .Values.statefulset.enabled }}
  volumeClaimTemplates:
    {{- if .Values.persistence.enabled }}
    - metadata:
        name: {{ template "nexus.fullname" . }}-data
        labels:
{{ include "nexus.labels" . | indent 10 }}
        {{- if .Values.persistence.annotations }}
        annotations:
{{ toYaml .Values.persistence.annotations | indent 10 }}
        {{- end }}
      spec:
        accessModes:
          - {{ .Values.persistence.accessMode | quote }}
        resources:
          requests:
            storage: {{ .Values.persistence.storageSize | quote }}
        {{- if .Values.persistence.storageClass }}
        {{- if (eq "-" .Values.persistence.storageClass) }}
        storageClassName: ""
        {{- else }}
        storageClassName: "{{ .Values.persistence.storageClass }}"
        {{- end }}
        {{- end }}
    {{- end }}

    {{- if and .Values.nexusBackup.enabled (.Values.nexusBackup.persistence.enabled) }}
    - metadata:
        name: {{ template "nexus.fullname" . }}-backup
        labels:
{{ include "nexus.labels" . | indent 10 }}
        {{- if .Values.nexusBackup.persistence.annotations }}
        annotations:
{{ toYaml .Values.nexusBackup.persistence.annotations | indent 10 }}
        {{- end }}
      spec:
        accessModes:
          - {{ .Values.nexusBackup.persistence.accessMode }}
        resources:
          requests:
            storage: {{ .Values.nexusBackup.persistence.storageSize | quote }}
        {{- if .Values.nexusBackup.persistence.storageClass }}
        {{- if (eq "-" .Values.nexusBackup.persistence.storageClass) }}
        storageClassName: ""
        {{- else }}
        storageClassName: "{{ .Values.nexusBackup.persistence.storageClass }}"
        {{- end }}
        {{- end }}
    {{- end }}
  {{- end }}
 # ingress.yaml
{{- if .Values.ingress.enabled -}}
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: {{ template "nexus.fullname" . }}
  labels:
{{ include "nexus.labels" . | indent 4 }}
  annotations:
    {{- range $key, $value := .Values.ingress.annotations }}
    {{ $key }}: {{ $value | quote }}
    {{- end }}
spec:
  rules:
{{- if .Values.nexusProxy.env.nexusHttpHost }}
    - host: {{ .Values.nexusProxy.env.nexusHttpHost }}
      http:
        paths:
          - backend:
            {{- if .Values.nexusProxy.svcName }}
              serviceName: {{ .Values.nexusProxy.svcName }}
            {{- else }}
              serviceName: {{ template "nexus.fullname" . }}
            {{- end }}
{{- if .Values.nexusProxy.enabled }}
              servicePort: {{ .Values.nexusProxy.port }}
{{- else }}
              servicePort: {{ .Values.nexus.nexusPort }}
{{- end }}
            path: {{ .Values.ingress.path }}
{{- end }}
{{- if .Values.nexusProxy.enabled -}}
  {{- if .Values.nexusProxy.env.nexusDockerHost }}
    - host: {{ .Values.nexusProxy.env.nexusDockerHost }}
      http:
        paths:
          - backend:
            {{- if .Values.nexusProxy.svcName }}
              serviceName: {{ .Values.nexusProxy.svcName }}
            {{- else }}
              serviceName: {{ template "nexus.fullname" . }}
            {{- end }}
              servicePort: {{ .Values.nexusProxy.port }}
            path: {{ .Values.ingress.path }}
  {{- end }}
{{- end -}}
{{- with .Values.ingress.rules  }}
{{ toYaml . | indent 2 }}
  {{- end -}}
{{- if .Values.ingress.tls.enabled }}
  tls:
    - hosts:
      {{- if .Values.nexusProxy.env.nexusHttpHost }}
        - {{ .Values.nexusProxy.env.nexusHttpHost }}
      {{- end }}
      {{- if .Values.nexusProxy.env.nexusDockerHost }}
        - {{ .Values.nexusProxy.env.nexusDockerHost }}
      {{- end }}
      {{- if .Values.ingress.tls.secretName }}
      secretName: {{ .Values.ingress.tls.secretName | quote }}
      {{- end }}
{{- end -}}
{{- end }}
 # proxy-ks-secret.yaml
{{- if .Values.nexusProxy.env.cloudIamAuthEnabled }}
apiVersion: v1
kind: Secret
metadata:
  name: {{ template "nexus.proxy-ks.name" . }}
  labels:
{{ include "nexus.labels" . | indent 4 }}
type: Opaque
data:
  keystore: {{ .Values.nexusProxy.secrets.keystore }}
  password: {{ .Values.nexusProxy.secrets.password | b64enc }}
{{- end}}
 # proxy-route.yaml
{{- if .Values.nexusProxyRoute.enabled }}
apiVersion: route.openshift.io/v1
kind: Route
metadata:
  name: {{ template "nexus.fullname" . }}
  labels: {{ .Values.nexusProxyRoute.labels }}
  annotations:
    {{- range $key, $value := .Values.nexusProxyRoute.annotations }}
    {{ $key }}: {{ $value | quote }}
    {{- end }}
spec:
  host: {{ .Values.nexusProxyRoute.path }}
  port:
    {{- if .Values.nexusProxy.svcName }}
      targetPort: {{ .Values.nexusProxy.svcName }}
    {{- else }}
      targetPort: {{ template "nexus.fullname" . }}
    {{- end }}
  tls:
    insecureEdgeTerminationPolicy: Redirect
    termination: edge
  to:
    kind: Service
{{- if .Values.nexusProxy.svcName }}
    name: {{ .Values.nexusProxy.svcName }}
{{- else }}
    name: {{ template "nexus.fullname" . }}
{{- end }}
    weight: 100
  wildcardPolicy: None
{{- end }}
 # proxy-svc.yaml
{{- if or .Values.nexusProxy.enabled .Values.ingress.enabled }}
apiVersion: v1
kind: Service
metadata:
{{- if .Values.nexusProxy.svcName }}
  name: {{ .Values.nexusProxy.svcName }}
{{- else }}
  name: {{ template "nexus.fullname" . }}
{{- end }}
  labels:
{{ include "nexus.labels" . | indent 4 }}
{{- if .Values.nexus.labels }}
{{ toYaml .Values.nexus.labels | indent 4 }}
{{- end }}
{{- if .Values.nexusProxy.labels }}
{{ toYaml .Values.nexusProxy.labels | indent 4 }}
{{- end }}
{{- if .Values.nexus.annotations }}
  annotations:
{{ toYaml .Values.nexus.annotations | indent 4 }}
{{- end }}
spec:
  ports:
{{- if .Values.nexusProxy.enabled }}
    - port: {{ .Values.nexusProxy.port }}
{{- else }}
    - port: {{ .Values.nexus.nexusPort }}
{{- end }}
{{- if .Values.nexusProxy.svcName }}
      name: {{ .Values.nexusProxy.svcName }}
{{- else }}
      name: {{ template "nexus.fullname" . }}
{{- end }}
      protocol: TCP
{{- if .Values.nexusProxy.enabled }}
      targetPort: {{ .Values.nexusProxy.targetPort }}
{{- else }}
      targetPort: {{ .Values.nexus.nexusPort }}
{{- end }}
  selector:
    app: {{ template "nexus.name" . }}
    release: {{ .Release.Name }}
  type: {{ .Values.nexus.service.type }}
  {{- if and (eq .Values.nexus.service.type "ClusterIP") .Values.nexus.service.clusterIP }}
  clusterIP: {{ .Values.nexus.service.clusterIP }}
  {{- end }}
{{- end }}
 # pv.yaml
{{- if not .Values.statefulset.enabled }}
{{- if .Values.persistence.pdName -}}
apiVersion: v1
kind: PersistentVolume
metadata:
  name: {{ .Values.persistence.pdName }}
  labels:
{{ include "nexus.labels" . | indent 4 }}
spec:
  capacity:
    storage: {{ .Values.persistence.storageSize }}
  accessModes:
    - ReadWriteOnce
  claimRef:
    namespace: {{ .Release.Namespace }}
    name: {{ template "nexus.fullname" . }}-data
  gcePersistentDisk:
    pdName: {{ .Values.persistence.pdName }}
    fsType: {{ .Values.persistence.fsType }}
{{- end }}
{{- end }}
 # pvc.yaml
{{- if not .Values.statefulset.enabled }}
{{- if and .Values.persistence.enabled (not .Values.persistence.existingClaim) }}
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: {{ template "nexus.fullname" . }}-data
  labels:
{{ include "nexus.labels" . | indent 4 }}
{{- if .Values.persistence.annotations }}
  annotations:
{{ toYaml .Values.persistence.annotations | indent 4 }}
{{- end }}
spec:
  accessModes:
    - {{ .Values.persistence.accessMode | quote }}
  resources:
    requests:
      storage: {{ .Values.persistence.storageSize | quote }}
{{- if .Values.persistence.storageClass }}
{{- if (eq "-" .Values.persistence.storageClass) }}
  storageClassName: ""
{{- else }}
  storageClassName: "{{ .Values.persistence.storageClass }}"
{{- end }}
{{- end }}
{{- end }}
{{- end }}
 # route.yaml
{{- if .Values.route.enabled }}
apiVersion: route.openshift.io/v1
kind: Route
metadata:
  name: {{ .Values.route.name }}
  labels: {{ .Values.route.labels }}
  annotations:
    {{- range $key, $value := .Values.route.annotations }}
    {{ $key }}: {{ $value | quote }}
    {{- end }}
spec:
  host: {{ .Values.route.path }}
  port:
    targetPort: {{ .Values.service.portName }}
  tls:
    insecureEdgeTerminationPolicy: Redirect
    termination: edge
  to:
    kind: Service
{{- if .Values.service.name }}
    name: {{ .Values.service.name }}
{{- else }}
    name: {{ template "nexus.name" . }}-service
{{- end }}
    weight: 100
  wildcardPolicy: None
{{- end }}
 # secret.yaml
{{- if .Values.secret.enabled -}}
apiVersion: v1
kind: Secret
metadata:
  name: {{ template "nexus.name" . }}-secret
  labels:
{{ include "nexus.labels" . | indent 4 }}
{{- if .Values.nexus.labels }}
{{ toYaml .Values.nexus.labels | indent 4 }}
{{- end }}
data:
{{ toYaml .Values.secret.data | indent 2 }}
{{- end}}
 # service.yaml
{{- if .Values.service.enabled -}}
apiVersion: v1
kind: Service
metadata:
{{- if .Values.service.name }}
  name: {{ .Values.service.name }}
{{- else }}
  name: {{ template "nexus.name" . }}-service
{{- end }}
  labels:
{{ include "nexus.labels" . | indent 4 }}
{{- if .Values.service.labels }}
{{ toYaml .Values.service.labels | indent 4 }}
{{- end }}
{{- if .Values.service.annotations }}
  annotations:
{{ toYaml .Values.service.annotations | indent 4 }}
{{- end }}
spec:
  ports:
  {{- if .Values.service.portName }}
  - name: {{ .Values.service.portName }}
    port: {{ .Values.service.port }}
    targetPort: {{ .Values.service.targetPort }}
  {{- end }}
  {{- with .Values.service.ports  }}
{{ toYaml . | indent 2 }}
  {{- end }}
  selector:
    app: {{ template "nexus.name" . }}
    release: {{ .Release.Name }}
  type: {{ .Values.service.serviceType }}
  {{ if .Values.service.loadBalancerSourceRanges }}
  loadBalancerSourceRanges:
    {{- range .Values.service.loadBalancerSourceRanges }}
    - {{ . }}
    {{- end }}
  {{ end }}
{{- end}}

 # serviceaccount.yaml
{{- if .Values.serviceAccount.create }}
apiVersion: v1
kind: ServiceAccount
metadata:
  name: {{ template "nexus.fullname" . }}
  {{- if .Values.serviceAccount.annotations }}
  annotations: {{ toYaml .Values.serviceAccount.annotations | nindent 4 }}
  {{- end }}
{{- end }}

statefulset:
  enabled: false
replicaCount: 1
# By default deploymentStrategy is set to rollingUpdate with maxSurge of 25% and maxUnavailable of 25% . you can change type to `Recreate` or can uncomment `rollingUpdate` specification and adjust them to your usage.
deploymentStrategy: {}
  # rollingUpdate:
  #   maxSurge: 25%
  #   maxUnavailable: 25%
  # type: RollingUpdate

nexus:
  imageName: sonatype/nexus3
  imageTag: 3.20.1
  imagePullPolicy: IfNotPresent
  env:
    - name: install4jAddVmParams
      value: "-Xms1200M -Xmx1200M -XX:MaxDirectMemorySize=2G -XX:+UnlockExperimentalVMOptions -XX:+UseCGroupMemoryLimitForHeap"
    - name: NEXUS_SECURITY_RANDOMPASSWORD
      value: "false"
  # nodeSelector:
  #   cloud.google.com/gke-nodepool: default-pool
  resources: {}
    # requests:
      ## Based on https://support.sonatype.com/hc/en-us/articles/115006448847#mem
      ## and https://twitter.com/analytically/status/894592422382063616:
      ##   Xms == Xmx
      ##   Xmx <= 4G
      ##   MaxDirectMemory >= 2G
      ##   Xmx + MaxDirectMemory <= RAM * 2/3 (hence the request for 4800Mi)
      ##   MaxRAMFraction=1 is not being set as it would allow the heap
      ##     to use all the available memory.
      # cpu: 250m
      # memory: 4800Mi
  # The ports should only be changed if the nexus image uses a different port
  dockerPort: 5003
  nexusPort: 8081
  service:
    type: NodePort
    # clusterIP: None
  # annotations: {}
    ## When using LoadBalancer service type, use the following AWS certificate from ACM
    ## https://aws.amazon.com/documentation/acm/
    # service.beta.kubernetes.io/aws-load-balancer-ssl-cert: "arn:aws:acm:eu-west-1:123456789:certificate/abc123-abc123-abc123-abc123"
    # service.beta.kubernetes.io/aws-load-balancer-backend-protocol: "https"
    # service.beta.kubernetes.io/aws-load-balancer-backend-port: "https"
  ## When using LoadBalancer service type, whitelist these source IP ranges
  ## https://kubernetes.io/docs/tasks/access-application-cluster/configure-cloud-provider-firewall/
  # loadBalancerSourceRanges:
  #   - 192.168.1.10/32
  # labels: {}
  securityContext:
    fsGroup: 2000
  podAnnotations: {}
  livenessProbe:
    initialDelaySeconds: 30
    periodSeconds: 30
    failureThreshold: 6
    # timeoutSeconds: 10
    path: /
  readinessProbe:
    initialDelaySeconds: 30
    periodSeconds: 30
    failureThreshold: 6
    # timeoutSeconds: 10
    path: /
  # hostAliases allows the modification of the hosts file inside a container
  hostAliases: []
  # - ip: "192.168.1.10"
  #   hostnames:
  #   - "example.com"
  #   - "www.example.com"

route:
  enabled: false
  name: docker
  portName: docker
  labels:
  annotations:
  # path: /docker

nexusProxy:
  enabled: true
  # svcName: proxy-svc
  imageName: quay.io/travelaudience/docker-nexus-proxy
  imageTag: 2.5.0
  imagePullPolicy: IfNotPresent
  port: 8080
  targetPort: 8080
  # labels: {}
  env:
    nexusDockerHost:
    nexusHttpHost:
    enforceHttps: false
    cloudIamAuthEnabled: false
## If cloudIamAuthEnabled is set to true uncomment the variables below and remove this line
  #   clientId: ""
  #   clientSecret: ""
  #   organizationId: ""
  #   redirectUrl: ""
  #   requiredMembershipVerification: "true"
  # secrets:
  #   keystore: ""
  #   password: ""
  resources: {}
    # requests:
      # cpu: 100m
      # memory: 256Mi
    # limits:
      # cpu: 200m
      # memory: 512Mi

nexusProxyRoute:
  enabled: false
  labels:
  annotations:
  # path: /nexus

persistence:
  enabled: true
  accessMode: ReadWriteOnce
  ## If defined, storageClass: <storageClass>
  ## If set to "-", storageClass: "", which disables dynamic provisioning
  ## If undefined (the default) or set to null, no storageClass spec is
  ##   set, choosing the default provisioner.  (gp2 on AWS, standard on
  ##   GKE, AWS & OpenStack)
  ##
  # existingClaim:
  # annotations:
  #  "helm.sh/resource-policy": keep
  # storageClass: "-"
  storageSize: 8Gi
  # If PersistentDisk already exists you can create a PV for it by including the 2 following keypairs.
  # pdName: nexus-data-disk
  # fsType: ext4

nexusBackup:
  enabled: false
  imageName: quay.io/travelaudience/docker-nexus-backup
  imageTag: 1.5.0
  imagePullPolicy: IfNotPresent
  env:
    targetBucket:
  nexusAdminPassword: "admin123"
  persistence:
    enabled: true
    # existingClaim:
    # annotations:
    #  "helm.sh/resource-policy": keep
    accessMode: ReadWriteOnce
    # See comment above for information on setting the backup storageClass
    # storageClass: "-"
    storageSize: 8Gi
    # If PersistentDisk already exists you can create a PV for it by including the 2 following keypairs.
    # pdName: nexus-backup-disk
    # fsType: ext4
  resources: {}
    # requests:
      # cpu: 100m
      # memory: 256Mi
    # limits:
      # cpu: 200m
      # memory: 512Mi

serviceAccount:
  # Specifies whether a service account should be created
  create: true
  # The name of the service account to use.
  # If not set and create is true, a name is generated using the fullname template
  name:
  annotations: {}

ingress:
  enabled: false
  path: /
  annotations: {}
  # # NOTE: Can't use 'false' due to https://github.com/jetstack/kube-lego/issues/173.
  # kubernetes.io/ingress.allow-http: true
  # kubernetes.io/ingress.class: gce
  # kubernetes.io/ingress.global-static-ip-name: ""
  # kubernetes.io/tls-acme: true
  tls:
    enabled: true
    secretName: nexus-tls
  # Specify custom rules in addition to or instead of the nexus-proxy rules
  rules:
  # - host: http://nexus.127.0.0.1.nip.io
  #   http:
  #     paths:
  #     - backend:
  #         serviceName: additional-svc
  #         servicePort: 80


tolerations: []

# # Enable configmap and add data in configmap
config:
  enabled: false
  mountPath: /sonatype-nexus-conf
  data:

deployment:
  # # Add annotations in deployment to enhance deployment configurations
  annotations: {}
  # # Add init containers. e.g. to be used to give specific permissions for nexus-data.
  # # Add your own init container or uncomment and modify the given example.
  initContainers:
  # - name: fmp-volume-permission
    # image: busybox
    # imagePullPolicy: IfNotPresent
    # command: ['chown','-R', '200', '/nexus-data']
    # volumeMounts:
      # - name: nexus-data
        # mountPath: /nexus-data
  # # Uncomment and modify this to run a command after starting the nexus container.
  postStart:
    command:    # '["/bin/sh", "-c", "ls"]'
  additionalContainers:
  additionalVolumes:
  additionalVolumeMounts:

# # To use an additional secret, set enable to true and add data
secret:
  enabled: false
  mountPath: /etc/secret-volume
  readOnly: true
  data:

# # To use an additional service, set enable to true
service:
  # name: additional-svc
  enabled: false
  labels: {}
  annotations: {}
  ports:
  - name: nexus-service
    targetPort: 80
    port: 80
