util.sh 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. #!/usr/bin/env bash
  2. # Copyright 2017 The Kubernetes Authors.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. # A library of helper functions for landing kubemark containers on a
  16. # pre-existing Kubernetes master. See test/kubemark/pre-existing/README.md
  17. # for me details on using a pre-existing provider.
  18. KUBE_ROOT=$(dirname "${BASH_SOURCE[0]}")/../..
  19. source "${KUBE_ROOT}/cluster/common.sh"
  20. source "${KUBE_ROOT}/hack/lib/util.sh"
  21. function detect-project() {
  22. if [[ -z "${MASTER_IP:-}" ]]; then
  23. echo "Set 'MASTER_IP' to the instance assigned to be the Kubernetes master" 1>&2
  24. exit 1
  25. fi
  26. if [[ -z "${PROJECT:-}" ]]; then
  27. echo "Set 'PROJECT' to the name of the container project: $CONTAINER_REGISTRY/$PROJECT/kubemark" >&2
  28. exit 1
  29. fi
  30. if [[ -z "${SERVICE_CLUSTER_IP_RANGE:-}" ]]; then
  31. cluster_range=$(echo "${MASTER_IP}" | awk -F '.' '{printf("%d.%d.%d.0", $1, $2, $3)}')
  32. SERVICE_CLUSTER_IP_RANGE="${SERVICE_CLUSTER_IP_RANGE:-$cluster_range/16}"
  33. fi
  34. }
  35. function create-certs {
  36. rm /tmp/kubeconfig
  37. execute-cmd-on-pre-existing-master-with-retries "sudo cat /etc/kubernetes/admin.conf" > /tmp/kubeconfig
  38. CA_CERT_BASE64=$(cat /tmp/kubeconfig | grep certificate-authority | awk '{print $2}' | head -n 1)
  39. KUBELET_CERT_BASE64=$(cat /tmp/kubeconfig | grep client-certificate-data | awk '{print $2}' | head -n 1)
  40. KUBELET_KEY_BASE64=$(cat /tmp/kubeconfig | grep client-key-data | awk '{print $2}' | head -n 1)
  41. # Local kubeconfig.kubemark vars
  42. KUBECFG_CERT_BASE64="${KUBELET_CERT_BASE64}"
  43. KUBECFG_KEY_BASE64="${KUBELET_KEY_BASE64}"
  44. # The pre-existing Kubernetes master already has these setup
  45. # Set these vars but don't use them
  46. CA_KEY_BASE64=$(dd if=/dev/urandom bs=128 count=1 2>/dev/null | base64 | tr -d "=+/" | dd bs=32 count=1 2>/dev/null)
  47. MASTER_CERT_BASE64=$(dd if=/dev/urandom bs=128 count=1 2>/dev/null | base64 | tr -d "=+/" | dd bs=32 count=1 2>/dev/null)
  48. MASTER_KEY_BASE64=$(dd if=/dev/urandom bs=128 count=1 2>/dev/null | base64 | tr -d "=+/" | dd bs=32 count=1 2>/dev/null)
  49. KUBEAPISERVER_CERT_BASE64=$(dd if=/dev/urandom bs=128 count=1 2>/dev/null | base64 | tr -d "=+/" | dd bs=32 count=1 2>/dev/null)
  50. KUBEAPISERVER_KEY_BASE64=$(dd if=/dev/urandom bs=128 count=1 2>/dev/null | base64 | tr -d "=+/" | dd bs=32 count=1 2>/dev/null)
  51. }