{{- if (include "wavefront.collector.createConfigmap" .) }}
apiVersion: v1
kind: ConfigMap
metadata:
  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 }}
  name: {{ template "common.names.fullname" . }}-collector-config
  {{- 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: {{ template "common.names.fullname" . }}-proxy:{{ .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 8 }}
    {{- end }}
      filters:
        # Filter out infrequently used kube-state-metrics.
        metricBlacklist:
        - 'kube.configmap.annotations.gauge'
        - 'kube.configmap.metadata.resource.version.gauge'
        - 'kube.endpoint.*'
        - 'kube.job.owner.gauge'
        - 'kube.job.labels.gauge'
        - 'kube.job.spec.completions.gauge'
        - 'kube.job.spec.parallelism.gauge'
        - 'kube.job.status.start.time.gauge'
        - 'kube.limitrange.*'
        - 'kube.namespace.annotations.gauge'
        - 'kube.persistentvolume.*'
        - 'kube.persistentvolumeclaim.*'
        - 'kube.pod.container.resource.limits.*'
        - 'kube.pod.container.*.reason.gauge'
        - 'kube.pod.owner.gauge'
        - 'kube.pod.start.time.gauge'
        - 'kube.pod.status.scheduled.gauge'
        - 'kube.pod.status.scheduled.time.gauge'
        - 'kube.replicationcontroller.created.gauge'
        - 'kube.replicationcontroller.metadata.generation.gauge'
        - 'kube.replicationcontroller.spec.replicas.gauge'
        - 'kube.resourcequota.*'
        - 'kube.secret.*'
        - 'kube.statefulset.*'
        - 'kube.storageclass.*'
        # Filter out generated labels
        tagExclude:
        - 'label?controller?revision*'
        - 'label?pod?template*'
        - 'annotation_kubectl_kubernetes_io_last_applied_configuration'

    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
        prefix: kubernetes.
        filters:
          metricBlacklist:
          - 'kubernetes.sys_container.*'
          - 'kubernetes.node.ephemeral_storage.*'

      internal_stats_source:
        prefix: kubernetes.

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

      telegraf_sources:
      - plugins: []

      {{- if .Values.collector.apiServerMetrics }}
      # 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:
          - 'kube.apiserver.apiserver.*'
          - 'kube.apiserver.etcd.*'
          - 'kube.apiserver.process.*'
      {{- 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.discovery.config }}

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

{{- end }}
