apiVersion: v1
kind: ConfigMap
metadata:
  name: event-exporter-app
  namespace: giantswarm
data:
  config.yaml: |
    logLevel: debug

    # We do not have webhooks configured on all Control Planes since they are
    # part of an optional feature, which is sending Kubernetes Events to Slack.
    # In case the webhook is not present Helm cannot template the configmap
    # properly. Therefore we do not configure any matchers nor receivers if
    # there is no webhook configured for a given Control Plane.

    route:
      routes:

        # We want to post kubernetes events from cluster-operator and aws-operator to grafana.
        - match:
          - kind: "Cluster|AWSCluster|MachineDeployment|AWSMachineDeployment|G8sControlPlane|AWSControlPlane"
            receiver: "grafana-firecracker"
        {{- if .Values.slack }}
        # We want to send Workload Cluster upgrade events to Slack via the webhook
        # receiver. Therefore we match for events attached to the CAPI Cluster
        # CRs.
        - match:
          - kind: "Cluster"
            reason: "ClusterIsUpdating"
            receiver: "webhook-upgrade"
        # We want to send Workload Cluster upgrade announcement to Slack via the webhook
        # receiver. Therefore we match for events attached to the CAPI Cluster
        # CRs.
        - match:
          - kind: "Cluster"
            reason: "ClusterUpgradeAnnouncement"
            receiver: "webhook-announcement"
        # We want to send Workload Cluster upgrade completed announcement to Slack via the webhook
        # receiver. Therefore we match for events attached to the CAPI Cluster
        # CRs.
        - match:
          - kind: "Cluster"
            reason: "ClusterUpdated"
            receiver: "webhook-upgrade-complete"
        {{- end }}

        # We want to ignore all other events once we handled the events we are
        # actually interested in. Therefore the drop rule has to be evaluated at
        # the end of the tree.
        - drop:
          - namespace: "*"

    receivers:

      {{- if .Values.slack }}
      - name: "webhook-announcement"
        webhook:
          endpoint: "{{ .Values.slack.webhook }}"
          layout:
            text: "{{ "{{" }} .Message {{ "}}" }}"
      - name: "webhook-upgrade"
        webhook:
          endpoint: "{{ .Values.slack.webhook }}"
          layout:
            text: "The cluster upgrade of {{ "{{" }} .InvolvedObject.Namespace {{ "}}" }}/{{ "{{" }} .InvolvedObject.Name {{ "}}" }} on {{ .Values.managementCluster.name }} has begun, this upgrade will include replacing of nodes."
      - name: "webhook-upgrade-complete"
        webhook:
          endpoint: "{{ .Values.slack.webhook }}"
          layout:
            text: "The cluster upgrade of {{ "{{" }} .InvolvedObject.Namespace {{ "}}" }}/{{ "{{" }} .InvolvedObject.Name {{ "}}" }} on {{ .Values.managementCluster.name }} has completed and all nodes have been replaced."
      {{- end }}

      - name: "grafana-firecracker"
        webhook:
          endpoint: "https://giantswarm.grafana.net/api/annotations/graphite"
          headers:
            Accept: application/json
            Content-Type: application/json
            Authorization: "{{ .Values.grafana.token }}"
          layout:
            what: "{{ "{{" }} .InvolvedObject.Kind {{ "}}" }} - {{ "{{" }} .Reason {{ "}}" }}"
            tags: ["event",
              "firecracker",
              "{{ "{{" }} .Reason {{ "}}" }}",
              "{{ "{{" }} .Type {{ "}}" }}",
              "{{ "{{" }} .InvolvedObject.Kind {{ "}}" }}",
              "{{ "{{" }} .InvolvedObject.Name {{ "}}" }}",
              "{{ .Values.managementCluster.Name }}",
              "{{ .Values.managementCluster.customer }}",
              "{{ .Values.provider.kind }}",
              {{- if .Values.testingEnvironment }}
              "testing",
              {{- else }}
              "stable",
              {{- end }}
              "{{ "{{" }} .Source.Component {{ "}}" }}"]
            data: "{{ "{{" }} .Message {{ "}}" }} for {{ "{{" }} .InvolvedObject.Kind {{ "}}" }} {{ "{{" }} .InvolvedObject.Namespace {{ "}}" }}/{{ "{{" }} .InvolvedObject.Name {{ "}}" }} on {{ .Values.managementCluster.name }} reported by {{ "{{" }} .Source.Component {{ "}}" }}"
