#cloud-config
write_files:
  - path: /etc/systemd/system/enable-coredns.service
    owner: "root:root"
    permissions: "0644"
    content: |
      [Unit]
      Description=Configure Kubernetes CoreDNS Addon

      [Service]
      Type=oneshot
      EnvironmentFile=/etc/sysconfig/heat-params
      ExecStart=/etc/sysconfig/enable-coredns.sh

      [Install]
      WantedBy=multi-user.target

  - path: /etc/sysconfig/enable-coredns.sh
    owner: "root:root"
    permissions: "0755"
    content: |
      #!/bin/sh

      TEMPLATE=/etc/kubernetes/addons/coredns-sa.yaml
      mkdir -p $(dirname ${TEMPLATE})
      cat > $TEMPLATE <<EOF
      apiVersion: v1
      kind: ServiceAccount
      metadata:
        name: coredns
        namespace: kube-system
        labels:
          kubernetes.io/cluster-service: "true"
          addonmanager.kubernetes.io/mode: Reconcile
      EOF

      TEMPLATE=/etc/kubernetes/addons/coredns-cm.yaml
      mkdir -p $(dirname ${TEMPLATE})
      cat > $TEMPLATE <<EOF
      apiVersion: v1
      kind: ConfigMap
      metadata:
        name: coredns
        namespace: kube-system
        labels:
          addonmanager.kubernetes.io/mode: EnsureExists
      data:
        Corefile: |
          .:53 {
              errors
              log stdout
              health
              kubernetes ${DNS_CLUSTER_DOMAIN} {
                cidrs ${PORTAL_NETWORK_CIDR}
              }
              proxy . /etc/resolv.conf
              cache 30
          }
      EOF

      TEMPLATE=/etc/kubernetes/addons/coredns-svc.yaml
      mkdir -p $(dirname ${TEMPLATE})
      cat > $TEMPLATE <<EOF
      apiVersion: v1
      kind: Service
      metadata:
        name: kube-dns
        namespace: kube-system
        labels:
          k8s-app: coredns
          kubernetes.io/cluster-service: "true"
          addonmanager.kubernetes.io/mode: Reconcile
          kubernetes.io/name: "CoreDNS"
      spec:
        selector:
          k8s-app: coredns
        clusterIP: ${DNS_SERVICE_IP}
        ports:
        - name: dns
          port: 53
          protocol: UDP
        - name: dns-tcp
          port: 53
          protocol: TCP
        - name: metrics
          port: 9153
          protocol: TCP
      EOF

      TEMPLATE=/etc/kubernetes/addons/coredns-de.yaml
      mkdir -p $(dirname ${TEMPLATE})
      cat > $TEMPLATE <<EOF
      apiVersion: extensions/v1beta1
      kind: Deployment
      metadata:
        name: coredns
        namespace: kube-system
        labels:
          k8s-app: coredns
          kubernetes.io/cluster-service: "true"
          kubernetes.io/name: "CoreDNS"
      spec:
        replicas: 1
        selector:
          matchLabels:
            k8s-app: coredns
        template:
          metadata:
            labels:
              k8s-app: coredns
            annotations:
              scheduler.alpha.kubernetes.io/critical-pod: ''
              scheduler.alpha.kubernetes.io/tolerations: '[{"key":"CriticalAddonsOnly", "operator":"Exists"}]'
          spec:
            serviceAccountName: coredns
            containers:
            - name: coredns
              image: coredns/coredns:007
              imagePullPolicy: Always
              args: [ "-conf", "/etc/coredns/Corefile" ]
              volumeMounts:
              - name: config-volume
                mountPath: /etc/coredns
              ports:
              - containerPort: 53
                name: dns
                protocol: UDP
              - containerPort: 53
                name: dns-tcp
                protocol: TCP
              - containerPort: 9153
                name: metrics
                protocol: TCP
              livenessProbe:
                httpGet:
                  path: /health
                  port: 8080
                  scheme: HTTP
                initialDelaySeconds: 60
                timeoutSeconds: 5
                successThreshold: 1
                failureThreshold: 5
            dnsPolicy: Default
            volumes:
              - name: config-volume
                configMap:
                  name: coredns
                  items:
                  - key: Corefile
                    path: Corefile
      EOF

      echo "Waiting for Kubernetes API..."
      until curl --silent "http://127.0.0.1:8080/version"
      do
          sleep 5
      done

      curl --silent -H "Content-Type: application/yaml" -XPOST -d"$(cat /etc/kubernetes/addons/coredns-sa.yaml)" "http://127.0.0.1:8080/api/v1/namespaces/kube-system/serviceaccounts" > /dev/null
      curl --silent -H "Content-Type: application/yaml" -XPOST -d"$(cat /etc/kubernetes/addons/coredns-cm.yaml)" "http://127.0.0.1:8080/api/v1/namespaces/kube-system/configmaps" > /dev/null
      curl --silent -H "Content-Type: application/yaml" -XPOST -d"$(cat /etc/kubernetes/addons/coredns-de.yaml)" "http://127.0.0.1:8080/apis/extensions/v1beta1/namespaces/kube-system/deployments" > /dev/null
      curl --silent -H "Content-Type: application/yaml" -XPOST -d"$(cat /etc/kubernetes/addons/coredns-svc.yaml)" "http://127.0.0.1:8080/api/v1/namespaces/kube-system/services" > /dev/null
