---
apiVersion: v1
kind: ConfigMap
metadata:
  name: apm-server-deployment-config
  namespace: logging
  labels:
    k8s-app: apm-server
data:
  apm-server.yml: |-
    cloud.id: ${ELASTIC_CLOUD_ID}
    cloud.auth: ${ELASTIC_CLOUD_AUTH}

    apm-server:
      host: 0.0.0.0:8200

    output.elasticsearch:
      hosts: ['${ELASTICSEARCH_HOST:elasticsearch-master.logging}:${ELASTICSEARCH_PORT:9200}']
      username: ${ELASTICSEARCH_USERNAME}
      password: ${ELASTICSEARCH_PASSWORD}
---

apiVersion: rbac.authorization.k8s.io/v1beta1
kind: ClusterRoleBinding
metadata:
  name: apm-server
subjects:
- kind: ServiceAccount
  name: apm-server
  namespace: logging
roleRef:
  kind: ClusterRole
  name: apm-server
  apiGroup: rbac.authorization.k8s.io
---
apiVersion: rbac.authorization.k8s.io/v1beta1
kind: ClusterRole
metadata:
  name: apm-server
  labels:
    k8s-app: apm-server
rules:
- apiGroups: [""]
  resources:
  - nodes
  - namespaces
  - events
  - pods
  verbs: ["get", "list", "watch"]
- apiGroups: ["extensions"]
  resources:
  - replicasets
  verbs: ["get", "list", "watch"]
- apiGroups: ["apps"]
  resources:
  - statefulsets
  - deployments
  verbs: ["get", "list", "watch"]
- apiGroups:
  - ""
  resources:
  - nodes/stats
  verbs:
  - get
---
apiVersion: v1
kind: ServiceAccount
metadata:
  name: apm-server
  namespace: logging
  labels:
    k8s-app: apm-server
---

apiVersion: apps/v1beta1
kind: Deployment
metadata:
  name: apm-server
  namespace: logging
  labels:
    k8s-app: apm-server
spec:
  template:
    metadata:
      labels:
        k8s-app: apm-server
    spec:
      serviceAccountName: apm-server
      containers:
      - name: apm-server
        image: elastic/apm-server:7.1.1
        ports:
        - containerPort: 8200
          name: http
        args: [
          "-c", "/etc/apm-server.yml",
          "-e",
        ]
        env:
        - name: ELASTICSEARCH_HOST
          value: elasticsearch-master.logging
        - name: ELASTICSEARCH_PORT
          value: "9200"
        - name: ELASTICSEARCH_USERNAME
          value: elastic
        - name: ELASTICSEARCH_PASSWORD
          value: changeme
        - name: ELASTIC_CLOUD_ID
          value: ""
        - name: ELASTIC_CLOUD_AUTH
          value: ""
        - name: NODE_NAME
          valueFrom:
            fieldRef:
              fieldPath: spec.nodeName
        securityContext:
          runAsUser: 0
        resources:
          limits:
            cpu: "300m"
            memory: 256Mi
          requests:
            cpu: 100m
            memory: 100Mi
        volumeMounts:
        - name: config
          mountPath: /etc/apm-server.yml
          readOnly: true
          subPath: apm-server.yml
      volumes:
      - name: config
        configMap:
          defaultMode: 0600
          name: apm-server-deployment-config
---
apiVersion: v1
kind: Service
metadata:
  name: apm-server
  namespace: logging
  labels:
    k8s-app: apm-server
spec:
  ports:
  - port: 8200
    name: http
  selector:
    k8s-app: apm-server
---
