util.sh 130 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585
  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 and constant for the local config.
  16. # Use the config file specified in $KUBE_CONFIG_FILE, or default to
  17. # config-default.sh.
  18. readonly GCE_MAX_LOCAL_SSD=8
  19. KUBE_ROOT=$(dirname "${BASH_SOURCE}")/../..
  20. source "${KUBE_ROOT}/cluster/gce/${KUBE_CONFIG_FILE-"config-default.sh"}"
  21. source "${KUBE_ROOT}/cluster/common.sh"
  22. source "${KUBE_ROOT}/hack/lib/util.sh"
  23. if [[ "${NODE_OS_DISTRIBUTION}" == "gci" || "${NODE_OS_DISTRIBUTION}" == "ubuntu" || "${NODE_OS_DISTRIBUTION}" == "custom" ]]; then
  24. source "${KUBE_ROOT}/cluster/gce/${NODE_OS_DISTRIBUTION}/node-helper.sh"
  25. else
  26. echo "Cannot operate on cluster using node os distro: ${NODE_OS_DISTRIBUTION}" >&2
  27. exit 1
  28. fi
  29. source "${KUBE_ROOT}/cluster/gce/windows/node-helper.sh"
  30. if [[ "${MASTER_OS_DISTRIBUTION}" == "trusty" || "${MASTER_OS_DISTRIBUTION}" == "gci" || "${MASTER_OS_DISTRIBUTION}" == "ubuntu" ]]; then
  31. source "${KUBE_ROOT}/cluster/gce/${MASTER_OS_DISTRIBUTION}/master-helper.sh"
  32. else
  33. echo "Cannot operate on cluster using master os distro: ${MASTER_OS_DISTRIBUTION}" >&2
  34. exit 1
  35. fi
  36. if [[ ${NODE_LOCAL_SSDS:-} -ge 1 ]] && [[ ! -z ${NODE_LOCAL_SSDS_EXT:-} ]] ; then
  37. echo -e "${color_red}Local SSD: Only one of NODE_LOCAL_SSDS and NODE_LOCAL_SSDS_EXT can be specified at once${color_norm}" >&2
  38. exit 2
  39. fi
  40. if [[ "${MASTER_OS_DISTRIBUTION}" == "gci" ]]; then
  41. DEFAULT_GCI_PROJECT=google-containers
  42. if [[ "${GCI_VERSION}" == "cos"* ]]; then
  43. DEFAULT_GCI_PROJECT=cos-cloud
  44. fi
  45. MASTER_IMAGE_PROJECT=${KUBE_GCE_MASTER_PROJECT:-${DEFAULT_GCI_PROJECT}}
  46. # If the master image is not set, we use the latest GCI image.
  47. # Otherwise, we respect whatever is set by the user.
  48. MASTER_IMAGE=${KUBE_GCE_MASTER_IMAGE:-${GCI_VERSION}}
  49. fi
  50. # Sets node image based on the specified os distro. Currently this function only
  51. # supports gci and debian.
  52. #
  53. # Requires:
  54. # NODE_OS_DISTRIBUTION
  55. # Sets:
  56. # DEFAULT_GCI_PROJECT
  57. # NODE_IMAGE
  58. # NODE_IMAGE_PROJECT
  59. function set-linux-node-image() {
  60. if [[ "${NODE_OS_DISTRIBUTION}" == "gci" ]]; then
  61. DEFAULT_GCI_PROJECT=google-containers
  62. if [[ "${GCI_VERSION}" == "cos"* ]]; then
  63. DEFAULT_GCI_PROJECT=cos-cloud
  64. fi
  65. # If the node image is not set, we use the latest GCI image.
  66. # Otherwise, we respect whatever is set by the user.
  67. NODE_IMAGE=${KUBE_GCE_NODE_IMAGE:-${GCI_VERSION}}
  68. NODE_IMAGE_PROJECT=${KUBE_GCE_NODE_PROJECT:-${DEFAULT_GCI_PROJECT}}
  69. fi
  70. }
  71. # Requires:
  72. # WINDOWS_NODE_OS_DISTRIBUTION
  73. # Sets:
  74. # WINDOWS_NODE_IMAGE_FAMILY
  75. # WINDOWS_NODE_IMAGE_PROJECT
  76. function set-windows-node-image() {
  77. WINDOWS_NODE_IMAGE_PROJECT="windows-cloud"
  78. if [[ "${WINDOWS_NODE_OS_DISTRIBUTION}" == "win2019" ]]; then
  79. WINDOWS_NODE_IMAGE_FAMILY="windows-2019-core-for-containers"
  80. elif [[ "${WINDOWS_NODE_OS_DISTRIBUTION}" == "win1809" ]]; then
  81. WINDOWS_NODE_IMAGE_FAMILY="windows-1809-core-for-containers"
  82. else
  83. echo "Unknown WINDOWS_NODE_OS_DISTRIBUTION ${WINDOWS_NODE_OS_DISTRIBUTION}" >&2
  84. exit 1
  85. fi
  86. }
  87. set-linux-node-image
  88. set-windows-node-image
  89. # Verify cluster autoscaler configuration.
  90. if [[ "${ENABLE_CLUSTER_AUTOSCALER}" == "true" ]]; then
  91. if [[ -z $AUTOSCALER_MIN_NODES ]]; then
  92. echo "AUTOSCALER_MIN_NODES not set."
  93. exit 1
  94. fi
  95. if [[ -z $AUTOSCALER_MAX_NODES ]]; then
  96. echo "AUTOSCALER_MAX_NODES not set."
  97. exit 1
  98. fi
  99. fi
  100. # These prefixes must not be prefixes of each other, so that they can be used to
  101. # detect mutually exclusive sets of nodes.
  102. NODE_INSTANCE_PREFIX=${NODE_INSTANCE_PREFIX:-"${INSTANCE_PREFIX}-minion"}
  103. WINDOWS_NODE_INSTANCE_PREFIX=${WINDOWS_NODE_INSTANCE_PREFIX:-"${INSTANCE_PREFIX}-windows-node"}
  104. NODE_TAGS="${NODE_TAG}"
  105. ALLOCATE_NODE_CIDRS=true
  106. PREEXISTING_NETWORK=false
  107. PREEXISTING_NETWORK_MODE=""
  108. KUBE_PROMPT_FOR_UPDATE=${KUBE_PROMPT_FOR_UPDATE:-"n"}
  109. # How long (in seconds) to wait for cluster initialization.
  110. KUBE_CLUSTER_INITIALIZATION_TIMEOUT=${KUBE_CLUSTER_INITIALIZATION_TIMEOUT:-300}
  111. function join_csv() {
  112. local IFS=','; echo "$*";
  113. }
  114. # This function returns the first string before the comma
  115. function split_csv() {
  116. echo "$*" | cut -d',' -f1
  117. }
  118. # Verify prereqs
  119. function verify-prereqs() {
  120. local cmd
  121. # we use openssl to generate certs
  122. kube::util::test_openssl_installed
  123. # ensure a version supported by easyrsa is installed
  124. if [ "$(openssl version | cut -d\ -f1)" == "LibreSSL" ]; then
  125. echo "LibreSSL is not supported. Please ensure openssl points to an OpenSSL binary"
  126. if [ "$(uname -s)" == "Darwin" ]; then
  127. echo 'On macOS we recommend using homebrew and adding "$(brew --prefix openssl)/bin" to your PATH'
  128. fi
  129. exit 1
  130. fi
  131. # we use gcloud to create the cluster, gsutil to stage binaries and data
  132. for cmd in gcloud gsutil; do
  133. if ! which "${cmd}" >/dev/null; then
  134. local resp="n"
  135. if [[ "${KUBE_PROMPT_FOR_UPDATE}" == "y" ]]; then
  136. echo "Can't find ${cmd} in PATH. Do you wish to install the Google Cloud SDK? [Y/n]"
  137. read resp
  138. fi
  139. if [[ "${resp}" != "n" && "${resp}" != "N" ]]; then
  140. curl https://sdk.cloud.google.com | bash
  141. fi
  142. if ! which "${cmd}" >/dev/null; then
  143. echo "Can't find ${cmd} in PATH, please fix and retry. The Google Cloud " >&2
  144. echo "SDK can be downloaded from https://cloud.google.com/sdk/." >&2
  145. exit 1
  146. fi
  147. fi
  148. done
  149. update-or-verify-gcloud
  150. }
  151. # Use the gcloud defaults to find the project. If it is already set in the
  152. # environment then go with that.
  153. #
  154. # Vars set:
  155. # PROJECT
  156. # NETWORK_PROJECT
  157. # PROJECT_REPORTED
  158. function detect-project() {
  159. if [[ -z "${PROJECT-}" ]]; then
  160. PROJECT=$(gcloud config list project --format 'value(core.project)')
  161. fi
  162. NETWORK_PROJECT=${NETWORK_PROJECT:-${PROJECT}}
  163. if [[ -z "${PROJECT-}" ]]; then
  164. echo "Could not detect Google Cloud Platform project. Set the default project using " >&2
  165. echo "'gcloud config set project <PROJECT>'" >&2
  166. exit 1
  167. fi
  168. if [[ -z "${PROJECT_REPORTED-}" ]]; then
  169. echo "Project: ${PROJECT}" >&2
  170. echo "Network Project: ${NETWORK_PROJECT}" >&2
  171. echo "Zone: ${ZONE}" >&2
  172. PROJECT_REPORTED=true
  173. fi
  174. }
  175. # Use gsutil to get the md5 hash for a particular tar
  176. function gsutil_get_tar_md5() {
  177. # location_tar could be local or in the cloud
  178. # local tar_location example ./_output/release-tars/kubernetes-server-linux-amd64.tar.gz
  179. # cloud tar_location example gs://kubernetes-staging-PROJECT/kubernetes-devel/kubernetes-server-linux-amd64.tar.gz
  180. local -r tar_location=$1
  181. #parse the output and return the md5 hash
  182. #the sed command at the end removes whitespace
  183. local -r tar_md5=$(gsutil hash -h -m ${tar_location} 2>/dev/null | grep "Hash (md5):" | awk -F ':' '{print $2}' | sed 's/^[[:space:]]*//g')
  184. echo "${tar_md5}"
  185. }
  186. # Copy a release tar and its accompanying hash.
  187. function copy-to-staging() {
  188. local -r staging_path=$1
  189. local -r gs_url=$2
  190. local -r tar=$3
  191. local -r hash=$4
  192. local -r basename_tar=$(basename ${tar})
  193. #check whether this tar alread exists and has the same hash
  194. #if it matches, then don't bother uploading it again
  195. #remote_tar_md5 checks the remote location for the existing tarball and its md5
  196. #staging_path example gs://kubernetes-staging-PROJECT/kubernetes-devel
  197. #basename_tar example kubernetes-server-linux-amd64.tar.gz
  198. local -r remote_tar_md5=$(gsutil_get_tar_md5 "${staging_path}/${basename_tar}")
  199. if [[ -n ${remote_tar_md5} ]]; then
  200. #local_tar_md5 checks the remote location for the existing tarball and its md5 hash
  201. #tar example ./_output/release-tars/kubernetes-server-linux-amd64.tar.gz
  202. local -r local_tar_md5=$(gsutil_get_tar_md5 "${tar}")
  203. if [[ "${remote_tar_md5}" == "${local_tar_md5}" ]]; then
  204. echo "+++ ${basename_tar} uploaded earlier, cloud and local file md5 match (md5 = ${local_tar_md5})"
  205. return 0
  206. fi
  207. fi
  208. echo "${hash}" > "${tar}.sha1"
  209. gsutil -m -q -h "Cache-Control:private, max-age=0" cp "${tar}" "${tar}.sha1" "${staging_path}"
  210. gsutil -m acl ch -g all:R "${gs_url}" "${gs_url}.sha1" >/dev/null 2>&1
  211. echo "+++ ${basename_tar} uploaded (sha1 = ${hash})"
  212. }
  213. # Given the cluster zone, return the list of regional GCS release
  214. # bucket suffixes for the release in preference order. GCS doesn't
  215. # give us an API for this, so we hardcode it.
  216. #
  217. # Assumed vars:
  218. # RELEASE_REGION_FALLBACK
  219. # REGIONAL_KUBE_ADDONS
  220. # ZONE
  221. # Vars set:
  222. # PREFERRED_REGION
  223. function set-preferred-region() {
  224. case ${ZONE} in
  225. asia-*)
  226. PREFERRED_REGION=("asia" "us" "eu")
  227. ;;
  228. europe-*)
  229. PREFERRED_REGION=("eu" "us" "asia")
  230. ;;
  231. *)
  232. PREFERRED_REGION=("us" "eu" "asia")
  233. ;;
  234. esac
  235. if [[ "${RELEASE_REGION_FALLBACK}" != "true" ]]; then
  236. PREFERRED_REGION=( "${PREFERRED_REGION[0]}" )
  237. fi
  238. }
  239. # Take the local tar files and upload them to Google Storage. They will then be
  240. # downloaded by the master as part of the start up script for the master.
  241. #
  242. # Assumed vars:
  243. # PROJECT
  244. # SERVER_BINARY_TAR
  245. # NODE_BINARY_TAR (optional)
  246. # KUBE_MANIFESTS_TAR
  247. # ZONE
  248. # Vars set:
  249. # SERVER_BINARY_TAR_URL
  250. # SERVER_BINARY_TAR_HASH
  251. # NODE_BINARY_TAR_URL
  252. # NODE_BINARY_TAR_HASH
  253. # KUBE_MANIFESTS_TAR_URL
  254. # KUBE_MANIFESTS_TAR_HASH
  255. function upload-tars() {
  256. SERVER_BINARY_TAR_URL=
  257. SERVER_BINARY_TAR_HASH=
  258. NODE_BINARY_TAR_URL=
  259. NODE_BINARY_TAR_HASH=
  260. KUBE_MANIFESTS_TAR_URL=
  261. KUBE_MANIFESTS_TAR_HASH=
  262. local project_hash
  263. if which md5 > /dev/null 2>&1; then
  264. project_hash=$(md5 -q -s "$PROJECT")
  265. else
  266. project_hash=$(echo -n "$PROJECT" | md5sum | awk '{ print $1 }')
  267. fi
  268. # This requires 1 million projects before the probability of collision is 50%
  269. # that's probably good enough for now :P
  270. project_hash=${project_hash:0:10}
  271. set-preferred-region
  272. if [[ "${ENABLE_DOCKER_REGISTRY_CACHE:-}" == "true" ]]; then
  273. DOCKER_REGISTRY_MIRROR_URL="https://mirror.gcr.io"
  274. fi
  275. SERVER_BINARY_TAR_HASH=$(sha1sum-file "${SERVER_BINARY_TAR}")
  276. if [[ -n "${NODE_BINARY_TAR:-}" ]]; then
  277. NODE_BINARY_TAR_HASH=$(sha1sum-file "${NODE_BINARY_TAR}")
  278. fi
  279. if [[ -n "${KUBE_MANIFESTS_TAR:-}" ]]; then
  280. KUBE_MANIFESTS_TAR_HASH=$(sha1sum-file "${KUBE_MANIFESTS_TAR}")
  281. fi
  282. local server_binary_tar_urls=()
  283. local node_binary_tar_urls=()
  284. local kube_manifest_tar_urls=()
  285. for region in "${PREFERRED_REGION[@]}"; do
  286. suffix="-${region}"
  287. if [[ "${suffix}" == "-us" ]]; then
  288. suffix=""
  289. fi
  290. local staging_bucket="gs://kubernetes-staging-${project_hash}${suffix}"
  291. # Ensure the buckets are created
  292. if ! gsutil ls "${staging_bucket}" >/dev/null; then
  293. echo "Creating ${staging_bucket}"
  294. gsutil mb -l "${region}" "${staging_bucket}"
  295. fi
  296. local staging_path="${staging_bucket}/${INSTANCE_PREFIX}-devel"
  297. echo "+++ Staging tars to Google Storage: ${staging_path}"
  298. local server_binary_gs_url="${staging_path}/${SERVER_BINARY_TAR##*/}"
  299. copy-to-staging "${staging_path}" "${server_binary_gs_url}" "${SERVER_BINARY_TAR}" "${SERVER_BINARY_TAR_HASH}"
  300. if [[ -n "${NODE_BINARY_TAR:-}" ]]; then
  301. local node_binary_gs_url="${staging_path}/${NODE_BINARY_TAR##*/}"
  302. copy-to-staging "${staging_path}" "${node_binary_gs_url}" "${NODE_BINARY_TAR}" "${NODE_BINARY_TAR_HASH}"
  303. fi
  304. # Convert from gs:// URL to an https:// URL
  305. server_binary_tar_urls+=("${server_binary_gs_url/gs:\/\//https://storage.googleapis.com/}")
  306. if [[ -n "${NODE_BINARY_TAR:-}" ]]; then
  307. node_binary_tar_urls+=("${node_binary_gs_url/gs:\/\//https://storage.googleapis.com/}")
  308. fi
  309. if [[ -n "${KUBE_MANIFESTS_TAR:-}" ]]; then
  310. local kube_manifests_gs_url="${staging_path}/${KUBE_MANIFESTS_TAR##*/}"
  311. copy-to-staging "${staging_path}" "${kube_manifests_gs_url}" "${KUBE_MANIFESTS_TAR}" "${KUBE_MANIFESTS_TAR_HASH}"
  312. # Convert from gs:// URL to an https:// URL
  313. kube_manifests_tar_urls+=("${kube_manifests_gs_url/gs:\/\//https://storage.googleapis.com/}")
  314. fi
  315. done
  316. SERVER_BINARY_TAR_URL=$(join_csv "${server_binary_tar_urls[@]}")
  317. if [[ -n "${NODE_BINARY_TAR:-}" ]]; then
  318. NODE_BINARY_TAR_URL=$(join_csv "${node_binary_tar_urls[@]}")
  319. fi
  320. if [[ -n "${KUBE_MANIFESTS_TAR:-}" ]]; then
  321. KUBE_MANIFESTS_TAR_URL=$(join_csv "${kube_manifests_tar_urls[@]}")
  322. fi
  323. }
  324. # Detect Linux and Windows nodes created in the instance group.
  325. #
  326. # Assumed vars:
  327. # NODE_INSTANCE_PREFIX
  328. # WINDOWS_NODE_INSTANCE_PREFIX
  329. # Vars set:
  330. # NODE_NAMES
  331. # INSTANCE_GROUPS
  332. # WINDOWS_NODE_NAMES
  333. # WINDOWS_INSTANCE_GROUPS
  334. function detect-node-names() {
  335. detect-project
  336. INSTANCE_GROUPS=()
  337. INSTANCE_GROUPS+=($(gcloud compute instance-groups managed list \
  338. --project "${PROJECT}" \
  339. --filter "name ~ '${NODE_INSTANCE_PREFIX}-.+' AND zone:(${ZONE})" \
  340. --format='value(name)' || true))
  341. WINDOWS_INSTANCE_GROUPS=()
  342. WINDOWS_INSTANCE_GROUPS+=($(gcloud compute instance-groups managed list \
  343. --project "${PROJECT}" \
  344. --filter "name ~ '${WINDOWS_NODE_INSTANCE_PREFIX}-.+' AND zone:(${ZONE})" \
  345. --format='value(name)' || true))
  346. NODE_NAMES=()
  347. if [[ -n "${INSTANCE_GROUPS[@]:-}" ]]; then
  348. for group in "${INSTANCE_GROUPS[@]}"; do
  349. NODE_NAMES+=($(gcloud compute instance-groups managed list-instances \
  350. "${group}" --zone "${ZONE}" --project "${PROJECT}" \
  351. --format='value(instance)'))
  352. done
  353. fi
  354. # Add heapster node name to the list too (if it exists).
  355. if [[ -n "${HEAPSTER_MACHINE_TYPE:-}" ]]; then
  356. NODE_NAMES+=("${NODE_INSTANCE_PREFIX}-heapster")
  357. fi
  358. WINDOWS_NODE_NAMES=()
  359. if [[ -n "${WINDOWS_INSTANCE_GROUPS[@]:-}" ]]; then
  360. for group in "${WINDOWS_INSTANCE_GROUPS[@]}"; do
  361. WINDOWS_NODE_NAMES+=($(gcloud compute instance-groups managed \
  362. list-instances "${group}" --zone "${ZONE}" --project "${PROJECT}" \
  363. --format='value(instance)'))
  364. done
  365. fi
  366. echo "INSTANCE_GROUPS=${INSTANCE_GROUPS[*]:-}" >&2
  367. echo "NODE_NAMES=${NODE_NAMES[*]:-}" >&2
  368. }
  369. # Detect the information about the minions
  370. #
  371. # Assumed vars:
  372. # ZONE
  373. # Vars set:
  374. # NODE_NAMES
  375. # KUBE_NODE_IP_ADDRESSES (array)
  376. function detect-nodes() {
  377. detect-project
  378. detect-node-names
  379. KUBE_NODE_IP_ADDRESSES=()
  380. for (( i=0; i<${#NODE_NAMES[@]}; i++)); do
  381. local node_ip=$(gcloud compute instances describe --project "${PROJECT}" --zone "${ZONE}" \
  382. "${NODE_NAMES[$i]}" --format='value(networkInterfaces[0].accessConfigs[0].natIP)')
  383. if [[ -z "${node_ip-}" ]] ; then
  384. echo "Did not find ${NODE_NAMES[$i]}" >&2
  385. else
  386. echo "Found ${NODE_NAMES[$i]} at ${node_ip}"
  387. KUBE_NODE_IP_ADDRESSES+=("${node_ip}")
  388. fi
  389. done
  390. if [[ -z "${KUBE_NODE_IP_ADDRESSES-}" ]]; then
  391. echo "Could not detect Kubernetes minion nodes. Make sure you've launched a cluster with 'kube-up.sh'" >&2
  392. exit 1
  393. fi
  394. }
  395. # Detect the IP for the master
  396. #
  397. # Assumed vars:
  398. # MASTER_NAME
  399. # ZONE
  400. # REGION
  401. # Vars set:
  402. # KUBE_MASTER
  403. # KUBE_MASTER_IP
  404. function detect-master() {
  405. detect-project
  406. KUBE_MASTER=${MASTER_NAME}
  407. echo "Trying to find master named '${MASTER_NAME}'" >&2
  408. if [[ -z "${KUBE_MASTER_IP-}" ]]; then
  409. local master_address_name="${MASTER_NAME}-ip"
  410. echo "Looking for address '${master_address_name}'" >&2
  411. if ! KUBE_MASTER_IP=$(gcloud compute addresses describe "${master_address_name}" \
  412. --project "${PROJECT}" --region "${REGION}" -q --format='value(address)') || \
  413. [[ -z "${KUBE_MASTER_IP-}" ]]; then
  414. echo "Could not detect Kubernetes master node. Make sure you've launched a cluster with 'kube-up.sh'" >&2
  415. exit 1
  416. fi
  417. fi
  418. echo "Using master: $KUBE_MASTER (external IP: $KUBE_MASTER_IP)" >&2
  419. }
  420. function load-or-gen-kube-bearertoken() {
  421. if [[ ! -z "${KUBE_CONTEXT:-}" ]]; then
  422. get-kubeconfig-bearertoken
  423. fi
  424. if [[ -z "${KUBE_BEARER_TOKEN:-}" ]]; then
  425. gen-kube-bearertoken
  426. fi
  427. }
  428. # Figure out which binary use on the server and assure it is available.
  429. # If KUBE_VERSION is specified use binaries specified by it, otherwise
  430. # use local dev binaries.
  431. #
  432. # Assumed vars:
  433. # KUBE_VERSION
  434. # KUBE_RELEASE_VERSION_REGEX
  435. # KUBE_CI_VERSION_REGEX
  436. # Vars set:
  437. # KUBE_TAR_HASH
  438. # SERVER_BINARY_TAR_URL
  439. # SERVER_BINARY_TAR_HASH
  440. function tars_from_version() {
  441. local sha1sum=""
  442. if which sha1sum >/dev/null 2>&1; then
  443. sha1sum="sha1sum"
  444. else
  445. sha1sum="shasum -a1"
  446. fi
  447. if [[ -z "${KUBE_VERSION-}" ]]; then
  448. find-release-tars
  449. upload-tars
  450. elif [[ ${KUBE_VERSION} =~ ${KUBE_RELEASE_VERSION_REGEX} ]]; then
  451. SERVER_BINARY_TAR_URL="https://storage.googleapis.com/kubernetes-release/release/${KUBE_VERSION}/kubernetes-server-linux-amd64.tar.gz"
  452. # TODO: Clean this up.
  453. KUBE_MANIFESTS_TAR_URL="${SERVER_BINARY_TAR_URL/server-linux-amd64/manifests}"
  454. KUBE_MANIFESTS_TAR_HASH=$(curl ${KUBE_MANIFESTS_TAR_URL} --silent --show-error | ${sha1sum} | awk '{print $1}')
  455. elif [[ ${KUBE_VERSION} =~ ${KUBE_CI_VERSION_REGEX} ]]; then
  456. SERVER_BINARY_TAR_URL="https://storage.googleapis.com/kubernetes-release-dev/ci/${KUBE_VERSION}/kubernetes-server-linux-amd64.tar.gz"
  457. # TODO: Clean this up.
  458. KUBE_MANIFESTS_TAR_URL="${SERVER_BINARY_TAR_URL/server-linux-amd64/manifests}"
  459. KUBE_MANIFESTS_TAR_HASH=$(curl ${KUBE_MANIFESTS_TAR_URL} --silent --show-error | ${sha1sum} | awk '{print $1}')
  460. else
  461. echo "Version doesn't match regexp" >&2
  462. exit 1
  463. fi
  464. if ! SERVER_BINARY_TAR_HASH=$(curl -Ss --fail "${SERVER_BINARY_TAR_URL}.sha1"); then
  465. echo "Failure trying to curl release .sha1"
  466. fi
  467. if ! curl -Ss --head "${SERVER_BINARY_TAR_URL}" >&/dev/null; then
  468. echo "Can't find release at ${SERVER_BINARY_TAR_URL}" >&2
  469. exit 1
  470. fi
  471. }
  472. # Reads kube-env metadata from master
  473. #
  474. # Assumed vars:
  475. # KUBE_MASTER
  476. # PROJECT
  477. # ZONE
  478. function get-master-env() {
  479. # TODO(zmerlynn): Make this more reliable with retries.
  480. gcloud compute --project ${PROJECT} ssh --zone ${ZONE} ${KUBE_MASTER} --command \
  481. "curl --fail --silent -H 'Metadata-Flavor: Google' \
  482. 'http://metadata/computeMetadata/v1/instance/attributes/kube-env'" 2>/dev/null
  483. gcloud compute --project ${PROJECT} ssh --zone ${ZONE} ${KUBE_MASTER} --command \
  484. "curl --fail --silent -H 'Metadata-Flavor: Google' \
  485. 'http://metadata/computeMetadata/v1/instance/attributes/kube-master-certs'" 2>/dev/null
  486. }
  487. # Quote something appropriate for a yaml string.
  488. #
  489. # TODO(zmerlynn): Note that this function doesn't so much "quote" as
  490. # "strip out quotes", and we really should be using a YAML library for
  491. # this, but PyYAML isn't shipped by default, and *rant rant rant ... SIGH*
  492. function yaml-quote {
  493. echo "'$(echo "${@:-}" | sed -e "s/'/''/g")'"
  494. }
  495. # Writes the cluster location into a temporary file.
  496. # Assumed vars
  497. # ZONE
  498. function write-cluster-location {
  499. cat >"${KUBE_TEMP}/cluster-location.txt" << EOF
  500. ${ZONE}
  501. EOF
  502. }
  503. # Writes the cluster name into a temporary file.
  504. # Assumed vars
  505. # CLUSTER_NAME
  506. function write-cluster-name {
  507. cat >"${KUBE_TEMP}/cluster-name.txt" << EOF
  508. ${CLUSTER_NAME}
  509. EOF
  510. }
  511. function write-master-env {
  512. # If the user requested that the master be part of the cluster, set the
  513. # environment variable to program the master kubelet to register itself.
  514. if [[ "${REGISTER_MASTER_KUBELET:-}" == "true" && -z "${KUBELET_APISERVER:-}" ]]; then
  515. KUBELET_APISERVER="${MASTER_NAME}"
  516. fi
  517. if [[ -z "${KUBERNETES_MASTER_NAME:-}" ]]; then
  518. KUBERNETES_MASTER_NAME="${MASTER_NAME}"
  519. fi
  520. construct-linux-kubelet-flags true
  521. build-linux-kube-env true "${KUBE_TEMP}/master-kube-env.yaml"
  522. build-kubelet-config true "linux" "${KUBE_TEMP}/master-kubelet-config.yaml"
  523. build-kube-master-certs "${KUBE_TEMP}/kube-master-certs.yaml"
  524. }
  525. function write-linux-node-env {
  526. if [[ -z "${KUBERNETES_MASTER_NAME:-}" ]]; then
  527. KUBERNETES_MASTER_NAME="${MASTER_NAME}"
  528. fi
  529. construct-linux-kubelet-flags false
  530. build-linux-kube-env false "${KUBE_TEMP}/node-kube-env.yaml"
  531. build-kubelet-config false "linux" "${KUBE_TEMP}/node-kubelet-config.yaml"
  532. }
  533. function write-windows-node-env {
  534. construct-windows-kubelet-flags
  535. construct-windows-kubeproxy-flags
  536. build-windows-kube-env "${KUBE_TEMP}/windows-node-kube-env.yaml"
  537. build-kubelet-config false "windows" "${KUBE_TEMP}/windows-node-kubelet-config.yaml"
  538. }
  539. function build-linux-node-labels {
  540. local master=$1
  541. local node_labels=""
  542. if [[ "${KUBE_PROXY_DAEMONSET:-}" == "true" && "${master}" != "true" ]]; then
  543. # Add kube-proxy daemonset label to node to avoid situation during cluster
  544. # upgrade/downgrade when there are two instances of kube-proxy running on a node.
  545. # TODO(liggitt): drop beta.kubernetes.io/kube-proxy-ds-ready in 1.16
  546. node_labels="node.kubernetes.io/kube-proxy-ds-ready=true,beta.kubernetes.io/kube-proxy-ds-ready=true"
  547. fi
  548. if [[ -n "${NODE_LABELS:-}" ]]; then
  549. node_labels="${node_labels:+${node_labels},}${NODE_LABELS}"
  550. fi
  551. if [[ -n "${NON_MASTER_NODE_LABELS:-}" && "${master}" != "true" ]]; then
  552. node_labels="${node_labels:+${node_labels},}${NON_MASTER_NODE_LABELS}"
  553. fi
  554. echo $node_labels
  555. }
  556. function build-windows-node-labels {
  557. local node_labels=""
  558. if [[ -n "${WINDOWS_NODE_LABELS:-}" ]]; then
  559. node_labels="${node_labels:+${node_labels},}${WINDOWS_NODE_LABELS}"
  560. fi
  561. if [[ -n "${WINDOWS_NON_MASTER_NODE_LABELS:-}" ]]; then
  562. node_labels="${node_labels:+${node_labels},}${WINDOWS_NON_MASTER_NODE_LABELS}"
  563. fi
  564. echo $node_labels
  565. }
  566. # yaml-map-string-stringarray converts the encoded structure to yaml format, and echoes the result
  567. # under the provided name. If the encoded structure is empty, echoes nothing.
  568. # 1: name to be output in yaml
  569. # 2: encoded map-string-string (which may contain duplicate keys - resulting in map-string-stringarray)
  570. # 3: key-value separator (defaults to ':')
  571. # 4: item separator (defaults to ',')
  572. function yaml-map-string-stringarray {
  573. declare -r name="${1}"
  574. declare -r encoded="${2}"
  575. declare -r kv_sep="${3:-:}"
  576. declare -r item_sep="${4:-,}"
  577. declare -a pairs # indexed array
  578. declare -A map # associative array
  579. IFS="${item_sep}" read -ra pairs <<<"${encoded}" # split on item_sep
  580. for pair in "${pairs[@]}"; do
  581. declare key
  582. declare value
  583. IFS="${kv_sep}" read -r key value <<<"${pair}" # split on kv_sep
  584. map[$key]="${map[$key]+${map[$key]}${item_sep}}${value}" # append values from duplicate keys
  585. done
  586. # only output if there is a non-empty map
  587. if [[ ${#map[@]} -gt 0 ]]; then
  588. echo "${name}:"
  589. for k in "${!map[@]}"; do
  590. echo " ${k}:"
  591. declare -a values
  592. IFS="${item_sep}" read -ra values <<<"${map[$k]}"
  593. for val in "${values[@]}"; do
  594. # declare across two lines so errexit can catch failures
  595. declare v
  596. v=$(yaml-quote "${val}")
  597. echo " - ${v}"
  598. done
  599. done
  600. fi
  601. }
  602. # yaml-map-string-string converts the encoded structure to yaml format, and echoes the result
  603. # under the provided name. If the encoded structure is empty, echoes nothing.
  604. # 1: name to be output in yaml
  605. # 2: encoded map-string-string (no duplicate keys)
  606. # 3: bool, whether to yaml-quote the value string in the output (defaults to true)
  607. # 4: key-value separator (defaults to ':')
  608. # 5: item separator (defaults to ',')
  609. function yaml-map-string-string {
  610. declare -r name="${1}"
  611. declare -r encoded="${2}"
  612. declare -r quote_val_string="${3:-true}"
  613. declare -r kv_sep="${4:-:}"
  614. declare -r item_sep="${5:-,}"
  615. declare -a pairs # indexed array
  616. declare -A map # associative array
  617. IFS="${item_sep}" read -ra pairs <<<"${encoded}" # split on item_sep # TODO(mtaufen): try quoting this too
  618. for pair in "${pairs[@]}"; do
  619. declare key
  620. declare value
  621. IFS="${kv_sep}" read -r key value <<<"${pair}" # split on kv_sep
  622. map[$key]="${value}" # add to associative array
  623. done
  624. # only output if there is a non-empty map
  625. if [[ ${#map[@]} -gt 0 ]]; then
  626. echo "${name}:"
  627. for k in "${!map[@]}"; do
  628. if [[ "${quote_val_string}" == "true" ]]; then
  629. # declare across two lines so errexit can catch failures
  630. declare v
  631. v=$(yaml-quote "${map[$k]}")
  632. echo " ${k}: ${v}"
  633. else
  634. echo " ${k}: ${map[$k]}"
  635. fi
  636. done
  637. fi
  638. }
  639. # Returns kubelet flags used on both Linux and Windows nodes.
  640. function construct-common-kubelet-flags {
  641. local flags="${KUBELET_TEST_LOG_LEVEL:-"--v=2"} ${KUBELET_TEST_ARGS:-}"
  642. flags+=" --cloud-provider=gce"
  643. # TODO(mtaufen): ROTATE_CERTIFICATES seems unused; delete it?
  644. if [[ -n "${ROTATE_CERTIFICATES:-}" ]]; then
  645. flags+=" --rotate-certificates=true"
  646. fi
  647. if [[ -n "${MAX_PODS_PER_NODE:-}" ]]; then
  648. flags+=" --max-pods=${MAX_PODS_PER_NODE}"
  649. fi
  650. echo $flags
  651. }
  652. # Sets KUBELET_ARGS with the kubelet flags for Linux nodes.
  653. # $1: if 'true', we're rendering flags for a master, else a node
  654. function construct-linux-kubelet-flags {
  655. local master="$1"
  656. local flags="$(construct-common-kubelet-flags)"
  657. # Keep in sync with CONTAINERIZED_MOUNTER_HOME in configure-helper.sh
  658. flags+=" --experimental-mounter-path=/home/kubernetes/containerized_mounter/mounter"
  659. flags+=" --experimental-check-node-capabilities-before-mount=true"
  660. # Keep in sync with the mkdir command in configure-helper.sh (until the TODO is resolved)
  661. flags+=" --cert-dir=/var/lib/kubelet/pki/"
  662. # Configure the directory that the Kubelet should use to store dynamic config checkpoints
  663. flags+=" --dynamic-config-dir=/var/lib/kubelet/dynamic-config"
  664. if [[ "${master}" == "true" ]]; then
  665. flags+=" ${MASTER_KUBELET_TEST_ARGS:-}"
  666. if [[ "${REGISTER_MASTER_KUBELET:-false}" == "true" ]]; then
  667. #TODO(mikedanese): allow static pods to start before creating a client
  668. #flags+=" --bootstrap-kubeconfig=/var/lib/kubelet/bootstrap-kubeconfig"
  669. #flags+=" --kubeconfig=/var/lib/kubelet/kubeconfig"
  670. flags+=" --register-with-taints=node-role.kubernetes.io/master=:NoSchedule"
  671. flags+=" --kubeconfig=/var/lib/kubelet/bootstrap-kubeconfig"
  672. flags+=" --register-schedulable=false"
  673. fi
  674. else # For nodes
  675. flags+=" ${NODE_KUBELET_TEST_ARGS:-}"
  676. flags+=" --bootstrap-kubeconfig=/var/lib/kubelet/bootstrap-kubeconfig"
  677. flags+=" --kubeconfig=/var/lib/kubelet/kubeconfig"
  678. fi
  679. # Network plugin
  680. if [[ -n "${NETWORK_PROVIDER:-}" || -n "${NETWORK_POLICY_PROVIDER:-}" ]]; then
  681. flags+=" --cni-bin-dir=/home/kubernetes/bin"
  682. if [[ "${NETWORK_POLICY_PROVIDER:-}" == "calico" || "${ENABLE_NETD:-}" == "true" ]]; then
  683. # Calico uses CNI always.
  684. # Note that network policy won't work for master node.
  685. if [[ "${master}" == "true" ]]; then
  686. flags+=" --network-plugin=${NETWORK_PROVIDER}"
  687. else
  688. flags+=" --network-plugin=cni"
  689. fi
  690. else
  691. # Otherwise use the configured value.
  692. flags+=" --network-plugin=${NETWORK_PROVIDER}"
  693. fi
  694. fi
  695. if [[ -n "${NON_MASQUERADE_CIDR:-}" ]]; then
  696. flags+=" --non-masquerade-cidr=${NON_MASQUERADE_CIDR}"
  697. fi
  698. flags+=" --volume-plugin-dir=${VOLUME_PLUGIN_DIR}"
  699. local node_labels="$(build-linux-node-labels ${master})"
  700. if [[ -n "${node_labels:-}" ]]; then
  701. flags+=" --node-labels=${node_labels}"
  702. fi
  703. if [[ -n "${NODE_TAINTS:-}" ]]; then
  704. flags+=" --register-with-taints=${NODE_TAINTS}"
  705. fi
  706. if [[ "${CONTAINER_RUNTIME:-}" != "docker" ]]; then
  707. flags+=" --container-runtime=remote"
  708. fi
  709. if [[ -n "${CONTAINER_RUNTIME_ENDPOINT:-}" ]]; then
  710. flags+=" --container-runtime-endpoint=${CONTAINER_RUNTIME_ENDPOINT}"
  711. fi
  712. KUBELET_ARGS="${flags}"
  713. }
  714. # Sets KUBELET_ARGS with the kubelet flags for Windows nodes.
  715. function construct-windows-kubelet-flags {
  716. local flags="$(construct-common-kubelet-flags)"
  717. # Note: NODE_KUBELET_TEST_ARGS is empty in typical kube-up runs.
  718. flags+=" ${NODE_KUBELET_TEST_ARGS:-}"
  719. local node_labels="$(build-windows-node-labels)"
  720. if [[ -n "${node_labels:-}" ]]; then
  721. flags+=" --node-labels=${node_labels}"
  722. fi
  723. # Concatenate common and windows-only node taints and apply them.
  724. local node_taints="${NODE_TAINTS:-}"
  725. if [[ -n "${node_taints}" && -n "${WINDOWS_NODE_TAINTS:-}" ]]; then
  726. node_taints+=":${WINDOWS_NODE_TAINTS}"
  727. else
  728. node_taints="${WINDOWS_NODE_TAINTS:-}"
  729. fi
  730. if [[ -n "${node_taints}" ]]; then
  731. flags+=" --register-with-taints=${node_taints}"
  732. fi
  733. # Many of these flags were adapted from
  734. # https://github.com/Microsoft/SDN/blob/master/Kubernetes/windows/start-kubelet.ps1.
  735. flags+=" --config=${WINDOWS_KUBELET_CONFIG_FILE}"
  736. # Path to a kubeconfig file that will be used to get client certificate for
  737. # kubelet. If the file specified by --kubeconfig does not exist, the bootstrap
  738. # kubeconfig is used to request a client certificate from the API server. On
  739. # success, a kubeconfig file referencing the generated client certificate and
  740. # key is written to the path specified by --kubeconfig. The client certificate
  741. # and key file will be stored in the directory pointed by --cert-dir.
  742. #
  743. # See also:
  744. # https://kubernetes.io/docs/reference/command-line-tools-reference/kubelet-tls-bootstrapping/
  745. flags+=" --bootstrap-kubeconfig=${WINDOWS_BOOTSTRAP_KUBECONFIG_FILE}"
  746. flags+=" --kubeconfig=${WINDOWS_KUBECONFIG_FILE}"
  747. # The directory where the TLS certs are located.
  748. flags+=" --cert-dir=${WINDOWS_PKI_DIR}"
  749. flags+=" --network-plugin=cni"
  750. flags+=" --cni-bin-dir=${WINDOWS_CNI_DIR}"
  751. flags+=" --cni-conf-dir=${WINDOWS_CNI_CONFIG_DIR}"
  752. flags+=" --pod-manifest-path=${WINDOWS_MANIFESTS_DIR}"
  753. # Windows images are large and we don't have gcr mirrors yet. Allow longer
  754. # pull progress deadline.
  755. flags+=" --image-pull-progress-deadline=5m"
  756. flags+=" --enable-debugging-handlers=true"
  757. # Configure kubelet to run as a windows service.
  758. flags+=" --windows-service=true"
  759. # TODO(mtaufen): Configure logging for kubelet running as a service. I haven't
  760. # been able to figure out how to direct stdout/stderr into log files when
  761. # configuring it to run via sc.exe, so we just manually override logging
  762. # config here.
  763. flags+=" --log-file=${WINDOWS_LOGS_DIR}\kubelet.log"
  764. # klog sets this to true internally, so need to override to false so we
  765. # actually log to the file
  766. flags+=" --logtostderr=false"
  767. # Configure flags with explicit empty string values. We can't escape
  768. # double-quotes, because they still break sc.exe after expansion in the
  769. # binPath parameter, and single-quotes get parsed as characters instead of
  770. # string delimiters.
  771. flags+=" --resolv-conf="
  772. # Both --cgroups-per-qos and --enforce-node-allocatable should be disabled on
  773. # windows; the latter requires the former to be enabled to work.
  774. flags+=" --cgroups-per-qos=false --enforce-node-allocatable="
  775. # Turn off kernel memory cgroup notification.
  776. flags+=" --experimental-kernel-memcg-notification=false"
  777. # TODO(#78628): Re-enable KubeletPodResources when the issue is fixed.
  778. # Force disable KubeletPodResources feature on Windows until #78628 is fixed.
  779. flags+=" --feature-gates=KubeletPodResources=false"
  780. KUBELET_ARGS="${flags}"
  781. }
  782. function construct-windows-kubeproxy-flags {
  783. local flags=""
  784. # Use the same log level as the Kubelet during tests.
  785. flags+=" ${KUBELET_TEST_LOG_LEVEL:-"--v=2"}"
  786. # Windows uses kernelspace proxymode
  787. flags+=" --proxy-mode=kernelspace"
  788. # Configure kube-proxy to run as a windows service.
  789. flags+=" --windows-service=true"
  790. # TODO(mtaufen): Configure logging for kube-proxy running as a service.
  791. # I haven't been able to figure out how to direct stdout/stderr into log
  792. # files when configuring it to run via sc.exe, so we just manually
  793. # override logging config here.
  794. flags+=" --log-file=${WINDOWS_LOGS_DIR}\kube-proxy.log"
  795. # klog sets this to true internally, so need to override to false
  796. # so we actually log to the file
  797. flags+=" --logtostderr=false"
  798. # Configure flags with explicit empty string values. We can't escape
  799. # double-quotes, because they still break sc.exe after expansion in the
  800. # binPath parameter, and single-quotes get parsed as characters instead
  801. # of string delimiters.
  802. flags+=" --resource-container="
  803. KUBEPROXY_ARGS="${flags}"
  804. }
  805. # $1: if 'true', we're rendering config for a master, else a node
  806. function build-kubelet-config {
  807. local master="$1"
  808. local os="$2"
  809. local file="$3"
  810. rm -f "${file}"
  811. {
  812. print-common-kubelet-config
  813. if [[ "${master}" == "true" ]]; then
  814. print-master-kubelet-config
  815. else
  816. print-common-node-kubelet-config
  817. if [[ "${os}" == "linux" ]]; then
  818. print-linux-node-kubelet-config
  819. elif [[ "${os}" == "windows" ]]; then
  820. print-windows-node-kubelet-config
  821. else
  822. echo "Unknown OS ${os}" >&2
  823. exit 1
  824. fi
  825. fi
  826. } > "${file}"
  827. }
  828. # cat the Kubelet config yaml in common between masters, linux nodes, and
  829. # windows nodes
  830. function print-common-kubelet-config {
  831. declare quoted_dns_server_ip
  832. declare quoted_dns_domain
  833. quoted_dns_server_ip=$(yaml-quote "${DNS_SERVER_IP}")
  834. if [[ "${ENABLE_NODELOCAL_DNS:-}" == "true" ]]; then
  835. quoted_dns_server_ip=$(yaml-quote "${LOCAL_DNS_IP}")
  836. fi
  837. quoted_dns_domain=$(yaml-quote "${DNS_DOMAIN}")
  838. cat <<EOF
  839. kind: KubeletConfiguration
  840. apiVersion: kubelet.config.k8s.io/v1beta1
  841. cgroupRoot: /
  842. clusterDNS:
  843. - ${quoted_dns_server_ip}
  844. clusterDomain: ${quoted_dns_domain}
  845. readOnlyPort: 10255
  846. EOF
  847. # Note: ENABLE_MANIFEST_URL is used by GKE.
  848. # TODO(mtaufen): remove this since it's not used in kubernetes/kubernetes nor
  849. # kubernetes/test-infra.
  850. if [[ "${ENABLE_MANIFEST_URL:-}" == "true" ]]; then
  851. declare quoted_manifest_url
  852. quoted_manifest_url=$(yaml-quote "${MANIFEST_URL}")
  853. cat <<EOF
  854. staticPodURL: ${quoted_manifest_url}
  855. EOF
  856. yaml-map-string-stringarray 'staticPodURLHeader' "${MANIFEST_URL_HEADER}"
  857. fi
  858. if [[ -n "${EVICTION_HARD:-}" ]]; then
  859. yaml-map-string-string 'evictionHard' "${EVICTION_HARD}" true '<'
  860. fi
  861. if [[ -n "${FEATURE_GATES:-}" ]]; then
  862. yaml-map-string-string 'featureGates' "${FEATURE_GATES}" false '='
  863. fi
  864. }
  865. # cat the Kubelet config yaml for masters
  866. function print-master-kubelet-config {
  867. cat <<EOF
  868. enableDebuggingHandlers: false
  869. hairpinMode: none
  870. staticPodPath: /etc/kubernetes/manifests
  871. authentication:
  872. webhook:
  873. enabled: false
  874. anonymous:
  875. enabled: true
  876. authorization:
  877. mode: AlwaysAllow
  878. EOF
  879. if [[ "${REGISTER_MASTER_KUBELET:-false}" == "false" ]]; then
  880. # Note: Standalone mode is used by GKE
  881. declare quoted_master_ip_range
  882. quoted_master_ip_range=$(yaml-quote "${MASTER_IP_RANGE}")
  883. cat <<EOF
  884. podCidr: ${quoted_master_ip_range}
  885. EOF
  886. fi
  887. }
  888. # cat the Kubelet config yaml in common between linux nodes and windows nodes
  889. function print-common-node-kubelet-config {
  890. cat <<EOF
  891. enableDebuggingHandlers: true
  892. EOF
  893. if [[ "${HAIRPIN_MODE:-}" == "promiscuous-bridge" ]] || \
  894. [[ "${HAIRPIN_MODE:-}" == "hairpin-veth" ]] || \
  895. [[ "${HAIRPIN_MODE:-}" == "none" ]]; then
  896. declare quoted_hairpin_mode
  897. quoted_hairpin_mode=$(yaml-quote "${HAIRPIN_MODE}")
  898. cat <<EOF
  899. hairpinMode: ${quoted_hairpin_mode}
  900. EOF
  901. fi
  902. }
  903. # cat the Kubelet config yaml for linux nodes
  904. function print-linux-node-kubelet-config {
  905. # Keep authentication.x509.clientCAFile in sync with CA_CERT_BUNDLE_PATH in configure-helper.sh
  906. cat <<EOF
  907. staticPodPath: /etc/kubernetes/manifests
  908. authentication:
  909. x509:
  910. clientCAFile: /etc/srv/kubernetes/pki/ca-certificates.crt
  911. EOF
  912. }
  913. # cat the Kubelet config yaml for windows nodes
  914. function print-windows-node-kubelet-config {
  915. # Notes:
  916. # - We don't run any static pods on Windows nodes yet.
  917. # TODO(mtaufen): Does it make any sense to set eviction thresholds for inodes
  918. # on Windows?
  919. # TODO(pjh, mtaufen): It may make sense to use a different hairpin mode on
  920. # Windows. We're currently using hairpin-veth, but
  921. # https://github.com/Microsoft/SDN/blob/master/Kubernetes/windows/start-kubelet.ps1#L121
  922. # uses promiscuous-bridge.
  923. # TODO(pjh, mtaufen): Does cgroupRoot make sense for Windows?
  924. # Keep authentication.x509.clientCAFile in sync with CA_CERT_BUNDLE_PATH in
  925. # k8s-node-setup.psm1.
  926. cat <<EOF
  927. authentication:
  928. x509:
  929. clientCAFile: '${WINDOWS_PKI_DIR}\ca-certificates.crt'
  930. EOF
  931. }
  932. function build-kube-master-certs {
  933. local file=$1
  934. rm -f ${file}
  935. cat >$file <<EOF
  936. KUBEAPISERVER_CERT: $(yaml-quote ${KUBEAPISERVER_CERT_BASE64:-})
  937. KUBEAPISERVER_KEY: $(yaml-quote ${KUBEAPISERVER_KEY_BASE64:-})
  938. CA_KEY: $(yaml-quote ${CA_KEY_BASE64:-})
  939. AGGREGATOR_CA_KEY: $(yaml-quote ${AGGREGATOR_CA_KEY_BASE64:-})
  940. REQUESTHEADER_CA_CERT: $(yaml-quote ${REQUESTHEADER_CA_CERT_BASE64:-})
  941. PROXY_CLIENT_CERT: $(yaml-quote ${PROXY_CLIENT_CERT_BASE64:-})
  942. PROXY_CLIENT_KEY: $(yaml-quote ${PROXY_CLIENT_KEY_BASE64:-})
  943. ETCD_APISERVER_CA_KEY: $(yaml-quote ${ETCD_APISERVER_CA_KEY_BASE64:-})
  944. ETCD_APISERVER_CA_CERT: $(yaml-quote ${ETCD_APISERVER_CA_CERT_BASE64:-})
  945. ETCD_APISERVER_SERVER_KEY: $(yaml-quote ${ETCD_APISERVER_SERVER_KEY_BASE64:-})
  946. ETCD_APISERVER_SERVER_CERT: $(yaml-quote ${ETCD_APISERVER_SERVER_CERT_BASE64:-})
  947. ETCD_APISERVER_CLIENT_KEY: $(yaml-quote ${ETCD_APISERVER_CLIENT_KEY_BASE64:-})
  948. ETCD_APISERVER_CLIENT_CERT: $(yaml-quote ${ETCD_APISERVER_CLIENT_CERT_BASE64:-})
  949. EOF
  950. }
  951. # $1: if 'true', we're building a master yaml, else a node
  952. function build-linux-kube-env {
  953. local master="$1"
  954. local file="$2"
  955. local server_binary_tar_url=$SERVER_BINARY_TAR_URL
  956. local kube_manifests_tar_url="${KUBE_MANIFESTS_TAR_URL:-}"
  957. if [[ "${master}" == "true" && "${MASTER_OS_DISTRIBUTION}" == "ubuntu" ]] || \
  958. [[ "${master}" == "false" && ("${NODE_OS_DISTRIBUTION}" == "ubuntu" || "${NODE_OS_DISTRIBUTION}" == "custom") ]]; then
  959. # TODO: Support fallback .tar.gz settings on Container Linux
  960. server_binary_tar_url=$(split_csv "${SERVER_BINARY_TAR_URL}")
  961. kube_manifests_tar_url=$(split_csv "${KUBE_MANIFESTS_TAR_URL}")
  962. fi
  963. rm -f ${file}
  964. cat >$file <<EOF
  965. CLUSTER_NAME: $(yaml-quote ${CLUSTER_NAME})
  966. ENV_TIMESTAMP: $(yaml-quote $(date -u +%Y-%m-%dT%T%z))
  967. INSTANCE_PREFIX: $(yaml-quote ${INSTANCE_PREFIX})
  968. NODE_INSTANCE_PREFIX: $(yaml-quote ${NODE_INSTANCE_PREFIX})
  969. NODE_TAGS: $(yaml-quote ${NODE_TAGS:-})
  970. NODE_NETWORK: $(yaml-quote ${NETWORK:-})
  971. NODE_SUBNETWORK: $(yaml-quote ${SUBNETWORK:-})
  972. CLUSTER_IP_RANGE: $(yaml-quote ${CLUSTER_IP_RANGE:-10.244.0.0/16})
  973. SERVER_BINARY_TAR_URL: $(yaml-quote ${server_binary_tar_url})
  974. SERVER_BINARY_TAR_HASH: $(yaml-quote ${SERVER_BINARY_TAR_HASH})
  975. PROJECT_ID: $(yaml-quote ${PROJECT})
  976. NETWORK_PROJECT_ID: $(yaml-quote ${NETWORK_PROJECT})
  977. SERVICE_CLUSTER_IP_RANGE: $(yaml-quote ${SERVICE_CLUSTER_IP_RANGE})
  978. KUBERNETES_MASTER_NAME: $(yaml-quote ${KUBERNETES_MASTER_NAME})
  979. ALLOCATE_NODE_CIDRS: $(yaml-quote ${ALLOCATE_NODE_CIDRS:-false})
  980. ENABLE_CLUSTER_MONITORING: $(yaml-quote ${ENABLE_CLUSTER_MONITORING:-none})
  981. ENABLE_PROMETHEUS_MONITORING: $(yaml-quote ${ENABLE_PROMETHEUS_MONITORING:-false})
  982. ENABLE_METRICS_SERVER: $(yaml-quote ${ENABLE_METRICS_SERVER:-false})
  983. ENABLE_METADATA_AGENT: $(yaml-quote ${ENABLE_METADATA_AGENT:-none})
  984. METADATA_AGENT_CPU_REQUEST: $(yaml-quote ${METADATA_AGENT_CPU_REQUEST:-})
  985. METADATA_AGENT_MEMORY_REQUEST: $(yaml-quote ${METADATA_AGENT_MEMORY_REQUEST:-})
  986. METADATA_AGENT_CLUSTER_LEVEL_CPU_REQUEST: $(yaml-quote ${METADATA_AGENT_CLUSTER_LEVEL_CPU_REQUEST:-})
  987. METADATA_AGENT_CLUSTER_LEVEL_MEMORY_REQUEST: $(yaml-quote ${METADATA_AGENT_CLUSTER_LEVEL_MEMORY_REQUEST:-})
  988. DOCKER_REGISTRY_MIRROR_URL: $(yaml-quote ${DOCKER_REGISTRY_MIRROR_URL:-})
  989. ENABLE_L7_LOADBALANCING: $(yaml-quote ${ENABLE_L7_LOADBALANCING:-none})
  990. ENABLE_CLUSTER_LOGGING: $(yaml-quote ${ENABLE_CLUSTER_LOGGING:-false})
  991. ENABLE_CLUSTER_UI: $(yaml-quote ${ENABLE_CLUSTER_UI:-false})
  992. ENABLE_NODE_PROBLEM_DETECTOR: $(yaml-quote ${ENABLE_NODE_PROBLEM_DETECTOR:-none})
  993. NODE_PROBLEM_DETECTOR_VERSION: $(yaml-quote ${NODE_PROBLEM_DETECTOR_VERSION:-})
  994. NODE_PROBLEM_DETECTOR_TAR_HASH: $(yaml-quote ${NODE_PROBLEM_DETECTOR_TAR_HASH:-})
  995. NODE_PROBLEM_DETECTOR_RELEASE_PATH: $(yaml-quote ${NODE_PROBLEM_DETECTOR_RELEASE_PATH:-})
  996. NODE_PROBLEM_DETECTOR_CUSTOM_FLAGS: $(yaml-quote ${NODE_PROBLEM_DETECTOR_CUSTOM_FLAGS:-})
  997. CNI_VERSION: $(yaml-quote ${CNI_VERSION:-})
  998. CNI_SHA1: $(yaml-quote ${CNI_SHA1:-})
  999. ENABLE_NODE_LOGGING: $(yaml-quote ${ENABLE_NODE_LOGGING:-false})
  1000. LOGGING_DESTINATION: $(yaml-quote ${LOGGING_DESTINATION:-})
  1001. ELASTICSEARCH_LOGGING_REPLICAS: $(yaml-quote ${ELASTICSEARCH_LOGGING_REPLICAS:-})
  1002. ENABLE_CLUSTER_DNS: $(yaml-quote ${ENABLE_CLUSTER_DNS:-false})
  1003. CLUSTER_DNS_CORE_DNS: $(yaml-quote ${CLUSTER_DNS_CORE_DNS:-true})
  1004. ENABLE_NODELOCAL_DNS: $(yaml-quote ${ENABLE_NODELOCAL_DNS:-false})
  1005. DNS_SERVER_IP: $(yaml-quote ${DNS_SERVER_IP:-})
  1006. LOCAL_DNS_IP: $(yaml-quote ${LOCAL_DNS_IP:-})
  1007. DNS_DOMAIN: $(yaml-quote ${DNS_DOMAIN:-})
  1008. DNS_MEMORY_LIMIT: $(yaml-quote ${DNS_MEMORY_LIMIT:-})
  1009. ENABLE_DNS_HORIZONTAL_AUTOSCALER: $(yaml-quote ${ENABLE_DNS_HORIZONTAL_AUTOSCALER:-false})
  1010. KUBE_PROXY_DAEMONSET: $(yaml-quote ${KUBE_PROXY_DAEMONSET:-false})
  1011. KUBE_PROXY_TOKEN: $(yaml-quote ${KUBE_PROXY_TOKEN:-})
  1012. KUBE_PROXY_MODE: $(yaml-quote ${KUBE_PROXY_MODE:-iptables})
  1013. NODE_PROBLEM_DETECTOR_TOKEN: $(yaml-quote ${NODE_PROBLEM_DETECTOR_TOKEN:-})
  1014. ADMISSION_CONTROL: $(yaml-quote ${ADMISSION_CONTROL:-})
  1015. ENABLE_POD_SECURITY_POLICY: $(yaml-quote ${ENABLE_POD_SECURITY_POLICY:-})
  1016. MASTER_IP_RANGE: $(yaml-quote ${MASTER_IP_RANGE})
  1017. RUNTIME_CONFIG: $(yaml-quote ${RUNTIME_CONFIG})
  1018. CA_CERT: $(yaml-quote ${CA_CERT_BASE64:-})
  1019. KUBELET_CERT: $(yaml-quote ${KUBELET_CERT_BASE64:-})
  1020. KUBELET_KEY: $(yaml-quote ${KUBELET_KEY_BASE64:-})
  1021. NETWORK_PROVIDER: $(yaml-quote ${NETWORK_PROVIDER:-})
  1022. NETWORK_POLICY_PROVIDER: $(yaml-quote ${NETWORK_POLICY_PROVIDER:-})
  1023. HAIRPIN_MODE: $(yaml-quote ${HAIRPIN_MODE:-})
  1024. E2E_STORAGE_TEST_ENVIRONMENT: $(yaml-quote ${E2E_STORAGE_TEST_ENVIRONMENT:-})
  1025. KUBE_DOCKER_REGISTRY: $(yaml-quote ${KUBE_DOCKER_REGISTRY:-})
  1026. KUBE_ADDON_REGISTRY: $(yaml-quote ${KUBE_ADDON_REGISTRY:-})
  1027. MULTIZONE: $(yaml-quote ${MULTIZONE:-})
  1028. NON_MASQUERADE_CIDR: $(yaml-quote ${NON_MASQUERADE_CIDR:-})
  1029. ENABLE_DEFAULT_STORAGE_CLASS: $(yaml-quote ${ENABLE_DEFAULT_STORAGE_CLASS:-})
  1030. ENABLE_APISERVER_ADVANCED_AUDIT: $(yaml-quote ${ENABLE_APISERVER_ADVANCED_AUDIT:-})
  1031. ENABLE_APISERVER_DYNAMIC_AUDIT: $(yaml-quote ${ENABLE_APISERVER_DYNAMIC_AUDIT:-})
  1032. ENABLE_CACHE_MUTATION_DETECTOR: $(yaml-quote ${ENABLE_CACHE_MUTATION_DETECTOR:-false})
  1033. ENABLE_PATCH_CONVERSION_DETECTOR: $(yaml-quote ${ENABLE_PATCH_CONVERSION_DETECTOR:-false})
  1034. ADVANCED_AUDIT_POLICY: $(yaml-quote ${ADVANCED_AUDIT_POLICY:-})
  1035. ADVANCED_AUDIT_BACKEND: $(yaml-quote ${ADVANCED_AUDIT_BACKEND:-log})
  1036. ADVANCED_AUDIT_TRUNCATING_BACKEND: $(yaml-quote ${ADVANCED_AUDIT_TRUNCATING_BACKEND:-true})
  1037. ADVANCED_AUDIT_LOG_MODE: $(yaml-quote ${ADVANCED_AUDIT_LOG_MODE:-})
  1038. ADVANCED_AUDIT_LOG_BUFFER_SIZE: $(yaml-quote ${ADVANCED_AUDIT_LOG_BUFFER_SIZE:-})
  1039. ADVANCED_AUDIT_LOG_MAX_BATCH_SIZE: $(yaml-quote ${ADVANCED_AUDIT_LOG_MAX_BATCH_SIZE:-})
  1040. ADVANCED_AUDIT_LOG_MAX_BATCH_WAIT: $(yaml-quote ${ADVANCED_AUDIT_LOG_MAX_BATCH_WAIT:-})
  1041. ADVANCED_AUDIT_LOG_THROTTLE_QPS: $(yaml-quote ${ADVANCED_AUDIT_LOG_THROTTLE_QPS:-})
  1042. ADVANCED_AUDIT_LOG_THROTTLE_BURST: $(yaml-quote ${ADVANCED_AUDIT_LOG_THROTTLE_BURST:-})
  1043. ADVANCED_AUDIT_LOG_INITIAL_BACKOFF: $(yaml-quote ${ADVANCED_AUDIT_LOG_INITIAL_BACKOFF:-})
  1044. ADVANCED_AUDIT_WEBHOOK_MODE: $(yaml-quote ${ADVANCED_AUDIT_WEBHOOK_MODE:-})
  1045. ADVANCED_AUDIT_WEBHOOK_BUFFER_SIZE: $(yaml-quote ${ADVANCED_AUDIT_WEBHOOK_BUFFER_SIZE:-})
  1046. ADVANCED_AUDIT_WEBHOOK_MAX_BATCH_SIZE: $(yaml-quote ${ADVANCED_AUDIT_WEBHOOK_MAX_BATCH_SIZE:-})
  1047. ADVANCED_AUDIT_WEBHOOK_MAX_BATCH_WAIT: $(yaml-quote ${ADVANCED_AUDIT_WEBHOOK_MAX_BATCH_WAIT:-})
  1048. ADVANCED_AUDIT_WEBHOOK_THROTTLE_QPS: $(yaml-quote ${ADVANCED_AUDIT_WEBHOOK_THROTTLE_QPS:-})
  1049. ADVANCED_AUDIT_WEBHOOK_THROTTLE_BURST: $(yaml-quote ${ADVANCED_AUDIT_WEBHOOK_THROTTLE_BURST:-})
  1050. ADVANCED_AUDIT_WEBHOOK_INITIAL_BACKOFF: $(yaml-quote ${ADVANCED_AUDIT_WEBHOOK_INITIAL_BACKOFF:-})
  1051. GCE_API_ENDPOINT: $(yaml-quote ${GCE_API_ENDPOINT:-})
  1052. GCE_GLBC_IMAGE: $(yaml-quote ${GCE_GLBC_IMAGE:-})
  1053. CUSTOM_INGRESS_YAML: |
  1054. $(echo "${CUSTOM_INGRESS_YAML:-}" | sed -e "s/'/''/g")
  1055. ENABLE_NODE_JOURNAL: $(yaml-quote ${ENABLE_NODE_JOURNAL:-false})
  1056. PROMETHEUS_TO_SD_ENDPOINT: $(yaml-quote ${PROMETHEUS_TO_SD_ENDPOINT:-})
  1057. PROMETHEUS_TO_SD_PREFIX: $(yaml-quote ${PROMETHEUS_TO_SD_PREFIX:-})
  1058. ENABLE_PROMETHEUS_TO_SD: $(yaml-quote ${ENABLE_PROMETHEUS_TO_SD:-false})
  1059. DISABLE_PROMETHEUS_TO_SD_IN_DS: $(yaml-quote ${DISABLE_PROMETHEUS_TO_SD_IN_DS:-false})
  1060. ENABLE_POD_PRIORITY: $(yaml-quote ${ENABLE_POD_PRIORITY:-})
  1061. CONTAINER_RUNTIME: $(yaml-quote ${CONTAINER_RUNTIME:-})
  1062. CONTAINER_RUNTIME_ENDPOINT: $(yaml-quote ${CONTAINER_RUNTIME_ENDPOINT:-})
  1063. CONTAINER_RUNTIME_NAME: $(yaml-quote ${CONTAINER_RUNTIME_NAME:-})
  1064. NODE_LOCAL_SSDS_EXT: $(yaml-quote ${NODE_LOCAL_SSDS_EXT:-})
  1065. LOAD_IMAGE_COMMAND: $(yaml-quote ${LOAD_IMAGE_COMMAND:-})
  1066. ZONE: $(yaml-quote ${ZONE})
  1067. REGION: $(yaml-quote ${REGION})
  1068. VOLUME_PLUGIN_DIR: $(yaml-quote ${VOLUME_PLUGIN_DIR})
  1069. KUBELET_ARGS: $(yaml-quote ${KUBELET_ARGS})
  1070. REQUIRE_METADATA_KUBELET_CONFIG_FILE: $(yaml-quote true)
  1071. ENABLE_NETD: $(yaml-quote ${ENABLE_NETD:-false})
  1072. ENABLE_NODE_TERMINATION_HANDLER: $(yaml-quote ${ENABLE_NODE_TERMINATION_HANDLER:-false})
  1073. CUSTOM_NETD_YAML: |
  1074. $(echo "${CUSTOM_NETD_YAML:-}" | sed -e "s/'/''/g")
  1075. CUSTOM_CALICO_NODE_DAEMONSET_YAML: |
  1076. $(echo "${CUSTOM_CALICO_NODE_DAEMONSET_YAML:-}" | sed -e "s/'/''/g")
  1077. CUSTOM_TYPHA_DEPLOYMENT_YAML: |
  1078. $(echo "${CUSTOM_TYPHA_DEPLOYMENT_YAML:-}" | sed -e "s/'/''/g")
  1079. EOF
  1080. if [[ "${master}" == "true" && "${MASTER_OS_DISTRIBUTION}" == "gci" ]] || \
  1081. [[ "${master}" == "false" && "${NODE_OS_DISTRIBUTION}" == "gci" ]] || \
  1082. [[ "${master}" == "true" && "${MASTER_OS_DISTRIBUTION}" == "cos" ]] || \
  1083. [[ "${master}" == "false" && "${NODE_OS_DISTRIBUTION}" == "cos" ]]; then
  1084. cat >>$file <<EOF
  1085. REMOUNT_VOLUME_PLUGIN_DIR: $(yaml-quote ${REMOUNT_VOLUME_PLUGIN_DIR:-true})
  1086. EOF
  1087. fi
  1088. if [ -n "${KUBE_APISERVER_REQUEST_TIMEOUT:-}" ]; then
  1089. cat >>$file <<EOF
  1090. KUBE_APISERVER_REQUEST_TIMEOUT: $(yaml-quote ${KUBE_APISERVER_REQUEST_TIMEOUT})
  1091. EOF
  1092. fi
  1093. if [ -n "${TERMINATED_POD_GC_THRESHOLD:-}" ]; then
  1094. cat >>$file <<EOF
  1095. TERMINATED_POD_GC_THRESHOLD: $(yaml-quote ${TERMINATED_POD_GC_THRESHOLD})
  1096. EOF
  1097. fi
  1098. if [[ "${master}" == "true" && ("${MASTER_OS_DISTRIBUTION}" == "trusty" || "${MASTER_OS_DISTRIBUTION}" == "gci" || "${MASTER_OS_DISTRIBUTION}" == "ubuntu") ]] || \
  1099. [[ "${master}" == "false" && ("${NODE_OS_DISTRIBUTION}" == "trusty" || "${NODE_OS_DISTRIBUTION}" == "gci" || "${NODE_OS_DISTRIBUTION}" = "ubuntu" || "${NODE_OS_DISTRIBUTION}" = "custom") ]] ; then
  1100. cat >>$file <<EOF
  1101. KUBE_MANIFESTS_TAR_URL: $(yaml-quote ${kube_manifests_tar_url})
  1102. KUBE_MANIFESTS_TAR_HASH: $(yaml-quote ${KUBE_MANIFESTS_TAR_HASH})
  1103. EOF
  1104. fi
  1105. if [ -n "${TEST_CLUSTER:-}" ]; then
  1106. cat >>$file <<EOF
  1107. TEST_CLUSTER: $(yaml-quote ${TEST_CLUSTER})
  1108. EOF
  1109. fi
  1110. if [ -n "${DOCKER_TEST_LOG_LEVEL:-}" ]; then
  1111. cat >>$file <<EOF
  1112. DOCKER_TEST_LOG_LEVEL: $(yaml-quote ${DOCKER_TEST_LOG_LEVEL})
  1113. EOF
  1114. fi
  1115. if [ -n "${DOCKER_LOG_DRIVER:-}" ]; then
  1116. cat >>$file <<EOF
  1117. DOCKER_LOG_DRIVER: $(yaml-quote ${DOCKER_LOG_DRIVER})
  1118. EOF
  1119. fi
  1120. if [ -n "${DOCKER_LOG_MAX_SIZE:-}" ]; then
  1121. cat >>$file <<EOF
  1122. DOCKER_LOG_MAX_SIZE: $(yaml-quote ${DOCKER_LOG_MAX_SIZE})
  1123. EOF
  1124. fi
  1125. if [ -n "${DOCKER_LOG_MAX_FILE:-}" ]; then
  1126. cat >>$file <<EOF
  1127. DOCKER_LOG_MAX_FILE: $(yaml-quote ${DOCKER_LOG_MAX_FILE})
  1128. EOF
  1129. fi
  1130. if [ -n "${FEATURE_GATES:-}" ]; then
  1131. cat >>$file <<EOF
  1132. FEATURE_GATES: $(yaml-quote ${FEATURE_GATES})
  1133. EOF
  1134. fi
  1135. if [ -n "${PROVIDER_VARS:-}" ]; then
  1136. local var_name
  1137. local var_value
  1138. for var_name in ${PROVIDER_VARS}; do
  1139. eval "local var_value=\$(yaml-quote \${${var_name}})"
  1140. cat >>$file <<EOF
  1141. ${var_name}: ${var_value}
  1142. EOF
  1143. done
  1144. fi
  1145. if [[ "${master}" == "true" ]]; then
  1146. # Master-only env vars.
  1147. cat >>$file <<EOF
  1148. KUBERNETES_MASTER: $(yaml-quote "true")
  1149. KUBE_USER: $(yaml-quote ${KUBE_USER})
  1150. KUBE_PASSWORD: $(yaml-quote ${KUBE_PASSWORD})
  1151. KUBE_BEARER_TOKEN: $(yaml-quote ${KUBE_BEARER_TOKEN})
  1152. MASTER_CERT: $(yaml-quote ${MASTER_CERT_BASE64:-})
  1153. MASTER_KEY: $(yaml-quote ${MASTER_KEY_BASE64:-})
  1154. KUBECFG_CERT: $(yaml-quote ${KUBECFG_CERT_BASE64:-})
  1155. KUBECFG_KEY: $(yaml-quote ${KUBECFG_KEY_BASE64:-})
  1156. KUBELET_APISERVER: $(yaml-quote ${KUBELET_APISERVER:-})
  1157. NUM_NODES: $(yaml-quote ${NUM_NODES})
  1158. STORAGE_BACKEND: $(yaml-quote ${STORAGE_BACKEND:-etcd3})
  1159. STORAGE_MEDIA_TYPE: $(yaml-quote ${STORAGE_MEDIA_TYPE:-})
  1160. ENABLE_GARBAGE_COLLECTOR: $(yaml-quote ${ENABLE_GARBAGE_COLLECTOR:-})
  1161. ENABLE_LEGACY_ABAC: $(yaml-quote ${ENABLE_LEGACY_ABAC:-})
  1162. MASTER_ADVERTISE_ADDRESS: $(yaml-quote ${MASTER_ADVERTISE_ADDRESS:-})
  1163. ETCD_CA_KEY: $(yaml-quote ${ETCD_CA_KEY_BASE64:-})
  1164. ETCD_CA_CERT: $(yaml-quote ${ETCD_CA_CERT_BASE64:-})
  1165. ETCD_PEER_KEY: $(yaml-quote ${ETCD_PEER_KEY_BASE64:-})
  1166. ETCD_PEER_CERT: $(yaml-quote ${ETCD_PEER_CERT_BASE64:-})
  1167. SERVICEACCOUNT_ISSUER: $(yaml-quote ${SERVICEACCOUNT_ISSUER:-})
  1168. EOF
  1169. # KUBE_APISERVER_REQUEST_TIMEOUT_SEC (if set) controls the --request-timeout
  1170. # flag
  1171. if [ -n "${KUBE_APISERVER_REQUEST_TIMEOUT_SEC:-}" ]; then
  1172. cat >>$file <<EOF
  1173. KUBE_APISERVER_REQUEST_TIMEOUT_SEC: $(yaml-quote ${KUBE_APISERVER_REQUEST_TIMEOUT_SEC})
  1174. EOF
  1175. fi
  1176. # ETCD_IMAGE (if set) allows to use a custom etcd image.
  1177. if [ -n "${ETCD_IMAGE:-}" ]; then
  1178. cat >>$file <<EOF
  1179. ETCD_IMAGE: $(yaml-quote ${ETCD_IMAGE})
  1180. EOF
  1181. fi
  1182. # ETCD_DOCKER_REPOSITORY (if set) allows to use a custom etcd docker repository to pull the etcd image from.
  1183. if [ -n "${ETCD_DOCKER_REPOSITORY:-}" ]; then
  1184. cat >>$file <<EOF
  1185. ETCD_DOCKER_REPOSITORY: $(yaml-quote ${ETCD_DOCKER_REPOSITORY})
  1186. EOF
  1187. fi
  1188. # ETCD_VERSION (if set) allows you to use custom version of etcd.
  1189. # The main purpose of using it may be rollback of etcd v3 API,
  1190. # where we need 3.0.* image, but are rolling back to 2.3.7.
  1191. if [ -n "${ETCD_VERSION:-}" ]; then
  1192. cat >>$file <<EOF
  1193. ETCD_VERSION: $(yaml-quote ${ETCD_VERSION})
  1194. EOF
  1195. fi
  1196. if [ -n "${ETCD_HOSTNAME:-}" ]; then
  1197. cat >>$file <<EOF
  1198. ETCD_HOSTNAME: $(yaml-quote ${ETCD_HOSTNAME})
  1199. EOF
  1200. fi
  1201. if [ -n "${ETCD_LIVENESS_PROBE_INITIAL_DELAY_SEC:-}" ]; then
  1202. cat >>$file <<EOF
  1203. ETCD_LIVENESS_PROBE_INITIAL_DELAY_SEC: $(yaml-quote ${ETCD_LIVENESS_PROBE_INITIAL_DELAY_SEC})
  1204. EOF
  1205. fi
  1206. if [ -n "${KUBE_APISERVER_LIVENESS_PROBE_INITIAL_DELAY_SEC:-}" ]; then
  1207. cat >>$file <<EOF
  1208. KUBE_APISERVER_LIVENESS_PROBE_INITIAL_DELAY_SEC: $(yaml-quote ${KUBE_APISERVER_LIVENESS_PROBE_INITIAL_DELAY_SEC})
  1209. EOF
  1210. fi
  1211. if [ -n "${ETCD_COMPACTION_INTERVAL_SEC:-}" ]; then
  1212. cat >>$file <<EOF
  1213. ETCD_COMPACTION_INTERVAL_SEC: $(yaml-quote ${ETCD_COMPACTION_INTERVAL_SEC})
  1214. EOF
  1215. fi
  1216. if [ -n "${ETCD_QUOTA_BACKEND_BYTES:-}" ]; then
  1217. cat >>$file <<EOF
  1218. ETCD_QUOTA_BACKEND_BYTES: $(yaml-quote ${ETCD_QUOTA_BACKEND_BYTES})
  1219. EOF
  1220. fi
  1221. if [ -n "${ETCD_EXTRA_ARGS:-}" ]; then
  1222. cat >>$file <<EOF
  1223. ETCD_EXTRA_ARGS: $(yaml-quote ${ETCD_EXTRA_ARGS})
  1224. EOF
  1225. fi
  1226. if [ -n "${ETCD_SERVERS:-}" ]; then
  1227. cat >>$file <<EOF
  1228. ETCD_SERVERS: $(yaml-quote ${ETCD_SERVERS})
  1229. EOF
  1230. fi
  1231. if [ -n "${ETCD_SERVERS_OVERRIDES:-}" ]; then
  1232. cat >>$file <<EOF
  1233. ETCD_SERVERS_OVERRIDES: $(yaml-quote ${ETCD_SERVERS_OVERRIDES})
  1234. EOF
  1235. fi
  1236. if [ -n "${APISERVER_TEST_ARGS:-}" ]; then
  1237. cat >>$file <<EOF
  1238. APISERVER_TEST_ARGS: $(yaml-quote ${APISERVER_TEST_ARGS})
  1239. EOF
  1240. fi
  1241. if [ -n "${CONTROLLER_MANAGER_TEST_ARGS:-}" ]; then
  1242. cat >>$file <<EOF
  1243. CONTROLLER_MANAGER_TEST_ARGS: $(yaml-quote ${CONTROLLER_MANAGER_TEST_ARGS})
  1244. EOF
  1245. fi
  1246. if [ -n "${CONTROLLER_MANAGER_TEST_LOG_LEVEL:-}" ]; then
  1247. cat >>$file <<EOF
  1248. CONTROLLER_MANAGER_TEST_LOG_LEVEL: $(yaml-quote ${CONTROLLER_MANAGER_TEST_LOG_LEVEL})
  1249. EOF
  1250. fi
  1251. if [ -n "${SCHEDULER_TEST_ARGS:-}" ]; then
  1252. cat >>$file <<EOF
  1253. SCHEDULER_TEST_ARGS: $(yaml-quote ${SCHEDULER_TEST_ARGS})
  1254. EOF
  1255. fi
  1256. if [ -n "${SCHEDULER_TEST_LOG_LEVEL:-}" ]; then
  1257. cat >>$file <<EOF
  1258. SCHEDULER_TEST_LOG_LEVEL: $(yaml-quote ${SCHEDULER_TEST_LOG_LEVEL})
  1259. EOF
  1260. fi
  1261. if [ -n "${INITIAL_ETCD_CLUSTER:-}" ]; then
  1262. cat >>$file <<EOF
  1263. INITIAL_ETCD_CLUSTER: $(yaml-quote ${INITIAL_ETCD_CLUSTER})
  1264. EOF
  1265. fi
  1266. if [ -n "${INITIAL_ETCD_CLUSTER_STATE:-}" ]; then
  1267. cat >>$file <<EOF
  1268. INITIAL_ETCD_CLUSTER_STATE: $(yaml-quote ${INITIAL_ETCD_CLUSTER_STATE})
  1269. EOF
  1270. fi
  1271. if [ -n "${CLUSTER_SIGNING_DURATION:-}" ]; then
  1272. cat >>$file <<EOF
  1273. CLUSTER_SIGNING_DURATION: $(yaml-quote ${CLUSTER_SIGNING_DURATION})
  1274. EOF
  1275. fi
  1276. if [[ "${NODE_ACCELERATORS:-}" == *"type=nvidia"* ]]; then
  1277. cat >>$file <<EOF
  1278. ENABLE_NVIDIA_GPU_DEVICE_PLUGIN: $(yaml-quote "true")
  1279. EOF
  1280. fi
  1281. if [ -n "${ADDON_MANAGER_LEADER_ELECTION:-}" ]; then
  1282. cat >>$file <<EOF
  1283. ADDON_MANAGER_LEADER_ELECTION: $(yaml-quote ${ADDON_MANAGER_LEADER_ELECTION})
  1284. EOF
  1285. fi
  1286. if [ -n "${API_SERVER_TEST_LOG_LEVEL:-}" ]; then
  1287. cat >>$file <<EOF
  1288. API_SERVER_TEST_LOG_LEVEL: $(yaml-quote ${API_SERVER_TEST_LOG_LEVEL})
  1289. EOF
  1290. fi
  1291. if [ -n "${ETCD_LISTEN_CLIENT_IP:-}" ]; then
  1292. cat >>$file <<EOF
  1293. ETCD_LISTEN_CLIENT_IP: $(yaml-quote ${ETCD_LISTEN_CLIENT_IP})
  1294. EOF
  1295. fi
  1296. else
  1297. # Node-only env vars.
  1298. cat >>$file <<EOF
  1299. KUBERNETES_MASTER: $(yaml-quote "false")
  1300. EXTRA_DOCKER_OPTS: $(yaml-quote ${EXTRA_DOCKER_OPTS:-})
  1301. EOF
  1302. if [ -n "${KUBEPROXY_TEST_ARGS:-}" ]; then
  1303. cat >>$file <<EOF
  1304. KUBEPROXY_TEST_ARGS: $(yaml-quote ${KUBEPROXY_TEST_ARGS})
  1305. EOF
  1306. fi
  1307. if [ -n "${KUBEPROXY_TEST_LOG_LEVEL:-}" ]; then
  1308. cat >>$file <<EOF
  1309. KUBEPROXY_TEST_LOG_LEVEL: $(yaml-quote ${KUBEPROXY_TEST_LOG_LEVEL})
  1310. EOF
  1311. fi
  1312. fi
  1313. if [[ "${ENABLE_CLUSTER_AUTOSCALER}" == "true" ]]; then
  1314. cat >>$file <<EOF
  1315. ENABLE_CLUSTER_AUTOSCALER: $(yaml-quote ${ENABLE_CLUSTER_AUTOSCALER})
  1316. AUTOSCALER_MIG_CONFIG: $(yaml-quote ${AUTOSCALER_MIG_CONFIG})
  1317. AUTOSCALER_EXPANDER_CONFIG: $(yaml-quote ${AUTOSCALER_EXPANDER_CONFIG})
  1318. EOF
  1319. if [[ "${master}" == "false" ]]; then
  1320. # TODO(kubernetes/autoscaler#718): AUTOSCALER_ENV_VARS is a hotfix for cluster autoscaler,
  1321. # which reads the kube-env to determine the shape of a node and was broken by #60020.
  1322. # This should be removed as soon as a more reliable source of information is available!
  1323. local node_labels="$(build-linux-node-labels false)"
  1324. local node_taints="${NODE_TAINTS:-}"
  1325. local autoscaler_env_vars="node_labels=${node_labels};node_taints=${node_taints}"
  1326. cat >>$file <<EOF
  1327. AUTOSCALER_ENV_VARS: $(yaml-quote ${autoscaler_env_vars})
  1328. EOF
  1329. fi
  1330. fi
  1331. if [ -n "${SCHEDULING_ALGORITHM_PROVIDER:-}" ]; then
  1332. cat >>$file <<EOF
  1333. SCHEDULING_ALGORITHM_PROVIDER: $(yaml-quote ${SCHEDULING_ALGORITHM_PROVIDER})
  1334. EOF
  1335. fi
  1336. if [ -n "${MAX_PODS_PER_NODE:-}" ]; then
  1337. cat >>$file <<EOF
  1338. MAX_PODS_PER_NODE: $(yaml-quote ${MAX_PODS_PER_NODE})
  1339. EOF
  1340. fi
  1341. }
  1342. function build-windows-kube-env {
  1343. local file="$1"
  1344. # For now the Windows kube-env is a superset of the Linux kube-env.
  1345. build-linux-kube-env false $file
  1346. cat >>$file <<EOF
  1347. WINDOWS_NODE_INSTANCE_PREFIX: $(yaml-quote ${WINDOWS_NODE_INSTANCE_PREFIX})
  1348. NODE_BINARY_TAR_URL: $(yaml-quote ${NODE_BINARY_TAR_URL})
  1349. NODE_BINARY_TAR_HASH: $(yaml-quote ${NODE_BINARY_TAR_HASH})
  1350. K8S_DIR: $(yaml-quote ${WINDOWS_K8S_DIR})
  1351. NODE_DIR: $(yaml-quote ${WINDOWS_NODE_DIR})
  1352. LOGS_DIR: $(yaml-quote ${WINDOWS_LOGS_DIR})
  1353. CNI_DIR: $(yaml-quote ${WINDOWS_CNI_DIR})
  1354. CNI_CONFIG_DIR: $(yaml-quote ${WINDOWS_CNI_CONFIG_DIR})
  1355. MANIFESTS_DIR: $(yaml-quote ${WINDOWS_MANIFESTS_DIR})
  1356. PKI_DIR: $(yaml-quote ${WINDOWS_PKI_DIR})
  1357. KUBELET_CONFIG_FILE: $(yaml-quote ${WINDOWS_KUBELET_CONFIG_FILE})
  1358. KUBEPROXY_ARGS: $(yaml-quote ${KUBEPROXY_ARGS})
  1359. KUBECONFIG_FILE: $(yaml-quote ${WINDOWS_KUBECONFIG_FILE})
  1360. BOOTSTRAP_KUBECONFIG_FILE: $(yaml-quote ${WINDOWS_BOOTSTRAP_KUBECONFIG_FILE})
  1361. KUBEPROXY_KUBECONFIG_FILE: $(yaml-quote ${WINDOWS_KUBEPROXY_KUBECONFIG_FILE})
  1362. EOF
  1363. }
  1364. function sha1sum-file() {
  1365. if which sha1sum >/dev/null 2>&1; then
  1366. sha1sum "$1" | awk '{ print $1 }'
  1367. else
  1368. shasum -a1 "$1" | awk '{ print $1 }'
  1369. fi
  1370. }
  1371. # Create certificate pairs for the cluster.
  1372. # $1: The public IP for the master.
  1373. #
  1374. # These are used for static cert distribution (e.g. static clustering) at
  1375. # cluster creation time. This will be obsoleted once we implement dynamic
  1376. # clustering.
  1377. #
  1378. # The following certificate pairs are created:
  1379. #
  1380. # - ca (the cluster's certificate authority)
  1381. # - server
  1382. # - kubelet
  1383. # - kubecfg (for kubectl)
  1384. #
  1385. # TODO(roberthbailey): Replace easyrsa with a simple Go program to generate
  1386. # the certs that we need.
  1387. #
  1388. # Assumed vars
  1389. # KUBE_TEMP
  1390. # MASTER_NAME
  1391. #
  1392. # Vars set:
  1393. # CERT_DIR
  1394. # CA_CERT_BASE64
  1395. # MASTER_CERT_BASE64
  1396. # MASTER_KEY_BASE64
  1397. # KUBELET_CERT_BASE64
  1398. # KUBELET_KEY_BASE64
  1399. # KUBECFG_CERT_BASE64
  1400. # KUBECFG_KEY_BASE64
  1401. function create-certs {
  1402. local -r primary_cn="${1}"
  1403. # Determine extra certificate names for master
  1404. local octets=($(echo "${SERVICE_CLUSTER_IP_RANGE}" | sed -e 's|/.*||' -e 's/\./ /g'))
  1405. ((octets[3]+=1))
  1406. local -r service_ip=$(echo "${octets[*]}" | sed 's/ /./g')
  1407. local sans=""
  1408. for extra in $@; do
  1409. if [[ -n "${extra}" ]]; then
  1410. sans="${sans}IP:${extra},"
  1411. fi
  1412. done
  1413. sans="${sans}IP:${service_ip},DNS:kubernetes,DNS:kubernetes.default,DNS:kubernetes.default.svc,DNS:kubernetes.default.svc.${DNS_DOMAIN},DNS:${MASTER_NAME}"
  1414. echo "Generating certs for alternate-names: ${sans}"
  1415. setup-easyrsa
  1416. PRIMARY_CN="${primary_cn}" SANS="${sans}" generate-certs
  1417. AGGREGATOR_PRIMARY_CN="${primary_cn}" AGGREGATOR_SANS="${sans}" generate-aggregator-certs
  1418. # By default, linux wraps base64 output every 76 cols, so we use 'tr -d' to remove whitespaces.
  1419. # Note 'base64 -w0' doesn't work on Mac OS X, which has different flags.
  1420. CA_KEY_BASE64=$(cat "${CERT_DIR}/pki/private/ca.key" | base64 | tr -d '\r\n')
  1421. CA_CERT_BASE64=$(cat "${CERT_DIR}/pki/ca.crt" | base64 | tr -d '\r\n')
  1422. MASTER_CERT_BASE64=$(cat "${CERT_DIR}/pki/issued/${MASTER_NAME}.crt" | base64 | tr -d '\r\n')
  1423. MASTER_KEY_BASE64=$(cat "${CERT_DIR}/pki/private/${MASTER_NAME}.key" | base64 | tr -d '\r\n')
  1424. KUBELET_CERT_BASE64=$(cat "${CERT_DIR}/pki/issued/kubelet.crt" | base64 | tr -d '\r\n')
  1425. KUBELET_KEY_BASE64=$(cat "${CERT_DIR}/pki/private/kubelet.key" | base64 | tr -d '\r\n')
  1426. KUBECFG_CERT_BASE64=$(cat "${CERT_DIR}/pki/issued/kubecfg.crt" | base64 | tr -d '\r\n')
  1427. KUBECFG_KEY_BASE64=$(cat "${CERT_DIR}/pki/private/kubecfg.key" | base64 | tr -d '\r\n')
  1428. KUBEAPISERVER_CERT_BASE64=$(cat "${CERT_DIR}/pki/issued/kube-apiserver.crt" | base64 | tr -d '\r\n')
  1429. KUBEAPISERVER_KEY_BASE64=$(cat "${CERT_DIR}/pki/private/kube-apiserver.key" | base64 | tr -d '\r\n')
  1430. # Setting up an addition directory (beyond pki) as it is the simplest way to
  1431. # ensure we get a different CA pair to sign the proxy-client certs and which
  1432. # we can send CA public key to the user-apiserver to validate communication.
  1433. AGGREGATOR_CA_KEY_BASE64=$(cat "${AGGREGATOR_CERT_DIR}/pki/private/ca.key" | base64 | tr -d '\r\n')
  1434. REQUESTHEADER_CA_CERT_BASE64=$(cat "${AGGREGATOR_CERT_DIR}/pki/ca.crt" | base64 | tr -d '\r\n')
  1435. PROXY_CLIENT_CERT_BASE64=$(cat "${AGGREGATOR_CERT_DIR}/pki/issued/proxy-client.crt" | base64 | tr -d '\r\n')
  1436. PROXY_CLIENT_KEY_BASE64=$(cat "${AGGREGATOR_CERT_DIR}/pki/private/proxy-client.key" | base64 | tr -d '\r\n')
  1437. }
  1438. # Set up easy-rsa directory structure.
  1439. #
  1440. # Assumed vars
  1441. # KUBE_TEMP
  1442. #
  1443. # Vars set:
  1444. # CERT_DIR
  1445. # AGGREGATOR_CERT_DIR
  1446. function setup-easyrsa {
  1447. local -r cert_create_debug_output=$(mktemp "${KUBE_TEMP}/cert_create_debug_output.XXX")
  1448. # Note: This was heavily cribbed from make-ca-cert.sh
  1449. (set -x
  1450. cd "${KUBE_TEMP}"
  1451. curl -L -O --connect-timeout 20 --retry 6 --retry-delay 2 https://storage.googleapis.com/kubernetes-release/easy-rsa/easy-rsa.tar.gz
  1452. tar xzf easy-rsa.tar.gz
  1453. mkdir easy-rsa-master/kubelet
  1454. cp -r easy-rsa-master/easyrsa3/* easy-rsa-master/kubelet
  1455. mkdir easy-rsa-master/aggregator
  1456. cp -r easy-rsa-master/easyrsa3/* easy-rsa-master/aggregator) &>${cert_create_debug_output} || true
  1457. CERT_DIR="${KUBE_TEMP}/easy-rsa-master/easyrsa3"
  1458. AGGREGATOR_CERT_DIR="${KUBE_TEMP}/easy-rsa-master/aggregator"
  1459. if [ ! -x "${CERT_DIR}/easyrsa" -o ! -x "${AGGREGATOR_CERT_DIR}/easyrsa" ]; then
  1460. # TODO(roberthbailey,porridge): add better error handling here,
  1461. # see https://github.com/kubernetes/kubernetes/issues/55229
  1462. cat "${cert_create_debug_output}" >&2
  1463. echo "=== Failed to setup easy-rsa: Aborting ===" >&2
  1464. exit 2
  1465. fi
  1466. }
  1467. # Runs the easy RSA commands to generate certificate files.
  1468. # The generated files are IN ${CERT_DIR}
  1469. #
  1470. # Assumed vars
  1471. # KUBE_TEMP
  1472. # MASTER_NAME
  1473. # CERT_DIR
  1474. # PRIMARY_CN: Primary canonical name
  1475. # SANS: Subject alternate names
  1476. #
  1477. #
  1478. function generate-certs {
  1479. local -r cert_create_debug_output=$(mktemp "${KUBE_TEMP}/cert_create_debug_output.XXX")
  1480. # Note: This was heavily cribbed from make-ca-cert.sh
  1481. (set -x
  1482. cd "${CERT_DIR}"
  1483. ./easyrsa init-pki
  1484. # this puts the cert into pki/ca.crt and the key into pki/private/ca.key
  1485. ./easyrsa --batch "--req-cn=${PRIMARY_CN}@$(date +%s)" build-ca nopass
  1486. ./easyrsa --subject-alt-name="${SANS}" build-server-full "${MASTER_NAME}" nopass
  1487. ./easyrsa build-client-full kube-apiserver nopass
  1488. kube::util::ensure-cfssl "${KUBE_TEMP}/cfssl"
  1489. # make the config for the signer
  1490. echo '{"signing":{"default":{"expiry":"43800h","usages":["signing","key encipherment","client auth"]}}}' > "ca-config.json"
  1491. # create the kubelet client cert with the correct groups
  1492. echo '{"CN":"kubelet","names":[{"O":"system:nodes"}],"hosts":[""],"key":{"algo":"rsa","size":2048}}' | "${CFSSL_BIN}" gencert -ca=pki/ca.crt -ca-key=pki/private/ca.key -config=ca-config.json - | "${CFSSLJSON_BIN}" -bare kubelet
  1493. mv "kubelet-key.pem" "pki/private/kubelet.key"
  1494. mv "kubelet.pem" "pki/issued/kubelet.crt"
  1495. rm -f "kubelet.csr"
  1496. # Make a superuser client cert with subject "O=system:masters, CN=kubecfg"
  1497. ./easyrsa --dn-mode=org \
  1498. --req-cn=kubecfg --req-org=system:masters \
  1499. --req-c= --req-st= --req-city= --req-email= --req-ou= \
  1500. build-client-full kubecfg nopass) &>${cert_create_debug_output} || true
  1501. local output_file_missing=0
  1502. local output_file
  1503. for output_file in \
  1504. "${CERT_DIR}/pki/private/ca.key" \
  1505. "${CERT_DIR}/pki/ca.crt" \
  1506. "${CERT_DIR}/pki/issued/${MASTER_NAME}.crt" \
  1507. "${CERT_DIR}/pki/private/${MASTER_NAME}.key" \
  1508. "${CERT_DIR}/pki/issued/kubelet.crt" \
  1509. "${CERT_DIR}/pki/private/kubelet.key" \
  1510. "${CERT_DIR}/pki/issued/kubecfg.crt" \
  1511. "${CERT_DIR}/pki/private/kubecfg.key" \
  1512. "${CERT_DIR}/pki/issued/kube-apiserver.crt" \
  1513. "${CERT_DIR}/pki/private/kube-apiserver.key"
  1514. do
  1515. if [[ ! -s "${output_file}" ]]; then
  1516. echo "Expected file ${output_file} not created" >&2
  1517. output_file_missing=1
  1518. fi
  1519. done
  1520. if (( $output_file_missing )); then
  1521. # TODO(roberthbailey,porridge): add better error handling here,
  1522. # see https://github.com/kubernetes/kubernetes/issues/55229
  1523. cat "${cert_create_debug_output}" >&2
  1524. echo "=== Failed to generate master certificates: Aborting ===" >&2
  1525. exit 2
  1526. fi
  1527. }
  1528. # Runs the easy RSA commands to generate aggregator certificate files.
  1529. # The generated files are in ${AGGREGATOR_CERT_DIR}
  1530. #
  1531. # Assumed vars
  1532. # KUBE_TEMP
  1533. # AGGREGATOR_MASTER_NAME
  1534. # AGGREGATOR_CERT_DIR
  1535. # AGGREGATOR_PRIMARY_CN: Primary canonical name
  1536. # AGGREGATOR_SANS: Subject alternate names
  1537. #
  1538. #
  1539. function generate-aggregator-certs {
  1540. local -r cert_create_debug_output=$(mktemp "${KUBE_TEMP}/cert_create_debug_output.XXX")
  1541. # Note: This was heavily cribbed from make-ca-cert.sh
  1542. (set -x
  1543. cd "${KUBE_TEMP}/easy-rsa-master/aggregator"
  1544. ./easyrsa init-pki
  1545. # this puts the cert into pki/ca.crt and the key into pki/private/ca.key
  1546. ./easyrsa --batch "--req-cn=${AGGREGATOR_PRIMARY_CN}@$(date +%s)" build-ca nopass
  1547. ./easyrsa --subject-alt-name="${AGGREGATOR_SANS}" build-server-full "${AGGREGATOR_MASTER_NAME}" nopass
  1548. ./easyrsa build-client-full aggregator-apiserver nopass
  1549. kube::util::ensure-cfssl "${KUBE_TEMP}/cfssl"
  1550. # make the config for the signer
  1551. echo '{"signing":{"default":{"expiry":"43800h","usages":["signing","key encipherment","client auth"]}}}' > "ca-config.json"
  1552. # create the aggregator client cert with the correct groups
  1553. echo '{"CN":"aggregator","hosts":[""],"key":{"algo":"rsa","size":2048}}' | "${CFSSL_BIN}" gencert -ca=pki/ca.crt -ca-key=pki/private/ca.key -config=ca-config.json - | "${CFSSLJSON_BIN}" -bare proxy-client
  1554. mv "proxy-client-key.pem" "pki/private/proxy-client.key"
  1555. mv "proxy-client.pem" "pki/issued/proxy-client.crt"
  1556. rm -f "proxy-client.csr"
  1557. # Make a superuser client cert with subject "O=system:masters, CN=kubecfg"
  1558. ./easyrsa --dn-mode=org \
  1559. --req-cn=proxy-clientcfg --req-org=system:aggregator \
  1560. --req-c= --req-st= --req-city= --req-email= --req-ou= \
  1561. build-client-full proxy-clientcfg nopass) &>${cert_create_debug_output} || true
  1562. local output_file_missing=0
  1563. local output_file
  1564. for output_file in \
  1565. "${AGGREGATOR_CERT_DIR}/pki/private/ca.key" \
  1566. "${AGGREGATOR_CERT_DIR}/pki/ca.crt" \
  1567. "${AGGREGATOR_CERT_DIR}/pki/issued/proxy-client.crt" \
  1568. "${AGGREGATOR_CERT_DIR}/pki/private/proxy-client.key"
  1569. do
  1570. if [[ ! -s "${output_file}" ]]; then
  1571. echo "Expected file ${output_file} not created" >&2
  1572. output_file_missing=1
  1573. fi
  1574. done
  1575. if (( $output_file_missing )); then
  1576. # TODO(roberthbailey,porridge): add better error handling here,
  1577. # see https://github.com/kubernetes/kubernetes/issues/55229
  1578. cat "${cert_create_debug_output}" >&2
  1579. echo "=== Failed to generate aggregator certificates: Aborting ===" >&2
  1580. exit 2
  1581. fi
  1582. }
  1583. #
  1584. # Using provided master env, extracts value from provided key.
  1585. #
  1586. # Args:
  1587. # $1 master env (kube-env of master; result of calling get-master-env)
  1588. # $2 env key to use
  1589. function get-env-val() {
  1590. local match=`(echo "${1}" | grep -E "^${2}:") || echo ""`
  1591. if [[ -z ${match} ]]; then
  1592. echo ""
  1593. fi
  1594. echo ${match} | cut -d : -f 2 | cut -d \' -f 2
  1595. }
  1596. # Load the master env by calling get-master-env, and extract important values
  1597. function parse-master-env() {
  1598. # Get required master env vars
  1599. local master_env=$(get-master-env)
  1600. KUBE_PROXY_TOKEN=$(get-env-val "${master_env}" "KUBE_PROXY_TOKEN")
  1601. NODE_PROBLEM_DETECTOR_TOKEN=$(get-env-val "${master_env}" "NODE_PROBLEM_DETECTOR_TOKEN")
  1602. CA_CERT_BASE64=$(get-env-val "${master_env}" "CA_CERT")
  1603. CA_KEY_BASE64=$(get-env-val "${master_env}" "CA_KEY")
  1604. KUBEAPISERVER_CERT_BASE64=$(get-env-val "${master_env}" "KUBEAPISERVER_CERT")
  1605. KUBEAPISERVER_KEY_BASE64=$(get-env-val "${master_env}" "KUBEAPISERVER_KEY")
  1606. EXTRA_DOCKER_OPTS=$(get-env-val "${master_env}" "EXTRA_DOCKER_OPTS")
  1607. KUBELET_CERT_BASE64=$(get-env-val "${master_env}" "KUBELET_CERT")
  1608. KUBELET_KEY_BASE64=$(get-env-val "${master_env}" "KUBELET_KEY")
  1609. MASTER_CERT_BASE64=$(get-env-val "${master_env}" "MASTER_CERT")
  1610. MASTER_KEY_BASE64=$(get-env-val "${master_env}" "MASTER_KEY")
  1611. AGGREGATOR_CA_KEY_BASE64=$(get-env-val "${master_env}" "AGGREGATOR_CA_KEY")
  1612. REQUESTHEADER_CA_CERT_BASE64=$(get-env-val "${master_env}" "REQUESTHEADER_CA_CERT")
  1613. PROXY_CLIENT_CERT_BASE64=$(get-env-val "${master_env}" "PROXY_CLIENT_CERT")
  1614. PROXY_CLIENT_KEY_BASE64=$(get-env-val "${master_env}" "PROXY_CLIENT_KEY")
  1615. ENABLE_LEGACY_ABAC=$(get-env-val "${master_env}" "ENABLE_LEGACY_ABAC")
  1616. ETCD_APISERVER_CA_KEY_BASE64=$(get-env-val "${master_env}" "ETCD_APISERVER_CA_KEY")
  1617. ETCD_APISERVER_CA_CERT_BASE64=$(get-env-val "${master_env}" "ETCD_APISERVER_CA_CERT")
  1618. ETCD_APISERVER_SERVER_KEY_BASE64=$(get-env-val "${master_env}" "ETCD_APISERVER_SERVER_KEY")
  1619. ETCD_APISERVER_SERVER_CERT_BASE64=$(get-env-val "${master_env}" "ETCD_APISERVER_SERVER_CERT")
  1620. ETCD_APISERVER_CLIENT_KEY_BASE64=$(get-env-val "${master_env}" "ETCD_APISERVER_CLIENT_KEY")
  1621. ETCD_APISERVER_CLIENT_CERT_BASE64=$(get-env-val "${master_env}" "ETCD_APISERVER_CLIENT_CERT")
  1622. }
  1623. # Update or verify required gcloud components are installed
  1624. # at minimum required version.
  1625. # Assumed vars
  1626. # KUBE_PROMPT_FOR_UPDATE
  1627. function update-or-verify-gcloud() {
  1628. local sudo_prefix=""
  1629. if [ ! -w $(dirname `which gcloud`) ]; then
  1630. sudo_prefix="sudo"
  1631. fi
  1632. # update and install components as needed
  1633. if [[ "${KUBE_PROMPT_FOR_UPDATE}" == "y" ]]; then
  1634. ${sudo_prefix} gcloud ${gcloud_prompt:-} components install alpha
  1635. ${sudo_prefix} gcloud ${gcloud_prompt:-} components install beta
  1636. ${sudo_prefix} gcloud ${gcloud_prompt:-} components update
  1637. else
  1638. local version=$(gcloud version --format=json)
  1639. python -c'
  1640. import json,sys
  1641. from distutils import version
  1642. minVersion = version.LooseVersion("1.3.0")
  1643. required = [ "alpha", "beta", "core" ]
  1644. data = json.loads(sys.argv[1])
  1645. rel = data.get("Google Cloud SDK")
  1646. if "CL @" in rel:
  1647. print("Using dev version of gcloud: %s" %rel)
  1648. exit(0)
  1649. if rel != "HEAD" and version.LooseVersion(rel) < minVersion:
  1650. print("gcloud version out of date ( < %s )" % minVersion)
  1651. exit(1)
  1652. missing = []
  1653. for c in required:
  1654. if not data.get(c):
  1655. missing += [c]
  1656. if missing:
  1657. for c in missing:
  1658. print ("missing required gcloud component \"{0}\"".format(c))
  1659. print ("Try running `gcloud components install {0}`".format(c))
  1660. exit(1)
  1661. ' """${version}"""
  1662. fi
  1663. }
  1664. # Robustly try to create a static ip.
  1665. # $1: The name of the ip to create
  1666. # $2: The name of the region to create the ip in.
  1667. function create-static-ip() {
  1668. detect-project
  1669. local attempt=0
  1670. local REGION="$2"
  1671. while true; do
  1672. if gcloud compute addresses create "$1" \
  1673. --project "${PROJECT}" \
  1674. --region "${REGION}" -q > /dev/null; then
  1675. # successful operation - wait until it's visible
  1676. start="$(date +%s)"
  1677. while true; do
  1678. now="$(date +%s)"
  1679. # Timeout set to 15 minutes
  1680. if [[ $((now - start)) -gt 900 ]]; then
  1681. echo "Timeout while waiting for master IP visibility"
  1682. exit 2
  1683. fi
  1684. if gcloud compute addresses describe "$1" --project "${PROJECT}" --region "${REGION}" >/dev/null 2>&1; then
  1685. break
  1686. fi
  1687. echo "Master IP not visible yet. Waiting..."
  1688. sleep 5
  1689. done
  1690. break
  1691. fi
  1692. if gcloud compute addresses describe "$1" \
  1693. --project "${PROJECT}" \
  1694. --region "${REGION}" >/dev/null 2>&1; then
  1695. # it exists - postcondition satisfied
  1696. break
  1697. fi
  1698. if (( attempt > 4 )); then
  1699. echo -e "${color_red}Failed to create static ip $1 ${color_norm}" >&2
  1700. exit 2
  1701. fi
  1702. attempt=$(($attempt+1))
  1703. echo -e "${color_yellow}Attempt $attempt failed to create static ip $1. Retrying.${color_norm}" >&2
  1704. sleep $(($attempt * 5))
  1705. done
  1706. }
  1707. # Robustly try to create a firewall rule.
  1708. # $1: The name of firewall rule.
  1709. # $2: IP ranges.
  1710. # $3: Target tags for this firewall rule.
  1711. function create-firewall-rule() {
  1712. detect-project
  1713. local attempt=0
  1714. while true; do
  1715. if ! gcloud compute firewall-rules create "$1" \
  1716. --project "${NETWORK_PROJECT}" \
  1717. --network "${NETWORK}" \
  1718. --source-ranges "$2" \
  1719. --target-tags "$3" \
  1720. --allow tcp,udp,icmp,esp,ah,sctp; then
  1721. if (( attempt > 4 )); then
  1722. echo -e "${color_red}Failed to create firewall rule $1 ${color_norm}" >&2
  1723. exit 2
  1724. fi
  1725. echo -e "${color_yellow}Attempt $(($attempt+1)) failed to create firewall rule $1. Retrying.${color_norm}" >&2
  1726. attempt=$(($attempt+1))
  1727. sleep $(($attempt * 5))
  1728. else
  1729. break
  1730. fi
  1731. done
  1732. }
  1733. # Format the string argument for gcloud network.
  1734. function make-gcloud-network-argument() {
  1735. local network_project="$1"
  1736. local region="$2"
  1737. local network="$3"
  1738. local subnet="$4"
  1739. local address="$5" # optional
  1740. local enable_ip_alias="$6" # optional
  1741. local alias_size="$7" # optional
  1742. local networkURL="projects/${network_project}/global/networks/${network}"
  1743. local subnetURL="projects/${network_project}/regions/${region}/subnetworks/${subnet:-}"
  1744. local ret=""
  1745. if [[ "${enable_ip_alias}" == 'true' ]]; then
  1746. ret="--network-interface"
  1747. ret="${ret} network=${networkURL}"
  1748. if [[ "${address:-}" == "no-address" ]]; then
  1749. ret="${ret},no-address"
  1750. else
  1751. ret="${ret},address=${address:-}"
  1752. fi
  1753. ret="${ret},subnet=${subnetURL}"
  1754. ret="${ret},aliases=pods-default:${alias_size}"
  1755. ret="${ret} --no-can-ip-forward"
  1756. else
  1757. if [[ -n ${subnet:-} ]]; then
  1758. ret="${ret} --subnet ${subnetURL}"
  1759. else
  1760. ret="${ret} --network ${networkURL}"
  1761. fi
  1762. ret="${ret} --can-ip-forward"
  1763. if [[ -n ${address:-} ]] && [[ "$address" != "no-address" ]]; then
  1764. ret="${ret} --address ${address}"
  1765. fi
  1766. fi
  1767. echo "${ret}"
  1768. }
  1769. # $1: version (required)
  1770. # $2: Prefix for the template name, i.e. NODE_INSTANCE_PREFIX or
  1771. # WINDOWS_NODE_INSTANCE_PREFIX.
  1772. function get-template-name-from-version() {
  1773. local -r version=${1}
  1774. local -r template_prefix=${2}
  1775. # trim template name to pass gce name validation
  1776. echo "${template_prefix}-template-${version}" | cut -c 1-63 | sed 's/[\.\+]/-/g;s/-*$//g'
  1777. }
  1778. # validates the NODE_LOCAL_SSDS_EXT variable
  1779. function validate-node-local-ssds-ext(){
  1780. ssdopts="${1}"
  1781. if [[ -z "${ssdopts[0]}" || -z "${ssdopts[1]}" || -z "${ssdopts[2]}" ]]; then
  1782. echo -e "${color_red}Local SSD: NODE_LOCAL_SSDS_EXT is malformed, found ${ssdopts[0]-_},${ssdopts[1]-_},${ssdopts[2]-_} ${color_norm}" >&2
  1783. exit 2
  1784. fi
  1785. if [[ "${ssdopts[1]}" != "scsi" && "${ssdopts[1]}" != "nvme" ]]; then
  1786. echo -e "${color_red}Local SSD: Interface must be scsi or nvme, found: ${ssdopts[1]} ${color_norm}" >&2
  1787. exit 2
  1788. fi
  1789. if [[ "${ssdopts[2]}" != "fs" && "${ssdopts[2]}" != "block" ]]; then
  1790. echo -e "${color_red}Local SSD: Filesystem type must be fs or block, found: ${ssdopts[2]} ${color_norm}" >&2
  1791. exit 2
  1792. fi
  1793. local_ssd_ext_count=$((local_ssd_ext_count+ssdopts[0]))
  1794. if [[ "${local_ssd_ext_count}" -gt "${GCE_MAX_LOCAL_SSD}" || "${local_ssd_ext_count}" -lt 1 ]]; then
  1795. echo -e "${color_red}Local SSD: Total number of local ssds must range from 1 to 8, found: ${local_ssd_ext_count} ${color_norm}" >&2
  1796. exit 2
  1797. fi
  1798. }
  1799. # Robustly try to create an instance template.
  1800. # $1: The name of the instance template.
  1801. # $2: The scopes flag.
  1802. # $3: String of comma-separated metadata-from-file entries.
  1803. # $4: String of comma-separated metadata (key=value) entries.
  1804. # $5: the node OS ("linux" or "windows").
  1805. function create-node-template() {
  1806. detect-project
  1807. detect-subnetworks
  1808. local template_name="$1"
  1809. local metadata_values="$4"
  1810. local os="$5"
  1811. # First, ensure the template doesn't exist.
  1812. # TODO(zmerlynn): To make this really robust, we need to parse the output and
  1813. # add retries. Just relying on a non-zero exit code doesn't
  1814. # distinguish an ephemeral failed call from a "not-exists".
  1815. if gcloud compute instance-templates describe "${template_name}" --project "${PROJECT}" &>/dev/null; then
  1816. echo "Instance template ${1} already exists; deleting." >&2
  1817. if ! gcloud compute instance-templates delete "${template_name}" --project "${PROJECT}" --quiet &>/dev/null; then
  1818. echo -e "${color_yellow}Failed to delete existing instance template${color_norm}" >&2
  1819. exit 2
  1820. fi
  1821. fi
  1822. local gcloud="gcloud"
  1823. local accelerator_args=""
  1824. # VMs with Accelerators cannot be live migrated.
  1825. # More details here - https://cloud.google.com/compute/docs/gpus/add-gpus#create-new-gpu-instance
  1826. if [[ ! -z "${NODE_ACCELERATORS}" ]]; then
  1827. accelerator_args="--maintenance-policy TERMINATE --restart-on-failure --accelerator ${NODE_ACCELERATORS}"
  1828. gcloud="gcloud beta"
  1829. fi
  1830. local preemptible_minions=""
  1831. if [[ "${PREEMPTIBLE_NODE}" == "true" ]]; then
  1832. preemptible_minions="--preemptible --maintenance-policy TERMINATE"
  1833. fi
  1834. local local_ssds=""
  1835. local_ssd_ext_count=0
  1836. if [[ ! -z ${NODE_LOCAL_SSDS_EXT:-} ]]; then
  1837. IFS=";" read -r -a ssdgroups <<< "${NODE_LOCAL_SSDS_EXT:-}"
  1838. for ssdgroup in "${ssdgroups[@]}"
  1839. do
  1840. IFS="," read -r -a ssdopts <<< "${ssdgroup}"
  1841. validate-node-local-ssds-ext "${ssdopts}"
  1842. for i in $(seq ${ssdopts[0]}); do
  1843. local_ssds="$local_ssds--local-ssd=interface=${ssdopts[1]} "
  1844. done
  1845. done
  1846. fi
  1847. if [[ ! -z ${NODE_LOCAL_SSDS+x} ]]; then
  1848. # The NODE_LOCAL_SSDS check below fixes issue #49171
  1849. # Some versions of seq will count down from 1 if "seq 0" is specified
  1850. if [[ ${NODE_LOCAL_SSDS} -ge 1 ]]; then
  1851. for i in $(seq ${NODE_LOCAL_SSDS}); do
  1852. local_ssds="$local_ssds--local-ssd=interface=SCSI "
  1853. done
  1854. fi
  1855. fi
  1856. local address=""
  1857. if [[ ${GCE_PRIVATE_CLUSTER:-} == "true" ]]; then
  1858. address="no-address"
  1859. fi
  1860. local network=$(make-gcloud-network-argument \
  1861. "${NETWORK_PROJECT}" \
  1862. "${REGION}" \
  1863. "${NETWORK}" \
  1864. "${SUBNETWORK:-}" \
  1865. "${address}" \
  1866. "${ENABLE_IP_ALIASES:-}" \
  1867. "${IP_ALIAS_SIZE:-}")
  1868. local node_image_flags=""
  1869. if [[ "${os}" == 'linux' ]]; then
  1870. node_image_flags="--image-project ${NODE_IMAGE_PROJECT} --image ${NODE_IMAGE}"
  1871. elif [[ "${os}" == 'windows' ]]; then
  1872. # TODO(pjh): revert back to using WINDOWS_NODE_IMAGE_FAMILY instead of
  1873. # pinning to the v20190312 image once #76666 is resolved.
  1874. node_image_flags="--image-project ${WINDOWS_NODE_IMAGE_PROJECT} --image=windows-server-1809-dc-core-for-containers-v20190709"
  1875. else
  1876. echo "Unknown OS ${os}" >&2
  1877. exit 1
  1878. fi
  1879. local metadata_flag="${metadata_values:+--metadata ${metadata_values}}"
  1880. local attempt=1
  1881. while true; do
  1882. echo "Attempt ${attempt} to create ${1}" >&2
  1883. if ! ${gcloud} compute instance-templates create \
  1884. "${template_name}" \
  1885. --project "${PROJECT}" \
  1886. --machine-type "${NODE_SIZE}" \
  1887. --boot-disk-type "${NODE_DISK_TYPE}" \
  1888. --boot-disk-size "${NODE_DISK_SIZE}" \
  1889. ${node_image_flags} \
  1890. --service-account "${NODE_SERVICE_ACCOUNT}" \
  1891. --tags "${NODE_TAG}" \
  1892. ${accelerator_args} \
  1893. ${local_ssds} \
  1894. --region "${REGION}" \
  1895. ${network} \
  1896. ${preemptible_minions} \
  1897. $2 \
  1898. --metadata-from-file $3 \
  1899. ${metadata_flag} >&2; then
  1900. if (( attempt > 5 )); then
  1901. echo -e "${color_red}Failed to create instance template ${template_name} ${color_norm}" >&2
  1902. exit 2
  1903. fi
  1904. echo -e "${color_yellow}Attempt ${attempt} failed to create instance template ${template_name}. Retrying.${color_norm}" >&2
  1905. attempt=$(($attempt+1))
  1906. sleep $(($attempt * 5))
  1907. # In case the previous attempt failed with something like a
  1908. # Backend Error and left the entry laying around, delete it
  1909. # before we try again.
  1910. gcloud compute instance-templates delete "${template_name}" --project "${PROJECT}" &>/dev/null || true
  1911. else
  1912. break
  1913. fi
  1914. done
  1915. }
  1916. # Instantiate a kubernetes cluster
  1917. #
  1918. # Assumed vars
  1919. # KUBE_ROOT
  1920. # <Various vars set in config file>
  1921. function kube-up() {
  1922. kube::util::ensure-temp-dir
  1923. detect-project
  1924. load-or-gen-kube-basicauth
  1925. load-or-gen-kube-bearertoken
  1926. # Make sure we have the tar files staged on Google Storage
  1927. find-release-tars
  1928. upload-tars
  1929. # ensure that environmental variables specifying number of migs to create
  1930. set_num_migs
  1931. if [[ ${KUBE_USE_EXISTING_MASTER:-} == "true" ]]; then
  1932. detect-master
  1933. parse-master-env
  1934. create-subnetworks
  1935. detect-subnetworks
  1936. # Windows nodes take longer to boot and setup so create them first.
  1937. create-windows-nodes
  1938. create-linux-nodes
  1939. elif [[ ${KUBE_REPLICATE_EXISTING_MASTER:-} == "true" ]]; then
  1940. if [[ "${MASTER_OS_DISTRIBUTION}" != "gci" && "${MASTER_OS_DISTRIBUTION}" != "ubuntu" ]]; then
  1941. echo "Master replication supported only for gci and ubuntu"
  1942. return 1
  1943. fi
  1944. create-loadbalancer
  1945. # If replication of master fails, we need to ensure that the replica is removed from etcd clusters.
  1946. if ! replicate-master; then
  1947. remove-replica-from-etcd 2379 || true
  1948. remove-replica-from-etcd 4002 || true
  1949. fi
  1950. else
  1951. check-existing
  1952. create-network
  1953. create-subnetworks
  1954. detect-subnetworks
  1955. create-cloud-nat-router
  1956. write-cluster-location
  1957. write-cluster-name
  1958. create-autoscaler-config
  1959. create-master
  1960. create-nodes-firewall
  1961. create-nodes-template
  1962. # Windows nodes take longer to boot and setup so create them first.
  1963. create-windows-nodes
  1964. create-linux-nodes
  1965. check-cluster
  1966. fi
  1967. }
  1968. function check-existing() {
  1969. local running_in_terminal=false
  1970. # May be false if tty is not allocated (for example with ssh -T).
  1971. if [[ -t 1 ]]; then
  1972. running_in_terminal=true
  1973. fi
  1974. if [[ ${running_in_terminal} == "true" || ${KUBE_UP_AUTOMATIC_CLEANUP} == "true" ]]; then
  1975. if ! check-resources; then
  1976. local run_kube_down="n"
  1977. echo "${KUBE_RESOURCE_FOUND} found." >&2
  1978. # Get user input only if running in terminal.
  1979. if [[ ${running_in_terminal} == "true" && ${KUBE_UP_AUTOMATIC_CLEANUP} == "false" ]]; then
  1980. read -p "Would you like to shut down the old cluster (call kube-down)? [y/N] " run_kube_down
  1981. fi
  1982. if [[ ${run_kube_down} == "y" || ${run_kube_down} == "Y" || ${KUBE_UP_AUTOMATIC_CLEANUP} == "true" ]]; then
  1983. echo "... calling kube-down" >&2
  1984. kube-down
  1985. fi
  1986. fi
  1987. fi
  1988. }
  1989. function check-network-mode() {
  1990. local mode="$(gcloud compute networks list --filter="name=('${NETWORK}')" --project ${NETWORK_PROJECT} --format='value(x_gcloud_subnet_mode)' || true)"
  1991. # The deprecated field uses lower case. Convert to upper case for consistency.
  1992. echo "$(echo $mode | tr [a-z] [A-Z])"
  1993. }
  1994. function create-network() {
  1995. if ! gcloud compute networks --project "${NETWORK_PROJECT}" describe "${NETWORK}" &>/dev/null; then
  1996. # The network needs to be created synchronously or we have a race. The
  1997. # firewalls can be added concurrent with instance creation.
  1998. local network_mode="auto"
  1999. if [[ "${CREATE_CUSTOM_NETWORK:-}" == "true" ]]; then
  2000. network_mode="custom"
  2001. fi
  2002. echo "Creating new ${network_mode} network: ${NETWORK}"
  2003. gcloud compute networks create --project "${NETWORK_PROJECT}" "${NETWORK}" --subnet-mode="${network_mode}"
  2004. else
  2005. PREEXISTING_NETWORK=true
  2006. PREEXISTING_NETWORK_MODE="$(check-network-mode)"
  2007. echo "Found existing network ${NETWORK} in ${PREEXISTING_NETWORK_MODE} mode."
  2008. fi
  2009. if ! gcloud compute firewall-rules --project "${NETWORK_PROJECT}" describe "${CLUSTER_NAME}-default-internal-master" &>/dev/null; then
  2010. gcloud compute firewall-rules create "${CLUSTER_NAME}-default-internal-master" \
  2011. --project "${NETWORK_PROJECT}" \
  2012. --network "${NETWORK}" \
  2013. --source-ranges "10.0.0.0/8" \
  2014. --allow "tcp:1-2379,tcp:2382-65535,udp:1-65535,icmp" \
  2015. --target-tags "${MASTER_TAG}"&
  2016. fi
  2017. if ! gcloud compute firewall-rules --project "${NETWORK_PROJECT}" describe "${CLUSTER_NAME}-default-internal-node" &>/dev/null; then
  2018. gcloud compute firewall-rules create "${CLUSTER_NAME}-default-internal-node" \
  2019. --project "${NETWORK_PROJECT}" \
  2020. --network "${NETWORK}" \
  2021. --source-ranges "10.0.0.0/8" \
  2022. --allow "tcp:1-65535,udp:1-65535,icmp" \
  2023. --target-tags "${NODE_TAG}"&
  2024. fi
  2025. if ! gcloud compute firewall-rules describe --project "${NETWORK_PROJECT}" "${NETWORK}-default-ssh" &>/dev/null; then
  2026. gcloud compute firewall-rules create "${NETWORK}-default-ssh" \
  2027. --project "${NETWORK_PROJECT}" \
  2028. --network "${NETWORK}" \
  2029. --source-ranges "0.0.0.0/0" \
  2030. --allow "tcp:22" &
  2031. fi
  2032. # Open up TCP 3389 to allow RDP connections.
  2033. if [[ ${NUM_WINDOWS_NODES} -gt 0 ]]; then
  2034. if ! gcloud compute firewall-rules describe --project "${NETWORK_PROJECT}" "${NETWORK}-default-rdp" &>/dev/null; then
  2035. gcloud compute firewall-rules create "${NETWORK}-default-rdp" \
  2036. --project "${NETWORK_PROJECT}" \
  2037. --network "${NETWORK}" \
  2038. --source-ranges "0.0.0.0/0" \
  2039. --allow "tcp:3389" &
  2040. fi
  2041. fi
  2042. }
  2043. function expand-default-subnetwork() {
  2044. gcloud compute networks update "${NETWORK}" \
  2045. --switch-to-custom-subnet-mode \
  2046. --project "${NETWORK_PROJECT}" \
  2047. --quiet || true
  2048. gcloud compute networks subnets expand-ip-range "${NETWORK}" \
  2049. --region="${REGION}" \
  2050. --project "${NETWORK_PROJECT}" \
  2051. --prefix-length=19 \
  2052. --quiet
  2053. }
  2054. function create-subnetworks() {
  2055. case ${ENABLE_IP_ALIASES} in
  2056. true) echo "IP aliases are enabled. Creating subnetworks.";;
  2057. false)
  2058. echo "IP aliases are disabled."
  2059. if [[ "${ENABLE_BIG_CLUSTER_SUBNETS}" = "true" ]]; then
  2060. if [[ "${PREEXISTING_NETWORK}" != "true" ]]; then
  2061. expand-default-subnetwork
  2062. else
  2063. echo "${color_yellow}Using pre-existing network ${NETWORK}, subnets won't be expanded to /19!${color_norm}"
  2064. fi
  2065. elif [[ "${CREATE_CUSTOM_NETWORK:-}" == "true" && "${PREEXISTING_NETWORK}" != "true" ]]; then
  2066. gcloud compute networks subnets create "${SUBNETWORK}" --project "${NETWORK_PROJECT}" --region "${REGION}" --network "${NETWORK}" --range "${NODE_IP_RANGE}"
  2067. fi
  2068. return;;
  2069. *) echo "${color_red}Invalid argument to ENABLE_IP_ALIASES${color_norm}"
  2070. exit 1;;
  2071. esac
  2072. # Look for the alias subnet, it must exist and have a secondary
  2073. # range configured.
  2074. local subnet=$(gcloud compute networks subnets describe \
  2075. --project "${NETWORK_PROJECT}" \
  2076. --region ${REGION} \
  2077. ${IP_ALIAS_SUBNETWORK} 2>/dev/null)
  2078. if [[ -z ${subnet} ]]; then
  2079. echo "Creating subnet ${NETWORK}:${IP_ALIAS_SUBNETWORK}"
  2080. gcloud compute networks subnets create \
  2081. ${IP_ALIAS_SUBNETWORK} \
  2082. --description "Automatically generated subnet for ${INSTANCE_PREFIX} cluster. This will be removed on cluster teardown." \
  2083. --project "${NETWORK_PROJECT}" \
  2084. --network ${NETWORK} \
  2085. --region ${REGION} \
  2086. --range ${NODE_IP_RANGE} \
  2087. --secondary-range "pods-default=${CLUSTER_IP_RANGE}" \
  2088. --secondary-range "services-default=${SERVICE_CLUSTER_IP_RANGE}"
  2089. echo "Created subnetwork ${IP_ALIAS_SUBNETWORK}"
  2090. else
  2091. if ! echo ${subnet} | grep --quiet secondaryIpRanges; then
  2092. echo "${color_red}Subnet ${IP_ALIAS_SUBNETWORK} does not have a secondary range${color_norm}"
  2093. exit 1
  2094. fi
  2095. fi
  2096. }
  2097. # detect-subnetworks sets the SUBNETWORK var if not already set
  2098. # Assumed vars:
  2099. # NETWORK
  2100. # REGION
  2101. # NETWORK_PROJECT
  2102. #
  2103. # Optional vars:
  2104. # SUBNETWORK
  2105. # IP_ALIAS_SUBNETWORK
  2106. function detect-subnetworks() {
  2107. if [[ -n ${SUBNETWORK:-} ]]; then
  2108. echo "Using subnet ${SUBNETWORK}"
  2109. return 0
  2110. fi
  2111. if [[ -n ${IP_ALIAS_SUBNETWORK:-} ]]; then
  2112. SUBNETWORK=${IP_ALIAS_SUBNETWORK}
  2113. echo "Using IP Alias subnet ${SUBNETWORK}"
  2114. return 0
  2115. fi
  2116. SUBNETWORK=$(gcloud compute networks subnets list \
  2117. --network=${NETWORK} \
  2118. --regions=${REGION} \
  2119. --project=${NETWORK_PROJECT} \
  2120. --limit=1 \
  2121. --format='value(name)' 2>/dev/null)
  2122. if [[ -n ${SUBNETWORK:-} ]]; then
  2123. echo "Found subnet for region ${REGION} in network ${NETWORK}: ${SUBNETWORK}"
  2124. return 0
  2125. fi
  2126. echo "${color_red}Could not find subnetwork with region ${REGION}, network ${NETWORK}, and project ${NETWORK_PROJECT}"
  2127. }
  2128. # Sets up Cloud NAT for the network.
  2129. # Assumed vars:
  2130. # NETWORK_PROJECT
  2131. # REGION
  2132. # NETWORK
  2133. function create-cloud-nat-router() {
  2134. if [[ ${GCE_PRIVATE_CLUSTER:-} == "true" ]]; then
  2135. gcloud compute routers create "$NETWORK-nat-router" \
  2136. --project $NETWORK_PROJECT \
  2137. --region $REGION \
  2138. --network $NETWORK
  2139. gcloud compute routers nats create "$NETWORK-nat-config" \
  2140. --project $NETWORK_PROJECT \
  2141. --router-region $REGION \
  2142. --router "$NETWORK-nat-router" \
  2143. --nat-all-subnet-ip-ranges \
  2144. --auto-allocate-nat-external-ips
  2145. fi
  2146. }
  2147. function delete-all-firewall-rules() {
  2148. if fws=$(gcloud compute firewall-rules list --project "${NETWORK_PROJECT}" --filter="network=${NETWORK}" --format="value(name)"); then
  2149. echo "Deleting firewall rules remaining in network ${NETWORK}: ${fws}"
  2150. delete-firewall-rules "$fws"
  2151. else
  2152. echo "Failed to list firewall rules from the network ${NETWORK}"
  2153. fi
  2154. }
  2155. # Ignores firewall rule arguments that do not exist in NETWORK_PROJECT.
  2156. function delete-firewall-rules() {
  2157. for fw in $@; do
  2158. if [[ -n $(gcloud compute firewall-rules --project "${NETWORK_PROJECT}" describe "${fw}" --format='value(name)' 2>/dev/null || true) ]]; then
  2159. gcloud compute firewall-rules delete --project "${NETWORK_PROJECT}" --quiet "${fw}" &
  2160. fi
  2161. done
  2162. kube::util::wait-for-jobs || {
  2163. echo -e "${color_red}Failed to delete firewall rules.${color_norm}" >&2
  2164. }
  2165. }
  2166. function delete-network() {
  2167. if [[ -n $(gcloud compute networks --project "${NETWORK_PROJECT}" describe "${NETWORK}" --format='value(name)' 2>/dev/null || true) ]]; then
  2168. if ! gcloud compute networks delete --project "${NETWORK_PROJECT}" --quiet "${NETWORK}"; then
  2169. echo "Failed to delete network '${NETWORK}'. Listing firewall-rules:"
  2170. gcloud compute firewall-rules --project "${NETWORK_PROJECT}" list --filter="network=${NETWORK}"
  2171. return 1
  2172. fi
  2173. fi
  2174. }
  2175. function delete-cloud-nat-router() {
  2176. if [[ ${GCE_PRIVATE_CLUSTER:-} == "true" ]]; then
  2177. if [[ -n $(gcloud compute routers describe --project "${NETWORK_PROJECT}" --region "${REGION}" "${NETWORK}-nat-router" --format='value(name)' 2>/dev/null || true) ]]; then
  2178. echo "Deleting Cloud NAT router..."
  2179. gcloud compute routers delete --project "${NETWORK_PROJECT}" --region "${REGION}" --quiet "${NETWORK}-nat-router"
  2180. fi
  2181. fi
  2182. }
  2183. function delete-subnetworks() {
  2184. # If running in custom mode network we need to delete subnets manually.
  2185. mode="$(check-network-mode)"
  2186. if [[ "${mode}" == "CUSTOM" ]]; then
  2187. if [[ "${ENABLE_BIG_CLUSTER_SUBNETS}" = "true" ]]; then
  2188. echo "Deleting default subnets..."
  2189. # This value should be kept in sync with number of regions.
  2190. local parallelism=9
  2191. gcloud compute networks subnets list --network="${NETWORK}" --project "${NETWORK_PROJECT}" --format='value(region.basename())' | \
  2192. xargs -i -P ${parallelism} gcloud --quiet compute networks subnets delete "${NETWORK}" --project "${NETWORK_PROJECT}" --region="{}" || true
  2193. elif [[ "${CREATE_CUSTOM_NETWORK:-}" == "true" ]]; then
  2194. echo "Deleting custom subnet..."
  2195. gcloud --quiet compute networks subnets delete "${SUBNETWORK}" --project "${NETWORK_PROJECT}" --region="${REGION}" || true
  2196. fi
  2197. return
  2198. fi
  2199. # If we reached here, it means we're not using custom network.
  2200. # So the only thing we need to check is if IP-aliases was turned
  2201. # on and we created a subnet for it. If so, we should delete it.
  2202. if [[ ${ENABLE_IP_ALIASES:-} == "true" ]]; then
  2203. # Only delete the subnet if we created it (i.e it's not pre-existing).
  2204. if [[ -z "${KUBE_GCE_IP_ALIAS_SUBNETWORK:-}" ]]; then
  2205. echo "Removing auto-created subnet ${NETWORK}:${IP_ALIAS_SUBNETWORK}"
  2206. if [[ -n $(gcloud compute networks subnets describe \
  2207. --project "${NETWORK_PROJECT}" \
  2208. --region ${REGION} \
  2209. ${IP_ALIAS_SUBNETWORK} 2>/dev/null) ]]; then
  2210. gcloud --quiet compute networks subnets delete \
  2211. --project "${NETWORK_PROJECT}" \
  2212. --region ${REGION} \
  2213. ${IP_ALIAS_SUBNETWORK}
  2214. fi
  2215. fi
  2216. fi
  2217. }
  2218. # Generates SSL certificates for etcd cluster peer to peer communication. Uses cfssl program.
  2219. #
  2220. # Assumed vars:
  2221. # KUBE_TEMP: temporary directory
  2222. #
  2223. # Args:
  2224. # $1: host name
  2225. # $2: CA certificate
  2226. # $3: CA key
  2227. #
  2228. # If CA cert/key is empty, the function will also generate certs for CA.
  2229. #
  2230. # Vars set:
  2231. # ETCD_CA_KEY_BASE64
  2232. # ETCD_CA_CERT_BASE64
  2233. # ETCD_PEER_KEY_BASE64
  2234. # ETCD_PEER_CERT_BASE64
  2235. #
  2236. function create-etcd-certs {
  2237. local host=${1}
  2238. local ca_cert=${2:-}
  2239. local ca_key=${3:-}
  2240. GEN_ETCD_CA_CERT="${ca_cert}" GEN_ETCD_CA_KEY="${ca_key}" \
  2241. generate-etcd-cert "${KUBE_TEMP}/cfssl" "${host}" "peer" "peer"
  2242. pushd "${KUBE_TEMP}/cfssl"
  2243. ETCD_CA_KEY_BASE64=$(cat "ca-key.pem" | base64 | tr -d '\r\n')
  2244. ETCD_CA_CERT_BASE64=$(cat "ca.pem" | gzip | base64 | tr -d '\r\n')
  2245. ETCD_PEER_KEY_BASE64=$(cat "peer-key.pem" | base64 | tr -d '\r\n')
  2246. ETCD_PEER_CERT_BASE64=$(cat "peer.pem" | gzip | base64 | tr -d '\r\n')
  2247. popd
  2248. }
  2249. # Generates SSL certificates for etcd-client and kube-apiserver communication. Uses cfssl program.
  2250. #
  2251. # Assumed vars:
  2252. # KUBE_TEMP: temporary directory
  2253. #
  2254. # Args:
  2255. # $1: host server name
  2256. # $2: host client name
  2257. # $3: CA certificate
  2258. # $4: CA key
  2259. #
  2260. # If CA cert/key is empty, the function will also generate certs for CA.
  2261. #
  2262. # Vars set:
  2263. # ETCD_APISERVER_CA_KEY_BASE64
  2264. # ETCD_APISERVER_CA_CERT_BASE64
  2265. # ETCD_APISERVER_SERVER_KEY_BASE64
  2266. # ETCD_APISERVER_SERVER_CERT_BASE64
  2267. # ETCD_APISERVER_CLIENT_KEY_BASE64
  2268. # ETCD_APISERVER_CLIENT_CERT_BASE64
  2269. #
  2270. function create-etcd-apiserver-certs {
  2271. local hostServer=${1}
  2272. local hostClient=${2}
  2273. local etcd_apiserver_ca_cert=${3:-}
  2274. local etcd_apiserver_ca_key=${4:-}
  2275. GEN_ETCD_CA_CERT="${etcd_apiserver_ca_cert}" GEN_ETCD_CA_KEY="${etcd_apiserver_ca_key}" \
  2276. generate-etcd-cert "${KUBE_TEMP}/cfssl" "${hostServer}" "server" "etcd-apiserver-server"
  2277. generate-etcd-cert "${KUBE_TEMP}/cfssl" "${hostClient}" "client" "etcd-apiserver-client"
  2278. pushd "${KUBE_TEMP}/cfssl"
  2279. ETCD_APISERVER_CA_KEY_BASE64=$(cat "ca-key.pem" | base64 | tr -d '\r\n')
  2280. ETCD_APISERVER_CA_CERT_BASE64=$(cat "ca.pem" | gzip | base64 | tr -d '\r\n')
  2281. ETCD_APISERVER_SERVER_KEY_BASE64=$(cat "etcd-apiserver-server-key.pem" | base64 | tr -d '\r\n')
  2282. ETCD_APISERVER_SERVER_CERT_BASE64=$(cat "etcd-apiserver-server.pem" | gzip | base64 | tr -d '\r\n')
  2283. ETCD_APISERVER_CLIENT_KEY_BASE64=$(cat "etcd-apiserver-client-key.pem" | base64 | tr -d '\r\n')
  2284. ETCD_APISERVER_CLIENT_CERT_BASE64=$(cat "etcd-apiserver-client.pem" | gzip | base64 | tr -d '\r\n')
  2285. popd
  2286. }
  2287. function create-master() {
  2288. echo "Starting master and configuring firewalls"
  2289. gcloud compute firewall-rules create "${MASTER_NAME}-https" \
  2290. --project "${NETWORK_PROJECT}" \
  2291. --network "${NETWORK}" \
  2292. --target-tags "${MASTER_TAG}" \
  2293. --allow tcp:443 &
  2294. # We have to make sure the disk is created before creating the master VM, so
  2295. # run this in the foreground.
  2296. gcloud compute disks create "${MASTER_NAME}-pd" \
  2297. --project "${PROJECT}" \
  2298. --zone "${ZONE}" \
  2299. --type "${MASTER_DISK_TYPE}" \
  2300. --size "${MASTER_DISK_SIZE}"
  2301. # Create rule for accessing and securing etcd servers.
  2302. if ! gcloud compute firewall-rules --project "${NETWORK_PROJECT}" describe "${MASTER_NAME}-etcd" &>/dev/null; then
  2303. gcloud compute firewall-rules create "${MASTER_NAME}-etcd" \
  2304. --project "${NETWORK_PROJECT}" \
  2305. --network "${NETWORK}" \
  2306. --source-tags "${MASTER_TAG}" \
  2307. --allow "tcp:2380,tcp:2381" \
  2308. --target-tags "${MASTER_TAG}" &
  2309. fi
  2310. # Generate a bearer token for this cluster. We push this separately
  2311. # from the other cluster variables so that the client (this
  2312. # computer) can forget it later. This should disappear with
  2313. # http://issue.k8s.io/3168
  2314. 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)
  2315. if [[ "${ENABLE_NODE_PROBLEM_DETECTOR:-}" == "standalone" ]]; then
  2316. NODE_PROBLEM_DETECTOR_TOKEN=$(dd if=/dev/urandom bs=128 count=1 2>/dev/null | base64 | tr -d "=+/" | dd bs=32 count=1 2>/dev/null)
  2317. fi
  2318. # Reserve the master's IP so that it can later be transferred to another VM
  2319. # without disrupting the kubelets.
  2320. create-static-ip "${MASTER_NAME}-ip" "${REGION}"
  2321. MASTER_RESERVED_IP=$(gcloud compute addresses describe "${MASTER_NAME}-ip" \
  2322. --project "${PROJECT}" --region "${REGION}" -q --format='value(address)')
  2323. if [[ "${REGISTER_MASTER_KUBELET:-}" == "true" ]]; then
  2324. KUBELET_APISERVER="${MASTER_RESERVED_IP}"
  2325. fi
  2326. KUBERNETES_MASTER_NAME="${MASTER_RESERVED_IP}"
  2327. MASTER_ADVERTISE_ADDRESS="${MASTER_RESERVED_IP}"
  2328. create-certs "${MASTER_RESERVED_IP}"
  2329. create-etcd-certs ${MASTER_NAME}
  2330. create-etcd-apiserver-certs "etcd-${MASTER_NAME}" ${MASTER_NAME}
  2331. if [[ "$(get-num-nodes)" -ge "50" ]]; then
  2332. # We block on master creation for large clusters to avoid doing too much
  2333. # unnecessary work in case master start-up fails (like creation of nodes).
  2334. create-master-instance "${MASTER_RESERVED_IP}"
  2335. else
  2336. create-master-instance "${MASTER_RESERVED_IP}" &
  2337. fi
  2338. }
  2339. # Adds master replica to etcd cluster.
  2340. #
  2341. # Assumed vars:
  2342. # REPLICA_NAME
  2343. # PROJECT
  2344. # EXISTING_MASTER_NAME
  2345. # EXISTING_MASTER_ZONE
  2346. #
  2347. # $1: etcd client port
  2348. # $2: etcd internal port
  2349. # returns the result of ssh command which adds replica
  2350. function add-replica-to-etcd() {
  2351. local -r client_port="${1}"
  2352. local -r internal_port="${2}"
  2353. gcloud compute ssh "${EXISTING_MASTER_NAME}" \
  2354. --project "${PROJECT}" \
  2355. --zone "${EXISTING_MASTER_ZONE}" \
  2356. --command \
  2357. "curl localhost:${client_port}/v2/members -XPOST -H \"Content-Type: application/json\" -d '{\"peerURLs\":[\"https://${REPLICA_NAME}:${internal_port}\"]}' -s"
  2358. return $?
  2359. }
  2360. # Sets EXISTING_MASTER_NAME and EXISTING_MASTER_ZONE variables.
  2361. #
  2362. # Assumed vars:
  2363. # PROJECT
  2364. #
  2365. # NOTE: Must be in sync with get-replica-name-regexp
  2366. function set-existing-master() {
  2367. local existing_master=$(gcloud compute instances list \
  2368. --project "${PROJECT}" \
  2369. --filter "name ~ '$(get-replica-name-regexp)'" \
  2370. --format "value(name,zone)" | head -n1)
  2371. EXISTING_MASTER_NAME="$(echo "${existing_master}" | cut -f1)"
  2372. EXISTING_MASTER_ZONE="$(echo "${existing_master}" | cut -f2)"
  2373. }
  2374. function replicate-master() {
  2375. set-replica-name
  2376. set-existing-master
  2377. echo "Experimental: replicating existing master ${EXISTING_MASTER_ZONE}/${EXISTING_MASTER_NAME} as ${ZONE}/${REPLICA_NAME}"
  2378. # Before we do anything else, we should configure etcd to expect more replicas.
  2379. if ! add-replica-to-etcd 2379 2380; then
  2380. echo "Failed to add master replica to etcd cluster."
  2381. return 1
  2382. fi
  2383. if ! add-replica-to-etcd 4002 2381; then
  2384. echo "Failed to add master replica to etcd events cluster."
  2385. return 1
  2386. fi
  2387. # We have to make sure the disk is created before creating the master VM, so
  2388. # run this in the foreground.
  2389. gcloud compute disks create "${REPLICA_NAME}-pd" \
  2390. --project "${PROJECT}" \
  2391. --zone "${ZONE}" \
  2392. --type "${MASTER_DISK_TYPE}" \
  2393. --size "${MASTER_DISK_SIZE}"
  2394. local existing_master_replicas="$(get-all-replica-names)"
  2395. replicate-master-instance "${EXISTING_MASTER_ZONE}" "${EXISTING_MASTER_NAME}" "${existing_master_replicas}"
  2396. # Add new replica to the load balancer.
  2397. gcloud compute target-pools add-instances "${MASTER_NAME}" \
  2398. --project "${PROJECT}" \
  2399. --zone "${ZONE}" \
  2400. --instances "${REPLICA_NAME}"
  2401. }
  2402. # Detaches old and ataches new external IP to a VM.
  2403. #
  2404. # Arguments:
  2405. # $1 - VM name
  2406. # $2 - VM zone
  2407. # $3 - external static IP; if empty will use an ephemeral IP address.
  2408. function attach-external-ip() {
  2409. local NAME=${1}
  2410. local ZONE=${2}
  2411. local IP_ADDR=${3:-}
  2412. local ACCESS_CONFIG_NAME=$(gcloud compute instances describe "${NAME}" \
  2413. --project "${PROJECT}" --zone "${ZONE}" \
  2414. --format="value(networkInterfaces[0].accessConfigs[0].name)")
  2415. gcloud compute instances delete-access-config "${NAME}" \
  2416. --project "${PROJECT}" --zone "${ZONE}" \
  2417. --access-config-name "${ACCESS_CONFIG_NAME}"
  2418. if [[ -z ${IP_ADDR} ]]; then
  2419. gcloud compute instances add-access-config "${NAME}" \
  2420. --project "${PROJECT}" --zone "${ZONE}" \
  2421. --access-config-name "${ACCESS_CONFIG_NAME}"
  2422. else
  2423. gcloud compute instances add-access-config "${NAME}" \
  2424. --project "${PROJECT}" --zone "${ZONE}" \
  2425. --access-config-name "${ACCESS_CONFIG_NAME}" \
  2426. --address "${IP_ADDR}"
  2427. fi
  2428. }
  2429. # Creates load balancer in front of apiserver if it doesn't exists already. Assumes there's only one
  2430. # existing master replica.
  2431. #
  2432. # Assumes:
  2433. # PROJECT
  2434. # MASTER_NAME
  2435. # ZONE
  2436. # REGION
  2437. function create-loadbalancer() {
  2438. detect-master
  2439. # Step 0: Return early if LB is already configured.
  2440. if gcloud compute forwarding-rules describe ${MASTER_NAME} \
  2441. --project "${PROJECT}" --region ${REGION} > /dev/null 2>&1; then
  2442. echo "Load balancer already exists"
  2443. return
  2444. fi
  2445. local EXISTING_MASTER_NAME="$(get-all-replica-names)"
  2446. local EXISTING_MASTER_ZONE=$(gcloud compute instances list "${EXISTING_MASTER_NAME}" \
  2447. --project "${PROJECT}" --format="value(zone)")
  2448. echo "Creating load balancer in front of an already existing master in ${EXISTING_MASTER_ZONE}"
  2449. # Step 1: Detach master IP address and attach ephemeral address to the existing master
  2450. attach-external-ip "${EXISTING_MASTER_NAME}" "${EXISTING_MASTER_ZONE}"
  2451. # Step 2: Create target pool.
  2452. gcloud compute target-pools create "${MASTER_NAME}" --project "${PROJECT}" --region "${REGION}"
  2453. # TODO: We should also add master instances with suffixes
  2454. gcloud compute target-pools add-instances "${MASTER_NAME}" --instances "${EXISTING_MASTER_NAME}" --project "${PROJECT}" --zone "${EXISTING_MASTER_ZONE}"
  2455. # Step 3: Create forwarding rule.
  2456. # TODO: This step can take up to 20 min. We need to speed this up...
  2457. gcloud compute forwarding-rules create ${MASTER_NAME} \
  2458. --project "${PROJECT}" --region ${REGION} \
  2459. --target-pool ${MASTER_NAME} --address=${KUBE_MASTER_IP} --ports=443
  2460. echo -n "Waiting for the load balancer configuration to propagate..."
  2461. local counter=0
  2462. until $(curl -k -m1 https://${KUBE_MASTER_IP} &> /dev/null); do
  2463. counter=$((counter+1))
  2464. echo -n .
  2465. if [[ ${counter} -ge 1800 ]]; then
  2466. echo -e "${color_red}TIMEOUT${color_norm}" >&2
  2467. echo -e "${color_red}Load balancer failed to initialize within ${counter} seconds.${color_norm}" >&2
  2468. exit 2
  2469. fi
  2470. done
  2471. echo "DONE"
  2472. }
  2473. function create-nodes-firewall() {
  2474. # Create a single firewall rule for all minions.
  2475. create-firewall-rule "${NODE_TAG}-all" "${CLUSTER_IP_RANGE}" "${NODE_TAG}" &
  2476. # Report logging choice (if any).
  2477. if [[ "${ENABLE_NODE_LOGGING-}" == "true" ]]; then
  2478. echo "+++ Logging using Fluentd to ${LOGGING_DESTINATION:-unknown}"
  2479. fi
  2480. # Wait for last batch of jobs
  2481. kube::util::wait-for-jobs || {
  2482. echo -e "${color_red}Some commands failed.${color_norm}" >&2
  2483. }
  2484. }
  2485. function get-scope-flags() {
  2486. local scope_flags=
  2487. if [[ -n "${NODE_SCOPES}" ]]; then
  2488. scope_flags="--scopes ${NODE_SCOPES}"
  2489. else
  2490. scope_flags="--no-scopes"
  2491. fi
  2492. echo "${scope_flags}"
  2493. }
  2494. function create-nodes-template() {
  2495. echo "Creating nodes."
  2496. local scope_flags=$(get-scope-flags)
  2497. write-linux-node-env
  2498. write-windows-node-env
  2499. # NOTE: these template names and their format must match
  2500. # create-[linux,windows]-nodes() as well as get-template()!
  2501. local linux_template_name="${NODE_INSTANCE_PREFIX}-template"
  2502. local windows_template_name="${WINDOWS_NODE_INSTANCE_PREFIX}-template"
  2503. create-linux-node-instance-template $linux_template_name
  2504. create-windows-node-instance-template $windows_template_name "${scope_flags[*]}"
  2505. }
  2506. # Assumes:
  2507. # - MAX_INSTANCES_PER_MIG
  2508. # - NUM_NODES
  2509. # - NUM_WINDOWS_NODES
  2510. # exports:
  2511. # - NUM_MIGS
  2512. # - NUM_WINDOWS_MIGS
  2513. function set_num_migs() {
  2514. local defaulted_max_instances_per_mig=${MAX_INSTANCES_PER_MIG:-1000}
  2515. if [[ ${defaulted_max_instances_per_mig} -le "0" ]]; then
  2516. echo "MAX_INSTANCES_PER_MIG cannot be negative. Assuming default 1000"
  2517. defaulted_max_instances_per_mig=1000
  2518. fi
  2519. export NUM_MIGS=$(((${NUM_NODES} + ${defaulted_max_instances_per_mig} - 1) / ${defaulted_max_instances_per_mig}))
  2520. export NUM_WINDOWS_MIGS=$(((${NUM_WINDOWS_NODES} + ${defaulted_max_instances_per_mig} - 1) / ${defaulted_max_instances_per_mig}))
  2521. }
  2522. # Assumes:
  2523. # - NUM_MIGS
  2524. # - NODE_INSTANCE_PREFIX
  2525. # - NUM_NODES
  2526. # - PROJECT
  2527. # - ZONE
  2528. function create-linux-nodes() {
  2529. local template_name="${NODE_INSTANCE_PREFIX}-template"
  2530. if [[ -z "${HEAPSTER_MACHINE_TYPE:-}" ]]; then
  2531. local -r nodes="${NUM_NODES}"
  2532. else
  2533. echo "Creating a special node for heapster with machine-type ${HEAPSTER_MACHINE_TYPE}"
  2534. create-heapster-node
  2535. local -r nodes=$(( NUM_NODES - 1 ))
  2536. fi
  2537. local instances_left=${nodes}
  2538. for ((i=1; i<=${NUM_MIGS}; i++)); do
  2539. local group_name="${NODE_INSTANCE_PREFIX}-group-$i"
  2540. if [[ $i == ${NUM_MIGS} ]]; then
  2541. # TODO: We don't add a suffix for the last group to keep backward compatibility when there's only one MIG.
  2542. # We should change it at some point, but note #18545 when changing this.
  2543. group_name="${NODE_INSTANCE_PREFIX}-group"
  2544. fi
  2545. # Spread the remaining number of nodes evenly
  2546. this_mig_size=$((${instances_left} / (${NUM_MIGS}-${i}+1)))
  2547. instances_left=$((instances_left-${this_mig_size}))
  2548. gcloud compute instance-groups managed \
  2549. create "${group_name}" \
  2550. --project "${PROJECT}" \
  2551. --zone "${ZONE}" \
  2552. --base-instance-name "${group_name}" \
  2553. --size "${this_mig_size}" \
  2554. --template "${template_name}" || true;
  2555. gcloud compute instance-groups managed wait-until-stable \
  2556. "${group_name}" \
  2557. --zone "${ZONE}" \
  2558. --project "${PROJECT}" \
  2559. --timeout "${MIG_WAIT_UNTIL_STABLE_TIMEOUT}" || true &
  2560. done
  2561. wait
  2562. }
  2563. # Assumes:
  2564. # - NUM_WINDOWS_MIGS
  2565. # - WINDOWS_NODE_INSTANCE_PREFIX
  2566. # - NUM_WINDOWS_NODES
  2567. # - PROJECT
  2568. # - ZONE
  2569. function create-windows-nodes() {
  2570. local template_name="${WINDOWS_NODE_INSTANCE_PREFIX}-template"
  2571. local -r nodes="${NUM_WINDOWS_NODES}"
  2572. local instances_left=${nodes}
  2573. for ((i=1; i<=${NUM_WINDOWS_MIGS}; i++)); do
  2574. local group_name="${WINDOWS_NODE_INSTANCE_PREFIX}-group-$i"
  2575. if [[ $i == ${NUM_WINDOWS_MIGS} ]]; then
  2576. # TODO: We don't add a suffix for the last group to keep backward compatibility when there's only one MIG.
  2577. # We should change it at some point, but note #18545 when changing this.
  2578. group_name="${WINDOWS_NODE_INSTANCE_PREFIX}-group"
  2579. fi
  2580. # Spread the remaining number of nodes evenly
  2581. this_mig_size=$((${instances_left} / (${NUM_WINDOWS_MIGS}-${i}+1)))
  2582. instances_left=$((instances_left-${this_mig_size}))
  2583. gcloud compute instance-groups managed \
  2584. create "${group_name}" \
  2585. --project "${PROJECT}" \
  2586. --zone "${ZONE}" \
  2587. --base-instance-name "${group_name}" \
  2588. --size "${this_mig_size}" \
  2589. --template "${template_name}" || true;
  2590. gcloud compute instance-groups managed wait-until-stable \
  2591. "${group_name}" \
  2592. --zone "${ZONE}" \
  2593. --project "${PROJECT}" \
  2594. --timeout "${MIG_WAIT_UNTIL_STABLE_TIMEOUT}" || true;
  2595. done
  2596. }
  2597. # Assumes:
  2598. # - NODE_INSTANCE_PREFIX
  2599. # - PROJECT
  2600. # - NETWORK_PROJECT
  2601. # - REGION
  2602. # - ZONE
  2603. # - HEAPSTER_MACHINE_TYPE
  2604. # - NODE_DISK_TYPE
  2605. # - NODE_DISK_SIZE
  2606. # - NODE_IMAGE_PROJECT
  2607. # - NODE_IMAGE
  2608. # - NODE_SERVICE_ACCOUNT
  2609. # - NODE_TAG
  2610. # - NETWORK
  2611. # - ENABLE_IP_ALIASES
  2612. # - SUBNETWORK
  2613. # - IP_ALIAS_SIZE
  2614. function create-heapster-node() {
  2615. local gcloud="gcloud"
  2616. local network=$(make-gcloud-network-argument \
  2617. "${NETWORK_PROJECT}" \
  2618. "${REGION}" \
  2619. "${NETWORK}" \
  2620. "${SUBNETWORK:-}" \
  2621. "" \
  2622. "${ENABLE_IP_ALIASES:-}" \
  2623. "${IP_ALIAS_SIZE:-}")
  2624. ${gcloud} compute instances \
  2625. create "${NODE_INSTANCE_PREFIX}-heapster" \
  2626. --project "${PROJECT}" \
  2627. --zone "${ZONE}" \
  2628. --machine-type="${HEAPSTER_MACHINE_TYPE}" \
  2629. --boot-disk-type "${NODE_DISK_TYPE}" \
  2630. --boot-disk-size "${NODE_DISK_SIZE}" \
  2631. --image-project="${NODE_IMAGE_PROJECT}" \
  2632. --image "${NODE_IMAGE}" \
  2633. --service-account "${NODE_SERVICE_ACCOUNT}" \
  2634. --tags "${NODE_TAG}" \
  2635. ${network} \
  2636. $(get-scope-flags) \
  2637. --metadata-from-file "$(get-node-instance-metadata-from-file)"
  2638. }
  2639. # Assumes:
  2640. # - NUM_MIGS
  2641. # - NODE_INSTANCE_PREFIX
  2642. # - PROJECT
  2643. # - ZONE
  2644. # - AUTOSCALER_MAX_NODES
  2645. # - AUTOSCALER_MIN_NODES
  2646. # Exports
  2647. # - AUTOSCALER_MIG_CONFIG
  2648. function create-cluster-autoscaler-mig-config() {
  2649. # Each MIG must have at least one node, so the min number of nodes
  2650. # must be greater or equal to the number of migs.
  2651. if [[ ${AUTOSCALER_MIN_NODES} -lt 0 ]]; then
  2652. echo "AUTOSCALER_MIN_NODES must be greater or equal 0"
  2653. exit 2
  2654. fi
  2655. # Each MIG must have at least one node, so the min number of nodes
  2656. # must be greater or equal to the number of migs.
  2657. if [[ ${AUTOSCALER_MAX_NODES} -lt ${NUM_MIGS} ]]; then
  2658. echo "AUTOSCALER_MAX_NODES must be greater or equal ${NUM_MIGS}"
  2659. exit 2
  2660. fi
  2661. if [[ ${NUM_WINDOWS_MIGS} -gt 0 ]]; then
  2662. # TODO(pjh): implement Windows support in this function.
  2663. echo "Not implemented yet: autoscaler config for Windows MIGs"
  2664. exit 2
  2665. fi
  2666. # The code assumes that the migs were created with create-nodes
  2667. # function which tries to evenly spread nodes across the migs.
  2668. AUTOSCALER_MIG_CONFIG=""
  2669. local left_min=${AUTOSCALER_MIN_NODES}
  2670. local left_max=${AUTOSCALER_MAX_NODES}
  2671. for ((i=1; i<=${NUM_MIGS}; i++)); do
  2672. local group_name="${NODE_INSTANCE_PREFIX}-group-$i"
  2673. if [[ $i == ${NUM_MIGS} ]]; then
  2674. # TODO: We don't add a suffix for the last group to keep backward compatibility when there's only one MIG.
  2675. # We should change it at some point, but note #18545 when changing this.
  2676. group_name="${NODE_INSTANCE_PREFIX}-group"
  2677. fi
  2678. this_mig_min=$((${left_min}/(${NUM_MIGS}-${i}+1)))
  2679. this_mig_max=$((${left_max}/(${NUM_MIGS}-${i}+1)))
  2680. left_min=$((left_min-$this_mig_min))
  2681. left_max=$((left_max-$this_mig_max))
  2682. local mig_url="https://www.googleapis.com/compute/v1/projects/${PROJECT}/zones/${ZONE}/instanceGroups/${group_name}"
  2683. AUTOSCALER_MIG_CONFIG="${AUTOSCALER_MIG_CONFIG} --nodes=${this_mig_min}:${this_mig_max}:${mig_url}"
  2684. done
  2685. AUTOSCALER_MIG_CONFIG="${AUTOSCALER_MIG_CONFIG} --scale-down-enabled=${AUTOSCALER_ENABLE_SCALE_DOWN}"
  2686. }
  2687. # Assumes:
  2688. # - NUM_MIGS
  2689. # - NODE_INSTANCE_PREFIX
  2690. # - PROJECT
  2691. # - ZONE
  2692. # - ENABLE_CLUSTER_AUTOSCALER
  2693. # - AUTOSCALER_MAX_NODES
  2694. # - AUTOSCALER_MIN_NODES
  2695. function create-autoscaler-config() {
  2696. # Create autoscaler for nodes configuration if requested
  2697. if [[ "${ENABLE_CLUSTER_AUTOSCALER}" == "true" ]]; then
  2698. create-cluster-autoscaler-mig-config
  2699. echo "Using autoscaler config: ${AUTOSCALER_MIG_CONFIG} ${AUTOSCALER_EXPANDER_CONFIG}"
  2700. fi
  2701. }
  2702. function check-cluster() {
  2703. detect-node-names
  2704. detect-master
  2705. echo "Waiting up to ${KUBE_CLUSTER_INITIALIZATION_TIMEOUT} seconds for cluster initialization."
  2706. echo
  2707. echo " This will continually check to see if the API for kubernetes is reachable."
  2708. echo " This may time out if there was some uncaught error during start up."
  2709. echo
  2710. # curl in mavericks is borked.
  2711. secure=""
  2712. if which sw_vers >& /dev/null; then
  2713. if [[ $(sw_vers | grep ProductVersion | awk '{print $2}') = "10.9."* ]]; then
  2714. secure="--insecure"
  2715. fi
  2716. fi
  2717. local start_time=$(date +%s)
  2718. local curl_out=$(mktemp)
  2719. kube::util::trap_add "rm -f ${curl_out}" EXIT
  2720. until curl --cacert "${CERT_DIR}/pki/ca.crt" \
  2721. -H "Authorization: Bearer ${KUBE_BEARER_TOKEN}" \
  2722. ${secure} \
  2723. --max-time 5 --fail \
  2724. "https://${KUBE_MASTER_IP}/api/v1/pods?limit=100" > "${curl_out}" 2>&1; do
  2725. local elapsed=$(($(date +%s) - ${start_time}))
  2726. if [[ ${elapsed} -gt ${KUBE_CLUSTER_INITIALIZATION_TIMEOUT} ]]; then
  2727. echo -e "${color_red}Cluster failed to initialize within ${KUBE_CLUSTER_INITIALIZATION_TIMEOUT} seconds.${color_norm}" >&2
  2728. echo "Last output from querying API server follows:" >&2
  2729. echo "-----------------------------------------------------" >&2
  2730. cat "${curl_out}" >&2
  2731. echo "-----------------------------------------------------" >&2
  2732. exit 2
  2733. fi
  2734. printf "."
  2735. sleep 2
  2736. done
  2737. echo "Kubernetes cluster created."
  2738. export KUBE_CERT="${CERT_DIR}/pki/issued/kubecfg.crt"
  2739. export KUBE_KEY="${CERT_DIR}/pki/private/kubecfg.key"
  2740. export CA_CERT="${CERT_DIR}/pki/ca.crt"
  2741. export CONTEXT="${PROJECT}_${INSTANCE_PREFIX}"
  2742. (
  2743. umask 077
  2744. # Update the user's kubeconfig to include credentials for this apiserver.
  2745. create-kubeconfig
  2746. )
  2747. # ensures KUBECONFIG is set
  2748. get-kubeconfig-basicauth
  2749. if [[ ${GCE_UPLOAD_KUBCONFIG_TO_MASTER_METADATA:-} == "true" ]]; then
  2750. gcloud compute instances add-metadata "${MASTER_NAME}" --zone="${ZONE}" --metadata-from-file="kubeconfig=${KUBECONFIG}" || true
  2751. fi
  2752. echo
  2753. echo -e "${color_green}Kubernetes cluster is running. The master is running at:"
  2754. echo
  2755. echo -e "${color_yellow} https://${KUBE_MASTER_IP}"
  2756. echo
  2757. echo -e "${color_green}The user name and password to use is located in ${KUBECONFIG}.${color_norm}"
  2758. echo
  2759. }
  2760. # Removes master replica from etcd cluster.
  2761. #
  2762. # Assumed vars:
  2763. # REPLICA_NAME
  2764. # PROJECT
  2765. # EXISTING_MASTER_NAME
  2766. # EXISTING_MASTER_ZONE
  2767. #
  2768. # $1: etcd client port
  2769. # returns the result of ssh command which removes replica
  2770. function remove-replica-from-etcd() {
  2771. local -r port="${1}"
  2772. [[ -n "${EXISTING_MASTER_NAME}" ]] || return
  2773. gcloud compute ssh "${EXISTING_MASTER_NAME}" \
  2774. --project "${PROJECT}" \
  2775. --zone "${EXISTING_MASTER_ZONE}" \
  2776. --command \
  2777. "curl -s localhost:${port}/v2/members/\$(curl -s localhost:${port}/v2/members -XGET | sed 's/{\\\"id/\n/g' | grep ${REPLICA_NAME}\\\" | cut -f 3 -d \\\") -XDELETE -L 2>/dev/null"
  2778. local -r res=$?
  2779. echo "Removing etcd replica, name: ${REPLICA_NAME}, port: ${port}, result: ${res}"
  2780. return "${res}"
  2781. }
  2782. # Delete a kubernetes cluster. This is called from test-teardown.
  2783. #
  2784. # Assumed vars:
  2785. # MASTER_NAME
  2786. # NODE_INSTANCE_PREFIX
  2787. # WINDOWS_NODE_INSTANCE_PREFIX
  2788. # ZONE
  2789. # This function tears down cluster resources 10 at a time to avoid issuing too many
  2790. # API calls and exceeding API quota. It is important to bring down the instances before bringing
  2791. # down the firewall rules and routes.
  2792. function kube-down() {
  2793. local -r batch=200
  2794. detect-project
  2795. detect-node-names # For INSTANCE_GROUPS and WINDOWS_INSTANCE_GROUPS
  2796. echo "Bringing down cluster"
  2797. set +e # Do not stop on error
  2798. if [[ "${KUBE_DELETE_NODES:-}" != "false" ]]; then
  2799. # Get the name of the managed instance group template before we delete the
  2800. # managed instance group. (The name of the managed instance group template may
  2801. # change during a cluster upgrade.)
  2802. local templates=$(get-template "${PROJECT}")
  2803. local all_instance_groups=(${INSTANCE_GROUPS[@]:-} ${WINDOWS_INSTANCE_GROUPS[@]:-})
  2804. for group in ${all_instance_groups[@]:-}; do
  2805. if gcloud compute instance-groups managed describe "${group}" --project "${PROJECT}" --zone "${ZONE}" &>/dev/null; then
  2806. gcloud compute instance-groups managed delete \
  2807. --project "${PROJECT}" \
  2808. --quiet \
  2809. --zone "${ZONE}" \
  2810. "${group}" &
  2811. fi
  2812. done
  2813. # Wait for last batch of jobs
  2814. kube::util::wait-for-jobs || {
  2815. echo -e "Failed to delete instance group(s)." >&2
  2816. }
  2817. for template in ${templates[@]:-}; do
  2818. if gcloud compute instance-templates describe --project "${PROJECT}" "${template}" &>/dev/null; then
  2819. gcloud compute instance-templates delete \
  2820. --project "${PROJECT}" \
  2821. --quiet \
  2822. "${template}"
  2823. fi
  2824. done
  2825. # Delete the special heapster node (if it exists).
  2826. if [[ -n "${HEAPSTER_MACHINE_TYPE:-}" ]]; then
  2827. local -r heapster_machine_name="${NODE_INSTANCE_PREFIX}-heapster"
  2828. if gcloud compute instances describe "${heapster_machine_name}" --zone "${ZONE}" --project "${PROJECT}" &>/dev/null; then
  2829. # Now we can safely delete the VM.
  2830. gcloud compute instances delete \
  2831. --project "${PROJECT}" \
  2832. --quiet \
  2833. --delete-disks all \
  2834. --zone "${ZONE}" \
  2835. "${heapster_machine_name}"
  2836. fi
  2837. fi
  2838. fi
  2839. local -r REPLICA_NAME="${KUBE_REPLICA_NAME:-$(get-replica-name)}"
  2840. set-existing-master
  2841. # Un-register the master replica from etcd and events etcd.
  2842. remove-replica-from-etcd 2379
  2843. remove-replica-from-etcd 4002
  2844. # Delete the master replica (if it exists).
  2845. if gcloud compute instances describe "${REPLICA_NAME}" --zone "${ZONE}" --project "${PROJECT}" &>/dev/null; then
  2846. # If there is a load balancer in front of apiservers we need to first update its configuration.
  2847. if gcloud compute target-pools describe "${MASTER_NAME}" --region "${REGION}" --project "${PROJECT}" &>/dev/null; then
  2848. gcloud compute target-pools remove-instances "${MASTER_NAME}" \
  2849. --project "${PROJECT}" \
  2850. --zone "${ZONE}" \
  2851. --instances "${REPLICA_NAME}"
  2852. fi
  2853. # Now we can safely delete the VM.
  2854. gcloud compute instances delete \
  2855. --project "${PROJECT}" \
  2856. --quiet \
  2857. --delete-disks all \
  2858. --zone "${ZONE}" \
  2859. "${REPLICA_NAME}"
  2860. fi
  2861. # Delete the master replica pd (possibly leaked by kube-up if master create failed).
  2862. # TODO(jszczepkowski): remove also possibly leaked replicas' pds
  2863. local -r replica_pd="${REPLICA_NAME:-${MASTER_NAME}}-pd"
  2864. if gcloud compute disks describe "${replica_pd}" --zone "${ZONE}" --project "${PROJECT}" &>/dev/null; then
  2865. gcloud compute disks delete \
  2866. --project "${PROJECT}" \
  2867. --quiet \
  2868. --zone "${ZONE}" \
  2869. "${replica_pd}"
  2870. fi
  2871. # Check if this are any remaining master replicas.
  2872. local REMAINING_MASTER_COUNT=$(gcloud compute instances list \
  2873. --project "${PROJECT}" \
  2874. --filter="name ~ '$(get-replica-name-regexp)'" \
  2875. --format "value(zone)" | wc -l)
  2876. # In the replicated scenario, if there's only a single master left, we should also delete load balancer in front of it.
  2877. if [[ "${REMAINING_MASTER_COUNT}" -eq 1 ]]; then
  2878. if gcloud compute forwarding-rules describe "${MASTER_NAME}" --region "${REGION}" --project "${PROJECT}" &>/dev/null; then
  2879. detect-master
  2880. local REMAINING_REPLICA_NAME="$(get-all-replica-names)"
  2881. local REMAINING_REPLICA_ZONE=$(gcloud compute instances list "${REMAINING_REPLICA_NAME}" \
  2882. --project "${PROJECT}" --format="value(zone)")
  2883. gcloud compute forwarding-rules delete \
  2884. --project "${PROJECT}" \
  2885. --region "${REGION}" \
  2886. --quiet \
  2887. "${MASTER_NAME}"
  2888. attach-external-ip "${REMAINING_REPLICA_NAME}" "${REMAINING_REPLICA_ZONE}" "${KUBE_MASTER_IP}"
  2889. gcloud compute target-pools delete \
  2890. --project "${PROJECT}" \
  2891. --region "${REGION}" \
  2892. --quiet \
  2893. "${MASTER_NAME}"
  2894. fi
  2895. fi
  2896. # If there are no more remaining master replicas, we should delete all remaining network resources.
  2897. if [[ "${REMAINING_MASTER_COUNT}" -eq 0 ]]; then
  2898. # Delete firewall rule for the master, etcd servers, and nodes.
  2899. delete-firewall-rules "${MASTER_NAME}-https" "${MASTER_NAME}-etcd" "${NODE_TAG}-all"
  2900. # Delete the master's reserved IP
  2901. if gcloud compute addresses describe "${MASTER_NAME}-ip" --region "${REGION}" --project "${PROJECT}" &>/dev/null; then
  2902. gcloud compute addresses delete \
  2903. --project "${PROJECT}" \
  2904. --region "${REGION}" \
  2905. --quiet \
  2906. "${MASTER_NAME}-ip"
  2907. fi
  2908. fi
  2909. if [[ "${KUBE_DELETE_NODES:-}" != "false" ]]; then
  2910. # Find out what minions are running.
  2911. local -a minions
  2912. minions=( $(gcloud compute instances list \
  2913. --project "${PROJECT}" \
  2914. --filter="(name ~ '${NODE_INSTANCE_PREFIX}-.+' OR name ~ '${WINDOWS_NODE_INSTANCE_PREFIX}-.+') AND zone:(${ZONE})" \
  2915. --format='value(name)') )
  2916. # If any minions are running, delete them in batches.
  2917. while (( "${#minions[@]}" > 0 )); do
  2918. echo Deleting nodes "${minions[*]::${batch}}"
  2919. gcloud compute instances delete \
  2920. --project "${PROJECT}" \
  2921. --quiet \
  2922. --delete-disks boot \
  2923. --zone "${ZONE}" \
  2924. "${minions[@]::${batch}}"
  2925. minions=( "${minions[@]:${batch}}" )
  2926. done
  2927. fi
  2928. # If there are no more remaining master replicas: delete routes, pd for influxdb and update kubeconfig
  2929. if [[ "${REMAINING_MASTER_COUNT}" -eq 0 ]]; then
  2930. # Delete routes.
  2931. local -a routes
  2932. # Clean up all routes w/ names like "<cluster-name>-<node-GUID>"
  2933. # e.g. "kubernetes-12345678-90ab-cdef-1234-567890abcdef". The name is
  2934. # determined by the node controller on the master.
  2935. # Note that this is currently a noop, as synchronously deleting the node MIG
  2936. # first allows the master to cleanup routes itself.
  2937. local TRUNCATED_PREFIX="${INSTANCE_PREFIX:0:26}"
  2938. routes=( $(gcloud compute routes list --project "${NETWORK_PROJECT}" \
  2939. --filter="name ~ '${TRUNCATED_PREFIX}-.{8}-.{4}-.{4}-.{4}-.{12}'" \
  2940. --format='value(name)') )
  2941. while (( "${#routes[@]}" > 0 )); do
  2942. echo Deleting routes "${routes[*]::${batch}}"
  2943. gcloud compute routes delete \
  2944. --project "${NETWORK_PROJECT}" \
  2945. --quiet \
  2946. "${routes[@]::${batch}}"
  2947. routes=( "${routes[@]:${batch}}" )
  2948. done
  2949. # Delete persistent disk for influx-db.
  2950. if gcloud compute disks describe "${INSTANCE_PREFIX}"-influxdb-pd --zone "${ZONE}" --project "${PROJECT}" &>/dev/null; then
  2951. gcloud compute disks delete \
  2952. --project "${PROJECT}" \
  2953. --quiet \
  2954. --zone "${ZONE}" \
  2955. "${INSTANCE_PREFIX}"-influxdb-pd
  2956. fi
  2957. # Delete all remaining firewall rules and network.
  2958. delete-firewall-rules \
  2959. "${CLUSTER_NAME}-default-internal-master" \
  2960. "${CLUSTER_NAME}-default-internal-node" \
  2961. "${NETWORK}-default-ssh" \
  2962. "${NETWORK}-default-rdp" \
  2963. "${NETWORK}-default-internal" # Pre-1.5 clusters
  2964. if [[ "${KUBE_DELETE_NETWORK}" == "true" ]]; then
  2965. delete-cloud-nat-router
  2966. # Delete all remaining firewall rules in the network.
  2967. delete-all-firewall-rules || true
  2968. delete-subnetworks || true
  2969. delete-network || true # might fail if there are leaked resources that reference the network
  2970. fi
  2971. # If there are no more remaining master replicas, we should update kubeconfig.
  2972. export CONTEXT="${PROJECT}_${INSTANCE_PREFIX}"
  2973. clear-kubeconfig
  2974. else
  2975. # If some master replicas remain: cluster has been changed, we need to re-validate it.
  2976. echo "... calling validate-cluster" >&2
  2977. # Override errexit
  2978. (validate-cluster) && validate_result="$?" || validate_result="$?"
  2979. # We have two different failure modes from validate cluster:
  2980. # - 1: fatal error - cluster won't be working correctly
  2981. # - 2: weak error - something went wrong, but cluster probably will be working correctly
  2982. # We just print an error message in case 2).
  2983. if [[ "${validate_result}" -eq 1 ]]; then
  2984. exit 1
  2985. elif [[ "${validate_result}" -eq 2 ]]; then
  2986. echo "...ignoring non-fatal errors in validate-cluster" >&2
  2987. fi
  2988. fi
  2989. set -e
  2990. }
  2991. # Prints name of one of the master replicas in the current zone. It will be either
  2992. # just MASTER_NAME or MASTER_NAME with a suffix for a replica (see get-replica-name-regexp).
  2993. #
  2994. # Assumed vars:
  2995. # PROJECT
  2996. # ZONE
  2997. # MASTER_NAME
  2998. #
  2999. # NOTE: Must be in sync with get-replica-name-regexp and set-replica-name.
  3000. function get-replica-name() {
  3001. echo $(gcloud compute instances list \
  3002. --project "${PROJECT}" \
  3003. --filter="name ~ '$(get-replica-name-regexp)' AND zone:(${ZONE})" \
  3004. --format "value(name)" | head -n1)
  3005. }
  3006. # Prints comma-separated names of all of the master replicas in all zones.
  3007. #
  3008. # Assumed vars:
  3009. # PROJECT
  3010. # MASTER_NAME
  3011. #
  3012. # NOTE: Must be in sync with get-replica-name-regexp and set-replica-name.
  3013. function get-all-replica-names() {
  3014. echo $(gcloud compute instances list \
  3015. --project "${PROJECT}" \
  3016. --filter="name ~ '$(get-replica-name-regexp)'" \
  3017. --format "value(name)" | tr "\n" "," | sed 's/,$//')
  3018. }
  3019. # Prints the number of all of the master replicas in all zones.
  3020. #
  3021. # Assumed vars:
  3022. # MASTER_NAME
  3023. function get-master-replicas-count() {
  3024. detect-project
  3025. local num_masters=$(gcloud compute instances list \
  3026. --project "${PROJECT}" \
  3027. --filter="name ~ '$(get-replica-name-regexp)'" \
  3028. --format "value(zone)" | wc -l)
  3029. echo -n "${num_masters}"
  3030. }
  3031. # Prints regexp for full master machine name. In a cluster with replicated master,
  3032. # VM names may either be MASTER_NAME or MASTER_NAME with a suffix for a replica.
  3033. function get-replica-name-regexp() {
  3034. echo "^${MASTER_NAME}(-...)?"
  3035. }
  3036. # Sets REPLICA_NAME to a unique name for a master replica that will match
  3037. # expected regexp (see get-replica-name-regexp).
  3038. #
  3039. # Assumed vars:
  3040. # PROJECT
  3041. # ZONE
  3042. # MASTER_NAME
  3043. #
  3044. # Sets:
  3045. # REPLICA_NAME
  3046. function set-replica-name() {
  3047. local instances=$(gcloud compute instances list \
  3048. --project "${PROJECT}" \
  3049. --filter="name ~ '$(get-replica-name-regexp)'" \
  3050. --format "value(name)")
  3051. suffix=""
  3052. while echo "${instances}" | grep "${suffix}" &>/dev/null; do
  3053. suffix="$(date | md5sum | head -c3)"
  3054. done
  3055. REPLICA_NAME="${MASTER_NAME}-${suffix}"
  3056. }
  3057. # Gets the instance templates in use by the cluster. It echos the template names
  3058. # so that the function output can be used.
  3059. # Assumed vars:
  3060. # NODE_INSTANCE_PREFIX
  3061. # WINDOWS_NODE_INSTANCE_PREFIX
  3062. #
  3063. # $1: project
  3064. function get-template() {
  3065. local linux_filter="${NODE_INSTANCE_PREFIX}-template(-(${KUBE_RELEASE_VERSION_DASHED_REGEX}|${KUBE_CI_VERSION_DASHED_REGEX}))?"
  3066. local windows_filter="${WINDOWS_NODE_INSTANCE_PREFIX}-template(-(${KUBE_RELEASE_VERSION_DASHED_REGEX}|${KUBE_CI_VERSION_DASHED_REGEX}))?"
  3067. gcloud compute instance-templates list \
  3068. --filter="name ~ '${linux_filter}' OR name ~ '${windows_filter}'" \
  3069. --project="${1}" --format='value(name)'
  3070. }
  3071. # Checks if there are any present resources related kubernetes cluster.
  3072. #
  3073. # Assumed vars:
  3074. # MASTER_NAME
  3075. # NODE_INSTANCE_PREFIX
  3076. # WINDOWS_NODE_INSTANCE_PREFIX
  3077. # ZONE
  3078. # REGION
  3079. # Vars set:
  3080. # KUBE_RESOURCE_FOUND
  3081. function check-resources() {
  3082. detect-project
  3083. detect-node-names
  3084. echo "Looking for already existing resources"
  3085. KUBE_RESOURCE_FOUND=""
  3086. if [[ -n "${INSTANCE_GROUPS[@]:-}" ]]; then
  3087. KUBE_RESOURCE_FOUND="Managed instance groups ${INSTANCE_GROUPS[@]}"
  3088. return 1
  3089. fi
  3090. if [[ -n "${WINDOWS_INSTANCE_GROUPS[@]:-}" ]]; then
  3091. KUBE_RESOURCE_FOUND="Managed instance groups ${WINDOWS_INSTANCE_GROUPS[@]}"
  3092. return 1
  3093. fi
  3094. if gcloud compute instance-templates describe --project "${PROJECT}" "${NODE_INSTANCE_PREFIX}-template" &>/dev/null; then
  3095. KUBE_RESOURCE_FOUND="Instance template ${NODE_INSTANCE_PREFIX}-template"
  3096. return 1
  3097. fi
  3098. if gcloud compute instance-templates describe --project "${PROJECT}" "${WINDOWS_NODE_INSTANCE_PREFIX}-template" &>/dev/null; then
  3099. KUBE_RESOURCE_FOUND="Instance template ${WINDOWS_NODE_INSTANCE_PREFIX}-template"
  3100. return 1
  3101. fi
  3102. if gcloud compute instances describe --project "${PROJECT}" "${MASTER_NAME}" --zone "${ZONE}" &>/dev/null; then
  3103. KUBE_RESOURCE_FOUND="Kubernetes master ${MASTER_NAME}"
  3104. return 1
  3105. fi
  3106. if gcloud compute disks describe --project "${PROJECT}" "${MASTER_NAME}"-pd --zone "${ZONE}" &>/dev/null; then
  3107. KUBE_RESOURCE_FOUND="Persistent disk ${MASTER_NAME}-pd"
  3108. return 1
  3109. fi
  3110. # Find out what minions are running.
  3111. local -a minions
  3112. minions=( $(gcloud compute instances list \
  3113. --project "${PROJECT}" \
  3114. --filter="(name ~ '${NODE_INSTANCE_PREFIX}-.+' OR name ~ '${WINDOWS_NODE_INSTANCE_PREFIX}-.+') AND zone:(${ZONE})" \
  3115. --format='value(name)') )
  3116. if (( "${#minions[@]}" > 0 )); then
  3117. KUBE_RESOURCE_FOUND="${#minions[@]} matching ${NODE_INSTANCE_PREFIX}-.+ or ${WINDOWS_NODE_INSTANCE_PREFIX}-.+"
  3118. return 1
  3119. fi
  3120. if gcloud compute firewall-rules describe --project "${NETWORK_PROJECT}" "${MASTER_NAME}-https" &>/dev/null; then
  3121. KUBE_RESOURCE_FOUND="Firewall rules for ${MASTER_NAME}-https"
  3122. return 1
  3123. fi
  3124. if gcloud compute firewall-rules describe --project "${NETWORK_PROJECT}" "${NODE_TAG}-all" &>/dev/null; then
  3125. KUBE_RESOURCE_FOUND="Firewall rules for ${MASTER_NAME}-all"
  3126. return 1
  3127. fi
  3128. local -a routes
  3129. routes=( $(gcloud compute routes list --project "${NETWORK_PROJECT}" \
  3130. --filter="name ~ '${INSTANCE_PREFIX}-minion-.{4}'" --format='value(name)') )
  3131. if (( "${#routes[@]}" > 0 )); then
  3132. KUBE_RESOURCE_FOUND="${#routes[@]} routes matching ${INSTANCE_PREFIX}-minion-.{4}"
  3133. return 1
  3134. fi
  3135. if gcloud compute addresses describe --project "${PROJECT}" "${MASTER_NAME}-ip" --region "${REGION}" &>/dev/null; then
  3136. KUBE_RESOURCE_FOUND="Master's reserved IP"
  3137. return 1
  3138. fi
  3139. if [[ ${GCE_PRIVATE_CLUSTER:-} == "true" ]]; then
  3140. if gcloud compute routers describe --project "${NETWORK_PROJECT}" --region "${REGION}" "${NETWORK}-nat-router" &>/dev/null; then
  3141. KUBE_RESOURCE_FOUND="Cloud NAT router"
  3142. return 1
  3143. fi
  3144. fi
  3145. # No resources found.
  3146. return 0
  3147. }
  3148. # -----------------------------------------------------------------------------
  3149. # Cluster specific test helpers used from hack/e2e.go
  3150. # Execute prior to running tests to build a release if required for env.
  3151. #
  3152. # Assumed Vars:
  3153. # KUBE_ROOT
  3154. function test-build-release() {
  3155. # Make a release
  3156. "${KUBE_ROOT}/build/release.sh"
  3157. }
  3158. # Execute prior to running tests to initialize required structure. This is
  3159. # called from hack/e2e.go only when running -up.
  3160. #
  3161. # Assumed vars:
  3162. # Variables from config.sh
  3163. function test-setup() {
  3164. # Detect the project into $PROJECT if it isn't set
  3165. detect-project
  3166. if [[ ${MULTIZONE:-} == "true" && -n ${E2E_ZONES:-} ]]; then
  3167. for KUBE_GCE_ZONE in ${E2E_ZONES}; do
  3168. KUBE_GCE_ZONE="${KUBE_GCE_ZONE}" KUBE_USE_EXISTING_MASTER="${KUBE_USE_EXISTING_MASTER:-}" "${KUBE_ROOT}/cluster/kube-up.sh"
  3169. KUBE_USE_EXISTING_MASTER="true" # For subsequent zones we use the existing master
  3170. done
  3171. else
  3172. "${KUBE_ROOT}/cluster/kube-up.sh"
  3173. fi
  3174. # Open up port 80 & 8080 so common containers on minions can be reached
  3175. # TODO(roberthbailey): Remove this once we are no longer relying on hostPorts.
  3176. local start=`date +%s`
  3177. gcloud compute firewall-rules create \
  3178. --project "${NETWORK_PROJECT}" \
  3179. --target-tags "${NODE_TAG}" \
  3180. --allow tcp:80,tcp:8080 \
  3181. --network "${NETWORK}" \
  3182. "${NODE_TAG}-${INSTANCE_PREFIX}-http-alt" 2> /dev/null || true
  3183. # As there is no simple way to wait longer for this operation we need to manually
  3184. # wait some additional time (20 minutes altogether).
  3185. while ! gcloud compute firewall-rules describe --project "${NETWORK_PROJECT}" "${NODE_TAG}-${INSTANCE_PREFIX}-http-alt" 2> /dev/null; do
  3186. if [[ $(($start + 1200)) -lt `date +%s` ]]; then
  3187. echo -e "${color_red}Failed to create firewall ${NODE_TAG}-${INSTANCE_PREFIX}-http-alt in ${NETWORK_PROJECT}" >&2
  3188. exit 1
  3189. fi
  3190. sleep 5
  3191. done
  3192. # Open up the NodePort range
  3193. # TODO(justinsb): Move to main setup, if we decide whether we want to do this by default.
  3194. start=`date +%s`
  3195. gcloud compute firewall-rules create \
  3196. --project "${NETWORK_PROJECT}" \
  3197. --target-tags "${NODE_TAG}" \
  3198. --allow tcp:30000-32767,udp:30000-32767 \
  3199. --network "${NETWORK}" \
  3200. "${NODE_TAG}-${INSTANCE_PREFIX}-nodeports" 2> /dev/null || true
  3201. # As there is no simple way to wait longer for this operation we need to manually
  3202. # wait some additional time (20 minutes altogether).
  3203. while ! gcloud compute firewall-rules describe --project "${NETWORK_PROJECT}" "${NODE_TAG}-${INSTANCE_PREFIX}-nodeports" 2> /dev/null; do
  3204. if [[ $(($start + 1200)) -lt `date +%s` ]]; then
  3205. echo -e "${color_red}Failed to create firewall ${NODE_TAG}-${INSTANCE_PREFIX}-nodeports in ${PROJECT}" >&2
  3206. exit 1
  3207. fi
  3208. sleep 5
  3209. done
  3210. }
  3211. # Execute after running tests to perform any required clean-up. This is called
  3212. # from hack/e2e.go
  3213. function test-teardown() {
  3214. detect-project
  3215. echo "Shutting down test cluster in background."
  3216. delete-firewall-rules \
  3217. "${NODE_TAG}-${INSTANCE_PREFIX}-http-alt" \
  3218. "${NODE_TAG}-${INSTANCE_PREFIX}-nodeports"
  3219. if [[ ${MULTIZONE:-} == "true" && -n ${E2E_ZONES:-} ]]; then
  3220. local zones=( ${E2E_ZONES} )
  3221. # tear them down in reverse order, finally tearing down the master too.
  3222. for ((zone_num=${#zones[@]}-1; zone_num>0; zone_num--)); do
  3223. KUBE_GCE_ZONE="${zones[zone_num]}" KUBE_USE_EXISTING_MASTER="true" "${KUBE_ROOT}/cluster/kube-down.sh"
  3224. done
  3225. KUBE_GCE_ZONE="${zones[0]}" KUBE_USE_EXISTING_MASTER="false" "${KUBE_ROOT}/cluster/kube-down.sh"
  3226. else
  3227. "${KUBE_ROOT}/cluster/kube-down.sh"
  3228. fi
  3229. }
  3230. # SSH to a node by name ($1) and run a command ($2).
  3231. function ssh-to-node() {
  3232. local node="$1"
  3233. local cmd="$2"
  3234. # Loop until we can successfully ssh into the box
  3235. for try in {1..5}; do
  3236. if gcloud compute ssh --ssh-flag="-o LogLevel=quiet" --ssh-flag="-o ConnectTimeout=30" --project "${PROJECT}" --zone="${ZONE}" "${node}" --command "echo test > /dev/null"; then
  3237. break
  3238. fi
  3239. sleep 5
  3240. done
  3241. # Then actually try the command.
  3242. gcloud compute ssh --ssh-flag="-o LogLevel=quiet" --ssh-flag="-o ConnectTimeout=30" --project "${PROJECT}" --zone="${ZONE}" "${node}" --command "${cmd}"
  3243. }
  3244. # Perform preparations required to run e2e tests
  3245. function prepare-e2e() {
  3246. detect-project
  3247. }
  3248. # Delete the image given by $1.
  3249. function delete-image() {
  3250. gcloud container images delete --quiet "$1"
  3251. }