apiVersion: apps/v1
kind: DaemonSet
metadata:
  name: registry-aliases-hosts-update
  namespace: kube-system
spec:
  selector:
    matchLabels:
      app: registry-aliases-hosts-update
  template:
    metadata:
      labels:
        app: registry-aliases-hosts-update
    spec:
      initContainers:
        - name: update
          image: registry.fedoraproject.org/fedora
          volumeMounts:
            - name: etchosts
              mountPath: /host-etc/hosts
              readOnly: false
          env:
            - name: REGISTRY_ALIASES
              valueFrom:
                configMapKeyRef:
                  name: registry-aliases
                  key: registryAliases
          command:
            - bash
            - -ce
            - |
              NL=$'\n'
              TAB=$'\t'
              HOSTS="$(cat /host-etc/hosts)"
              [ -z "$REGISTRY_SERVICE_HOST" ] && echo "Failed to get hosts entry for default registry" && exit 1;
              for H in $REGISTRY_ALIASES; do                
                echo "$HOSTS" | grep "$H"  || HOSTS="$HOSTS$NL$REGISTRY_SERVICE_HOST$TAB$H";
              done;
              echo "$HOSTS" | diff -u /host-etc/hosts - || echo "$HOSTS" > /host-etc/hosts
              echo "Done."
      containers:
        - name: pause-for-update
          image: gcr.io/google_containers/pause-amd64:3.1@sha256:59eec8837a4d942cc19a52b8c09ea75121acc38114a2c68b98983ce9356b8610
      terminationGracePeriodSeconds: 30
      volumes:
        - name: etchosts
          hostPath:
            path: /etc/hosts
