apiVersion: v1
kind: Namespace
metadata:
  name: datakit

---

apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
  name: datakit
rules:
- apiGroups: ["rbac.authorization.k8s.io"]
  resources: ["clusterroles"]
  verbs: ["get", "list", "watch"]
- apiGroups: [""]
  resources: ["nodes", "nodes/stats", "nodes/metrics", "namespaces", "pods", "pods/log", "events", "services", "endpoints", "persistentvolumes", "persistentvolumeclaims"]
  verbs: ["get", "list", "watch"]
- apiGroups: ["apps"]
  resources: ["deployments", "daemonsets", "statefulsets", "replicasets"]
  verbs: ["get", "list", "watch"]
- apiGroups: ["batch"]
  resources: ["jobs", "cronjobs"]
  verbs: [ "get", "list", "watch"]
- apiGroups: ["guance.com"]
  resources: ["datakits"]
  verbs: ["get","list"]
- apiGroups: ["monitoring.coreos.com"]
  resources: ["podmonitors", "servicemonitors"]
  verbs: ["get", "list"]
- apiGroups: ["metrics.k8s.io"]
  resources: ["pods", "nodes"]
  verbs: ["get", "list"]
- nonResourceURLs: ["/metrics"]
  verbs: ["get"]

---

apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
  name: datakits.guance.com
spec:
  group: guance.com
  versions:
    - name: v1beta1
      served: true
      storage: true
      schema:
        openAPIV3Schema:
          type: object
          properties:
            spec:
              type: object
              properties:
                instances:
                  type: array
                  items:
                    type: object
                    properties:
                      k8sNamespace:
                        type: string
                      k8sDeployment:
                        type: string
                      datakit/logs:
                        type: string
                      inputConf:
                        type: string
  scope: Namespaced
  names:
    plural: datakits
    singular: datakit
    kind: Datakit
    shortNames:
    - dk

---

apiVersion: v1
kind: ServiceAccount
metadata:
  name: datakit
  namespace: datakit

---

apiVersion: v1
kind: Service
metadata:
  name: datakit-service
  namespace: datakit
spec:
  selector:
    app: daemonset-datakit
  ports:
    - protocol: TCP
      port: 9529
      targetPort: 9529

---

apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  name: datakit
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: datakit
subjects:
- kind: ServiceAccount
  name: datakit
  namespace: datakit

---

apiVersion: apps/v1
kind: DaemonSet
metadata:
  labels:
    app: daemonset-datakit
  name: datakit
  namespace: datakit
spec:
  revisionHistoryLimit: 10
  selector:
    matchLabels:
      app: daemonset-datakit
  template:
    metadata:
      labels:
        app: daemonset-datakit
    spec:
      hostNetwork: true
      dnsPolicy: ClusterFirstWithHostNet
      containers:
      - env:
        - name: POD_NAME
          valueFrom:
            fieldRef:
              fieldPath: metadata.name

        - name: ENV_K8S_NODE_IP
          valueFrom:
            fieldRef:
              apiVersion: v1
              fieldPath: status.hostIP

        - name: ENV_K8S_NODE_NAME
          valueFrom:
            fieldRef:
              apiVersion: v1
              fieldPath: spec.nodeName

        #- name: ENV_K8S_CLUSTER_NODE_NAME
        #  value: cluster_a_$(ENV_K8S_NODE_NAME)

        - name: ENV_DATAWAY
          value: https://openway.guance.com?token=<YOUR-WORKSPACE-TOKEN> # Fill your real Dataway server and(or) workspace token
        - name: ENV_CLUSTER_NAME_K8S
          value: default
        - name: ENV_GLOBAL_HOST_TAGS
          value: host=__datakit_hostname,host_ip=__datakit_ip
        - name: ENV_GLOBAL_ELECTION_TAGS
          value: ""                                            # Default not set
        - name: ENV_DEFAULT_ENABLED_INPUTS
          value: dk,cpu,disk,diskio,mem,swap,system,hostobject,net,host_processes,container
        - name: ENV_ENABLE_ELECTION
          value: enable
        - name: ENV_HTTP_LISTEN
          value: 0.0.0.0:9529
        - name: HOST_PROC
          value: /rootfs/proc
        - name: HOST_SYS
          value: /rootfs/sys
        - name: HOST_ETC
          value: /rootfs/etc
        - name: HOST_VAR
          value: /rootfs/var
        - name: HOST_RUN
          value: /rootfs/run
        - name: HOST_DEV
          value: /rootfs/dev
        - name: HOST_ROOT
          value: /rootfs
        # - name: ENV_GIT_BRANCH
        #   value: master
        # - name: ENV_GIT_INTERVAL
        #   value: 10s
        # - name: ENV_GIT_URL
        #   value: http://username:password@github.com/path/to/repository.git
        # # ---iploc-start
        #- name: ENV_IPDB
        #  value: iploc
        # # ---iploc-end
        image: pubrepo.guance.com/datakit/datakit:1.60.0
        imagePullPolicy: Always
        name: datakit
        ports:
        - containerPort: 9529
          hostPort: 9529
          name: port
          protocol: TCP
        resources:
          requests:
            cpu: "200m"
            memory: "128Mi"
          limits:
            cpu: "2000m"
            memory: "4Gi"
        securityContext:
          privileged: true
        volumeMounts:
        - mountPath: /usr/local/datakit/cache
          name: cache
          readOnly: false
        - mountPath: /rootfs
          name: rootfs
          mountPropagation: HostToContainer
        - mountPath: /var/run
          name: run
          mountPropagation: HostToContainer
        - mountPath: /sys/kernel/debug
          name: debugfs
        - mountPath: /var/lib/containerd/container_logs
          name: container-logs
        # # ---iploc-start
        #- mountPath: /usr/local/datakit/data/ipdb/iploc/
        #  name: datakit-ipdb
        # # ---iploc-end
        #- mountPath: /usr/local/datakit/conf.d/db/mysql.conf
        #  name: datakit-conf
        #  subPath: mysql.conf
        #  readOnly: true
        #- mountPath: /usr/local/datakit/conf.d/db/redis.conf
        #  name: datakit-conf
        #  subPath: redis.conf
        #  readOnly: true
        # - mountPath: /usr/local/datakit/conf.d/pythond/pythond.conf
        #   name: datakit-conf
        #   subPath: pythond.conf
        #   readOnly: true
        # - name: hellopythond
        #   mountPath: /usr/local/datakit/python.d/hellopythond/
        #   readOnly: true
        workingDir: /usr/local/datakit
      # # ---iploc-start
      #initContainers:
      #  - args:
      #      - tar -xf /opt/iploc.tar.gz -C /usr/local/datakit/data/ipdb/iploc/
      #    command:
      #      - bash
      #      - -c
      #    image: pubrepo.guance.com/datakit/iploc:1.0
      #    imagePullPolicy: IfNotPresent
      #    name: init-volume
      #    resources: {}
      #    volumeMounts:
      #      - mountPath: /usr/local/datakit/data/ipdb/iploc/
      #        name: datakit-ipdb
      # # ---iploc-end
      hostIPC: true
      hostPID: true
      restartPolicy: Always
      serviceAccount: datakit
      serviceAccountName: datakit
      tolerations:
      - operator: Exists
      volumes:
      - configMap:
          name: datakit-conf
        name: datakit-conf
      # - name: hellopythond
      #   configMap:
      #     name: python-scripts
      - hostPath:
          path: /
        name: rootfs
      - hostPath:
          path: /var/run
        name: run
      - hostPath:
          path: /sys/kernel/debug
        name: debugfs
      - hostPath:
          path: /root/datakit_cache
        name: cache
      - hostPath:
          path: /var/lib/containerd/container_logs
        name: container-logs
      # # ---iploc-start
      #- emptyDir: {}
      #  name: datakit-ipdb
      # # ---iploc-end
  updateStrategy:
    rollingUpdate:
      maxUnavailable: 1
    type: RollingUpdate

---

apiVersion: v1
kind: ConfigMap
metadata:
  name: datakit-conf
  namespace: datakit
data:
    #mysql.conf: |-
    #  [inputs.mysql]
    #  ...
    #redis.conf: |-
    #  [inputs.redis]
    #  ...
    # pythond.conf: |
    #   [[inputs.pythond]]
    #     # Python input name
    #     name = 'some-python-inputs'  # required
    #     # System environments to run Python
    #     #envs = ['LD_LIBRARY_PATH=/path/to/lib:$LD_LIBRARY_PATH',]
    #     # Python path(recomment abstract Python path)
    #     cmd = "python3" # required. python3 is recommended.
    #     # Python scripts relative path
    #     dirs = ["hellopythond"]
---

# apiVersion: v1
# kind: ConfigMap
# metadata:
#   name: python-scripts
#   namespace: datakit
# data:
#     hellopythond.py: |
#       from datakit_framework import DataKitFramework
#       import random
#       class HelloPythond(DataKitFramework):
#           __name = 'HelloPythond'
#           interval = 10
#           def run(self):
#               rd = random.randint(1, 100)
#               data = [
#                 {
#                     "measurement": "random_number",
#                     "tags": {
#                       "tag_name": "tag_value",
#                     },
#                     "fields": {
#                       "number": rd,
#                     },
#                 },
#               ]
#               in_data = {
#                   'M':data,
#                   'input': "pyfromgit"
#               }
#               return self.report(in_data)

---

