create-dynamic-salt-files.sh 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. #!/bin/bash
  2. # Copyright 2014 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. #generate token files
  16. KUBELET_TOKEN=$(dd if=/dev/urandom bs=128 count=1 2>/dev/null | base64 | tr -d "=+/" | dd bs=32 count=1 2>/dev/null)
  17. KUBE_PROXY_TOKEN=$(dd if=/dev/urandom bs=128 count=1 2>/dev/null | base64 | tr -d "=+/" | dd bs=32 count=1 2>/dev/null)
  18. known_tokens_file="/srv/salt-overlay/salt/kube-apiserver/known_tokens.csv"
  19. if [[ ! -f "${known_tokens_file}" ]]; then
  20. mkdir -p /srv/salt-overlay/salt/kube-apiserver
  21. known_tokens_file="/srv/salt-overlay/salt/kube-apiserver/known_tokens.csv"
  22. (umask u=rw,go= ;
  23. echo "$KUBELET_TOKEN,kubelet,kubelet" > $known_tokens_file;
  24. echo "$KUBE_PROXY_TOKEN,kube_proxy,kube_proxy" >> $known_tokens_file)
  25. mkdir -p /srv/salt-overlay/salt/kubelet
  26. kubelet_auth_file="/srv/salt-overlay/salt/kubelet/kubernetes_auth"
  27. (umask u=rw,go= ; echo "{\"BearerToken\": \"$KUBELET_TOKEN\", \"Insecure\": true }" > $kubelet_auth_file)
  28. kubelet_kubeconfig_file="/srv/salt-overlay/salt/kubelet/kubeconfig"
  29. mkdir -p /srv/salt-overlay/salt/kubelet
  30. (umask 077;
  31. cat > "${kubelet_kubeconfig_file}" << EOF
  32. apiVersion: v1
  33. kind: Config
  34. clusters:
  35. - cluster:
  36. insecure-skip-tls-verify: true
  37. name: local
  38. contexts:
  39. - context:
  40. cluster: local
  41. user: kubelet
  42. name: service-account-context
  43. current-context: service-account-context
  44. users:
  45. - name: kubelet
  46. user:
  47. token: ${KUBELET_TOKEN}
  48. EOF
  49. )
  50. mkdir -p /srv/salt-overlay/salt/kube-proxy
  51. kube_proxy_kubeconfig_file="/srv/salt-overlay/salt/kube-proxy/kubeconfig"
  52. # Make a kubeconfig file with the token.
  53. # TODO(etune): put apiserver certs into secret too, and reference from authfile,
  54. # so that "Insecure" is not needed.
  55. (umask 077;
  56. cat > "${kube_proxy_kubeconfig_file}" << EOF
  57. apiVersion: v1
  58. kind: Config
  59. clusters:
  60. - cluster:
  61. insecure-skip-tls-verify: true
  62. name: local
  63. contexts:
  64. - context:
  65. cluster: local
  66. user: kube-proxy
  67. name: service-account-context
  68. current-context: service-account-context
  69. users:
  70. - name: kube-proxy
  71. user:
  72. token: ${KUBE_PROXY_TOKEN}
  73. EOF
  74. )
  75. # Generate tokens for other "service accounts". Append to known_tokens.
  76. #
  77. # NB: If this list ever changes, this script actually has to
  78. # change to detect the existence of this file, kill any deleted
  79. # old tokens and add any new tokens (to handle the upgrade case).
  80. service_accounts=("system:scheduler" "system:controller_manager" "system:logging" "system:monitoring" "system:dns")
  81. for account in "${service_accounts[@]}"; do
  82. token=$(dd if=/dev/urandom bs=128 count=1 2>/dev/null | base64 | tr -d "=+/" | dd bs=32 count=1 2>/dev/null)
  83. echo "${token},${account},${account}" >> "${known_tokens_file}"
  84. done
  85. fi
  86. readonly BASIC_AUTH_FILE="/srv/salt-overlay/salt/kube-apiserver/basic_auth.csv"
  87. if [[ ! -e "${BASIC_AUTH_FILE}" ]]; then
  88. mkdir -p /srv/salt-overlay/salt/kube-apiserver
  89. (umask 077;
  90. echo "${KUBE_PASSWORD},${KUBE_USER},admin" > "${BASIC_AUTH_FILE}")
  91. fi
  92. # Create the overlay files for the salt tree. We create these in a separate
  93. # place so that we can blow away the rest of the salt configs on a kube-push and
  94. # re-apply these.
  95. mkdir -p /srv/salt-overlay/pillar
  96. cat <<EOF >/srv/salt-overlay/pillar/cluster-params.sls
  97. instance_prefix: '$(echo "$INSTANCE_PREFIX" | sed -e "s/'/''/g")'
  98. node_instance_prefix: $NODE_INSTANCE_PREFIX
  99. service_cluster_ip_range: $SERVICE_CLUSTER_IP_RANGE
  100. enable_cluster_monitoring: "${ENABLE_CLUSTER_MONITORING:-none}"
  101. enable_cluster_logging: "${ENABLE_CLUSTER_LOGGING:false}"
  102. enable_cluster_ui: "${ENABLE_CLUSTER_UI:true}"
  103. enable_node_logging: "${ENABLE_NODE_LOGGING:false}"
  104. logging_destination: $LOGGING_DESTINATION
  105. elasticsearch_replicas: $ELASTICSEARCH_LOGGING_REPLICAS
  106. enable_cluster_dns: "${ENABLE_CLUSTER_DNS:-false}"
  107. dns_replicas: ${DNS_REPLICAS:-1}
  108. dns_server: $DNS_SERVER_IP
  109. dns_domain: $DNS_DOMAIN
  110. federations_domain_map: ''
  111. e2e_storage_test_environment: "${E2E_STORAGE_TEST_ENVIRONMENT:-false}"
  112. cluster_cidr: "$NODE_IP_RANGES"
  113. allocate_node_cidrs: "${ALLOCATE_NODE_CIDRS:-true}"
  114. admission_control: NamespaceLifecycle,LimitRanger,SecurityContextDeny,ServiceAccount,DefaultStorageClass,ResourceQuota
  115. EOF