apiVersion: v1
kind: Namespace
metadata:
  name: rsdatakit-system
---
apiVersion: v1
kind: ServiceAccount
metadata:
  name: rsdatakit
  namespace: rsdatakit-system
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
  name: rsdatakit
rules:
  - apiGroups: [""]
    resources: ["nodes", "pods", "services"]
    verbs: ["get", "list", "watch"]
  - apiGroups: [""]
    resources: ["nodes/stats"]
    verbs: ["get"]
  - apiGroups: ["discovery.k8s.io"]
    resources: ["endpointslices"]
    verbs: ["get", "list", "watch"]
  - apiGroups: ["monitoring.coreos.com"]
    resources: ["podmonitors", "servicemonitors"]
    verbs: ["get", "list", "watch"]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  name: rsdatakit
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: rsdatakit
subjects:
  - kind: ServiceAccount
    name: rsdatakit
    namespace: rsdatakit-system
---
apiVersion: v1
kind: ConfigMap
metadata:
  name: rsdatakit
  namespace: rsdatakit-system
data:
  rsdatakit.toml: |
    [agent]
    state_dir = "/var/lib/rsdatakit"
    health_addr = "0.0.0.0:4319"

    [dataway]
    url = "http://127.0.0.1:9528"
    token = ""
    timeout_secs = 15

    [kubernetes]
    enabled = true
    metrics_enabled = true
    objects_enabled = true
    cluster_name = "default"
    refresh_secs = 60
    object_interval_secs = 300
    label_tags = ["app.kubernetes.io/name", "app.kubernetes.io/component", "app.kubernetes.io/version"]

    [kubernetes.prometheus]
    enabled = false
    scrape_interval_secs = 30
    scrape_timeout_secs = 10
    honor_timestamps = true
    keep_exist_metric_name = true
    discover_pod_annotations = true
    discover_service_annotations = true
    discover_pod_monitors = true
    discover_service_monitors = true
    max_targets = 256
    max_concurrent_scrapes = 4
    max_response_bytes = 4194304
    max_samples_per_scrape = 50000
    max_discovery_objects = 20000
    max_discovery_bytes = 33554432

    [metrics]
    interval_secs = 15
    mounts = []
    enable_virtual_interfaces = false

    [logs]
    enabled = false
    paths = ["/var/log/pods/rsdatakit-system_rsdatakit-*/*/*.log"]
    source = "kubernetes"
    service = ""
    max_files = 512
    discovery_secs = 5
    max_line_bytes = 262144
    json_as_fields = false
    copytruncate_mode = "auto"
    state_dir = "/var/lib/rsdatakit"
    journald_enabled = false

    [logs.multiline]
    continuation_regex = ""
    flush_timeout_ms = 1000
    max_bytes = 262144

    [otlp]
    enabled = false
    http_addr = "0.0.0.0:4318"
    grpc_addr = "0.0.0.0:4317"
    bearer_token = ""
    max_request_bytes = 4194304
    max_concurrent_requests = 8
    max_inflight_bytes = 16777216

    [wal]
    dir = "/var/lib/rsdatakit/wal"
    max_bytes = 268435456
---
apiVersion: apps/v1
kind: DaemonSet
metadata:
  name: rsdatakit
  namespace: rsdatakit-system
  labels:
    app.kubernetes.io/name: rsdatakit
spec:
  selector:
    matchLabels:
      app.kubernetes.io/name: rsdatakit
  updateStrategy:
    type: RollingUpdate
    rollingUpdate:
      maxUnavailable: 10%
  template:
    metadata:
      labels:
        app.kubernetes.io/name: rsdatakit
    spec:
      serviceAccountName: rsdatakit
      automountServiceAccountToken: true
      hostNetwork: true
      dnsPolicy: ClusterFirstWithHostNet
      terminationGracePeriodSeconds: 40
      nodeSelector:
        kubernetes.io/os: linux
      tolerations:
        - operator: Exists
      containers:
        - name: rsdatakit
          image: pubrepo.guance.com/datakit/rsdatakit:1.2.0
          imagePullPolicy: IfNotPresent
          args: ["--config", "/etc/rsdatakit/rsdatakit.toml"]
          env:
            - name: RSDATAKIT_HOST
              valueFrom:
                fieldRef:
                  fieldPath: spec.nodeName
            - name: RSDATAKIT_K8S_ENABLED
              value: "true"
            - name: RSDATAKIT_K8S_CLUSTER_NAME
              value: "default"
            - name: RSDATAKIT_K8S_PROMETHEUS_ENABLED
              value: "true"
            - name: RSDATAKIT_K8S_NODE_NAME
              valueFrom:
                fieldRef:
                  fieldPath: spec.nodeName
            - name: RSDATAKIT_K8S_NODE_IP
              valueFrom:
                fieldRef:
                  fieldPath: status.hostIP
            - name: RSDATAKIT_DATAWAY_URL
              valueFrom:
                secretKeyRef:
                  name: rsdatakit-secrets
                  key: dataway-url
            - name: RSDATAKIT_DATAWAY_TOKEN
              valueFrom:
                secretKeyRef:
                  name: rsdatakit-secrets
                  key: dataway-token
            - name: RSDATAKIT_OTLP_HTTP_ADDR
              value: "0.0.0.0:4318"
            - name: RSDATAKIT_OTLP_GRPC_ADDR
              value: "0.0.0.0:4317"
            - name: RSDATAKIT_OTLP_BEARER_TOKEN
              valueFrom:
                secretKeyRef:
                  name: rsdatakit-secrets
                  key: otlp-token
          ports:
            - name: health
              containerPort: 4319
              protocol: TCP
            - name: otlp-grpc
              containerPort: 4317
              hostPort: 4317
              protocol: TCP
            - name: otlp-http
              containerPort: 4318
              hostPort: 4318
              protocol: TCP
          readinessProbe:
            httpGet:
              path: /health
              port: health
            initialDelaySeconds: 5
            periodSeconds: 10
            timeoutSeconds: 2
          livenessProbe:
            tcpSocket:
              port: health
            initialDelaySeconds: 15
            periodSeconds: 20
            timeoutSeconds: 2
            failureThreshold: 3
          resources:
            requests:
              cpu: 50m
              memory: 64Mi
            limits:
              cpu: "1"
              memory: 512Mi
          securityContext:
            allowPrivilegeEscalation: false
            readOnlyRootFilesystem: true
            capabilities:
              drop: ["ALL"]
          volumeMounts:
            - name: config
              mountPath: /etc/rsdatakit
              readOnly: true
            - name: pod-logs
              mountPath: /var/log/pods
              readOnly: true
            - name: docker-container-logs
              mountPath: /var/lib/docker/containers
              readOnly: true
            - name: state
              mountPath: /var/lib/rsdatakit
      volumes:
        - name: config
          configMap:
            name: rsdatakit
        - name: pod-logs
          hostPath:
            path: /var/log/pods
            type: Directory
        - name: docker-container-logs
          hostPath:
            path: /var/lib/docker/containers
            type: DirectoryOrCreate
        - name: state
          hostPath:
            path: /var/lib/rsdatakit
            type: DirectoryOrCreate
---
apiVersion: v1
kind: Service
metadata:
  name: rsdatakit-otlp
  namespace: rsdatakit-system
spec:
  selector:
    app.kubernetes.io/name: rsdatakit
  internalTrafficPolicy: Local
  ports:
    - name: otlp-grpc
      port: 4317
      targetPort: otlp-grpc
    - name: otlp-http
      port: 4318
      targetPort: otlp-http
