{{ if .Values.collector.enabled -}}
---
###
### Tracing Collector Service
###
apiVersion: v1
kind: ConfigMap
metadata:
  name: collector-config
  namespace: {{ .Release.Namespace }}
  labels:
    linkerd.io/extension: jaeger
    component: collector
    {{- with .Values.commonLabels }}{{ toYaml . | trim | nindent 4 }}{{- end }}
data:
  collector-config: |
    {{- toYaml .Values.collector.config | trim | nindent 4 }}
---
apiVersion: v1
kind: Service
metadata:
  name: collector
  namespace: {{ .Release.Namespace }}
  labels:
    linkerd.io/extension: jaeger
    component: collector
    {{- with .Values.commonLabels }}{{ toYaml . | trim | nindent 4 }}{{- end }}
spec:
  type: ClusterIP
  ports:
  - name: otlp
    port: 4317
    protocol: TCP
    targetPort: 4317
  - name: otlp-http
    port: 4318
    protocol: TCP
    targetPort: 4318
  - name: opencensus
    port: 55678
    protocol: TCP
    targetPort: 55678
  - name: zipkin
    port: 9411
    protocol: TCP
    targetPort: 9411
  - name: jaeger-thrift
    port: 14268
    protocol: TCP
    targetPort: 14268
  - name: jaeger-grpc
    port: 14250
    protocol: TCP
    targetPort: 14250
  selector:
    component: collector
---
{{- $tree := deepCopy . }}
apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    linkerd.io/extension: jaeger
    app.kubernetes.io/name: collector
    app.kubernetes.io/part-of: Linkerd
    component: collector
    {{- with .Values.commonLabels }}{{ toYaml . | trim | nindent 4 }}{{- end }}
  name: collector
  namespace: {{ .Release.Namespace }}
spec:
  replicas: {{ .Values.collector.replicas }}
  revisionHistoryLimit: {{.Values.revisionHistoryLimit}}
  selector:
    matchLabels:
      component: collector
  {{- if .Values.enablePodAntiAffinity }}
  strategy:
    rollingUpdate:
      maxUnavailable: 1
  {{- end }}
  minReadySeconds: 5
  progressDeadlineSeconds: 120
  template:
    metadata:
      annotations:
        checksum/config: {{ toYaml .Values.collector.config | trim | sha256sum }}
        cluster-autoscaler.kubernetes.io/safe-to-evict: "true"
        config.alpha.linkerd.io/proxy-wait-before-exit-seconds: "0"
        config.linkerd.io/proxy-await: "enabled"
        linkerd.io/inject: enabled
        prometheus.io/path: /metrics
        prometheus.io/port: "8888"
        prometheus.io/scrape: "true"
      labels:
        linkerd.io/extension: jaeger
        component: collector
        {{- with .Values.podLabels }}{{ toYaml . | trim | nindent 8 }}{{- end }}
    spec:
      {{- if .Values.collector.tolerations -}}
      {{- include "linkerd.tolerations" (dict "Values" .Values.collector) | nindent 6 }}
      {{- end -}}
      {{- include "linkerd.node-selector" (dict "Values" .Values.collector) | nindent 6 }}
      {{- $_ := set $tree "component" "collector" -}}
      {{- $_ := set $tree "label" "component" -}}
      {{- include "linkerd.affinity" $tree | nindent 6 }}
      automountServiceAccountToken: false
      containers:
      - command:
        - {{ .Values.collector.command }}
        - --config=/conf/collector-config.yaml
        env:
        - name: GOGC
          value: "80"
        - name: HOST_IP
          valueFrom:
            fieldRef:
              fieldPath: status.hostIP
        - name: POD_NAMESPACE
          valueFrom:
            fieldRef:
              apiVersion: v1
              fieldPath: metadata.namespace
        {{- with .Values.collector.env }}
        {{- toYaml . | nindent 8 }}
        {{- end }}
        image: {{.Values.collector.image.name}}:{{.Values.collector.image.version}}
        {{- with .Values.collector.image.pullPolicy }}
        imagePullPolicy: {{.}}
        {{- end }}
        livenessProbe:
          httpGet:
            path: /
            port: 13133
        name: ot-collector
        ports:
        - containerPort: 13133
        - containerPort: 4317
        - containerPort: 4318
        - containerPort: 55678
        - containerPort: 9411
        - containerPort: 14268
        - containerPort: 14250
        - containerPort: 8888
        readinessProbe:
          httpGet:
            path: /
            port: 13133
        {{- if .Values.collector.resources -}}
        {{- include "partials.resources" .Values.collector.resources | nindent 8 }}
        {{- end }}
        securityContext:
          allowPrivilegeEscalation: false
          capabilities:
            drop:
            - ALL
          readOnlyRootFilesystem: true
          runAsNonRoot: true
          runAsUser: {{.Values.collector.UID | default .Values.defaultUID}}
          runAsGroup: {{.Values.collector.GID | default .Values.defaultGID}}
          seccompProfile:
            type: RuntimeDefault
        volumeMounts:
        - mountPath: /conf
          name: collector-config-val
        - mountPath: /var/run/secrets/kubernetes.io/serviceaccount
          name: kube-api-access
          readOnly: true
      securityContext:
        seccompProfile:
          type: RuntimeDefault
      serviceAccountName: collector
      volumes:
      - configMap:
          items:
          - key: collector-config
            path: collector-config.yaml
          name: collector-config
        name: collector-config-val
      - {{- include "partials.volumes.manual-mount-service-account-token" . | indent 8 | trimPrefix (repeat 7 " ") }}
{{ end -}}
{{ if .Values.jaeger.enabled -}}
---
###
### Tracing Jaeger Service
###
apiVersion: v1
kind: Service
metadata:
  name: jaeger
  namespace: {{ .Release.Namespace }}
  labels:
    linkerd.io/extension: jaeger
    component: jaeger
    {{- with .Values.commonLabels }}{{ toYaml . | trim | nindent 4 }}{{- end }}
spec:
  type: ClusterIP
  selector:
    component: jaeger
  ports:
    - name: collection
      port: 14268
    - name: grpc
      port: 14250
    - name: ui
      port: 16686
---
{{- $tree := deepCopy . }}
apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    linkerd.io/extension: jaeger
    app.kubernetes.io/name: jaeger
    app.kubernetes.io/part-of: Linkerd
    component: jaeger
    {{- with .Values.commonLabels }}{{ toYaml . | trim | nindent 4 }}{{- end }}
  name: jaeger
  namespace: {{ .Release.Namespace }}
spec:
  replicas: 1
  revisionHistoryLimit: {{.Values.revisionHistoryLimit}}
  selector:
    matchLabels:
      component: jaeger
  {{- if .Values.enablePodAntiAffinity }}
  strategy:
    rollingUpdate:
      maxUnavailable: 1
  {{- end }}
  template:
    metadata:
      annotations:
        linkerd.io/inject: enabled
        config.linkerd.io/proxy-await: "enabled"
        prometheus.io/path: /metrics
        prometheus.io/port: "14269"
        prometheus.io/scrape: "true"
        cluster-autoscaler.kubernetes.io/safe-to-evict: "true"
      labels:
        linkerd.io/extension: jaeger
        component: jaeger
        {{- with .Values.podLabels }}{{ toYaml . | trim | nindent 8 }}{{- end }}
    spec:
      {{- if .Values.jaeger.tolerations -}}
      {{- include "linkerd.tolerations" (dict "Values" .Values.jaeger) | nindent 6 }}
      {{- end -}}
      {{- include "linkerd.node-selector" (dict "Values" .Values.jaeger) | nindent 6 }}
      {{- $_ := set $tree "component" "jaeger" -}}
      {{- $_ := set $tree "label" "component" -}}
      {{- include "linkerd.affinity" $tree | nindent 6 }}
      automountServiceAccountToken: false
      containers:
      - args:
        {{-  range .Values.jaeger.args }}
        - {{ . -}}
        {{ end }}
        image: {{.Values.jaeger.image.name}}:{{.Values.jaeger.image.version}}
        {{- with .Values.jaeger.image.pullPolicy }}
        imagePullPolicy: {{.}}
        {{- end }}
        name: jaeger
        ports:
        - containerPort: 14269
          name: admin
        - containerPort: 14268
          name: collection
        - containerPort: 14250
          name: grpc
        - containerPort: 16686
          name: ui
        {{- if .Values.jaeger.resources -}}
        {{- include "partials.resources" .Values.jaeger.resources | nindent 8 }}
        {{- end }}
        securityContext:
          allowPrivilegeEscalation: false
          capabilities:
            drop:
            - ALL
          readOnlyRootFilesystem: true
          runAsNonRoot: true
          runAsUser: {{.Values.jaeger.UID | default .Values.defaultUID}}
          runAsGroup: {{.Values.jaeger.GID | default .Values.defaultGID}}
          seccompProfile:
            type: RuntimeDefault
        volumeMounts:
        - mountPath: /var/run/secrets/kubernetes.io/serviceaccount
          name: kube-api-access
          readOnly: true
      dnsPolicy: ClusterFirst
      securityContext:
        seccompProfile:
          type: RuntimeDefault
      serviceAccountName: jaeger
      volumes:
      - {{- include "partials.volumes.manual-mount-service-account-token" . | indent 8 | trimPrefix (repeat 7 " ") }}
{{ end -}}
