# _helpers.tpl
{{/* vim: set filetype=mustache: */}}
{{/*
Expand the name of the chart.
*/}}
{{- define "jaeger-operator.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 "jaeger-operator.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 the name of the service account to use
*/}}
{{- define "jaeger-operator.serviceAccountName" -}}
{{- if .Values.serviceAccount.create -}}
    {{ default (include "jaeger-operator.fullname" .) .Values.serviceAccount.name }}
{{- else -}}
    {{ default "default" .Values.serviceAccount.name }}
{{- end -}}
{{- end -}}

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

{{/* Generate basic labels */}}
{{- define "jaeger-operator.labels" }}
app.kubernetes.io/name: {{ include "jaeger-operator.name" . }}
{{- end }}
 # crds.yaml
{{- if .Values.crd.install }}
{{- range $path, $bytes := .Files.Glob "crds/*.yaml" }}
{{ $.Files.Get $path }}
---
{{- end }}
{{- end }}
 # deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ include "jaeger-operator.fullname" . }}
  labels:
{{ include "jaeger-operator.labels" . | indent 4 }}
spec:
  replicas: 1
  selector:
    matchLabels:
{{ include "jaeger-operator.labels" . | indent 6 }}
  template:
    metadata:
      name: {{ include "jaeger-operator.fullname" . }}
      labels:
{{ include "jaeger-operator.labels" . | indent 8 }}
    spec:
      {{- if .Values.serviceAccount.create }}
      serviceAccountName: {{ include "jaeger-operator.serviceAccountName" . }}
      {{- end }}
      {{- with .Values.securityContext }}
      securityContext:
{{ toYaml . | indent 8 }}
      {{- end }}
      containers:
        - name: {{ include "jaeger-operator.fullname" . }}
          image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          ports:
          - containerPort: 8383
            name: metrics
          args: ["start"]
          env:
            - name: WATCH_NAMESPACE
              {{- if .Values.rbac.clusterRole }}
              value: ""
              {{- else }}
              valueFrom:
                fieldRef:
                  fieldPath: metadata.namespace
              {{- end }}
            - name: POD_NAME
              valueFrom:
                fieldRef:
                  fieldPath: metadata.name
            - name: POD_NAMESPACE
              valueFrom:
                fieldRef:
                  fieldPath: metadata.namespace
            - name: OPERATOR_NAME
              value: {{ include "jaeger-operator.fullname" . | quote }}
          resources:
{{ toYaml .Values.resources | indent 12 }}
    {{- with .Values.nodeSelector }}
      nodeSelector:
{{ toYaml . | indent 8 }}
    {{- end }}
    {{- with .Values.affinity }}
      affinity:
{{ toYaml . | indent 8 }}
    {{- end }}
    {{- with .Values.tolerations }}
      tolerations:
{{ toYaml . | indent 8 }}
    {{- end }}
 # jaeger.yaml
{{- if .Values.jaeger.create }}
apiVersion: jaegertracing.io/v1
kind: Jaeger
metadata:
  name: {{ include "jaeger-operator.fullname" . }}-jaeger
{{- with .Values.jaeger.spec }}
spec:
{{ toYaml . | indent 2}}
{{- end }}
{{- end }}
 # psp.yaml
{{- if and .Values.rbac.create .Values.rbac.pspEnabled }}
apiVersion: policy/v1beta1
kind: PodSecurityPolicy
metadata:
  name: {{ include "jaeger-operator.fullname" . }}-operator-psp
  labels:
{{ include "jaeger-operator.labels" . | indent 4 }}
spec:
  privileged: false
  allowPrivilegeEscalation: false
  volumes:
    - 'configMap'
    - 'emptyDir'
    - 'projected'
    - 'secret'
    - 'downwardAPI'
    - 'persistentVolumeClaim'
  hostNetwork: false
  hostIPC: false
  hostPID: false
  runAsUser:
    rule: 'MustRunAsNonRoot'
  seLinux:
    rule: 'RunAsAny'
  supplementalGroups:
    rule: 'MustRunAs'
    ranges:
      - min: 1
        max: 65535
  fsGroup:
    rule: 'MustRunAs'
    ranges:
      - min: 1
        max: 65535
  readOnlyRootFilesystem: false
{{- end }}
 # role-binding.yaml
{{- if .Values.rbac.create }}
kind: {{ if .Values.rbac.clusterRole }}Cluster{{ end }}RoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: {{ include "jaeger-operator.fullname" . }}
  namespace: {{ .Release.Namespace }}
  labels:
{{ include "jaeger-operator.labels" . | indent 4 }}
subjects:
- kind: ServiceAccount
  namespace: {{ .Release.Namespace }}
  name: {{ include "jaeger-operator.serviceAccountName" . }}
roleRef:
  kind: {{ if .Values.rbac.clusterRole }}Cluster{{ end }}Role
  name: {{ include "jaeger-operator.fullname" . }}
  apiGroup: rbac.authorization.k8s.io
{{- end }}
 # role.yaml
{{- if .Values.rbac.create }}
kind: {{ if .Values.rbac.clusterRole }}Cluster{{ end }}Role
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: {{ include "jaeger-operator.fullname" . }}
  namespace: {{ .Release.Namespace }}
  labels:
{{ include "jaeger-operator.labels" . | indent 4 }}
rules:
- apiGroups:
  - ""
  resources:
  - pods
  - services
  - endpoints
  - persistentvolumeclaims
  - events
  - configmaps
  - secrets
  - serviceaccounts
  verbs:
  - '*'
- apiGroups:
  - apps
  resources:
  - deployments
  - daemonsets
  - replicasets
  - statefulsets
  verbs:
  - '*'
- apiGroups:
  - monitoring.coreos.com
  resources:
  - servicemonitors
  verbs:
  - get
  - create
- apiGroups:
  - io.jaegertracing
  resources:
  - '*'
  verbs:
  - '*'
- apiGroups:
  - extensions
  resources:
  - replicasets
  - deployments
  - daemonsets
  - statefulsets
  - ingresses
  verbs:
  - "*"
- apiGroups:
  - batch
  resources:
  - jobs
  - cronjobs
  verbs:
  - "*"
- apiGroups:
  - route.openshift.io
  resources:
  - routes
  verbs:
  - "*"
- apiGroups:
  - logging.openshift.io
  resources:
  - elasticsearches
  verbs:
  - '*'
- apiGroups:
  - jaegertracing.io
  resources:
  - '*'
  verbs:
  - '*'
- apiGroups:
  - rbac.authorization.k8s.io
  resources:
  - clusterrolebindings
  verbs:
  - '*'
- apiGroups:
  - apps
  - extensions
  resourceNames:
  - jaeger-operator
  resources:
  - deployments/finalizers
  verbs:
  - update
{{- if .Values.rbac.pspEnabled }}
- apiGroups: ['policy']
  resources: ['podsecuritypolicies']
  verbs:     ['use']
  resourceNames:
  - {{ include "jaeger-operator.fullname" . }}-operator-psp
{{- end }}
{{- end }}
 # service-account.yaml
{{- if .Values.serviceAccount.create }}
apiVersion: v1
kind: ServiceAccount
metadata:
  name: {{ include "jaeger-operator.serviceAccountName" . }}
  namespace: {{ .Release.Namespace }}
  labels:
{{ include "jaeger-operator.labels" . | indent 4 }}
{{- end }}
 # service.yaml
apiVersion: v1
kind: Service
metadata:
  name: {{ include "jaeger-operator.fullname" . }}-metrics
  labels:
{{ include "jaeger-operator.labels" . | indent 4 }}
spec:
  ports:
  - name: metrics
    port: 8383
    protocol: TCP
    targetPort: 8383
  selector:
    app.kubernetes.io/name: {{ include "jaeger-operator.name" . }}
    app.kubernetes.io/instance: {{ .Release.Name }}
  type: ClusterIP

# Default values for jaeger-operator.
# This is a YAML-formatted file.
# Declare variables to be passed into your templates.
image:
  repository: jaegertracing/jaeger-operator
  tag: 1.15.1
  pullPolicy: IfNotPresent

crd:
  install: true

jaeger:
  # Specifies whether Jaeger instance should be created
  create: false
  spec: {}

rbac:
  # Specifies whether RBAC resources should be created
  create: true
  pspEnabled: false
  clusterRole: false

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

resources: {}
  # limits:
  #  cpu: 100m
  #  memory: 128Mi
  # requests:
  #  cpu: 100m
  #  memory: 128Mi

nodeSelector: {}

tolerations: []

affinity: {}

securityContext: {}
