util.sh 150 KB

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