#!/bin/bash # Copyright 2018 by the contributors # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. set -o verbose set -o errexit set -o nounset set -o pipefail # Sanity check: This is a mustache template, so make the script die if any of # these aren't set. test -n "{{LoadBalancerDns}}" test -n "{{LoadBalancerName}}" test -n "{{ClusterToken}}" test -n "{{NetworkingProvider}}" test -n "/tmp/NetworkingProvider.yaml" test -n "/tmp/dashboard.yaml" test -n "/tmp/default.storageclass.yaml" test -n "/tmp/network-policy.yaml" test -n "{{Region}}" test -n "{{ClusterInfoBucket}}" test -n "{{ClusterDNSProvider}}" INSTANCE_ID=$(ec2metadata --instance-id) # Add this machine (master) to the load balancer for external access aws elb register-instances-with-load-balancer \ --load-balancer-name {{LoadBalancerName}} \ --instances ${INSTANCE_ID} \ --region {{Region}} # kubeadm wants lowercase for DNS (as it probably should) LB_DNS=$(echo "{{LoadBalancerDns}}" | tr 'A-Z' 'a-z') # get load balancer ip address to be advertised LB_IPV4=$(dig +short {{LoadBalancerDns}}) HOSTNAME="$(hostname -f 2>/dev/null || curl http://169.254.169.254/latest/meta-data/local-hostname)" # reset kubeadm (workaround for kubelet package presence) kubeadm reset --force cat >/tmp/kubeadm.yaml <> /tmp/kubeadm.yaml if [[ "{{ClusterDNSProvider}}" == "CoreDNS" ]]; then echo " type: CoreDNS" >> /tmp/kubeadm.yaml else echo " type: kube-dns" >> /tmp/kubeadm.yaml fi if [[ "{{NetworkingProvider}}" == "calico" ]]; then cat >>/tmp/kubeadm.yaml <$KUBECONFIG_OUTPUT sed -i "s~https://${LB_IPV4}:6443~https://${LB_DNS}:443~" $KUBECONFIG_OUTPUT chown ubuntu:ubuntu $KUBECONFIG_OUTPUT chmod 0600 $KUBECONFIG_OUTPUT # And for local debugging, set up ~/.kube/config for the main user account on # the master. mkdir -p /home/ubuntu/.kube cp /etc/kubernetes/admin.conf /home/ubuntu/.kube/config chown -R ubuntu:ubuntu /home/ubuntu/.kube