apiVersion: apps/v1
kind: Deployment
metadata:
  name: metric-agent
  namespace: imxc
  labels:
    app: metric-agent
spec:
  selector:
    matchLabels:
      app: metric-agent
  replicas: 1
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        app: metric-agent
    spec:
      containers:
      - name: metric-agent
        image: {{ .Values.global.IMXC_IN_REGISTRY }}/metric-agent:{{ .Values.global.METRIC_AGENT_VERSION }}
        imagePullPolicy: IfNotPresent
        ports:
        - containerPort: 14271
        - containerPort: 14272
        args:
          - --config.file=/etc/metric-agent/metric-agent.yml
        env:
          - name: STORAGE_TYPE
            value: datagate
          - name: DATAGATE
            value: datagate:50051
          - name: CLUSTER_ID
            value: cloudmoa
#          - name: USER_ID
#            value: mskim@ex-em.com
        volumeMounts:
          - mountPath: /etc/metric-agent/
            name: config-volume
        resources:
          requests:
            memory: "256Mi"
            cpu: "100m"
          limits:
            memory: "1000Mi"
            cpu: "300m"
      volumes:
        - name: config-volume
          configMap:
            name: metric-agent-config
      securityContext:
        runAsUser: 1000
---
apiVersion: v1
kind: Service
metadata:
  name: metric-agent
  namespace: imxc
  labels:
    app: metric-agent
spec:
  ports:
    - name: metric
      port: 14271
      targetPort: 14271
  selector:
    app: metric-agent

---
apiVersion: v1
kind: ConfigMap
metadata:
  name: metric-agent-config
  namespace: imxc
data:
  metric-agent.yml: |
    global:
      scrape_interval:     10s
      evaluation_interval: 5s # Evaluate rules every 15 seconds. The default is every 1 minute.

    scrape_configs:
      - job_name: 'kubernetes-kubelet'
        scheme: https
        tls_config:
          ca_file: /var/run/secrets/kubernetes.io/serviceaccount/ca.crt
        bearer_token_file: /var/run/secrets/kubernetes.io/serviceaccount/token
        kubernetes_sd_configs:
        - role: node
        relabel_configs:
        - action: labelmap
          regex: __meta_kubernetes_node_label_(.+)
        - target_label: __address__
          replacement: kubernetes.default.svc:443
        - source_labels: [__meta_kubernetes_node_name]
          regex: (.+)
          target_label: __metrics_path__
          replacement: /api/v1/nodes/${1}/proxy/metrics
        - source_labels: [__meta_kubernetes_node_label_kubernetes_io_hostname]
          target_label: xm_node_id
        - target_label: xm_clst_id
          replacement: 'cloudmoa'
        - target_label: xm_entity_type
          replacement: 'Node'

        # added by mskim 8/19
        metric_relabel_configs:
        - source_labels: [ __name__ ]
          regex: (kubelet_running_pod_count)
          action: keep


      - job_name: 'kubernetes-node-agent'
        tls_config:
          ca_file: /var/run/secrets/kubernetes.io/serviceaccount/ca.crt
        bearer_token_file: /var/run/secrets/kubernetes.io/serviceaccount/token
        kubernetes_sd_configs:
        - role: node

        relabel_configs:
        - action: labelmap
          regex: __meta_kubernetes_node_label_(.+)
        - source_labels: [__meta_kubernetes_role]
          action: replace
          target_label: kubernetes_role
        - source_labels: [__address__]
          regex: '(.*):10250'
          replacement: '${1}:9100'
          target_label: __address__
        - source_labels: [__meta_kubernetes_node_label_kubernetes_io_hostname]
          target_label: __instance__
          # set "name" value to "job"
        - source_labels: [job]
          regex: 'kubernetes-(.*)'
          replacement: '${1}'
          target_label: name
        - target_label: xm_clst_id
          replacement: 'cloudmoa'
        - source_labels: [__meta_kubernetes_node_label_kubernetes_io_hostname]
          target_label: xm_node_id
        - target_label: xm_entity_type
          replacement: 'Node'
        - source_labels: [__meta_kubernetes_namespace]
          separator: ;
          regex: (.*)
          target_label: xm_namespace
          replacement: $1
          action: replace

        # added by mskim 8/19
        metric_relabel_configs:
        - source_labels: [ __name__ ]
          regex: (node_boot_time_seconds|node_context_switches_total|node_cpu_frequency_max_hertz|node_cpu_package_throttles_total|node_cpu_seconds_total|node_disk_io_time_seconds_total|node_disk_read_bytes_total|node_disk_read_time_seconds_total|node_disk_reads_completed_total|node_disk_write_time_seconds_total|node_disk_writes_completed_total|node_disk_written_bytes_total|node_filefd_allocated|node_filesystem_avail_bytes|node_filesystem_free_bytes|node_filesystem_size_bytes|node_load1|node_load15|node_load5|node_memory_Active_bytes|node_memory_Buffers_bytes|node_memory_Cached_bytes|node_memory_MemAvailable_bytes|node_memory_MemFree_bytes|node_memory_MemTotal_bytes|node_memory_SwapCached_bytes|node_memory_SwapFree_bytes|node_memory_SwapTotal_bytes|node_network_receive_bytes_total|node_network_receive_bytes_total|node_network_transmit_bytes_total|node_network_transmit_bytes_total)
          action: keep

      - job_name: 'kubernetes-cadvisor'
        scheme: https

        tls_config:
          ca_file: /var/run/secrets/kubernetes.io/serviceaccount/ca.crt
          insecure_skip_verify: true
        bearer_token_file: /var/run/secrets/kubernetes.io/serviceaccount/token

        kubernetes_sd_configs:
        - role: node
        metrics_path: /metrics/cadvisor

        relabel_configs:
        - action: labelmap
          regex: __meta_kubernetes_node_label_(.+)
        - target_label: xm_clst_id
          replacement: 'cloudmoa'
        - source_labels: [__meta_kubernetes_node_label_kubernetes_io_hostname]
          target_label: xm_node_id
        - target_label: xm_entity_type
          replacement: 'Container'

{{- if semverCompare ">=1.16-0" .Capabilities.KubeVersion.GitVersion }}
        metric_relabel_configs:
        - source_labels: [namespace]
          target_label: xm_namespace
        - source_labels: [pod]
          target_label: xm_pod_id
        - source_labels: [container]
          target_label: xm_cont_name
        - source_labels: [id]
          target_label: xm_cont_id
        # added by mskim 8/19
        - source_labels: [ __name__ ]
          regex: (container_cpu_cfs_throttled_seconds_total|container_cpu_system_seconds_total|container_cpu_usage_seconds_total|container_cpu_user_seconds_total|container_fs_limit_bytes|container_fs_reads_bytes_total|container_fs_usage_bytes|container_fs_writes_bytes_total|container_last_seen|container_memory_cache|container_memory_max_usage_bytes|container_memory_swap|container_memory_usage_bytes|container_memory_working_set_bytes|container_network_receive_bytes_total|container_network_transmit_bytes_total|container_spec_memory_limit_bytes)
          action: keep

  {{- else }}
        metric_relabel_configs:
        - source_labels: [namespace]
          target_label: xm_namespace
        - source_labels: [pod_name]
          target_label: xm_pod_id
        - source_labels: [container_name]
          target_label: xm_cont_name
        - source_labels: [id]
          target_label: xm_cont_id
        # added by mskim 8/19
        - source_labels: [ __name__ ]
          regex: (container_cpu_cfs_throttled_seconds_total|container_cpu_system_seconds_total|container_cpu_usage_seconds_total|container_cpu_user_seconds_total|container_fs_limit_bytes|container_fs_reads_bytes_total|container_fs_usage_bytes|container_fs_writes_bytes_total|container_last_seen|container_memory_cache|container_memory_max_usage_bytes|container_memory_swap|container_memory_usage_bytes|container_memory_working_set_bytes|container_network_receive_bytes_total|container_network_transmit_bytes_total|container_spec_memory_limit_bytes)
          action: keep
{{- end }}
        # CLOUD-8671 | 데이터 필터링 설정 추가
        - source_labels: [ __name__, image ]
          separator: "@"
          regex: "container_cpu.*@"
          action: drop
        - source_labels: [ __name__, name ]
          separator: "@"
          regex: "container_memory.*@"
          action: drop

      - job_name: 'kafka-consumer'
        metrics_path: /remote_prom
        scrape_interval: 5s
        scrape_timeout: 5s
        scheme: kafka
        static_configs:
        - targets: ['kafka-broker:9094']
        params:
          #server_addrs: ['broker.default.svc.k8s:9094']
          server_addrs: ['kafka-broker:9094']
          encoding: [proto3]
          contents: [remote_write]
          compression: [snappy]
          group: [remote-write-consumer]
          workers: [50]

      # job for API server (SpringBoot) commented by ersione 2019-09-19
      - job_name: 'imxc-api'
        metrics_path: '/actuator/prometheus'
        scrape_interval: 5s
        static_configs:
        - targets: ['imxc-api-service:8080']
      - job_name: 'imxc-noti'
        metrics_path: '/actuator/prometheus'
        scrape_interval: 15s
        static_configs:
        - targets: ['noti-server-service:8080']
      #- job_name: 'imxc-auth'
      #  metrics_path: '/actuator/prometheus'
      #  scrape_interval: 15s
      #  static_configs:
      #  - targets: ['auth-server-service:8480']



      - job_name: 'alertmanager-exporter'
        metrics_path: '/metrics'
        scrape_interval: 5s
        static_configs:
        - targets: ['alertmanager:9093']


      # modified by seungtak choi 2020-02-18
      - job_name: 'cmoa-collector'
        scrape_interval: 5s
        kubernetes_sd_configs:
          - role: endpoints
            namespaces:
              names:
                - imxc
        relabel_configs:
          - source_labels: [__meta_kubernetes_service_name]
            action: keep
            regex:  cmoa-collector

      # added by dwkim  2021-03-15
      - job_name: 'elasticsearch'
        scrape_interval: 5s
        kubernetes_sd_configs:
        - role: endpoints
          namespaces:
            names:
            - imxc
        relabel_configs:
        - target_label: xm_clst_id
          replacement: 'cloudmoa'
        - source_labels: [__meta_kubernetes_pod_node_name]
          target_label: xm_node_id
        - source_labels: [__meta_kubernetes_namespace]
          target_label: xm_namespace
        - source_labels: [__meta_kubernetes_service_name]
          action: keep
          regex: es-exporter-elasticsearch-exporter

      # kafka-exporter prometheus 수집 룰 추가
      - job_name: 'kafka-exporter'
        kubernetes_sd_configs:
        - role: endpoints
          namespaces:
            names:
            - imxc
        scheme: http
        relabel_configs:
        - action: labelmap
          regex: __meta_kubernetes_service_label_(.+)
        - action: labelmap
          regex: __meta_kubernetes_service_annotation_(.+)
        - source_labels: [__meta_kubernetes_pod_container_port_number]
          action: keep
          regex: '(.*)9308'

      # kafka-jmx-exporter configuration yaml 수집룰 추가
      - job_name: 'kafka-jmx'
        kubernetes_sd_configs:
        - role: endpoints
          namespaces:
            names:
            - imxc
        scheme: http
        relabel_configs:
        - action: labelmap
          regex: __meta_kubernetes_service_label_(.+)
        - action: labelmap
          regex: __meta_kubernetes_service_annotation_(.+)
        - source_labels: [__meta_kubernetes_pod_container_port_number]
          action: keep
          regex: '(.*)9010'

      # job for API Server(Spring Cloud Notification Server) commented by hjyoon 2022-01-26
      - job_name: 'cmoa-noti'
        metrics_path: '/actuator/prometheus'
        scrape_interval: 15s
        static_configs:
        - targets: ['noti-server-service:8080']
