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

{{- /*
Credit: @technosophos
https://github.com/technosophos/common-chart/
labels.standard prints the standard Helm labels.
The standard labels are frequently used in metadata.
*/ -}}
{{- define "labels.standard" -}}
app: {{ include "hl-composer.name" . }}
heritage: {{ .Release.Service | quote }}
release: {{ .Release.Name | quote }}
chart: {{ include "hl-composer.chart" . }}
{{- end -}}
 # hl-composer-cli-deployment.yaml
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ include "hl-composer.fullname" . }}-cli
  labels:
    name: {{ include "hl-composer.fullname" . }}-cli
{{ include "labels.standard" . | indent 4 }}
spec:
  replicas: 1
  selector:
    matchLabels:
      app: {{ include "hl-composer.name" . }}
      release: {{ .Release.Name }}
  template:
    metadata:
      name: {{ include "hl-composer.fullname" . }}-cli
      labels:
        name: {{ include "hl-composer.fullname" . }}-cli
{{ include "labels.standard" . | indent 8 }}
    spec:
      volumes:
        - name: persistent-volume
        {{- if .Values.persistence.enabled }}
          persistentVolumeClaim:
            claimName: {{ .Values.persistence.existingClaim | default (include "hl-composer.fullname" .) }}
        {{- else }}
          emptyDir: {}
        {{- end }}
        {{- if .Values.cli.secrets.blockchainNetwork }}
        - name: blockchain-network
          secret:
            secretName: {{ .Values.cli.secrets.blockchainNetwork }}
        {{- end }}
        {{- if .Values.cli.secrets.adminCert }}
        - name: admin-cert
          secret:
            secretName: {{ .Values.cli.secrets.adminCert }}
        {{- end }}
        {{- if .Values.cli.secrets.adminKey }}
        - name: admin-key
          secret:
            secretName: {{ .Values.cli.secrets.adminKey }}
        {{- end }}
        {{- if .Values.cli.secrets.hlcConnection }}
        - name: hlc-connection
          configMap:
            name: {{ .Values.cli.secrets.hlcConnection }}
        {{- end }}
      containers:
        - name: cli
          image: "{{ .Values.cli.image.repository }}:{{ .Values.cli.image.tag }}"
          imagePullPolicy: {{ .Values.cli.image.pullPolicy }}
        # TODO: Add liveness and readiness probes
        # Run infinitely
          command:
            - sh
            - -c
            - |
              tail -f /dev/null
          volumeMounts:
            - mountPath: /home/composer/.composer
              name: persistent-volume
            {{- if .Values.cli.secrets.blockchainNetwork }}
            - mountPath: /hl_config/blockchain_network
              name: blockchain-network
            {{- end }}
            {{- if .Values.cli.secrets.adminCert }}
            - mountPath: /hl_config/admin/signcerts
              name: admin-cert
            {{- end }}
            {{- if .Values.cli.secrets.adminKey }}
            - mountPath: /hl_config/admin/keystore
              name: admin-key
            {{- end }}
            {{- if .Values.cli.secrets.hlcConnection }}
            - mountPath: /hl_config/hlc-connection
              name: hlc-connection
            {{- end }}
          resources:
{{ toYaml .Values.cli.resources | indent 12 }}
    {{- with .Values.cli.nodeSelector }}
      nodeSelector:
{{ toYaml . | indent 8 }}
    {{- end }}
    {{- with .Values.cli.affinity }}
      affinity:
{{ toYaml . | indent 8 }}
    {{- end }}
    {{- with .Values.cli.tolerations }}
      tolerations:
{{ toYaml . | indent 8 }}
    {{- end }}
 # hl-composer-pg-deployment.yaml
{{- if .Values.pg.enabled -}}
apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ include "hl-composer.fullname" . }}-pg
  labels:
    name: {{ include "hl-composer.fullname" . }}-pg
{{ include "labels.standard" . | indent 4 }}
spec:
  replicas: 1
  selector:
    matchLabels:
      app: {{ include "hl-composer.name" . }}
      release: {{ .Release.Name }}
  template:
    metadata:
      name: {{ include "hl-composer.fullname" . }}-pg
      labels:
        name: {{ include "hl-composer.fullname" . }}-pg
{{ include "labels.standard" . | indent 8 }}
    spec:
      volumes:
        - name: persistent-volume
        {{- if .Values.persistence.enabled }}
          persistentVolumeClaim:
            claimName: {{ .Values.persistence.existingClaim | default (include "hl-composer.fullname" .) }}
        {{- else }}
          emptyDir: {}
        {{- end }}
      containers:
        - name: composer-playground
          image: "{{ .Values.pg.image.repository }}:{{ .Values.pg.image.tag }}"
          imagePullPolicy: {{ .Values.pg.image.pullPolicy }}
          # TODO: Add liveness and readiness probes
          volumeMounts:
            - mountPath: /home/composer/.composer
              name: persistent-volume
          resources:
{{ toYaml .Values.pg.resources | indent 12 }}
    {{- with .Values.pg.nodeSelector }}
      nodeSelector:
{{ toYaml . | indent 8 }}
    {{- end }}
    {{- with .Values.pg.affinity }}
      affinity:
{{ toYaml . | indent 8 }}
    {{- end }}
    {{- with .Values.pg.tolerations }}
      tolerations:
{{ toYaml . | indent 8 }}
    {{- end }}
{{- end }}
 # hl-composer-pg-ingress.yaml
{{- if and .Values.pg.enabled .Values.pg.ingress.enabled -}}
{{- $fullName := include "hl-composer.fullname" . -}}
{{- $ingressPath := .Values.pg.ingress.path -}}
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: {{ $fullName }}-pg
  labels:
{{ include "labels.standard" . | indent 4 }}
{{- with .Values.pg.ingress.annotations }}
  annotations:
{{ toYaml . | indent 4 }}
{{- end }}
spec:
{{- if .Values.pg.ingress.tls }}
  tls:
  {{- range .Values.pg.ingress.tls }}
    - hosts:
      {{- range .hosts }}
        - {{ . }}
      {{- end }}
      secretName: {{ .secretName }}
  {{- end }}
{{- end }}
  rules:
  {{- range .Values.pg.ingress.hosts }}
    - host: {{ . }}
      http:
        paths:
          - path: {{ $ingressPath }}
            backend:
              serviceName: {{ $fullName }}-pg
              servicePort: http
  {{- end }}
{{- end }}
 # hl-composer-pg-service.yaml
{{- if .Values.pg.enabled -}}
apiVersion: v1
kind: Service
metadata:
  name: {{ include "hl-composer.fullname" . }}-pg
  labels:
{{ include "labels.standard" . | indent 4 }}
spec:
  type: {{ .Values.pg.service.type }}
  ports:
    - port: {{ .Values.pg.service.port }}
      targetPort: 8080
      protocol: TCP
      name: http
  selector:
    name: {{ include "hl-composer.fullname" . }}-pg
    app: {{ include "hl-composer.name" . }}
    release: {{ .Release.Name }}
{{- end}} # hl-composer-pvc.yaml
{{- if and .Values.persistence.enabled (not .Values.persistence.existingClaim) }}
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: {{ include "hl-composer.fullname" . }}
  labels:
{{ include "labels.standard" . | 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.size | quote }}
  {{- if .Values.persistence.storageClass }}
  {{- if (eq "-" .Values.persistence.storageClass) }}
  storageClassName: ""
  {{- else }}
  storageClassName: "{{ .Values.persistence.storageClass }}"
  {{- end }}
  {{- end }}
{{- end }}
 # hl-composer-rest-deployment.yaml
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ include "hl-composer.fullname" . }}-rest
  labels:
    name: {{ include "hl-composer.fullname" . }}-rest
{{ include "labels.standard" . | indent 4 }}
spec:
  replicas: 1
  selector:
    matchLabels:
      app: {{ include "hl-composer.name" . }}
      release: {{ .Release.Name }}
  template:
    metadata:
      name: {{ include "hl-composer.fullname" . }}-rest
      labels:
        name: {{ include "hl-composer.fullname" . }}-rest
{{ include "labels.standard" . | indent 8 }}
    spec:
      volumes:
        - name: persistent-volume
        {{- if .Values.persistence.enabled }}
          persistentVolumeClaim:
            claimName: {{ .Values.persistence.existingClaim | default (include "hl-composer.fullname" .) }}
        {{- else }}
          emptyDir: {}
        {{- end }}
      containers:
        - name: rest-server
          image: "{{ .Values.rest.image.repository }}:{{ .Values.rest.image.tag }}"
          imagePullPolicy: {{ .Values.rest.image.pullPolicy }}
          # TODO: Add liveness and readiness probes
          envFrom:
            - secretRef:
                name: {{ include "hl-composer.fullname" . }}-rest
          env:
            - name: COMPOSER_CARD
              value: {{ .Values.rest.config.composerRestServerCard }}
            - name: COMPOSER_NAMESPACES
              value: never
          volumeMounts:
            - name: persistent-volume
              mountPath: /home/composer/.composer
          resources:
{{ toYaml .Values.rest.resources | indent 12 }}
    {{- with .Values.rest.nodeSelector }}
      nodeSelector:
{{ toYaml . | indent 8 }}
    {{- end }}
    {{- with .Values.rest.affinity }}
      affinity:
{{ toYaml . | indent 8 }}
    {{- end }}
    {{- with .Values.rest.tolerations }}
      tolerations:
{{ toYaml . | indent 8 }}
    {{- end }}
 # hl-composer-rest-ingress.yaml
{{- if .Values.rest.ingress.enabled -}}
{{- $fullName := include "hl-composer.fullname" . -}}
{{- $ingressPath := .Values.rest.ingress.path -}}
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: {{ $fullName }}-rest
  labels:
{{ include "labels.standard" . | indent 4 }}
{{- with .Values.rest.ingress.annotations }}
  annotations:
{{ toYaml . | indent 4 }}
{{- end }}
spec:
{{- if .Values.rest.ingress.tls }}
  tls:
  {{- range .Values.rest.ingress.tls }}
    - hosts:
      {{- range .hosts }}
        - {{ . }}
      {{- end }}
      secretName: {{ .secretName }}
  {{- end }}
{{- end }}
  rules:
  {{- range .Values.rest.ingress.hosts }}
    - host: {{ . }}
      http:
        paths:
          - path: {{ $ingressPath }}
            backend:
              serviceName: {{ $fullName }}-rest
              servicePort: http
  {{- end }}
{{- end }}
 # hl-composer-rest-secret.yaml
apiVersion: v1
kind: Secret
metadata:
  name: {{ include "hl-composer.fullname" . }}-rest
  labels:
{{ include "labels.standard" . | indent 4 }}
type: Opaque
data:
  {{ if .Values.rest.config.apiKey }}
  COMPOSER_APIKEY:  {{ .Values.rest.config.apiKey | b64enc | quote }}
  {{ else }}
  COMPOSER_APIKEY: {{ randAlphaNum 24 | b64enc | quote }}
  {{ end }}
 # hl-composer-rest-service.yaml
---
apiVersion: v1
kind: Service
metadata:
  name: {{ include "hl-composer.fullname" $ }}-rest
  labels:
{{ include "labels.standard" . | indent 4 }}
spec:
  type: {{ .Values.rest.service.type }}
  ports:
    - port: {{ .Values.rest.service.port }}
      targetPort: 3000
      protocol: TCP
      name: http
  selector:
    name: {{ include "hl-composer.fullname" . }}-rest
    app: {{ include "hl-composer.name" . }}
    release: {{ .Release.Name }}

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

persistence:
  # By default we must set this to false to enable chart to pass tests
  # However, in a proper deployment, you should set this to "true", so that the different components can share data
  enabled: false
  annotations: {}
  capacity: 1Gi
  ## If defined, storageClassName: <storageClass>
  ## If set to "-", storageClassName: "", which disables dynamic provisioning
  ## If undefined (the default) or set to null, no storageClassName spec is
  ##   set, choosing the default provisioner.  (gp2 on AWS, standard on
  ##   GKE, AWS & OpenStack)
  ##
  storageClass: ""
  accessMode: ReadWriteMany

cli:
  image:
    repository: hyperledger/composer-cli
    tag: 0.20.0
    pullPolicy: IfNotPresent

  secrets: {}
    # This should contain the packaged .bna network file.
    # blockchainNetwork: bc--bna
    # We need a secret for the Organisation certificate and key
    # adminCert: hl--peer-admincert
    # adminKey: hl--peer-adminkey
    # Composer Connection JSON
    # hlcConnection: hl--connection

  resources: {}
    # We usually recommend not to specify default resources and to leave this as a conscious
    # choice for the user. This also increases chances charts run on environments with little
    # resources, such as Minikube. 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: 128Mi
    # requests:
    #  cpu: 100m
    #  memory: 128Mi

  nodeSelector: {}

  tolerations: []

  affinity: {}

rest:
  image:
    repository: hyperledger/composer-rest-server
    tag: 0.20.0
    pullPolicy: IfNotPresent

  service:
    # Cluster IP or LoadBalancer
    type: ClusterIP
    port: 3000

  # Ingress for Composer REST
  ingress:
    enabled: false
    annotations: {}
      # kubernetes.io/ingress.class: nginx
      # certmanager.k8s.io/cluster-issuer: "letsencrypt-staging"
    path: /
    hosts: []
    #  - hl-composer-rest.local
    tls: []
    #  - secretName: hl-composer-rest-tls
    #    hosts:
    #      - hl-composer-rest.local

  config:
    # Composer REST server API key
    # apiKey:
    # Card for network connection
    composerRestServerCard: admin@test-network

  resources: {}
    # We usually recommend not to specify default resources and to leave this as a conscious
    # choice for the user. This also increases chances charts run on environments with little
    # resources, such as Minikube. 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: 128Mi
    # requests:
    #  cpu: 100m
    #  memory: 128Mi

  nodeSelector: {}

  tolerations: []

  affinity: {}

pg:
  enabled: true

  image:
    repository: hyperledger/composer-playground
    tag: 0.20.0
    pullPolicy: IfNotPresent

  service:
    # Cluster IP or LoadBalancer
    type: ClusterIP
    port: 8080

  # Ingress for Composer PlayGround
  ingress:
    enabled: false
    annotations: {}
      # kubernetes.io/ingress.class: nginx
      # certmanager.k8s.io/cluster-issuer: "letsencrypt-staging"
    path: /
    hosts: []
    #  - hl-composer-pg.local
    tls: []
    #  - secretName: hl-composer-pg-tls
    #    hosts:
    #      - hl-composer-pg.local

  resources: {}
    # We usually recommend not to specify default resources and to leave this as a conscious
    # choice for the user. This also increases chances charts run on environments with little
    # resources, such as Minikube. 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: 128Mi
    # requests:
    #  cpu: 100m
    #  memory: 128Mi

  nodeSelector: {}

  tolerations: []

  affinity: {}
