{{- if and .Values.collector.enabled (include "wavefront.collector.createConfigmap" .) }}
apiVersion: v1
kind: ConfigMap
metadata:
  name: {{ printf "%s-collector-config" (include "common.names.fullname" .) }}
  namespace: {{ .Release.Namespace | quote }}
  labels: {{- include "common.labels.standard" . | nindent 4 }}
    app.kubernetes.io/component: collector
    {{- if .Values.commonLabels }}
    {{- include "common.tplvalues.render" ( dict "value" .Values.commonLabels "context" $ ) | nindent 4 }}
    {{- end }}
  {{- if .Values.commonAnnotations }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" .Values.commonAnnotations "context" $ ) | nindent 4 }}
  {{- end }}
data:
  config.yaml: |
    clusterName: {{ .Values.clusterName }}
    enableDiscovery: {{ .Values.collector.discovery.enabled }}
    enableEvents: {{ .Values.collector.events.enabled }}
    defaultCollectionInterval: {{ .Values.collector.interval | default "60s" }}
    flushInterval: {{ .Values.collector.flushInterval | default "10s" }}
    sinkExportDataTimeout: {{ .Values.collector.sinkDelay | default "20s" }}

    sinks:
      {{- if .Values.collector.useProxy }}
      {{- if .Values.collector.proxyAddress }}
      - proxyAddress: {{ .Values.collector.proxyAddress }}
      {{- else }}
      - proxyAddress: {{ printf "%s-proxy:%d" (include "common.names.fullname" .) (int .Values.proxy.port )}}
      {{- end }}
      {{- else }}
      - server: {{ .Values.wavefront.url }}
        token: {{ .Values.wavefront.token }}
      {{- end }}
        {{- if .Values.collector.tags }}
        tags: {{- include "common.tplvalues.render" ( dict "value" .Values.collector.tags "context" . ) | nindent 10 }}
        {{- end }}
        {{- if .Values.collector.filters }}
        filters: {{- include "common.tplvalues.render" ( dict "value" .Values.collector.filters "context" . ) | nindent 10 }}
        {{- end }}

    sources:
      kubernetes_source:
        {{- if .Values.collector.useReadOnlyPort }}
        url:
        kubeletPort: 10255
        kubeletHttps: false
        {{- else }}
        url: https://kubernetes.default.svc
        kubeletPort: 10250
        kubeletHttps: true
        {{- end }}
        {{- if .Values.serviceAccount.create }}
        useServiceAccount: true
        {{- else }}
        useServiceAccount: false
        {{- end }}
        insecure: true
        {{- if .Values.collector.usePKSPrefix }}
        prefix: pks.kubernetes.
        {{- else }}
        prefix: kubernetes.
        {{- end }}
        filters:
          metricBlacklist:
            - 'kubernetes.sys_container.*'
            - 'kubernetes.node.ephemeral_storage.*'

      internal_stats_source:
        {{- if .Values.collector.usePKSPrefix }}
        prefix: pks.kubernetes.
        {{- else }}
        prefix: kubernetes.
        {{- end }}

      {{- if .Values.collector.kubernetesState }}
      kubernetes_state_source:
        {{- if .Values.collector.usePKSPrefix }}
        prefix: pks.kubernetes.
        {{- else }}
        prefix: kubernetes.
        {{- end }}
      {{- end }}

      {{- if .Values.collector.hostOSMetrics }}
      telegraf_sources:
        - plugins: []
      {{- end }}

      {{- if or .Values.collector.apiServerMetrics .Values.collector.controlplane.enabled }}
      # Kubernetes API Server
      prometheus_sources:
        - url: https://kubernetes.default.svc.cluster.local:443/metrics
          httpConfig:
            bearer_token_file: /var/run/secrets/kubernetes.io/serviceaccount/token
            tls_config:
              ca_file: /var/run/secrets/kubernetes.io/serviceaccount/ca.crt
              insecure_skip_verify: true
          prefix: kube.apiserver.
          filters:
            metricWhitelist:
              {{- if .Values.collector.apiServerMetrics }}
              - 'kube.apiserver.apiserver.*'
              - 'kube.apiserver.etcd.*'
              - 'kube.apiserver.process.*'
              {{- end }}
              {{- if .Values.collector.controlplane.enabled }}
              - 'kube.apiserver.etcd.request.duration.seconds.bucket'
              - 'kube.apiserver.etcd.object.counts.gauge'
              - 'kube.apiserver.etcd.db.total.size.in.bytes.gauge'
              - 'kube.apiserver.apiserver.request.duration.seconds.bucket'
              - 'kube.apiserver.apiserver.request.total.counter'
              - 'kube.apiserver.workqueue.adds.total.counter'
              - 'kube.apiserver.workqueue.queue.duration.seconds.bucket'
              {{- end }}
      {{- end }}

    {{- if .Values.collector.events.enabled }}
    events:
      {{- if .Values.collector.events.filters }}
      filters: {{- include "common.tplvalues.render" ( dict "value" .Values.collector.events.filters "context" . ) | nindent 8 }}
      {{- end }}
    {{- end }}

    {{- if .Values.collector.discovery.enabled }}

    discovery:
      {{- if .Values.collector.discovery.annotationPrefix }}
      annotation_prefix: {{ .Values.collector.discovery.annotationPrefix | quote }}
      {{- end }}
      {{- if .Values.collector.discovery.enableRuntimeConfigs }}
      enable_runtime_plugins: {{ .Values.collector.discovery.enableRuntimeConfigs }}
      {{- end }}

      plugins:
        # auto-discover kube DNS
        - name: kube-dns-discovery
          type: prometheus
          selectors:
            images:
              - '*kube-dns/sidecar*'
            labels:
              k8s-app:
                - kube-dns
          port: 10054
          path: /metrics
          scheme: http
          prefix: kube.dns.
          filters:
            metricWhitelist:
              - 'kube.dns.http.request.duration.microseconds'
              - 'kube.dns.http.request.size.bytes'
              - 'kube.dns.http.requests.total.counter'
              - 'kube.dns.http.response.size.bytes'
              - 'kube.dns.kubedns.dnsmasq.*'
              - 'kube.dns.process.*'

        # auto-discover coredns
        - name: coredns-discovery
          type: prometheus
          selectors:
            images:
              - '*coredns:*'
            labels:
              k8s-app:
                - kube-dns
          port: 9153
          path: /metrics
          scheme: http
          prefix: kube.coredns.
          filters:
            metricWhitelist:
              - 'kube.coredns.coredns.cache.*'
              - 'kube.coredns.coredns.dns.request.count.total.counter'
              - 'kube.coredns.coredns.dns.request.duration.seconds'
              - 'kube.coredns.coredns.dns.request.size.bytes'
              - 'kube.coredns.coredns.dns.request.type.count.total.counter'
              - 'kube.coredns.coredns.dns.response.rcode.count.total.counter'
              - 'kube.coredns.coredns.dns.response.size.bytes'
              - 'kube.coredns.process.*'
              {{- if .Values.collector.controlplane.enabled }}
              - 'kube.coredns.coredns.dns.request.duration.seconds.bucket'
              - 'kube.coredns.coredns.dns.responses.total.counter'
              {{- end }}

      {{- if .Values.collector.discovery.config }}
      # user supplied discovery config
      {{- include "common.tplvalues.render" ( dict "value" .Values.collector.discovery.config "context" . ) | nindent 6 }}
      {{- end }}
    {{- end }}

{{- end }}
