<commit_before>#cloud-config

coreos:
  update:
    reboot-strategy: "off"

  flannel:
    interface: $private_ipv4

  units:
  - name: etcd2.service
    command: start

  - name: bootstrap.service
    command: start
    content: |
      [Service]
      ExecStart=/bin/bash -e /tmp/bootstrap.sh
      Type=oneshot

      # Specify the version of Kubernetes assets to deploy.
      # If left blank, the same version as the host's kubelet will be used.
      Environment=K8S_VER=v1.0.3

write_files:
- path: /tmp/bootstrap.sh
  content: |
    /bin/bash /tmp/controller.sh init
    /bin/bash /tmp/controller.sh start

- path: /tmp/controller.sh
  content: |
    #!/bin/bash -e

    function usage {
        echo "USAGE: $0 <command>"
        echo "Commands:"
        echo -e "\tinit \tInitialize control node services"
        echo -e "\tstart \tStart control node services"
    }

    if [ -z $1 ]; then
        usage
        exit 1
    fi

    CMD=$1

    # Sanity check kubelet is available (missing will exit with set -e)
    which kubelet

    KUBELET_VER=$(kubelet --version | cut -d ' ' -f 2 | sed 's/\(v[0-9]\.[0-9]\.[0-9]\).*$/\1/')

    if [ -z "$K8S_VER" ]; then
        K8S_VER=$KUBELET_VER
    fi

    if [ "$K8S_VER" != "$KUBELET_VER" ]; then
        echo "WARNING: Kubernetes version mismatch. KUBELET_VER=$KUBELET_VER K8S_VER=$K8S_VER"
    fi

    function init_config {
        local REQUIRED=('ADVERTISE_IP' 'POD_NETWORK' 'ETCD_ENDPOINTS' 'SERVICE_IP_RANGE' 'K8S_SERVICE_IP' 'DNS_SERVICE_IP' )

        if [ -z $ADVERTISE_IP ]; then
            export ADVERTISE_IP=$(awk -F= '/COREOS_PUBLIC_IPV4/ {print $2}' /etc/environment)
        fi
        if [ -z $POD_NETWORK ]; then
            export POD_NETWORK="10.2.0.0/16"
        fi
        if [ -z $ETCD_ENDPOINTS ]; then
            export ETCD_ENDPOINTS="http://127.0.0.1:2379"
        fi
        if [ -z $SERVICE_IP_RANGE ]; then
            export SERVICE_IP_RANGE="10.3.0.0/24"
        fi
        if [ -z $K8S_SERVICE_IP ]; then
            export K8S_SERVICE_IP="10.3.0.1"
        fi
        if [ -z $DNS_SERVICE_IP ]; then
            export DNS_SERVICE_IP="10.3.0.10"
        fi
        for REQ in "${REQUIRED[@]}"; do
            if [ -z "$(eval echo \$$REQ)" ]; then
                echo "Missing required config value: ${REQ}"
                exit 1
            fi
        done

        # For use in single-node deployments
        if [ "$REGISTER_NODE" != "true" ]; then
            export REGISTER_NODE="false"
        fi
    }

    function init_flannel {
        echo "Waiting for etcd..."
        while true
        do
            IFS=',' read -ra ES <<< "$ETCD_ENDPOINTS"
            for ETCD in "${ES[@]}"; do
                echo "Trying: $ETCD"
                if [ -n "$(curl --silent "$ETCD/v2/machines")" ]; then
                    local ACTIVE_ETCD=$ETCD
                    break
                fi
                sleep 1
            done
            if [ -n "$ACTIVE_ETCD" ]; then
                break
            fi
        done
        RES=$(curl --silent -X PUT -d "value={\"Network\":\"$POD_NETWORK\"}" "$ACTIVE_ETCD/v2/keys/coreos.com/network/config?prevExist=false")
        if [ -z "$(echo $RES | grep '"action":"create"')" ] && [ -z "$(echo $RES | grep 'Key already exists')" ]; then
            echo "Unexpected error configuring flannel pod network: $RES"
        fi
    }

    function init_docker {
        local TEMPLATE=/etc/systemd/system/docker.service.d/40-flannel.conf
        [ -f $TEMPLATE ] || {
            echo "TEMPLATE: $TEMPLATE"
            mkdir -p $(dirname $TEMPLATE)
            cat << EOF > $TEMPLATE
    [Unit]
    Requires=flanneld.service
    After=flanneld.service
    EOF
        }

        # reload now before docker commands are run in later
        # init steps or dockerd will start before flanneld
        systemctl daemon-reload
    }

    function init_templates {
        local TEMPLATE=/etc/systemd/system/kubelet.service
        [ -f $TEMPLATE ] || {
            echo "TEMPLATE: $TEMPLATE"
            mkdir -p $(dirname $TEMPLATE)
            cat << EOF > $TEMPLATE
    [Service]
    ExecStartPre=/usr/bin/mkdir -p /etc/kubernetes/manifests
    ExecStart=/usr/bin/kubelet \
      --api_servers=http://127.0.0.1:8080 \
      --register-node=${REGISTER_NODE} \
      --allow-privileged=true \
      --config=/etc/kubernetes/manifests \
      --hostname-override=${ADVERTISE_IP} \
      --cluster_dns=${DNS_SERVICE_IP} \
      --cluster_domain=cluster.local \
      --cadvisor-port=0
    Restart=always
    RestartSec=10

    [Install]
    WantedBy=multi-user.target
    EOF<commit_msg>k8s: always register with single-node deployments<commit_after>#cloud-config

coreos:
  update:
    reboot-strategy: "off"

  flannel:
    interface: $private_ipv4

  units:
  - name: etcd2.service
    command: start

  - name: bootstrap.service
    command: start
    content: |
      [Service]
      ExecStart=/bin/bash -e /tmp/bootstrap.sh
      Type=oneshot

      # Specify the version of Kubernetes assets to deploy.
      # If left blank, the same version as the host's kubelet will be used.
      Environment=K8S_VER=v1.0.3

write_files:
- path: /tmp/bootstrap.sh
  content: |
    /bin/bash /tmp/controller.sh init
    /bin/bash /tmp/controller.sh start

- path: /tmp/controller.sh
  content: |
    #!/bin/bash -e

    function usage {
        echo "USAGE: $0 <command>"
        echo "Commands:"
        echo -e "\tinit \tInitialize control node services"
        echo -e "\tstart \tStart control node services"
    }

    if [ -z $1 ]; then
        usage
        exit 1
    fi

    CMD=$1

    # Sanity check kubelet is available (missing will exit with set -e)
    which kubelet

    KUBELET_VER=$(kubelet --version | cut -d ' ' -f 2 | sed 's/\(v[0-9]\.[0-9]\.[0-9]\).*$/\1/')

    if [ -z "$K8S_VER" ]; then
        K8S_VER=$KUBELET_VER
    fi

    if [ "$K8S_VER" != "$KUBELET_VER" ]; then
        echo "WARNING: Kubernetes version mismatch. KUBELET_VER=$KUBELET_VER K8S_VER=$K8S_VER"
    fi

    function init_config {
        local REQUIRED=('ADVERTISE_IP' 'POD_NETWORK' 'ETCD_ENDPOINTS' 'SERVICE_IP_RANGE' 'K8S_SERVICE_IP' 'DNS_SERVICE_IP' )

        if [ -z $ADVERTISE_IP ]; then
            export ADVERTISE_IP=$(awk -F= '/COREOS_PUBLIC_IPV4/ {print $2}' /etc/environment)
        fi
        if [ -z $POD_NETWORK ]; then
            export POD_NETWORK="10.2.0.0/16"
        fi
        if [ -z $ETCD_ENDPOINTS ]; then
            export ETCD_ENDPOINTS="http://127.0.0.1:2379"
        fi
        if [ -z $SERVICE_IP_RANGE ]; then
            export SERVICE_IP_RANGE="10.3.0.0/24"
        fi
        if [ -z $K8S_SERVICE_IP ]; then
            export K8S_SERVICE_IP="10.3.0.1"
        fi
        if [ -z $DNS_SERVICE_IP ]; then
            export DNS_SERVICE_IP="10.3.0.10"
        fi
        for REQ in "${REQUIRED[@]}"; do
            if [ -z "$(eval echo \$$REQ)" ]; then
                echo "Missing required config value: ${REQ}"
                exit 1
            fi
        done
    }

    function init_flannel {
        echo "Waiting for etcd..."
        while true
        do
            IFS=',' read -ra ES <<< "$ETCD_ENDPOINTS"
            for ETCD in "${ES[@]}"; do
                echo "Trying: $ETCD"
                if [ -n "$(curl --silent "$ETCD/v2/machines")" ]; then
                    local ACTIVE_ETCD=$ETCD
                    break
                fi
                sleep 1
            done
            if [ -n "$ACTIVE_ETCD" ]; then
                break
            fi
        done
        RES=$(curl --silent -X PUT -d "value={\"Network\":\"$POD_NETWORK\"}" "$ACTIVE_ETCD/v2/keys/coreos.com/network/config?prevExist=false")
        if [ -z "$(echo $RES | grep '"action":"create"')" ] && [ -z "$(echo $RES | grep 'Key already exists')" ]; then
            echo "Unexpected error configuring flannel pod network: $RES"
        fi
    }

    function init_docker {
        local TEMPLATE=/etc/systemd/system/docker.service.d/40-flannel.conf
        [ -f $TEMPLATE ] || {
            echo "TEMPLATE: $TEMPLATE"
            mkdir -p $(dirname $TEMPLATE)
            cat << EOF > $TEMPLATE
    [Unit]
    Requires=flanneld.service
    After=flanneld.service
    EOF
        }

        # reload now before docker commands are run in later
        # init steps or dockerd will start before flanneld
        systemctl daemon-reload
    }

    function init_templates {
        local TEMPLATE=/etc/systemd/system/kubelet.service
        [ -f $TEMPLATE ] || {
            echo "TEMPLATE: $TEMPLATE"
            mkdir -p $(dirname $TEMPLATE)
            cat << EOF > $TEMPLATE
    [Service]
    ExecStartPre=/usr/bin/mkdir -p /etc/kubernetes/manifests
    ExecStart=/usr/bin/kubelet \
      --api_servers=http://127.0.0.1:8080 \
      --register-node=true \
      --allow-privileged=true \
      --config=/etc/kubernetes/manifests \
      --hostname-override=${ADVERTISE_IP} \
      --cluster_dns=${DNS_SERVICE_IP} \
      --cluster_domain=cluster.local \
      --cadvisor-port=0
    Restart=always
    RestartSec=10

    [Install]
    WantedBy=multi-user.target
    EOF
{'fraction_non_alphanumeric': 0.1264518956826649, 'fraction_numerical': 0.018080210387902695, 'mean_word_length': 1.8074438634266379, 'pattern_counts': {'":': 4, '<': 19, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 12, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4912851', 'n_tokens_mistral': 3221, 'n_tokens_neox': 3067, 'n_words': 720}