# Uncomment if you want to change the template messages.
# ---
# apiVersion: v1
# kind: ConfigMap
# metadata:
#   name: fluxcloud-templates
# data:
#   title.tmpl: |
#     Applied flux changes to cluster {{ getenv "CLUSTER" }}
#   body.tmpl: |
#     Event: {{ .EventString }}
#     {{ if and (ne .EventType "commit") (gt (len .Commits) 0) }}Commits:
#     {{ range .Commits }}
#     * {{ call $.FormatLink (print $.VCSLink "/commit/" .Revision) (truncate .Revision 7) }}: {{ .Message }}
#     {{end}}{{end}}
#     {{ if (gt (len .EventServiceIDs) 0) }}Resources updated:
#     {{ range .EventServiceIDs }}
#     * {{ . }}
#     {{ end }}{{ end }}
#     {{ if gt (len .Errors) 0 }}Errors:
#     {{ range .Errors }}
#     Resource {{ .ID }}, file: {{ .Path }}:
#     
#     > {{ .Error }}
#     {{ end }}{{ end }}
#   commit.tmpl: |
#     {{ .VCSLink }}/commit/{{ .Commit }}
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: flux
spec:
  replicas: 1
  selector:
    matchLabels:
      name: flux
  strategy:
    type: Recreate
  template:
    metadata:
      annotations:
        prometheus.io.port: "3031" # tell prometheus to scrape /metrics endpoint's port.
      labels:
        name: flux
    spec:
      serviceAccount: flux
      volumes:
      - name: git-key
        secret:
          secretName: flux-git-deploy
          defaultMode: 0400 # when mounted read-only, we won't be able to chmod

      # This is a tmpfs used for generating SSH keys. In K8s >= 1.10,
      # mounted secrets are read-only, so we need a separate volume we
      # can write to.
      - name: git-keygen
        emptyDir:
          medium: Memory

      containers:
      - name: flux
        # There are no ":latest" images for flux. Find the most recent
        # release or image version at https://quay.io/weaveworks/flux
        # and replace the tag here.
        image: quay.io/weaveworks/flux:1.9.0
        imagePullPolicy: IfNotPresent
        ports:
        - containerPort: 3030 # informational
        volumeMounts:
        - name: git-key
          mountPath: /etc/fluxd/ssh # to match image's ~/.ssh/config
          readOnly: true # this will be the case perforce in K8s >=1.10
        - name: git-keygen
          mountPath: /var/fluxd/keygen # to match image's ~/.ssh/config
        args:

        # if you deployed memcached in a different namespace to flux,
        # or with a different service name, you can supply these
        # following two arguments to tell fluxd how to connect to it.
        # - --memcached-hostname=memcached.default.svc.cluster.local
        # - --memcached-service=memcached

        # this must be supplied, and be in the tmpfs (emptyDir)
        # mounted above, for K8s >= 1.10
        - --ssh-keygen-dir=/var/fluxd/keygen
        # replace (at least) the following URL
        - --git-url=git@github.com:weaveworks/flux-example
        - --git-branch=master
        - --listen-metrics=:3031
        - --connect=ws://127.0.0.1:3032
      - name: fluxcloud
        image: tfgco/fluxcloud:v0.5.0
        imagePullPolicy: Always
        ports:
        - containerPort: 3032
        env:
        # Set these environment variables:
        - name: SLACK_URL
          value: "https://hooks.slack.com/services/WEBHOOK_DATA"
        - name: SLACK_CHANNEL
          value: "#kubernetes"
        # Or configure multiple channels
        # (comma separated <channel>=<namespace>) string:
        #  value: "#kubernetes=*,#team=team"
        # Optional: legacy Slack API token
        # - name: SLACK_TOKEN
        #   valueFrom:
        #     secretKeyRef:
        #       key: token
        #       name: slack-token
        - name: SLACK_USERNAME
          value: Flux Deployer
        - name: SLACK_ICON_EMOJI
          value: ":heart:"
        - name: GITHUB_URL
          value: "https://github.com/topfreegames/fluxcloud/"
        - name: LISTEN_ADDRESS
          value: ":3032"
      # Uncomment if you want to change the template messages.
      #   volumeMounts:
      #   - name: templates-volume
      #     mountPath: /app/templates
      # volumes:
      # - name: templates-volume
      #   configMap:
      #     name: fluxcloud-templates
