# _helpers.tpl
{{/* vim: set filetype=mustache: */}}
{{/*
Expand the name of the chart.
*/}}
{{- define "satisfy.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 "satisfy.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 "satisfy.chart" -}}
{{- printf "%s-%s" .Chart.Name .Chart.Version | replace "+" "_" | trunc 63 | trimSuffix "-" -}}
{{- end -}}

{{- define "satisfy.image" -}}
{{- if .Values.image.digest -}}
{{- printf "%s@%s" .Values.image.repository .Values.image.digest -}}
{{- else -}}
{{- printf "%s:%s" .Values.image.repository .Values.image.tag -}}
{{- end -}}
{{- end -}}

{{/*
Use apps/v1 when supported, fallback to apps/v1beta2 if it's not.
*/}}
{{- define "satisfy.deployAPIVersion" -}}
{{- if .Capabilities.APIVersions.Has "apps/v1" -}}
{{- printf "apps/v1" -}}
{{- else -}}
{{- printf "apps/v1beta2" -}}
{{- end -}}
{{- end -}}
 # deployment.yaml
{{- $apiVersion := include "satisfy.deployAPIVersion" . -}}
apiVersion: {{ $apiVersion }}
kind: Deployment
metadata:
  name: {{ include "satisfy.fullname" . }}
  labels:
    app.kubernetes.io/name: {{ include "satisfy.name" . }}
    helm.sh/chart: {{ include "satisfy.chart" . }}
    app.kubernetes.io/instance: {{ .Release.Name }}
    app.kubernetes.io/managed-by: {{ .Release.Service }}
spec:
  replicas: 1
  selector:
    matchLabels:
      app.kubernetes.io/name: {{ include "satisfy.name" . }}
      app.kubernetes.io/instance: {{ .Release.Name }}
  template:
    metadata:
      labels:
        app.kubernetes.io/name: {{ include "satisfy.name" . }}
        app.kubernetes.io/instance: {{ .Release.Name }}
    spec:
      terminationGracePeriodSeconds: {{ .Values.terminationGracePeriodSeconds }}
      {{- with .Values.image.pullSecrets }}
      imagePullSecrets:
{{ toYaml . | indent 8 }}
      {{- end }}
      initContainers:
        - name: init
          image: "{{ include "satisfy.image" . }}"
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          command:
            - /bin/sh
            - -c
            - |
              set -x
              set -e
              if [[ -e /app_persist/satis.json ]]; then
                echo >&2 "Existing installation found, moving on.."
                exit 0
              else
                echo >&2 "Seeding new instance.."
                cp -r /app/* /app_persist/
                for ref in $(find /app_persist -mindepth 1 -maxdepth 1 -not -name ".snapshot"); do
                  chown -R satisfy:satisfy $ref
                done
              fi
              echo >&2 "Init completed.."
          securityContext:
            runAsUser: 0
          volumeMounts:
            - name: app-persist
              mountPath: /app_persist
      containers:
        - name: satisfy
          image: "{{ include "satisfy.image" . }}"
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          ports:
            - name: http
              containerPort: 80
              protocol: TCP
          env:
            - name: REPO_NAME
              value: "{{ .Values.satisfy.repoName }}"
            - name: HOMEPAGE
              value: "{{ .Values.satisfy.homepage }}"
            {{- if .Values.satisfy.sshPrivateKey }}
            - name: SSH_PRIVATE_KEY
              valueFrom:
                secretKeyRef:
                  name: {{ template "satisfy.fullname" . }}
                  key: SSH_PRIVATE_KEY
            {{- end }}
          {{- if .Values.livenessProbe.enabled }}
          livenessProbe:
            httpGet:
              path: /
              port: http
            initialDelaySeconds: 5
            periodSeconds: 20
            timeoutSeconds: 10
            successThreshold: 1
            failureThreshold: 3
          {{- end }}
          {{- if .Values.readinessProbe.enabled }}
          readinessProbe:
            httpGet:
              path: /
              port: http
            initialDelaySeconds: 5
            periodSeconds: 20
            timeoutSeconds: 10
            successThreshold: 1
            failureThreshold: 3
          {{- end }}
          volumeMounts:
            - name: app-persist
              mountPath: /app
{{- with .Values.resources }}
          resources:
{{ toYaml . | indent 12 }}
{{- end }}
      volumes:
        - name: app-persist
        {{- if .Values.persistence.enabled }}
          persistentVolumeClaim:
            claimName: {{ template "satisfy.fullname" . }}
        {{- else }}
          emptyDir: {}
        {{- end -}}
    {{- with .Values.nodeSelector }}
      nodeSelector:
{{ toYaml . | indent 8 }}
    {{- end }}
    {{- with .Values.affinity }}
      affinity:
{{ toYaml . | indent 8 }}
    {{- end }}
    {{- with .Values.tolerations }}
      tolerations:
{{ toYaml . | indent 8 }}
    {{- end }}
 # ingress.yaml
{{- if .Values.ingress.enabled -}}
{{- $fullName := include "satisfy.fullname" . -}}
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: {{ $fullName }}
  labels:
    app.kubernetes.io/name: {{ include "satisfy.name" . }}
    helm.sh/chart: {{ include "satisfy.chart" . }}
    app.kubernetes.io/instance: {{ .Release.Name }}
    app.kubernetes.io/managed-by: {{ .Release.Service }}
{{- with .Values.ingress.labels }}
{{ toYaml . | indent 4 }}
{{- end }}
{{- with .Values.ingress.annotations }}
  annotations:
{{ toYaml . | indent 4 }}
{{- end }}
spec:
{{- if .Values.ingress.tls }}
  tls:
  {{- range .Values.ingress.tls }}
    - hosts:
      {{- range .hosts }}
        - {{ . | quote }}
      {{- end }}
      secretName: {{ .secretName }}
  {{- end }}
{{- end }}
  rules:
  {{- range .Values.ingress.hosts }}
    - host: {{ . | quote }}
      http:
        paths:
          - path: /
            backend:
              serviceName: {{ $fullName }}
              servicePort: http
  {{- end }}
{{- end }}
 # pvc.yaml
{{- if and .Values.persistence.enabled (not .Values.persistence.existingClaim) -}}
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: {{ include "satisfy.fullname" . }}
  labels:
    app.kubernetes.io/name: {{ include "satisfy.name" . }}
    helm.sh/chart: {{ include "satisfy.chart" . }}
    app.kubernetes.io/instance: {{ .Release.Name }}
    app.kubernetes.io/managed-by: {{ .Release.Service }}
spec:
  accessModes:
    - {{ .Values.persistence.accessMode | quote }}
  resources:
    requests:
      storage: {{ .Values.persistence.size | quote }}
{{- if .Values.persistence.storageClass }}
{{- if (eq "-" .Values.persistence.storageClass) }}
  storageClassName: ""
{{- else }}
  storageClassName: "{{ .Values.persistence.storageClass }}"
{{- end }}
{{- end }}
{{- end }}
 # secret.yaml
{{- if .Values.satisfy.sshPrivateKey }}
apiVersion: v1
kind: Secret
metadata:
  name: {{ include "satisfy.fullname" . }}
  labels:
    app.kubernetes.io/name: {{ include "satisfy.name" . }}
    helm.sh/chart: {{ include "satisfy.chart" . }}
    app.kubernetes.io/instance: {{ .Release.Name }}
    app.kubernetes.io/managed-by: {{ .Release.Service }}
type: Opaque
data:
  SSH_PRIVATE_KEY: {{ .Values.satisfy.sshPrivateKey | b64enc }}
{{- end -}}
 # service.yaml
apiVersion: v1
kind: Service
metadata:
  name: {{ include "satisfy.fullname" . }}
  labels:
    app.kubernetes.io/name: {{ include "satisfy.name" . }}
    helm.sh/chart: {{ include "satisfy.chart" . }}
    app.kubernetes.io/instance: {{ .Release.Name }}
    app.kubernetes.io/managed-by: {{ .Release.Service }}
spec:
  type: {{ .Values.service.type }}
  ports:
    - port: {{ .Values.service.port }}
      targetPort: http
      protocol: TCP
      name: http
  selector:
    app.kubernetes.io/name: {{ include "satisfy.name" . }}
    app.kubernetes.io/instance: {{ .Release.Name }}

# Default values for Satisfy.
# This is a YAML-formatted file.
# Declare variables to be passed into your templates.

image:
  repository: docker.io/anapsix/satisfy
  # image.digest takes precedence:
  # i.e. if both image.tag and image.digest are present digest will be used
  # tag: v3.0.4
  digest: sha256:fae78e3809e9d08990c7d7d6699a734f7e584c28ce49381a1a699e1694481ab8
  pullPolicy: Always
  pullSecrets: []
    # - secret1
    # - secret2

service:
  type: ClusterIP
  port: 80

ingress:
  enabled: false
  annotations: {}
    # kubernetes.io/ingress.class: nginx
    # kubernetes.io/tls-acme: "true"
    # certmanager.k8s.io/cluster-issuer: self-signed
    # ingress.kubernetes.io/force-ssl-redirect: "true"
  labels: []
    # traffic-type: external
  hosts: []
    # - composer.local
  tls: []
   # - secretName: composer-cert
   #   hosts:
   #     - composer.local

terminationGracePeriodSeconds: 15

livenessProbe:
  enabled: true

readinessProbe:
  enabled: true

resources: {}
  # If you do want to specify resources, uncomment the following
  # lines, adjust them as necessary, and remove the curly braces after 'resources:'.
  # limits:
  #   cpu: 100m
  #   memory: 64Mi
  # requests:
  #   cpu: 100m
  #   memory: 64Mi

nodeSelector: {}
tolerations: []
affinity: {}

persistence:
  enabled: true
  accessMode: ReadWriteOnce
  size: 8Gi
  storageClass: ~  # set your PV storage class here
  ## A manually managed Persistent Volume and Claim
  ## Requires persistence.enabled: true
  ## If defined, PVC must be created manually before volume will be bound
  # existingClaim:


## Application settings
## For more details see Satis documentaion
## https://getcomposer.org/doc/articles/handling-private-packages-with-satis.md#setup
satisfy:
  repoName: myrepo
  homepage: http://composer.local  # set it to FQDN of your ingress
  sshPrivateKey: ~  # set it via CLI
                    # for example: `--set satisfy.sshPrivateKey="$(<~/.ssh/id_rsa)"`
