util.sh 150 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994
  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. CONTAINER_RUNTIME_TEST_HANDLER: $(yaml-quote ${CONTAINER_RUNTIME_TEST_HANDLER:-})
  1105. UBUNTU_INSTALL_CONTAINERD_VERSION: $(yaml-quote ${UBUNTU_INSTALL_CONTAINERD_VERSION:-})
  1106. UBUNTU_INSTALL_RUNC_VERSION: $(yaml-quote ${UBUNTU_INSTALL_RUNC_VERSION:-})
  1107. NODE_LOCAL_SSDS_EXT: $(yaml-quote ${NODE_LOCAL_SSDS_EXT:-})
  1108. LOAD_IMAGE_COMMAND: $(yaml-quote ${LOAD_IMAGE_COMMAND:-})
  1109. ZONE: $(yaml-quote ${ZONE})
  1110. REGION: $(yaml-quote ${REGION})
  1111. VOLUME_PLUGIN_DIR: $(yaml-quote ${VOLUME_PLUGIN_DIR})
  1112. KUBELET_ARGS: $(yaml-quote ${KUBELET_ARGS})
  1113. REQUIRE_METADATA_KUBELET_CONFIG_FILE: $(yaml-quote true)
  1114. ENABLE_NETD: $(yaml-quote ${ENABLE_NETD:-false})
  1115. ENABLE_NODE_TERMINATION_HANDLER: $(yaml-quote ${ENABLE_NODE_TERMINATION_HANDLER:-false})
  1116. CUSTOM_NETD_YAML: |
  1117. $(echo "${CUSTOM_NETD_YAML:-}" | sed -e "s/'/''/g")
  1118. CUSTOM_CALICO_NODE_DAEMONSET_YAML: |
  1119. $(echo "${CUSTOM_CALICO_NODE_DAEMONSET_YAML:-}" | sed -e "s/'/''/g")
  1120. CUSTOM_TYPHA_DEPLOYMENT_YAML: |
  1121. $(echo "${CUSTOM_TYPHA_DEPLOYMENT_YAML:-}" | sed -e "s/'/''/g")
  1122. EOF
  1123. if [[ "${master}" == "true" && "${MASTER_OS_DISTRIBUTION}" == "gci" ]] || \
  1124. [[ "${master}" == "false" && "${NODE_OS_DISTRIBUTION}" == "gci" ]] || \
  1125. [[ "${master}" == "true" && "${MASTER_OS_DISTRIBUTION}" == "cos" ]] || \
  1126. [[ "${master}" == "false" && "${NODE_OS_DISTRIBUTION}" == "cos" ]]; then
  1127. cat >>$file <<EOF
  1128. REMOUNT_VOLUME_PLUGIN_DIR: $(yaml-quote ${REMOUNT_VOLUME_PLUGIN_DIR:-true})
  1129. EOF
  1130. fi
  1131. if [[ "${master}" == "false" ]]; then
  1132. cat >>$file <<EOF
  1133. KONNECTIVITY_AGENT_CA_CERT: $(yaml-quote ${KONNECTIVITY_AGENT_CA_CERT_BASE64:-})
  1134. KONNECTIVITY_AGENT_CLIENT_KEY: $(yaml-quote ${KONNECTIVITY_AGENT_CLIENT_KEY_BASE64:-})
  1135. KONNECTIVITY_AGENT_CLIENT_CERT: $(yaml-quote ${KONNECTIVITY_AGENT_CLIENT_CERT_BASE64:-})
  1136. EOF
  1137. fi
  1138. if [ -n "${KUBE_APISERVER_REQUEST_TIMEOUT:-}" ]; then
  1139. cat >>$file <<EOF
  1140. KUBE_APISERVER_REQUEST_TIMEOUT: $(yaml-quote ${KUBE_APISERVER_REQUEST_TIMEOUT})
  1141. EOF
  1142. fi
  1143. if [ -n "${TERMINATED_POD_GC_THRESHOLD:-}" ]; then
  1144. cat >>$file <<EOF
  1145. TERMINATED_POD_GC_THRESHOLD: $(yaml-quote ${TERMINATED_POD_GC_THRESHOLD})
  1146. EOF
  1147. fi
  1148. if [[ "${master}" == "true" && ("${MASTER_OS_DISTRIBUTION}" == "trusty" || "${MASTER_OS_DISTRIBUTION}" == "gci" || "${MASTER_OS_DISTRIBUTION}" == "ubuntu") ]] || \
  1149. [[ "${master}" == "false" && ("${NODE_OS_DISTRIBUTION}" == "trusty" || "${NODE_OS_DISTRIBUTION}" == "gci" || "${NODE_OS_DISTRIBUTION}" = "ubuntu" || "${NODE_OS_DISTRIBUTION}" = "custom") ]] ; then
  1150. cat >>$file <<EOF
  1151. KUBE_MANIFESTS_TAR_URL: $(yaml-quote ${kube_manifests_tar_url})
  1152. KUBE_MANIFESTS_TAR_HASH: $(yaml-quote ${KUBE_MANIFESTS_TAR_HASH})
  1153. EOF
  1154. fi
  1155. if [ -n "${TEST_CLUSTER:-}" ]; then
  1156. cat >>$file <<EOF
  1157. TEST_CLUSTER: $(yaml-quote ${TEST_CLUSTER})
  1158. EOF
  1159. fi
  1160. if [ -n "${DOCKER_TEST_LOG_LEVEL:-}" ]; then
  1161. cat >>$file <<EOF
  1162. DOCKER_TEST_LOG_LEVEL: $(yaml-quote ${DOCKER_TEST_LOG_LEVEL})
  1163. EOF
  1164. fi
  1165. if [ -n "${DOCKER_LOG_DRIVER:-}" ]; then
  1166. cat >>$file <<EOF
  1167. DOCKER_LOG_DRIVER: $(yaml-quote ${DOCKER_LOG_DRIVER})
  1168. EOF
  1169. fi
  1170. if [ -n "${DOCKER_LOG_MAX_SIZE:-}" ]; then
  1171. cat >>$file <<EOF
  1172. DOCKER_LOG_MAX_SIZE: $(yaml-quote ${DOCKER_LOG_MAX_SIZE})
  1173. EOF
  1174. fi
  1175. if [ -n "${DOCKER_LOG_MAX_FILE:-}" ]; then
  1176. cat >>$file <<EOF
  1177. DOCKER_LOG_MAX_FILE: $(yaml-quote ${DOCKER_LOG_MAX_FILE})
  1178. EOF
  1179. fi
  1180. if [ -n "${FEATURE_GATES:-}" ]; then
  1181. cat >>$file <<EOF
  1182. FEATURE_GATES: $(yaml-quote ${FEATURE_GATES})
  1183. EOF
  1184. fi
  1185. if [ -n "${RUN_CONTROLLERS:-}" ]; then
  1186. cat >>$file <<EOF
  1187. RUN_CONTROLLERS: $(yaml-quote ${RUN_CONTROLLERS})
  1188. EOF
  1189. fi
  1190. if [ -n "${PROVIDER_VARS:-}" ]; then
  1191. local var_name
  1192. local var_value
  1193. for var_name in ${PROVIDER_VARS}; do
  1194. eval "local var_value=\$(yaml-quote \${${var_name}})"
  1195. cat >>$file <<EOF
  1196. ${var_name}: ${var_value}
  1197. EOF
  1198. done
  1199. fi
  1200. if [[ "${master}" == "true" ]]; then
  1201. # Master-only env vars.
  1202. cat >>$file <<EOF
  1203. KUBERNETES_MASTER: $(yaml-quote "true")
  1204. KUBE_USER: $(yaml-quote ${KUBE_USER})
  1205. KUBE_PASSWORD: $(yaml-quote ${KUBE_PASSWORD})
  1206. KUBE_BEARER_TOKEN: $(yaml-quote ${KUBE_BEARER_TOKEN})
  1207. MASTER_CERT: $(yaml-quote ${MASTER_CERT_BASE64:-})
  1208. MASTER_KEY: $(yaml-quote ${MASTER_KEY_BASE64:-})
  1209. KUBECFG_CERT: $(yaml-quote ${KUBECFG_CERT_BASE64:-})
  1210. KUBECFG_KEY: $(yaml-quote ${KUBECFG_KEY_BASE64:-})
  1211. KUBELET_APISERVER: $(yaml-quote ${KUBELET_APISERVER:-})
  1212. NUM_NODES: $(yaml-quote ${NUM_NODES})
  1213. STORAGE_BACKEND: $(yaml-quote ${STORAGE_BACKEND:-etcd3})
  1214. STORAGE_MEDIA_TYPE: $(yaml-quote ${STORAGE_MEDIA_TYPE:-})
  1215. ENABLE_GARBAGE_COLLECTOR: $(yaml-quote ${ENABLE_GARBAGE_COLLECTOR:-})
  1216. ENABLE_LEGACY_ABAC: $(yaml-quote ${ENABLE_LEGACY_ABAC:-})
  1217. MASTER_ADVERTISE_ADDRESS: $(yaml-quote ${MASTER_ADVERTISE_ADDRESS:-})
  1218. ETCD_CA_KEY: $(yaml-quote ${ETCD_CA_KEY_BASE64:-})
  1219. ETCD_CA_CERT: $(yaml-quote ${ETCD_CA_CERT_BASE64:-})
  1220. ETCD_PEER_KEY: $(yaml-quote ${ETCD_PEER_KEY_BASE64:-})
  1221. ETCD_PEER_CERT: $(yaml-quote ${ETCD_PEER_CERT_BASE64:-})
  1222. SERVICEACCOUNT_ISSUER: $(yaml-quote ${SERVICEACCOUNT_ISSUER:-})
  1223. EOF
  1224. # KUBE_APISERVER_REQUEST_TIMEOUT_SEC (if set) controls the --request-timeout
  1225. # flag
  1226. if [ -n "${KUBE_APISERVER_REQUEST_TIMEOUT_SEC:-}" ]; then
  1227. cat >>$file <<EOF
  1228. KUBE_APISERVER_REQUEST_TIMEOUT_SEC: $(yaml-quote ${KUBE_APISERVER_REQUEST_TIMEOUT_SEC})
  1229. EOF
  1230. fi
  1231. # ETCD_IMAGE (if set) allows to use a custom etcd image.
  1232. if [ -n "${ETCD_IMAGE:-}" ]; then
  1233. cat >>$file <<EOF
  1234. ETCD_IMAGE: $(yaml-quote ${ETCD_IMAGE})
  1235. EOF
  1236. fi
  1237. # ETCD_DOCKER_REPOSITORY (if set) allows to use a custom etcd docker repository to pull the etcd image from.
  1238. if [ -n "${ETCD_DOCKER_REPOSITORY:-}" ]; then
  1239. cat >>$file <<EOF
  1240. ETCD_DOCKER_REPOSITORY: $(yaml-quote ${ETCD_DOCKER_REPOSITORY})
  1241. EOF
  1242. fi
  1243. # ETCD_VERSION (if set) allows you to use custom version of etcd.
  1244. # The main purpose of using it may be rollback of etcd v3 API,
  1245. # where we need 3.0.* image, but are rolling back to 2.3.7.
  1246. if [ -n "${ETCD_VERSION:-}" ]; then
  1247. cat >>$file <<EOF
  1248. ETCD_VERSION: $(yaml-quote ${ETCD_VERSION})
  1249. EOF
  1250. fi
  1251. if [ -n "${ETCD_HOSTNAME:-}" ]; then
  1252. cat >>$file <<EOF
  1253. ETCD_HOSTNAME: $(yaml-quote ${ETCD_HOSTNAME})
  1254. EOF
  1255. fi
  1256. if [ -n "${ETCD_LIVENESS_PROBE_INITIAL_DELAY_SEC:-}" ]; then
  1257. cat >>$file <<EOF
  1258. ETCD_LIVENESS_PROBE_INITIAL_DELAY_SEC: $(yaml-quote ${ETCD_LIVENESS_PROBE_INITIAL_DELAY_SEC})
  1259. EOF
  1260. fi
  1261. if [ -n "${KUBE_APISERVER_LIVENESS_PROBE_INITIAL_DELAY_SEC:-}" ]; then
  1262. cat >>$file <<EOF
  1263. KUBE_APISERVER_LIVENESS_PROBE_INITIAL_DELAY_SEC: $(yaml-quote ${KUBE_APISERVER_LIVENESS_PROBE_INITIAL_DELAY_SEC})
  1264. EOF
  1265. fi
  1266. if [ -n "${ETCD_COMPACTION_INTERVAL_SEC:-}" ]; then
  1267. cat >>$file <<EOF
  1268. ETCD_COMPACTION_INTERVAL_SEC: $(yaml-quote ${ETCD_COMPACTION_INTERVAL_SEC})
  1269. EOF
  1270. fi
  1271. if [ -n "${ETCD_QUOTA_BACKEND_BYTES:-}" ]; then
  1272. cat >>$file <<EOF
  1273. ETCD_QUOTA_BACKEND_BYTES: $(yaml-quote ${ETCD_QUOTA_BACKEND_BYTES})
  1274. EOF
  1275. fi
  1276. if [ -n "${ETCD_EXTRA_ARGS:-}" ]; then
  1277. cat >>$file <<EOF
  1278. ETCD_EXTRA_ARGS: $(yaml-quote ${ETCD_EXTRA_ARGS})
  1279. EOF
  1280. fi
  1281. if [ -n "${ETCD_SERVERS:-}" ]; then
  1282. cat >>$file <<EOF
  1283. ETCD_SERVERS: $(yaml-quote ${ETCD_SERVERS})
  1284. EOF
  1285. fi
  1286. if [ -n "${ETCD_SERVERS_OVERRIDES:-}" ]; then
  1287. cat >>$file <<EOF
  1288. ETCD_SERVERS_OVERRIDES: $(yaml-quote ${ETCD_SERVERS_OVERRIDES})
  1289. EOF
  1290. fi
  1291. if [ -n "${APISERVER_TEST_ARGS:-}" ]; then
  1292. cat >>$file <<EOF
  1293. APISERVER_TEST_ARGS: $(yaml-quote ${APISERVER_TEST_ARGS})
  1294. EOF
  1295. fi
  1296. if [ -n "${CONTROLLER_MANAGER_TEST_ARGS:-}" ]; then
  1297. cat >>$file <<EOF
  1298. CONTROLLER_MANAGER_TEST_ARGS: $(yaml-quote ${CONTROLLER_MANAGER_TEST_ARGS})
  1299. EOF
  1300. fi
  1301. if [ -n "${CONTROLLER_MANAGER_TEST_LOG_LEVEL:-}" ]; then
  1302. cat >>$file <<EOF
  1303. CONTROLLER_MANAGER_TEST_LOG_LEVEL: $(yaml-quote ${CONTROLLER_MANAGER_TEST_LOG_LEVEL})
  1304. EOF
  1305. fi
  1306. if [ -n "${SCHEDULER_TEST_ARGS:-}" ]; then
  1307. cat >>$file <<EOF
  1308. SCHEDULER_TEST_ARGS: $(yaml-quote ${SCHEDULER_TEST_ARGS})
  1309. EOF
  1310. fi
  1311. if [ -n "${SCHEDULER_TEST_LOG_LEVEL:-}" ]; then
  1312. cat >>$file <<EOF
  1313. SCHEDULER_TEST_LOG_LEVEL: $(yaml-quote ${SCHEDULER_TEST_LOG_LEVEL})
  1314. EOF
  1315. fi
  1316. if [ -n "${INITIAL_ETCD_CLUSTER:-}" ]; then
  1317. cat >>$file <<EOF
  1318. INITIAL_ETCD_CLUSTER: $(yaml-quote ${INITIAL_ETCD_CLUSTER})
  1319. EOF
  1320. fi
  1321. if [ -n "${INITIAL_ETCD_CLUSTER_STATE:-}" ]; then
  1322. cat >>$file <<EOF
  1323. INITIAL_ETCD_CLUSTER_STATE: $(yaml-quote ${INITIAL_ETCD_CLUSTER_STATE})
  1324. EOF
  1325. fi
  1326. if [ -n "${CLUSTER_SIGNING_DURATION:-}" ]; then
  1327. cat >>$file <<EOF
  1328. CLUSTER_SIGNING_DURATION: $(yaml-quote ${CLUSTER_SIGNING_DURATION})
  1329. EOF
  1330. fi
  1331. if [[ "${NODE_ACCELERATORS:-}" == *"type=nvidia"* ]]; then
  1332. cat >>$file <<EOF
  1333. ENABLE_NVIDIA_GPU_DEVICE_PLUGIN: $(yaml-quote "true")
  1334. EOF
  1335. fi
  1336. if [ -n "${ADDON_MANAGER_LEADER_ELECTION:-}" ]; then
  1337. cat >>$file <<EOF
  1338. ADDON_MANAGER_LEADER_ELECTION: $(yaml-quote ${ADDON_MANAGER_LEADER_ELECTION})
  1339. EOF
  1340. fi
  1341. if [ -n "${API_SERVER_TEST_LOG_LEVEL:-}" ]; then
  1342. cat >>$file <<EOF
  1343. API_SERVER_TEST_LOG_LEVEL: $(yaml-quote ${API_SERVER_TEST_LOG_LEVEL})
  1344. EOF
  1345. fi
  1346. if [ -n "${ETCD_LISTEN_CLIENT_IP:-}" ]; then
  1347. cat >>$file <<EOF
  1348. ETCD_LISTEN_CLIENT_IP: $(yaml-quote ${ETCD_LISTEN_CLIENT_IP})
  1349. EOF
  1350. fi
  1351. else
  1352. # Node-only env vars.
  1353. cat >>$file <<EOF
  1354. KUBERNETES_MASTER: $(yaml-quote "false")
  1355. EXTRA_DOCKER_OPTS: $(yaml-quote ${EXTRA_DOCKER_OPTS:-})
  1356. EOF
  1357. if [ -n "${KUBEPROXY_TEST_ARGS:-}" ]; then
  1358. cat >>$file <<EOF
  1359. KUBEPROXY_TEST_ARGS: $(yaml-quote ${KUBEPROXY_TEST_ARGS})
  1360. EOF
  1361. fi
  1362. if [ -n "${KUBEPROXY_TEST_LOG_LEVEL:-}" ]; then
  1363. cat >>$file <<EOF
  1364. KUBEPROXY_TEST_LOG_LEVEL: $(yaml-quote ${KUBEPROXY_TEST_LOG_LEVEL})
  1365. EOF
  1366. fi
  1367. fi
  1368. if [[ "${ENABLE_CLUSTER_AUTOSCALER}" == "true" ]]; then
  1369. cat >>$file <<EOF
  1370. ENABLE_CLUSTER_AUTOSCALER: $(yaml-quote ${ENABLE_CLUSTER_AUTOSCALER})
  1371. AUTOSCALER_MIG_CONFIG: $(yaml-quote ${AUTOSCALER_MIG_CONFIG})
  1372. AUTOSCALER_EXPANDER_CONFIG: $(yaml-quote ${AUTOSCALER_EXPANDER_CONFIG})
  1373. EOF
  1374. if [[ "${master}" == "false" ]]; then
  1375. # TODO(kubernetes/autoscaler#718): AUTOSCALER_ENV_VARS is a hotfix for cluster autoscaler,
  1376. # which reads the kube-env to determine the shape of a node and was broken by #60020.
  1377. # This should be removed as soon as a more reliable source of information is available!
  1378. local node_labels="$(build-linux-node-labels node)"
  1379. local node_taints="${NODE_TAINTS:-}"
  1380. local autoscaler_env_vars="node_labels=${node_labels};node_taints=${node_taints}"
  1381. cat >>$file <<EOF
  1382. AUTOSCALER_ENV_VARS: $(yaml-quote ${autoscaler_env_vars})
  1383. EOF
  1384. fi
  1385. fi
  1386. if [ -n "${SCHEDULING_ALGORITHM_PROVIDER:-}" ]; then
  1387. cat >>$file <<EOF
  1388. SCHEDULING_ALGORITHM_PROVIDER: $(yaml-quote ${SCHEDULING_ALGORITHM_PROVIDER})
  1389. EOF
  1390. fi
  1391. if [ -n "${MAX_PODS_PER_NODE:-}" ]; then
  1392. cat >>$file <<EOF
  1393. MAX_PODS_PER_NODE: $(yaml-quote ${MAX_PODS_PER_NODE})
  1394. EOF
  1395. fi
  1396. if [[ "${ENABLE_EGRESS_VIA_KONNECTIVITY_SERVICE:-false}" == "true" ]]; then
  1397. cat >>$file <<EOF
  1398. ENABLE_EGRESS_VIA_KONNECTIVITY_SERVICE: $(yaml-quote ${ENABLE_EGRESS_VIA_KONNECTIVITY_SERVICE})
  1399. EOF
  1400. fi
  1401. }
  1402. function build-windows-kube-env {
  1403. local file="$1"
  1404. # For now the Windows kube-env is a superset of the Linux kube-env.
  1405. build-linux-kube-env false $file
  1406. cat >>$file <<EOF
  1407. WINDOWS_NODE_INSTANCE_PREFIX: $(yaml-quote ${WINDOWS_NODE_INSTANCE_PREFIX})
  1408. NODE_BINARY_TAR_URL: $(yaml-quote ${NODE_BINARY_TAR_URL})
  1409. NODE_BINARY_TAR_HASH: $(yaml-quote ${NODE_BINARY_TAR_HASH})
  1410. K8S_DIR: $(yaml-quote ${WINDOWS_K8S_DIR})
  1411. NODE_DIR: $(yaml-quote ${WINDOWS_NODE_DIR})
  1412. LOGS_DIR: $(yaml-quote ${WINDOWS_LOGS_DIR})
  1413. CNI_DIR: $(yaml-quote ${WINDOWS_CNI_DIR})
  1414. CNI_CONFIG_DIR: $(yaml-quote ${WINDOWS_CNI_CONFIG_DIR})
  1415. WINDOWS_CNI_STORAGE_PATH: $(yaml-quote ${WINDOWS_CNI_STORAGE_PATH})
  1416. WINDOWS_CNI_VERSION: $(yaml-quote ${WINDOWS_CNI_VERSION})
  1417. MANIFESTS_DIR: $(yaml-quote ${WINDOWS_MANIFESTS_DIR})
  1418. PKI_DIR: $(yaml-quote ${WINDOWS_PKI_DIR})
  1419. CA_FILE_PATH: $(yaml-quote ${WINDOWS_CA_FILE})
  1420. KUBELET_CONFIG_FILE: $(yaml-quote ${WINDOWS_KUBELET_CONFIG_FILE})
  1421. KUBEPROXY_ARGS: $(yaml-quote ${KUBEPROXY_ARGS})
  1422. KUBECONFIG_FILE: $(yaml-quote ${WINDOWS_KUBECONFIG_FILE})
  1423. BOOTSTRAP_KUBECONFIG_FILE: $(yaml-quote ${WINDOWS_BOOTSTRAP_KUBECONFIG_FILE})
  1424. KUBEPROXY_KUBECONFIG_FILE: $(yaml-quote ${WINDOWS_KUBEPROXY_KUBECONFIG_FILE})
  1425. EOF
  1426. }
  1427. function sha1sum-file() {
  1428. if which sha1sum >/dev/null 2>&1; then
  1429. sha1sum "$1" | awk '{ print $1 }'
  1430. else
  1431. shasum -a1 "$1" | awk '{ print $1 }'
  1432. fi
  1433. }
  1434. # Create certificate pairs for the cluster.
  1435. # $1: The public IP for the master.
  1436. #
  1437. # These are used for static cert distribution (e.g. static clustering) at
  1438. # cluster creation time. This will be obsoleted once we implement dynamic
  1439. # clustering.
  1440. #
  1441. # The following certificate pairs are created:
  1442. #
  1443. # - ca (the cluster's certificate authority)
  1444. # - server
  1445. # - kubelet
  1446. # - kubecfg (for kubectl)
  1447. #
  1448. # TODO(roberthbailey): Replace easyrsa with a simple Go program to generate
  1449. # the certs that we need.
  1450. #
  1451. # Assumed vars
  1452. # KUBE_TEMP
  1453. # MASTER_NAME
  1454. #
  1455. # Vars set:
  1456. # CERT_DIR
  1457. # CA_CERT_BASE64
  1458. # MASTER_CERT_BASE64
  1459. # MASTER_KEY_BASE64
  1460. # KUBELET_CERT_BASE64
  1461. # KUBELET_KEY_BASE64
  1462. # KUBECFG_CERT_BASE64
  1463. # KUBECFG_KEY_BASE64
  1464. function create-certs {
  1465. local -r primary_cn="${1}"
  1466. # Determine extra certificate names for master
  1467. local octets=($(echo "${SERVICE_CLUSTER_IP_RANGE}" | sed -e 's|/.*||' -e 's/\./ /g'))
  1468. ((octets[3]+=1))
  1469. local -r service_ip=$(echo "${octets[*]}" | sed 's/ /./g')
  1470. local sans=""
  1471. for extra in $@; do
  1472. if [[ -n "${extra}" ]]; then
  1473. sans="${sans}IP:${extra},"
  1474. fi
  1475. done
  1476. sans="${sans}IP:${service_ip},DNS:kubernetes,DNS:kubernetes.default,DNS:kubernetes.default.svc,DNS:kubernetes.default.svc.${DNS_DOMAIN},DNS:${MASTER_NAME}"
  1477. echo "Generating certs for alternate-names: ${sans}"
  1478. setup-easyrsa
  1479. PRIMARY_CN="${primary_cn}" SANS="${sans}" generate-certs
  1480. AGGREGATOR_PRIMARY_CN="${primary_cn}" AGGREGATOR_SANS="${sans}" generate-aggregator-certs
  1481. KONNECTIVITY_SERVER_PRIMARY_CN="${primary_cn}" KONNECTIVITY_SERVER_SANS="${sans}" generate-konnectivity-server-certs
  1482. # By default, linux wraps base64 output every 76 cols, so we use 'tr -d' to remove whitespaces.
  1483. # Note 'base64 -w0' doesn't work on Mac OS X, which has different flags.
  1484. CA_KEY_BASE64=$(cat "${CERT_DIR}/pki/private/ca.key" | base64 | tr -d '\r\n')
  1485. CA_CERT_BASE64=$(cat "${CERT_DIR}/pki/ca.crt" | base64 | tr -d '\r\n')
  1486. MASTER_CERT_BASE64=$(cat "${CERT_DIR}/pki/issued/${MASTER_NAME}.crt" | base64 | tr -d '\r\n')
  1487. MASTER_KEY_BASE64=$(cat "${CERT_DIR}/pki/private/${MASTER_NAME}.key" | base64 | tr -d '\r\n')
  1488. KUBELET_CERT_BASE64=$(cat "${CERT_DIR}/pki/issued/kubelet.crt" | base64 | tr -d '\r\n')
  1489. KUBELET_KEY_BASE64=$(cat "${CERT_DIR}/pki/private/kubelet.key" | base64 | tr -d '\r\n')
  1490. KUBECFG_CERT_BASE64=$(cat "${CERT_DIR}/pki/issued/kubecfg.crt" | base64 | tr -d '\r\n')
  1491. KUBECFG_KEY_BASE64=$(cat "${CERT_DIR}/pki/private/kubecfg.key" | base64 | tr -d '\r\n')
  1492. KUBEAPISERVER_CERT_BASE64=$(cat "${CERT_DIR}/pki/issued/kube-apiserver.crt" | base64 | tr -d '\r\n')
  1493. KUBEAPISERVER_KEY_BASE64=$(cat "${CERT_DIR}/pki/private/kube-apiserver.key" | base64 | tr -d '\r\n')
  1494. # Setting up an addition directory (beyond pki) as it is the simplest way to
  1495. # ensure we get a different CA pair to sign the proxy-client certs and which
  1496. # we can send CA public key to the user-apiserver to validate communication.
  1497. AGGREGATOR_CA_KEY_BASE64=$(cat "${AGGREGATOR_CERT_DIR}/pki/private/ca.key" | base64 | tr -d '\r\n')
  1498. REQUESTHEADER_CA_CERT_BASE64=$(cat "${AGGREGATOR_CERT_DIR}/pki/ca.crt" | base64 | tr -d '\r\n')
  1499. PROXY_CLIENT_CERT_BASE64=$(cat "${AGGREGATOR_CERT_DIR}/pki/issued/proxy-client.crt" | base64 | tr -d '\r\n')
  1500. PROXY_CLIENT_KEY_BASE64=$(cat "${AGGREGATOR_CERT_DIR}/pki/private/proxy-client.key" | base64 | tr -d '\r\n')
  1501. # Setting up the Kubernetes API Server Konnectivity Server auth.
  1502. # This includes certs for both API Server to Konnectivity Server and
  1503. # Konnectivity Agent to Konnectivity Server.
  1504. KONNECTIVITY_SERVER_CA_KEY_BASE64=$(cat "${KONNECTIVITY_SERVER_CERT_DIR}/pki/private/ca.key" | base64 | tr -d '\r\n')
  1505. KONNECTIVITY_SERVER_CA_CERT_BASE64=$(cat "${KONNECTIVITY_SERVER_CERT_DIR}/pki/ca.crt" | base64 | tr -d '\r\n')
  1506. KONNECTIVITY_SERVER_CERT_BASE64=$(cat "${KONNECTIVITY_SERVER_CERT_DIR}/pki/issued/server.crt" | base64 | tr -d '\r\n')
  1507. KONNECTIVITY_SERVER_KEY_BASE64=$(cat "${KONNECTIVITY_SERVER_CERT_DIR}/pki/private/server.key" | base64 | tr -d '\r\n')
  1508. KONNECTIVITY_SERVER_CLIENT_CERT_BASE64=$(cat "${KONNECTIVITY_SERVER_CERT_DIR}/pki/issued/client.crt" | base64 | tr -d '\r\n')
  1509. KONNECTIVITY_SERVER_CLIENT_KEY_BASE64=$(cat "${KONNECTIVITY_SERVER_CERT_DIR}/pki/private/client.key" | base64 | tr -d '\r\n')
  1510. KONNECTIVITY_AGENT_CA_KEY_BASE64=$(cat "${KONNECTIVITY_AGENT_CERT_DIR}/pki/private/ca.key" | base64 | tr -d '\r\n')
  1511. KONNECTIVITY_AGENT_CA_CERT_BASE64=$(cat "${KONNECTIVITY_AGENT_CERT_DIR}/pki/ca.crt" | base64 | tr -d '\r\n')
  1512. KONNECTIVITY_AGENT_CERT_BASE64=$(cat "${KONNECTIVITY_AGENT_CERT_DIR}/pki/issued/server.crt" | base64 | tr -d '\r\n')
  1513. KONNECTIVITY_AGENT_KEY_BASE64=$(cat "${KONNECTIVITY_AGENT_CERT_DIR}/pki/private/server.key" | base64 | tr -d '\r\n')
  1514. KONNECTIVITY_AGENT_CLIENT_CERT_BASE64=$(cat "${KONNECTIVITY_AGENT_CERT_DIR}/pki/issued/client.crt" | base64 | tr -d '\r\n')
  1515. KONNECTIVITY_AGENT_CLIENT_KEY_BASE64=$(cat "${KONNECTIVITY_AGENT_CERT_DIR}/pki/private/client.key" | base64 | tr -d '\r\n')
  1516. }
  1517. # Set up easy-rsa directory structure.
  1518. #
  1519. # Assumed vars
  1520. # KUBE_TEMP
  1521. #
  1522. # Vars set:
  1523. # CERT_DIR
  1524. # AGGREGATOR_CERT_DIR
  1525. function setup-easyrsa {
  1526. local -r cert_create_debug_output=$(mktemp "${KUBE_TEMP}/cert_create_debug_output.XXX")
  1527. # Note: This was heavily cribbed from make-ca-cert.sh
  1528. (set -x
  1529. cd "${KUBE_TEMP}"
  1530. curl -L -O --connect-timeout 20 --retry 6 --retry-delay 2 https://storage.googleapis.com/kubernetes-release/easy-rsa/easy-rsa.tar.gz
  1531. tar xzf easy-rsa.tar.gz
  1532. mkdir easy-rsa-master/kubelet
  1533. cp -r easy-rsa-master/easyrsa3/* easy-rsa-master/kubelet
  1534. mkdir easy-rsa-master/aggregator
  1535. cp -r easy-rsa-master/easyrsa3/* easy-rsa-master/aggregator
  1536. mkdir easy-rsa-master/konnectivity-server
  1537. cp -r easy-rsa-master/easyrsa3/* easy-rsa-master/konnectivity-server
  1538. mkdir easy-rsa-master/konnectivity-agent
  1539. cp -r easy-rsa-master/easyrsa3/* easy-rsa-master/konnectivity-agent) &>${cert_create_debug_output} || true
  1540. CERT_DIR="${KUBE_TEMP}/easy-rsa-master/easyrsa3"
  1541. AGGREGATOR_CERT_DIR="${KUBE_TEMP}/easy-rsa-master/aggregator"
  1542. KONNECTIVITY_SERVER_CERT_DIR="${KUBE_TEMP}/easy-rsa-master/konnectivity-server"
  1543. KONNECTIVITY_AGENT_CERT_DIR="${KUBE_TEMP}/easy-rsa-master/konnectivity-agent"
  1544. if [ ! -x "${CERT_DIR}/easyrsa" -o ! -x "${AGGREGATOR_CERT_DIR}/easyrsa" ]; then
  1545. # TODO(roberthbailey,porridge): add better error handling here,
  1546. # see https://github.com/kubernetes/kubernetes/issues/55229
  1547. cat "${cert_create_debug_output}" >&2
  1548. echo "=== Failed to setup easy-rsa: Aborting ===" >&2
  1549. exit 2
  1550. fi
  1551. }
  1552. # Runs the easy RSA commands to generate certificate files.
  1553. # The generated files are IN ${CERT_DIR}
  1554. #
  1555. # Assumed vars
  1556. # KUBE_TEMP
  1557. # MASTER_NAME
  1558. # CERT_DIR
  1559. # PRIMARY_CN: Primary canonical name
  1560. # SANS: Subject alternate names
  1561. #
  1562. #
  1563. function generate-certs {
  1564. local -r cert_create_debug_output=$(mktemp "${KUBE_TEMP}/cert_create_debug_output.XXX")
  1565. # Note: This was heavily cribbed from make-ca-cert.sh
  1566. (set -x
  1567. cd "${CERT_DIR}"
  1568. ./easyrsa init-pki
  1569. # this puts the cert into pki/ca.crt and the key into pki/private/ca.key
  1570. ./easyrsa --batch "--req-cn=${PRIMARY_CN}@$(date +%s)" build-ca nopass
  1571. ./easyrsa --subject-alt-name="${SANS}" build-server-full "${MASTER_NAME}" nopass
  1572. ./easyrsa build-client-full kube-apiserver nopass
  1573. kube::util::ensure-cfssl "${KUBE_TEMP}/cfssl"
  1574. # make the config for the signer
  1575. echo '{"signing":{"default":{"expiry":"43800h","usages":["signing","key encipherment","client auth"]}}}' > "ca-config.json"
  1576. # create the kubelet client cert with the correct groups
  1577. 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
  1578. mv "kubelet-key.pem" "pki/private/kubelet.key"
  1579. mv "kubelet.pem" "pki/issued/kubelet.crt"
  1580. rm -f "kubelet.csr"
  1581. # Make a superuser client cert with subject "O=system:masters, CN=kubecfg"
  1582. ./easyrsa --dn-mode=org \
  1583. --req-cn=kubecfg --req-org=system:masters \
  1584. --req-c= --req-st= --req-city= --req-email= --req-ou= \
  1585. build-client-full kubecfg nopass) &>${cert_create_debug_output} || true
  1586. local output_file_missing=0
  1587. local output_file
  1588. for output_file in \
  1589. "${CERT_DIR}/pki/private/ca.key" \
  1590. "${CERT_DIR}/pki/ca.crt" \
  1591. "${CERT_DIR}/pki/issued/${MASTER_NAME}.crt" \
  1592. "${CERT_DIR}/pki/private/${MASTER_NAME}.key" \
  1593. "${CERT_DIR}/pki/issued/kubelet.crt" \
  1594. "${CERT_DIR}/pki/private/kubelet.key" \
  1595. "${CERT_DIR}/pki/issued/kubecfg.crt" \
  1596. "${CERT_DIR}/pki/private/kubecfg.key" \
  1597. "${CERT_DIR}/pki/issued/kube-apiserver.crt" \
  1598. "${CERT_DIR}/pki/private/kube-apiserver.key"
  1599. do
  1600. if [[ ! -s "${output_file}" ]]; then
  1601. echo "Expected file ${output_file} not created" >&2
  1602. output_file_missing=1
  1603. fi
  1604. done
  1605. if (( $output_file_missing )); then
  1606. # TODO(roberthbailey,porridge): add better error handling here,
  1607. # see https://github.com/kubernetes/kubernetes/issues/55229
  1608. cat "${cert_create_debug_output}" >&2
  1609. echo "=== Failed to generate master certificates: Aborting ===" >&2
  1610. exit 2
  1611. fi
  1612. }
  1613. # Runs the easy RSA commands to generate aggregator certificate files.
  1614. # The generated files are in ${AGGREGATOR_CERT_DIR}
  1615. #
  1616. # Assumed vars
  1617. # KUBE_TEMP
  1618. # AGGREGATOR_MASTER_NAME
  1619. # AGGREGATOR_CERT_DIR
  1620. # AGGREGATOR_PRIMARY_CN: Primary canonical name
  1621. # AGGREGATOR_SANS: Subject alternate names
  1622. #
  1623. #
  1624. function generate-aggregator-certs {
  1625. local -r cert_create_debug_output=$(mktemp "${KUBE_TEMP}/cert_create_debug_output.XXX")
  1626. # Note: This was heavily cribbed from make-ca-cert.sh
  1627. (set -x
  1628. cd "${KUBE_TEMP}/easy-rsa-master/aggregator"
  1629. ./easyrsa init-pki
  1630. # this puts the cert into pki/ca.crt and the key into pki/private/ca.key
  1631. ./easyrsa --batch "--req-cn=${AGGREGATOR_PRIMARY_CN}@$(date +%s)" build-ca nopass
  1632. ./easyrsa --subject-alt-name="${AGGREGATOR_SANS}" build-server-full "${AGGREGATOR_MASTER_NAME}" nopass
  1633. ./easyrsa build-client-full aggregator-apiserver nopass
  1634. kube::util::ensure-cfssl "${KUBE_TEMP}/cfssl"
  1635. # make the config for the signer
  1636. echo '{"signing":{"default":{"expiry":"43800h","usages":["signing","key encipherment","client auth"]}}}' > "ca-config.json"
  1637. # create the aggregator client cert with the correct groups
  1638. 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
  1639. mv "proxy-client-key.pem" "pki/private/proxy-client.key"
  1640. mv "proxy-client.pem" "pki/issued/proxy-client.crt"
  1641. rm -f "proxy-client.csr"
  1642. # Make a superuser client cert with subject "O=system:masters, CN=kubecfg"
  1643. ./easyrsa --dn-mode=org \
  1644. --req-cn=proxy-clientcfg --req-org=system:aggregator \
  1645. --req-c= --req-st= --req-city= --req-email= --req-ou= \
  1646. build-client-full proxy-clientcfg nopass) &>${cert_create_debug_output} || true
  1647. local output_file_missing=0
  1648. local output_file
  1649. for output_file in \
  1650. "${AGGREGATOR_CERT_DIR}/pki/private/ca.key" \
  1651. "${AGGREGATOR_CERT_DIR}/pki/ca.crt" \
  1652. "${AGGREGATOR_CERT_DIR}/pki/issued/proxy-client.crt" \
  1653. "${AGGREGATOR_CERT_DIR}/pki/private/proxy-client.key"
  1654. do
  1655. if [[ ! -s "${output_file}" ]]; then
  1656. echo "Expected file ${output_file} not created" >&2
  1657. output_file_missing=1
  1658. fi
  1659. done
  1660. if (( $output_file_missing )); then
  1661. # TODO(roberthbailey,porridge): add better error handling here,
  1662. # see https://github.com/kubernetes/kubernetes/issues/55229
  1663. cat "${cert_create_debug_output}" >&2
  1664. echo "=== Failed to generate aggregator certificates: Aborting ===" >&2
  1665. exit 2
  1666. fi
  1667. }
  1668. # Runs the easy RSA commands to generate server side certificate files
  1669. # for the konnectivity server. This includes both server side to both
  1670. # konnectivity-server and konnectivity-agent.
  1671. # The generated files are in ${KONNECTIVITY_SERVER_CERT_DIR} and
  1672. # ${KONNECTIVITY_AGENT_CERT_DIR}
  1673. #
  1674. # Assumed vars
  1675. # KUBE_TEMP
  1676. # KONNECTIVITY_SERVER_CERT_DIR
  1677. # KONNECTIVITY_SERVER_PRIMARY_CN: Primary canonical name
  1678. # KONNECTIVITY_SERVER_SANS: Subject alternate names
  1679. #
  1680. function generate-konnectivity-server-certs {
  1681. local -r cert_create_debug_output=$(mktemp "${KUBE_TEMP}/cert_create_debug_output.XXX")
  1682. # Note: This was heavily cribbed from make-ca-cert.sh
  1683. (set -x
  1684. # Make the client <-> konnectivity server side certificates.
  1685. cd "${KUBE_TEMP}/easy-rsa-master/konnectivity-server"
  1686. ./easyrsa init-pki
  1687. # this puts the cert into pki/ca.crt and the key into pki/private/ca.key
  1688. ./easyrsa --batch "--req-cn=${KONNECTIVITY_SERVER_PRIMARY_CN}@$(date +%s)" build-ca nopass
  1689. ./easyrsa --subject-alt-name="IP:127.0.0.1,${KONNECTIVITY_SERVER_SANS}" build-server-full server nopass
  1690. ./easyrsa build-client-full client nopass
  1691. kube::util::ensure-cfssl "${KUBE_TEMP}/cfssl"
  1692. # make the config for the signer
  1693. echo '{"signing":{"default":{"expiry":"43800h","usages":["signing","key encipherment","client auth"]}}}' > "ca-config.json"
  1694. # create the konnectivity server cert with the correct groups
  1695. 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
  1696. rm -f "konnectivity-server.csr"
  1697. # Make the agent <-> konnectivity server side certificates.
  1698. cd "${KUBE_TEMP}/easy-rsa-master/konnectivity-agent"
  1699. ./easyrsa init-pki
  1700. # this puts the cert into pki/ca.crt and the key into pki/private/ca.key
  1701. ./easyrsa --batch "--req-cn=${KONNECTIVITY_SERVER_PRIMARY_CN}@$(date +%s)" build-ca nopass
  1702. ./easyrsa --subject-alt-name="${KONNECTIVITY_SERVER_SANS}" build-server-full server nopass
  1703. ./easyrsa build-client-full client nopass
  1704. kube::util::ensure-cfssl "${KUBE_TEMP}/cfssl"
  1705. # make the config for the signer
  1706. echo '{"signing":{"default":{"expiry":"43800h","usages":["signing","key encipherment","agent auth"]}}}' > "ca-config.json"
  1707. # create the konnectivity server cert with the correct groups
  1708. 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
  1709. rm -f "konnectivity-agent.csr"
  1710. echo `ls ${KONNECTIVITY_SERVER_CERT_DIR}/pki/`
  1711. echo `ls ${KONNECTIVITY_SERVER_CERT_DIR}/pki/private/`
  1712. echo `ls ${KONNECTIVITY_SERVER_CERT_DIR}/pki/issued/`
  1713. echo `ls ${KONNECTIVITY_AGENT_CERT_DIR}/pki/`
  1714. echo `ls ${KONNECTIVITY_AGENT_CERT_DIR}/pki/private/`
  1715. echo `ls ${KONNECTIVITY_AGENT_CERT_DIR}/pki/issued/`
  1716. echo "completed main certificate section") &>${cert_create_debug_output} || true
  1717. local output_file_missing=0
  1718. local output_file
  1719. for output_file in \
  1720. "${KONNECTIVITY_SERVER_CERT_DIR}/pki/private/ca.key" \
  1721. "${KONNECTIVITY_SERVER_CERT_DIR}/pki/ca.crt" \
  1722. "${KONNECTIVITY_SERVER_CERT_DIR}/pki/issued/server.crt" \
  1723. "${KONNECTIVITY_SERVER_CERT_DIR}/pki/private/server.key" \
  1724. "${KONNECTIVITY_SERVER_CERT_DIR}/pki/issued/client.crt" \
  1725. "${KONNECTIVITY_SERVER_CERT_DIR}/pki/private/client.key" \
  1726. "${KONNECTIVITY_AGENT_CERT_DIR}/pki/private/ca.key" \
  1727. "${KONNECTIVITY_AGENT_CERT_DIR}/pki/ca.crt" \
  1728. "${KONNECTIVITY_AGENT_CERT_DIR}/pki/issued/server.crt" \
  1729. "${KONNECTIVITY_AGENT_CERT_DIR}/pki/private/server.key" \
  1730. "${KONNECTIVITY_AGENT_CERT_DIR}/pki/issued/client.crt" \
  1731. "${KONNECTIVITY_AGENT_CERT_DIR}/pki/private/client.key"
  1732. do
  1733. if [[ ! -s "${output_file}" ]]; then
  1734. echo "Expected file ${output_file} not created" >&2
  1735. output_file_missing=1
  1736. fi
  1737. done
  1738. if (( $output_file_missing )); then
  1739. # TODO(roberthbailey,porridge): add better error handling here,
  1740. # see https://github.com/kubernetes/kubernetes/issues/55229
  1741. cat "${cert_create_debug_output}" >&2
  1742. echo "=== Failed to generate konnectivity-server certificates: Aborting ===" >&2
  1743. exit 2
  1744. fi
  1745. }
  1746. # Using provided master env, extracts value from provided key.
  1747. #
  1748. # Args:
  1749. # $1 master env (kube-env of master; result of calling get-master-env)
  1750. # $2 env key to use
  1751. function get-env-val() {
  1752. local match=`(echo "${1}" | grep -E "^${2}:") || echo ""`
  1753. if [[ -z ${match} ]]; then
  1754. echo ""
  1755. fi
  1756. echo ${match} | cut -d : -f 2 | cut -d \' -f 2
  1757. }
  1758. # Load the master env by calling get-master-env, and extract important values
  1759. function parse-master-env() {
  1760. # Get required master env vars
  1761. local master_env=$(get-master-env)
  1762. KUBE_PROXY_TOKEN=$(get-env-val "${master_env}" "KUBE_PROXY_TOKEN")
  1763. NODE_PROBLEM_DETECTOR_TOKEN=$(get-env-val "${master_env}" "NODE_PROBLEM_DETECTOR_TOKEN")
  1764. CA_CERT_BASE64=$(get-env-val "${master_env}" "CA_CERT")
  1765. CA_KEY_BASE64=$(get-env-val "${master_env}" "CA_KEY")
  1766. KUBEAPISERVER_CERT_BASE64=$(get-env-val "${master_env}" "KUBEAPISERVER_CERT")
  1767. KUBEAPISERVER_KEY_BASE64=$(get-env-val "${master_env}" "KUBEAPISERVER_KEY")
  1768. EXTRA_DOCKER_OPTS=$(get-env-val "${master_env}" "EXTRA_DOCKER_OPTS")
  1769. KUBELET_CERT_BASE64=$(get-env-val "${master_env}" "KUBELET_CERT")
  1770. KUBELET_KEY_BASE64=$(get-env-val "${master_env}" "KUBELET_KEY")
  1771. MASTER_CERT_BASE64=$(get-env-val "${master_env}" "MASTER_CERT")
  1772. MASTER_KEY_BASE64=$(get-env-val "${master_env}" "MASTER_KEY")
  1773. AGGREGATOR_CA_KEY_BASE64=$(get-env-val "${master_env}" "AGGREGATOR_CA_KEY")
  1774. REQUESTHEADER_CA_CERT_BASE64=$(get-env-val "${master_env}" "REQUESTHEADER_CA_CERT")
  1775. PROXY_CLIENT_CERT_BASE64=$(get-env-val "${master_env}" "PROXY_CLIENT_CERT")
  1776. PROXY_CLIENT_KEY_BASE64=$(get-env-val "${master_env}" "PROXY_CLIENT_KEY")
  1777. ENABLE_LEGACY_ABAC=$(get-env-val "${master_env}" "ENABLE_LEGACY_ABAC")
  1778. ETCD_APISERVER_CA_KEY_BASE64=$(get-env-val "${master_env}" "ETCD_APISERVER_CA_KEY")
  1779. ETCD_APISERVER_CA_CERT_BASE64=$(get-env-val "${master_env}" "ETCD_APISERVER_CA_CERT")
  1780. ETCD_APISERVER_SERVER_KEY_BASE64=$(get-env-val "${master_env}" "ETCD_APISERVER_SERVER_KEY")
  1781. ETCD_APISERVER_SERVER_CERT_BASE64=$(get-env-val "${master_env}" "ETCD_APISERVER_SERVER_CERT")
  1782. ETCD_APISERVER_CLIENT_KEY_BASE64=$(get-env-val "${master_env}" "ETCD_APISERVER_CLIENT_KEY")
  1783. ETCD_APISERVER_CLIENT_CERT_BASE64=$(get-env-val "${master_env}" "ETCD_APISERVER_CLIENT_CERT")
  1784. KONNECTIVITY_SERVER_CA_KEY_BASE64=$(get-env-val "${master_env}" "KONNECTIVITY_SERVER_CA_KEY")
  1785. KONNECTIVITY_SERVER_CA_CERT_BASE64=$(get-env-val "${master_env}" "KONNECTIVITY_SERVER_CA_CERT")
  1786. KONNECTIVITY_SERVER_CERT_BASE64=$(get-env-val "${master_env}" "KONNECTIVITY_SERVER_CERT")
  1787. KONNECTIVITY_SERVER_KEY_BASE64=$(get-env-val "${master_env}" "KONNECTIVITY_SERVER_KEY")
  1788. KONNECTIVITY_SERVER_CLIENT_CERT_BASE64=$(get-env-val "${master_env}" "KONNECTIVITY_SERVER_CLIENT_CERT")
  1789. KONNECTIVITY_SERVER_CLIENT_KEY_BASE64=$(get-env-val "${master_env}" "KONNECTIVITY_SERVER_CLIENT_KEY")
  1790. KONNECTIVITY_AGENT_CA_KEY_BASE64=$(get-env-val "${master_env}" "KONNECTIVITY_AGENT_CA_KEY")
  1791. KONNECTIVITY_AGENT_CA_CERT_BASE64=$(get-env-val "${master_env}" "KONNECTIVITY_AGENT_CA_CERT")
  1792. KONNECTIVITY_AGENT_CERT_BASE64=$(get-env-val "${master_env}" "KONNECTIVITY_AGENT_CERT")
  1793. KONNECTIVITY_AGENT_KEY_BASE64=$(get-env-val "${master_env}" "KONNECTIVITY_AGENT_KEY")
  1794. }
  1795. # Update or verify required gcloud components are installed
  1796. # at minimum required version.
  1797. # Assumed vars
  1798. # KUBE_PROMPT_FOR_UPDATE
  1799. function update-or-verify-gcloud() {
  1800. local sudo_prefix=""
  1801. if [ ! -w $(dirname `which gcloud`) ]; then
  1802. sudo_prefix="sudo"
  1803. fi
  1804. # update and install components as needed
  1805. if [[ "${KUBE_PROMPT_FOR_UPDATE}" == "y" ]]; then
  1806. ${sudo_prefix} gcloud ${gcloud_prompt:-} components install alpha
  1807. ${sudo_prefix} gcloud ${gcloud_prompt:-} components install beta
  1808. ${sudo_prefix} gcloud ${gcloud_prompt:-} components update
  1809. else
  1810. local version=$(gcloud version --format=json)
  1811. python -c'
  1812. import json,sys
  1813. from distutils import version
  1814. minVersion = version.LooseVersion("1.3.0")
  1815. required = [ "alpha", "beta", "core" ]
  1816. data = json.loads(sys.argv[1])
  1817. rel = data.get("Google Cloud SDK")
  1818. if "CL @" in rel:
  1819. print("Using dev version of gcloud: %s" %rel)
  1820. exit(0)
  1821. if rel != "HEAD" and version.LooseVersion(rel) < minVersion:
  1822. print("gcloud version out of date ( < %s )" % minVersion)
  1823. exit(1)
  1824. missing = []
  1825. for c in required:
  1826. if not data.get(c):
  1827. missing += [c]
  1828. if missing:
  1829. for c in missing:
  1830. print ("missing required gcloud component \"{0}\"".format(c))
  1831. print ("Try running `gcloud components install {0}`".format(c))
  1832. exit(1)
  1833. ' """${version}"""
  1834. fi
  1835. }
  1836. # Robustly try to create a static ip.
  1837. # $1: The name of the ip to create
  1838. # $2: The name of the region to create the ip in.
  1839. function create-static-ip() {
  1840. detect-project
  1841. local attempt=0
  1842. local REGION="$2"
  1843. while true; do
  1844. if gcloud compute addresses create "$1" \
  1845. --project "${PROJECT}" \
  1846. --region "${REGION}" -q > /dev/null; then
  1847. # successful operation - wait until it's visible
  1848. start="$(date +%s)"
  1849. while true; do
  1850. now="$(date +%s)"
  1851. # Timeout set to 15 minutes
  1852. if [[ $((now - start)) -gt 900 ]]; then
  1853. echo "Timeout while waiting for master IP visibility"
  1854. exit 2
  1855. fi
  1856. if gcloud compute addresses describe "$1" --project "${PROJECT}" --region "${REGION}" >/dev/null 2>&1; then
  1857. break
  1858. fi
  1859. echo "Master IP not visible yet. Waiting..."
  1860. sleep 5
  1861. done
  1862. break
  1863. fi
  1864. if gcloud compute addresses describe "$1" \
  1865. --project "${PROJECT}" \
  1866. --region "${REGION}" >/dev/null 2>&1; then
  1867. # it exists - postcondition satisfied
  1868. break
  1869. fi
  1870. if (( attempt > 4 )); then
  1871. echo -e "${color_red}Failed to create static ip $1 ${color_norm}" >&2
  1872. exit 2
  1873. fi
  1874. attempt=$(($attempt+1))
  1875. echo -e "${color_yellow}Attempt $attempt failed to create static ip $1. Retrying.${color_norm}" >&2
  1876. sleep $(($attempt * 5))
  1877. done
  1878. }
  1879. # Robustly try to create a firewall rule.
  1880. # $1: The name of firewall rule.
  1881. # $2: IP ranges.
  1882. # $3: Target tags for this firewall rule.
  1883. function create-firewall-rule() {
  1884. detect-project
  1885. local attempt=0
  1886. while true; do
  1887. if ! gcloud compute firewall-rules create "$1" \
  1888. --project "${NETWORK_PROJECT}" \
  1889. --network "${NETWORK}" \
  1890. --source-ranges "$2" \
  1891. --target-tags "$3" \
  1892. --allow tcp,udp,icmp,esp,ah,sctp; then
  1893. if (( attempt > 4 )); then
  1894. echo -e "${color_red}Failed to create firewall rule $1 ${color_norm}" >&2
  1895. exit 2
  1896. fi
  1897. echo -e "${color_yellow}Attempt $(($attempt+1)) failed to create firewall rule $1. Retrying.${color_norm}" >&2
  1898. attempt=$(($attempt+1))
  1899. sleep $(($attempt * 5))
  1900. else
  1901. break
  1902. fi
  1903. done
  1904. }
  1905. # Format the string argument for gcloud network.
  1906. function make-gcloud-network-argument() {
  1907. local network_project="$1"
  1908. local region="$2"
  1909. local network="$3"
  1910. local subnet="$4"
  1911. local address="$5" # optional
  1912. local enable_ip_alias="$6" # optional
  1913. local alias_size="$7" # optional
  1914. local networkURL="projects/${network_project}/global/networks/${network}"
  1915. local subnetURL="projects/${network_project}/regions/${region}/subnetworks/${subnet:-}"
  1916. local ret=""
  1917. if [[ "${enable_ip_alias}" == 'true' ]]; then
  1918. ret="--network-interface"
  1919. ret="${ret} network=${networkURL}"
  1920. if [[ "${address:-}" == "no-address" ]]; then
  1921. ret="${ret},no-address"
  1922. else
  1923. ret="${ret},address=${address:-}"
  1924. fi
  1925. ret="${ret},subnet=${subnetURL}"
  1926. ret="${ret},aliases=pods-default:${alias_size}"
  1927. ret="${ret} --no-can-ip-forward"
  1928. else
  1929. if [[ -n ${subnet:-} ]]; then
  1930. ret="${ret} --subnet ${subnetURL}"
  1931. else
  1932. ret="${ret} --network ${networkURL}"
  1933. fi
  1934. ret="${ret} --can-ip-forward"
  1935. if [[ -n ${address:-} ]] && [[ "$address" != "no-address" ]]; then
  1936. ret="${ret} --address ${address}"
  1937. fi
  1938. fi
  1939. echo "${ret}"
  1940. }
  1941. # $1: version (required)
  1942. # $2: Prefix for the template name, i.e. NODE_INSTANCE_PREFIX or
  1943. # WINDOWS_NODE_INSTANCE_PREFIX.
  1944. function get-template-name-from-version() {
  1945. local -r version=${1}
  1946. local -r template_prefix=${2}
  1947. # trim template name to pass gce name validation
  1948. echo "${template_prefix}-template-${version}" | cut -c 1-63 | sed 's/[\.\+]/-/g;s/-*$//g'
  1949. }
  1950. # validates the NODE_LOCAL_SSDS_EXT variable
  1951. function validate-node-local-ssds-ext(){
  1952. ssdopts="${1}"
  1953. if [[ -z "${ssdopts[0]}" || -z "${ssdopts[1]}" || -z "${ssdopts[2]}" ]]; then
  1954. echo -e "${color_red}Local SSD: NODE_LOCAL_SSDS_EXT is malformed, found ${ssdopts[0]-_},${ssdopts[1]-_},${ssdopts[2]-_} ${color_norm}" >&2
  1955. exit 2
  1956. fi
  1957. if [[ "${ssdopts[1]}" != "scsi" && "${ssdopts[1]}" != "nvme" ]]; then
  1958. echo -e "${color_red}Local SSD: Interface must be scsi or nvme, found: ${ssdopts[1]} ${color_norm}" >&2
  1959. exit 2
  1960. fi
  1961. if [[ "${ssdopts[2]}" != "fs" && "${ssdopts[2]}" != "block" ]]; then
  1962. echo -e "${color_red}Local SSD: Filesystem type must be fs or block, found: ${ssdopts[2]} ${color_norm}" >&2
  1963. exit 2
  1964. fi
  1965. local_ssd_ext_count=$((local_ssd_ext_count+ssdopts[0]))
  1966. if [[ "${local_ssd_ext_count}" -gt "${GCE_MAX_LOCAL_SSD}" || "${local_ssd_ext_count}" -lt 1 ]]; then
  1967. 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
  1968. exit 2
  1969. fi
  1970. }
  1971. # Robustly try to create an instance template.
  1972. # $1: The name of the instance template.
  1973. # $2: The scopes flag.
  1974. # $3: String of comma-separated metadata-from-file entries.
  1975. # $4: String of comma-separated metadata (key=value) entries.
  1976. # $5: the node OS ("linux" or "windows").
  1977. function create-node-template() {
  1978. detect-project
  1979. detect-subnetworks
  1980. local template_name="$1"
  1981. local metadata_values="$4"
  1982. local os="$5"
  1983. local machine_type="$6"
  1984. # First, ensure the template doesn't exist.
  1985. # TODO(zmerlynn): To make this really robust, we need to parse the output and
  1986. # add retries. Just relying on a non-zero exit code doesn't
  1987. # distinguish an ephemeral failed call from a "not-exists".
  1988. if gcloud compute instance-templates describe "${template_name}" --project "${PROJECT}" &>/dev/null; then
  1989. echo "Instance template ${1} already exists; deleting." >&2
  1990. if ! gcloud compute instance-templates delete "${template_name}" --project "${PROJECT}" --quiet &>/dev/null; then
  1991. echo -e "${color_yellow}Failed to delete existing instance template${color_norm}" >&2
  1992. exit 2
  1993. fi
  1994. fi
  1995. local gcloud="gcloud"
  1996. local accelerator_args=""
  1997. # VMs with Accelerators cannot be live migrated.
  1998. # More details here - https://cloud.google.com/compute/docs/gpus/add-gpus#create-new-gpu-instance
  1999. if [[ ! -z "${NODE_ACCELERATORS}" ]]; then
  2000. accelerator_args="--maintenance-policy TERMINATE --restart-on-failure --accelerator ${NODE_ACCELERATORS}"
  2001. gcloud="gcloud beta"
  2002. fi
  2003. local preemptible_minions=""
  2004. if [[ "${PREEMPTIBLE_NODE}" == "true" ]]; then
  2005. preemptible_minions="--preemptible --maintenance-policy TERMINATE"
  2006. fi
  2007. local local_ssds=""
  2008. local_ssd_ext_count=0
  2009. if [[ ! -z ${NODE_LOCAL_SSDS_EXT:-} ]]; then
  2010. IFS=";" read -r -a ssdgroups <<< "${NODE_LOCAL_SSDS_EXT:-}"
  2011. for ssdgroup in "${ssdgroups[@]}"
  2012. do
  2013. IFS="," read -r -a ssdopts <<< "${ssdgroup}"
  2014. validate-node-local-ssds-ext "${ssdopts}"
  2015. for i in $(seq ${ssdopts[0]}); do
  2016. local_ssds="$local_ssds--local-ssd=interface=${ssdopts[1]} "
  2017. done
  2018. done
  2019. fi
  2020. if [[ ! -z ${NODE_LOCAL_SSDS+x} ]]; then
  2021. # The NODE_LOCAL_SSDS check below fixes issue #49171
  2022. # Some versions of seq will count down from 1 if "seq 0" is specified
  2023. if [[ ${NODE_LOCAL_SSDS} -ge 1 ]]; then
  2024. for i in $(seq ${NODE_LOCAL_SSDS}); do
  2025. local_ssds="$local_ssds--local-ssd=interface=SCSI "
  2026. done
  2027. fi
  2028. fi
  2029. local address=""
  2030. if [[ ${GCE_PRIVATE_CLUSTER:-} == "true" ]]; then
  2031. address="no-address"
  2032. fi
  2033. local network=$(make-gcloud-network-argument \
  2034. "${NETWORK_PROJECT}" \
  2035. "${REGION}" \
  2036. "${NETWORK}" \
  2037. "${SUBNETWORK:-}" \
  2038. "${address}" \
  2039. "${ENABLE_IP_ALIASES:-}" \
  2040. "${IP_ALIAS_SIZE:-}")
  2041. local node_image_flags=""
  2042. if [[ "${os}" == 'linux' ]]; then
  2043. node_image_flags="--image-project ${NODE_IMAGE_PROJECT} --image ${NODE_IMAGE}"
  2044. elif [[ "${os}" == 'windows' ]]; then
  2045. node_image_flags="--image-project ${WINDOWS_NODE_IMAGE_PROJECT} --image ${WINDOWS_NODE_IMAGE}"
  2046. else
  2047. echo "Unknown OS ${os}" >&2
  2048. exit 1
  2049. fi
  2050. local metadata_flag="${metadata_values:+--metadata ${metadata_values}}"
  2051. local attempt=1
  2052. while true; do
  2053. echo "Attempt ${attempt} to create ${1}" >&2
  2054. if ! ${gcloud} compute instance-templates create \
  2055. "${template_name}" \
  2056. --project "${PROJECT}" \
  2057. --machine-type "${machine_type}" \
  2058. --boot-disk-type "${NODE_DISK_TYPE}" \
  2059. --boot-disk-size "${NODE_DISK_SIZE}" \
  2060. ${node_image_flags} \
  2061. --service-account "${NODE_SERVICE_ACCOUNT}" \
  2062. --tags "${NODE_TAG}" \
  2063. ${accelerator_args} \
  2064. ${local_ssds} \
  2065. --region "${REGION}" \
  2066. ${network} \
  2067. ${preemptible_minions} \
  2068. $2 \
  2069. --metadata-from-file $3 \
  2070. ${metadata_flag} >&2; then
  2071. if (( attempt > 5 )); then
  2072. echo -e "${color_red}Failed to create instance template ${template_name} ${color_norm}" >&2
  2073. exit 2
  2074. fi
  2075. echo -e "${color_yellow}Attempt ${attempt} failed to create instance template ${template_name}. Retrying.${color_norm}" >&2
  2076. attempt=$(($attempt+1))
  2077. sleep $(($attempt * 5))
  2078. # In case the previous attempt failed with something like a
  2079. # Backend Error and left the entry laying around, delete it
  2080. # before we try again.
  2081. gcloud compute instance-templates delete "${template_name}" --project "${PROJECT}" &>/dev/null || true
  2082. else
  2083. break
  2084. fi
  2085. done
  2086. }
  2087. # Instantiate a kubernetes cluster
  2088. #
  2089. # Assumed vars
  2090. # KUBE_ROOT
  2091. # <Various vars set in config file>
  2092. function kube-up() {
  2093. kube::util::ensure-temp-dir
  2094. detect-project
  2095. load-or-gen-kube-basicauth
  2096. load-or-gen-kube-bearertoken
  2097. # Make sure we have the tar files staged on Google Storage
  2098. find-release-tars
  2099. upload-tars
  2100. # ensure that environmental variables specifying number of migs to create
  2101. set_num_migs
  2102. if [[ ${KUBE_USE_EXISTING_MASTER:-} == "true" ]]; then
  2103. detect-master
  2104. parse-master-env
  2105. create-subnetworks
  2106. detect-subnetworks
  2107. # Windows nodes take longer to boot and setup so create them first.
  2108. create-windows-nodes
  2109. create-linux-nodes
  2110. elif [[ ${KUBE_REPLICATE_EXISTING_MASTER:-} == "true" ]]; then
  2111. detect-master
  2112. if [[ "${MASTER_OS_DISTRIBUTION}" != "gci" && "${MASTER_OS_DISTRIBUTION}" != "ubuntu" ]]; then
  2113. echo "Master replication supported only for gci and ubuntu"
  2114. return 1
  2115. fi
  2116. if [[ ${GCE_PRIVATE_CLUSTER:-} == "true" ]]; then
  2117. create-internal-loadbalancer
  2118. fi
  2119. create-loadbalancer
  2120. # If replication of master fails, we need to ensure that the replica is removed from etcd clusters.
  2121. if ! replicate-master; then
  2122. remove-replica-from-etcd 2379 true || true
  2123. remove-replica-from-etcd 4002 false || true
  2124. fi
  2125. else
  2126. check-existing
  2127. create-network
  2128. create-subnetworks
  2129. detect-subnetworks
  2130. create-cloud-nat-router
  2131. write-cluster-location
  2132. write-cluster-name
  2133. create-autoscaler-config
  2134. create-master
  2135. create-nodes-firewall
  2136. create-nodes-template
  2137. if [[ "${KUBE_CREATE_NODES}" == "true" ]]; then
  2138. # Windows nodes take longer to boot and setup so create them first.
  2139. create-windows-nodes
  2140. create-linux-nodes
  2141. fi
  2142. check-cluster
  2143. fi
  2144. }
  2145. function check-existing() {
  2146. local running_in_terminal=false
  2147. # May be false if tty is not allocated (for example with ssh -T).
  2148. if [[ -t 1 ]]; then
  2149. running_in_terminal=true
  2150. fi
  2151. if [[ ${running_in_terminal} == "true" || ${KUBE_UP_AUTOMATIC_CLEANUP} == "true" ]]; then
  2152. if ! check-resources; then
  2153. local run_kube_down="n"
  2154. echo "${KUBE_RESOURCE_FOUND} found." >&2
  2155. # Get user input only if running in terminal.
  2156. if [[ ${running_in_terminal} == "true" && ${KUBE_UP_AUTOMATIC_CLEANUP} == "false" ]]; then
  2157. read -p "Would you like to shut down the old cluster (call kube-down)? [y/N] " run_kube_down
  2158. fi
  2159. if [[ ${run_kube_down} == "y" || ${run_kube_down} == "Y" || ${KUBE_UP_AUTOMATIC_CLEANUP} == "true" ]]; then
  2160. echo "... calling kube-down" >&2
  2161. kube-down
  2162. fi
  2163. fi
  2164. fi
  2165. }
  2166. function check-network-mode() {
  2167. local mode="$(gcloud compute networks list --filter="name=('${NETWORK}')" --project ${NETWORK_PROJECT} --format='value(x_gcloud_subnet_mode)' || true)"
  2168. # The deprecated field uses lower case. Convert to upper case for consistency.
  2169. echo "$(echo $mode | tr [a-z] [A-Z])"
  2170. }
  2171. function create-network() {
  2172. if ! gcloud compute networks --project "${NETWORK_PROJECT}" describe "${NETWORK}" &>/dev/null; then
  2173. # The network needs to be created synchronously or we have a race. The
  2174. # firewalls can be added concurrent with instance creation.
  2175. local network_mode="auto"
  2176. if [[ "${CREATE_CUSTOM_NETWORK:-}" == "true" ]]; then
  2177. network_mode="custom"
  2178. fi
  2179. echo "Creating new ${network_mode} network: ${NETWORK}"
  2180. gcloud compute networks create --project "${NETWORK_PROJECT}" "${NETWORK}" --subnet-mode="${network_mode}"
  2181. else
  2182. PREEXISTING_NETWORK=true
  2183. PREEXISTING_NETWORK_MODE="$(check-network-mode)"
  2184. echo "Found existing network ${NETWORK} in ${PREEXISTING_NETWORK_MODE} mode."
  2185. fi
  2186. if ! gcloud compute firewall-rules --project "${NETWORK_PROJECT}" describe "${CLUSTER_NAME}-default-internal-master" &>/dev/null; then
  2187. gcloud compute firewall-rules create "${CLUSTER_NAME}-default-internal-master" \
  2188. --project "${NETWORK_PROJECT}" \
  2189. --network "${NETWORK}" \
  2190. --source-ranges "10.0.0.0/8" \
  2191. --allow "tcp:1-2379,tcp:2382-65535,udp:1-65535,icmp" \
  2192. --target-tags "${MASTER_TAG}"&
  2193. fi
  2194. if ! gcloud compute firewall-rules --project "${NETWORK_PROJECT}" describe "${CLUSTER_NAME}-default-internal-node" &>/dev/null; then
  2195. gcloud compute firewall-rules create "${CLUSTER_NAME}-default-internal-node" \
  2196. --project "${NETWORK_PROJECT}" \
  2197. --network "${NETWORK}" \
  2198. --source-ranges "10.0.0.0/8" \
  2199. --allow "tcp:1-65535,udp:1-65535,icmp" \
  2200. --target-tags "${NODE_TAG}"&
  2201. fi
  2202. if ! gcloud compute firewall-rules describe --project "${NETWORK_PROJECT}" "${NETWORK}-default-ssh" &>/dev/null; then
  2203. gcloud compute firewall-rules create "${NETWORK}-default-ssh" \
  2204. --project "${NETWORK_PROJECT}" \
  2205. --network "${NETWORK}" \
  2206. --source-ranges "0.0.0.0/0" \
  2207. --allow "tcp:22" &
  2208. fi
  2209. # Open up TCP 3389 to allow RDP connections.
  2210. if [[ ${NUM_WINDOWS_NODES} -gt 0 ]]; then
  2211. if ! gcloud compute firewall-rules describe --project "${NETWORK_PROJECT}" "${NETWORK}-default-rdp" &>/dev/null; then
  2212. gcloud compute firewall-rules create "${NETWORK}-default-rdp" \
  2213. --project "${NETWORK_PROJECT}" \
  2214. --network "${NETWORK}" \
  2215. --source-ranges "0.0.0.0/0" \
  2216. --allow "tcp:3389" &
  2217. fi
  2218. fi
  2219. }
  2220. function expand-default-subnetwork() {
  2221. gcloud compute networks update "${NETWORK}" \
  2222. --switch-to-custom-subnet-mode \
  2223. --project "${NETWORK_PROJECT}" \
  2224. --quiet || true
  2225. gcloud compute networks subnets expand-ip-range "${NETWORK}" \
  2226. --region="${REGION}" \
  2227. --project "${NETWORK_PROJECT}" \
  2228. --prefix-length=19 \
  2229. --quiet
  2230. }
  2231. function create-subnetworks() {
  2232. case ${ENABLE_IP_ALIASES} in
  2233. true) echo "IP aliases are enabled. Creating subnetworks.";;
  2234. false)
  2235. echo "IP aliases are disabled."
  2236. if [[ "${ENABLE_BIG_CLUSTER_SUBNETS}" = "true" ]]; then
  2237. if [[ "${PREEXISTING_NETWORK}" != "true" ]]; then
  2238. expand-default-subnetwork
  2239. else
  2240. echo "${color_yellow}Using pre-existing network ${NETWORK}, subnets won't be expanded to /19!${color_norm}"
  2241. fi
  2242. elif [[ "${CREATE_CUSTOM_NETWORK:-}" == "true" && "${PREEXISTING_NETWORK}" != "true" ]]; then
  2243. gcloud compute networks subnets create "${SUBNETWORK}" --project "${NETWORK_PROJECT}" --region "${REGION}" --network "${NETWORK}" --range "${NODE_IP_RANGE}"
  2244. fi
  2245. return;;
  2246. *) echo "${color_red}Invalid argument to ENABLE_IP_ALIASES${color_norm}"
  2247. exit 1;;
  2248. esac
  2249. # Look for the alias subnet, it must exist and have a secondary
  2250. # range configured.
  2251. local subnet=$(gcloud compute networks subnets describe \
  2252. --project "${NETWORK_PROJECT}" \
  2253. --region ${REGION} \
  2254. ${IP_ALIAS_SUBNETWORK} 2>/dev/null)
  2255. if [[ -z ${subnet} ]]; then
  2256. echo "Creating subnet ${NETWORK}:${IP_ALIAS_SUBNETWORK}"
  2257. gcloud compute networks subnets create \
  2258. ${IP_ALIAS_SUBNETWORK} \
  2259. --description "Automatically generated subnet for ${INSTANCE_PREFIX} cluster. This will be removed on cluster teardown." \
  2260. --project "${NETWORK_PROJECT}" \
  2261. --network ${NETWORK} \
  2262. --region ${REGION} \
  2263. --range ${NODE_IP_RANGE} \
  2264. --secondary-range "pods-default=${CLUSTER_IP_RANGE}" \
  2265. --secondary-range "services-default=${SERVICE_CLUSTER_IP_RANGE}"
  2266. echo "Created subnetwork ${IP_ALIAS_SUBNETWORK}"
  2267. else
  2268. if ! echo ${subnet} | grep --quiet secondaryIpRanges; then
  2269. echo "${color_red}Subnet ${IP_ALIAS_SUBNETWORK} does not have a secondary range${color_norm}"
  2270. exit 1
  2271. fi
  2272. fi
  2273. }
  2274. # detect-subnetworks sets the SUBNETWORK var if not already set
  2275. # Assumed vars:
  2276. # NETWORK
  2277. # REGION
  2278. # NETWORK_PROJECT
  2279. #
  2280. # Optional vars:
  2281. # SUBNETWORK
  2282. # IP_ALIAS_SUBNETWORK
  2283. function detect-subnetworks() {
  2284. if [[ -n ${SUBNETWORK:-} ]]; then
  2285. echo "Using subnet ${SUBNETWORK}"
  2286. return 0
  2287. fi
  2288. if [[ -n ${IP_ALIAS_SUBNETWORK:-} ]]; then
  2289. SUBNETWORK=${IP_ALIAS_SUBNETWORK}
  2290. echo "Using IP Alias subnet ${SUBNETWORK}"
  2291. return 0
  2292. fi
  2293. SUBNETWORK=$(gcloud compute networks subnets list \
  2294. --network=${NETWORK} \
  2295. --regions=${REGION} \
  2296. --project=${NETWORK_PROJECT} \
  2297. --limit=1 \
  2298. --format='value(name)' 2>/dev/null)
  2299. if [[ -n ${SUBNETWORK:-} ]]; then
  2300. echo "Found subnet for region ${REGION} in network ${NETWORK}: ${SUBNETWORK}"
  2301. return 0
  2302. fi
  2303. echo "${color_red}Could not find subnetwork with region ${REGION}, network ${NETWORK}, and project ${NETWORK_PROJECT}"
  2304. }
  2305. # Sets up Cloud NAT for the network.
  2306. # Assumed vars:
  2307. # NETWORK_PROJECT
  2308. # REGION
  2309. # NETWORK
  2310. function create-cloud-nat-router() {
  2311. if [[ ${GCE_PRIVATE_CLUSTER:-} == "true" ]]; then
  2312. if gcloud compute routers describe "$NETWORK-nat-router" --project $NETWORK_PROJECT --region $REGION &>/dev/null; then
  2313. echo "Cloud nat already exists"
  2314. return 0
  2315. fi
  2316. gcloud compute routers create "$NETWORK-nat-router" \
  2317. --project $NETWORK_PROJECT \
  2318. --region $REGION \
  2319. --network $NETWORK
  2320. gcloud compute routers nats create "$NETWORK-nat-config" \
  2321. --project $NETWORK_PROJECT \
  2322. --router-region $REGION \
  2323. --router "$NETWORK-nat-router" \
  2324. --nat-primary-subnet-ip-ranges \
  2325. --auto-allocate-nat-external-ips \
  2326. ${GCE_PRIVATE_CLUSTER_PORTS_PER_VM:+--min-ports-per-vm ${GCE_PRIVATE_CLUSTER_PORTS_PER_VM}}
  2327. fi
  2328. }
  2329. function delete-all-firewall-rules() {
  2330. if fws=$(gcloud compute firewall-rules list --project "${NETWORK_PROJECT}" --filter="network=${NETWORK}" --format="value(name)"); then
  2331. echo "Deleting firewall rules remaining in network ${NETWORK}: ${fws}"
  2332. delete-firewall-rules "$fws"
  2333. else
  2334. echo "Failed to list firewall rules from the network ${NETWORK}"
  2335. fi
  2336. }
  2337. # Ignores firewall rule arguments that do not exist in NETWORK_PROJECT.
  2338. function delete-firewall-rules() {
  2339. for fw in $@; do
  2340. if [[ -n $(gcloud compute firewall-rules --project "${NETWORK_PROJECT}" describe "${fw}" --format='value(name)' 2>/dev/null || true) ]]; then
  2341. gcloud compute firewall-rules delete --project "${NETWORK_PROJECT}" --quiet "${fw}" &
  2342. fi
  2343. done
  2344. kube::util::wait-for-jobs || {
  2345. echo -e "${color_red}Failed to delete firewall rules.${color_norm}" >&2
  2346. }
  2347. }
  2348. function delete-network() {
  2349. if [[ -n $(gcloud compute networks --project "${NETWORK_PROJECT}" describe "${NETWORK}" --format='value(name)' 2>/dev/null || true) ]]; then
  2350. if ! gcloud compute networks delete --project "${NETWORK_PROJECT}" --quiet "${NETWORK}"; then
  2351. echo "Failed to delete network '${NETWORK}'. Listing firewall-rules:"
  2352. gcloud compute firewall-rules --project "${NETWORK_PROJECT}" list --filter="network=${NETWORK}"
  2353. return 1
  2354. fi
  2355. fi
  2356. }
  2357. function delete-cloud-nat-router() {
  2358. if [[ ${GCE_PRIVATE_CLUSTER:-} == "true" ]]; then
  2359. if [[ -n $(gcloud compute routers describe --project "${NETWORK_PROJECT}" --region "${REGION}" "${NETWORK}-nat-router" --format='value(name)' 2>/dev/null || true) ]]; then
  2360. echo "Deleting Cloud NAT router..."
  2361. gcloud compute routers delete --project "${NETWORK_PROJECT}" --region "${REGION}" --quiet "${NETWORK}-nat-router"
  2362. fi
  2363. fi
  2364. }
  2365. function delete-subnetworks() {
  2366. # If running in custom mode network we need to delete subnets manually.
  2367. mode="$(check-network-mode)"
  2368. if [[ "${mode}" == "CUSTOM" ]]; then
  2369. if [[ "${ENABLE_BIG_CLUSTER_SUBNETS}" = "true" ]]; then
  2370. echo "Deleting default subnets..."
  2371. # This value should be kept in sync with number of regions.
  2372. local parallelism=9
  2373. gcloud compute networks subnets list --network="${NETWORK}" --project "${NETWORK_PROJECT}" --format='value(region.basename())' | \
  2374. xargs -i -P ${parallelism} gcloud --quiet compute networks subnets delete "${NETWORK}" --project "${NETWORK_PROJECT}" --region="{}" || true
  2375. elif [[ "${CREATE_CUSTOM_NETWORK:-}" == "true" ]]; then
  2376. echo "Deleting custom subnet..."
  2377. gcloud --quiet compute networks subnets delete "${SUBNETWORK}" --project "${NETWORK_PROJECT}" --region="${REGION}" || true
  2378. fi
  2379. return
  2380. fi
  2381. # If we reached here, it means we're not using custom network.
  2382. # So the only thing we need to check is if IP-aliases was turned
  2383. # on and we created a subnet for it. If so, we should delete it.
  2384. if [[ ${ENABLE_IP_ALIASES:-} == "true" ]]; then
  2385. # Only delete the subnet if we created it (i.e it's not pre-existing).
  2386. if [[ -z "${KUBE_GCE_IP_ALIAS_SUBNETWORK:-}" ]]; then
  2387. echo "Removing auto-created subnet ${NETWORK}:${IP_ALIAS_SUBNETWORK}"
  2388. if [[ -n $(gcloud compute networks subnets describe \
  2389. --project "${NETWORK_PROJECT}" \
  2390. --region ${REGION} \
  2391. ${IP_ALIAS_SUBNETWORK} 2>/dev/null) ]]; then
  2392. gcloud --quiet compute networks subnets delete \
  2393. --project "${NETWORK_PROJECT}" \
  2394. --region ${REGION} \
  2395. ${IP_ALIAS_SUBNETWORK}
  2396. fi
  2397. fi
  2398. fi
  2399. }
  2400. # Generates SSL certificates for etcd cluster peer to peer communication. Uses cfssl program.
  2401. #
  2402. # Assumed vars:
  2403. # KUBE_TEMP: temporary directory
  2404. #
  2405. # Args:
  2406. # $1: host name
  2407. # $2: CA certificate
  2408. # $3: CA key
  2409. #
  2410. # If CA cert/key is empty, the function will also generate certs for CA.
  2411. #
  2412. # Vars set:
  2413. # ETCD_CA_KEY_BASE64
  2414. # ETCD_CA_CERT_BASE64
  2415. # ETCD_PEER_KEY_BASE64
  2416. # ETCD_PEER_CERT_BASE64
  2417. #
  2418. function create-etcd-certs {
  2419. local host=${1}
  2420. local ca_cert=${2:-}
  2421. local ca_key=${3:-}
  2422. GEN_ETCD_CA_CERT="${ca_cert}" GEN_ETCD_CA_KEY="${ca_key}" \
  2423. generate-etcd-cert "${KUBE_TEMP}/cfssl" "${host}" "peer" "peer"
  2424. pushd "${KUBE_TEMP}/cfssl"
  2425. ETCD_CA_KEY_BASE64=$(cat "ca-key.pem" | base64 | tr -d '\r\n')
  2426. ETCD_CA_CERT_BASE64=$(cat "ca.pem" | gzip | base64 | tr -d '\r\n')
  2427. ETCD_PEER_KEY_BASE64=$(cat "peer-key.pem" | base64 | tr -d '\r\n')
  2428. ETCD_PEER_CERT_BASE64=$(cat "peer.pem" | gzip | base64 | tr -d '\r\n')
  2429. popd
  2430. }
  2431. # Generates SSL certificates for etcd-client and kube-apiserver communication. Uses cfssl program.
  2432. #
  2433. # Assumed vars:
  2434. # KUBE_TEMP: temporary directory
  2435. #
  2436. # Args:
  2437. # $1: host server name
  2438. # $2: host client name
  2439. # $3: CA certificate
  2440. # $4: CA key
  2441. #
  2442. # If CA cert/key is empty, the function will also generate certs for CA.
  2443. #
  2444. # Vars set:
  2445. # ETCD_APISERVER_CA_KEY_BASE64
  2446. # ETCD_APISERVER_CA_CERT_BASE64
  2447. # ETCD_APISERVER_SERVER_KEY_BASE64
  2448. # ETCD_APISERVER_SERVER_CERT_BASE64
  2449. # ETCD_APISERVER_CLIENT_KEY_BASE64
  2450. # ETCD_APISERVER_CLIENT_CERT_BASE64
  2451. #
  2452. function create-etcd-apiserver-certs {
  2453. local hostServer=${1}
  2454. local hostClient=${2}
  2455. local etcd_apiserver_ca_cert=${3:-}
  2456. local etcd_apiserver_ca_key=${4:-}
  2457. GEN_ETCD_CA_CERT="${etcd_apiserver_ca_cert}" GEN_ETCD_CA_KEY="${etcd_apiserver_ca_key}" \
  2458. generate-etcd-cert "${KUBE_TEMP}/cfssl" "${hostServer}" "server" "etcd-apiserver-server"
  2459. generate-etcd-cert "${KUBE_TEMP}/cfssl" "${hostClient}" "client" "etcd-apiserver-client"
  2460. pushd "${KUBE_TEMP}/cfssl"
  2461. ETCD_APISERVER_CA_KEY_BASE64=$(cat "ca-key.pem" | base64 | tr -d '\r\n')
  2462. ETCD_APISERVER_CA_CERT_BASE64=$(cat "ca.pem" | gzip | base64 | tr -d '\r\n')
  2463. ETCD_APISERVER_SERVER_KEY_BASE64=$(cat "etcd-apiserver-server-key.pem" | base64 | tr -d '\r\n')
  2464. ETCD_APISERVER_SERVER_CERT_BASE64=$(cat "etcd-apiserver-server.pem" | gzip | base64 | tr -d '\r\n')
  2465. ETCD_APISERVER_CLIENT_KEY_BASE64=$(cat "etcd-apiserver-client-key.pem" | base64 | tr -d '\r\n')
  2466. ETCD_APISERVER_CLIENT_CERT_BASE64=$(cat "etcd-apiserver-client.pem" | gzip | base64 | tr -d '\r\n')
  2467. popd
  2468. }
  2469. function create-master() {
  2470. echo "Starting master and configuring firewalls"
  2471. gcloud compute firewall-rules create "${MASTER_NAME}-https" \
  2472. --project "${NETWORK_PROJECT}" \
  2473. --network "${NETWORK}" \
  2474. --target-tags "${MASTER_TAG}" \
  2475. --allow tcp:443 &
  2476. echo "Configuring firewall for apiserver konnectivity server"
  2477. if [[ "${ENABLE_EGRESS_VIA_KONNECTIVITY_SERVICE:-false}" == "true" ]]; then
  2478. gcloud compute firewall-rules create "${MASTER_NAME}-konnectivity-server" \
  2479. --project "${NETWORK_PROJECT}" \
  2480. --network "${NETWORK}" \
  2481. --target-tags "${MASTER_TAG}" \
  2482. --allow tcp:8132 &
  2483. fi
  2484. # We have to make sure the disk is created before creating the master VM, so
  2485. # run this in the foreground.
  2486. gcloud compute disks create "${MASTER_NAME}-pd" \
  2487. --project "${PROJECT}" \
  2488. --zone "${ZONE}" \
  2489. --type "${MASTER_DISK_TYPE}" \
  2490. --size "${MASTER_DISK_SIZE}"
  2491. # Create rule for accessing and securing etcd servers.
  2492. if ! gcloud compute firewall-rules --project "${NETWORK_PROJECT}" describe "${MASTER_NAME}-etcd" &>/dev/null; then
  2493. gcloud compute firewall-rules create "${MASTER_NAME}-etcd" \
  2494. --project "${NETWORK_PROJECT}" \
  2495. --network "${NETWORK}" \
  2496. --source-tags "${MASTER_TAG}" \
  2497. --allow "tcp:2380,tcp:2381" \
  2498. --target-tags "${MASTER_TAG}" &
  2499. fi
  2500. # Generate a bearer token for this cluster. We push this separately
  2501. # from the other cluster variables so that the client (this
  2502. # computer) can forget it later. This should disappear with
  2503. # http://issue.k8s.io/3168
  2504. 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)
  2505. if [[ "${ENABLE_NODE_PROBLEM_DETECTOR:-}" == "standalone" ]]; then
  2506. 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)
  2507. fi
  2508. # Reserve the master's IP so that it can later be transferred to another VM
  2509. # without disrupting the kubelets.
  2510. create-static-ip "${MASTER_NAME}-ip" "${REGION}"
  2511. MASTER_RESERVED_IP=$(gcloud compute addresses describe "${MASTER_NAME}-ip" \
  2512. --project "${PROJECT}" --region "${REGION}" -q --format='value(address)')
  2513. if [[ "${REGISTER_MASTER_KUBELET:-}" == "true" ]]; then
  2514. KUBELET_APISERVER="${MASTER_RESERVED_IP}"
  2515. fi
  2516. KUBERNETES_MASTER_NAME="${MASTER_RESERVED_IP}"
  2517. MASTER_ADVERTISE_ADDRESS="${MASTER_RESERVED_IP}"
  2518. MASTER_INTERNAL_IP=""
  2519. if [[ ${GCE_PRIVATE_CLUSTER:-} == "true" ]]; then
  2520. gcloud compute addresses create "${MASTER_NAME}-internal-ip" --project "${PROJECT}" --region $REGION --subnet $SUBNETWORK
  2521. MASTER_INTERNAL_IP=$(gcloud compute addresses describe "${MASTER_NAME}-internal-ip" --project "${PROJECT}" --region "${REGION}" -q --format='value(address)')
  2522. echo "Master internal ip is: $MASTER_INTERNAL_IP"
  2523. KUBERNETES_MASTER_NAME="${MASTER_INTERNAL_IP}"
  2524. MASTER_ADVERTISE_ADDRESS="${MASTER_INTERNAL_IP}"
  2525. fi
  2526. create-certs "${MASTER_RESERVED_IP}" "${MASTER_INTERNAL_IP}"
  2527. create-etcd-certs ${MASTER_NAME}
  2528. create-etcd-apiserver-certs "etcd-${MASTER_NAME}" ${MASTER_NAME}
  2529. if [[ "$(get-num-nodes)" -ge "50" ]]; then
  2530. # We block on master creation for large clusters to avoid doing too much
  2531. # unnecessary work in case master start-up fails (like creation of nodes).
  2532. create-master-instance "${MASTER_RESERVED_IP}" "${MASTER_INTERNAL_IP}"
  2533. else
  2534. create-master-instance "${MASTER_RESERVED_IP}" "${MASTER_INTERNAL_IP}" &
  2535. fi
  2536. }
  2537. # Adds master replica to etcd cluster.
  2538. #
  2539. # Assumed vars:
  2540. # REPLICA_NAME
  2541. # PROJECT
  2542. # EXISTING_MASTER_NAME
  2543. # EXISTING_MASTER_ZONE
  2544. #
  2545. # $1: etcd client port
  2546. # $2: etcd internal port
  2547. # $3: whether etcd communication should use mtls
  2548. # returns the result of ssh command which adds replica
  2549. function add-replica-to-etcd() {
  2550. local -r client_port="${1}"
  2551. local -r internal_port="${2}"
  2552. local -r use_mtls="${3}"
  2553. TLSARG=""
  2554. PROTO="http://"
  2555. if [[ "${use_mtls}" == "true" ]]; then
  2556. # Keep in sync with ETCD_APISERVER_CA_CERT_PATH, ETCD_APISERVER_CLIENT_CERT_PATH and ETCD_APISERVER_CLIENT_KEY_PATH in configure-helper.sh.
  2557. 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"
  2558. PROTO="https://"
  2559. fi
  2560. 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"
  2561. return $?
  2562. }
  2563. # Sets EXISTING_MASTER_NAME and EXISTING_MASTER_ZONE variables.
  2564. #
  2565. # Assumed vars:
  2566. # PROJECT
  2567. #
  2568. # NOTE: Must be in sync with get-replica-name-regexp
  2569. function set-existing-master() {
  2570. local existing_master=$(gcloud compute instances list \
  2571. --project "${PROJECT}" \
  2572. --filter "name ~ '$(get-replica-name-regexp)'" \
  2573. --format "value(name,zone)" | head -n1)
  2574. EXISTING_MASTER_NAME="$(echo "${existing_master}" | cut -f1)"
  2575. EXISTING_MASTER_ZONE="$(echo "${existing_master}" | cut -f2)"
  2576. }
  2577. function replicate-master() {
  2578. set-replica-name
  2579. set-existing-master
  2580. echo "Experimental: replicating existing master ${EXISTING_MASTER_ZONE}/${EXISTING_MASTER_NAME} as ${ZONE}/${REPLICA_NAME}"
  2581. # Before we do anything else, we should configure etcd to expect more replicas.
  2582. if ! add-replica-to-etcd 2379 2380 true; then
  2583. echo "Failed to add master replica to etcd cluster."
  2584. return 1
  2585. fi
  2586. if ! add-replica-to-etcd 4002 2381 false; then
  2587. echo "Failed to add master replica to etcd events cluster."
  2588. return 1
  2589. fi
  2590. # We have to make sure the disk is created before creating the master VM, so
  2591. # run this in the foreground.
  2592. gcloud compute disks create "${REPLICA_NAME}-pd" \
  2593. --project "${PROJECT}" \
  2594. --zone "${ZONE}" \
  2595. --type "${MASTER_DISK_TYPE}" \
  2596. --size "${MASTER_DISK_SIZE}"
  2597. local existing_master_replicas="$(get-all-replica-names)"
  2598. replicate-master-instance "${EXISTING_MASTER_ZONE}" "${EXISTING_MASTER_NAME}" "${existing_master_replicas}"
  2599. # Add new replica to the load balancer.
  2600. gcloud compute target-pools add-instances "${MASTER_NAME}" \
  2601. --project "${PROJECT}" \
  2602. --zone "${ZONE}" \
  2603. --instances "${REPLICA_NAME}"
  2604. if [[ "${GCE_PRIVATE_CLUSTER:-}" == "true" ]]; then
  2605. add-to-internal-loadbalancer "${REPLICA_NAME}" "${ZONE}"
  2606. fi
  2607. }
  2608. # Detaches old and ataches new external IP to a VM.
  2609. #
  2610. # Arguments:
  2611. # $1 - VM name
  2612. # $2 - VM zone
  2613. # $3 - external static IP; if empty will use an ephemeral IP address.
  2614. function attach-external-ip() {
  2615. local NAME=${1}
  2616. local ZONE=${2}
  2617. local IP_ADDR=${3:-}
  2618. local ACCESS_CONFIG_NAME=$(gcloud compute instances describe "${NAME}" \
  2619. --project "${PROJECT}" --zone "${ZONE}" \
  2620. --format="value(networkInterfaces[0].accessConfigs[0].name)")
  2621. gcloud compute instances delete-access-config "${NAME}" \
  2622. --project "${PROJECT}" --zone "${ZONE}" \
  2623. --access-config-name "${ACCESS_CONFIG_NAME}"
  2624. if [[ -z ${IP_ADDR} ]]; then
  2625. gcloud compute instances add-access-config "${NAME}" \
  2626. --project "${PROJECT}" --zone "${ZONE}" \
  2627. --access-config-name "${ACCESS_CONFIG_NAME}"
  2628. else
  2629. gcloud compute instances add-access-config "${NAME}" \
  2630. --project "${PROJECT}" --zone "${ZONE}" \
  2631. --access-config-name "${ACCESS_CONFIG_NAME}" \
  2632. --address "${IP_ADDR}"
  2633. fi
  2634. }
  2635. # Creates load balancer in front of apiserver if it doesn't exists already. Assumes there's only one
  2636. # existing master replica.
  2637. #
  2638. # Assumes:
  2639. # PROJECT
  2640. # MASTER_NAME
  2641. # ZONE
  2642. # REGION
  2643. function create-loadbalancer() {
  2644. # Step 0: Return early if LB is already configured.
  2645. if gcloud compute forwarding-rules describe ${MASTER_NAME} \
  2646. --project "${PROJECT}" --region ${REGION} > /dev/null 2>&1; then
  2647. echo "Load balancer already exists"
  2648. return
  2649. fi
  2650. local EXISTING_MASTER_NAME="$(get-all-replica-names)"
  2651. local EXISTING_MASTER_ZONE=$(gcloud compute instances list "${EXISTING_MASTER_NAME}" \
  2652. --project "${PROJECT}" --format="value(zone)")
  2653. echo "Creating load balancer in front of an already existing master in ${EXISTING_MASTER_ZONE}"
  2654. # Step 1: Detach master IP address and attach ephemeral address to the existing master
  2655. attach-external-ip "${EXISTING_MASTER_NAME}" "${EXISTING_MASTER_ZONE}"
  2656. # Step 2: Create target pool.
  2657. gcloud compute target-pools create "${MASTER_NAME}" --project "${PROJECT}" --region "${REGION}"
  2658. # TODO: We should also add master instances with suffixes
  2659. gcloud compute target-pools add-instances "${MASTER_NAME}" --instances "${EXISTING_MASTER_NAME}" --project "${PROJECT}" --zone "${EXISTING_MASTER_ZONE}"
  2660. # Step 3: Create forwarding rule.
  2661. # TODO: This step can take up to 20 min. We need to speed this up...
  2662. gcloud compute forwarding-rules create ${MASTER_NAME} \
  2663. --project "${PROJECT}" --region ${REGION} \
  2664. --target-pool ${MASTER_NAME} --address=${KUBE_MASTER_IP} --ports=443
  2665. echo -n "Waiting for the load balancer configuration to propagate..."
  2666. local counter=0
  2667. until $(curl -k -m1 https://${KUBE_MASTER_IP} &> /dev/null); do
  2668. counter=$((counter+1))
  2669. echo -n .
  2670. if [[ ${counter} -ge 1800 ]]; then
  2671. echo -e "${color_red}TIMEOUT${color_norm}" >&2
  2672. echo -e "${color_red}Load balancer failed to initialize within ${counter} seconds.${color_norm}" >&2
  2673. exit 2
  2674. fi
  2675. done
  2676. echo "DONE"
  2677. }
  2678. # attach-internal-master-ip attach internal ip to existing master.
  2679. #
  2680. # Assumes:
  2681. # * PROJECT
  2682. function attach-internal-master-ip() {
  2683. local name="${1}"
  2684. local zone="${2}"
  2685. local ip="${3}"
  2686. 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';' -)
  2687. aliases="${aliases:+${aliases};}${ip}/32"
  2688. echo "Setting ${name}'s aliases to '${aliases}' (added ${ip})"
  2689. # Attach ${ip} to ${name}
  2690. gcloud compute instances network-interfaces update "${name}" --project "${PROJECT}" --zone "${zone}" --aliases="${aliases}"
  2691. run-gcloud-command "${name}" "${zone}" 'sudo ip route add to local '${ip}'/32 dev $(ip route | grep default | awk '\''{print $5}'\'')' || true
  2692. return $?
  2693. }
  2694. # detach-internal-master-ip detaches internal ip from existing master.
  2695. #
  2696. # Assumes:
  2697. # * PROJECT
  2698. function detach-internal-master-ip() {
  2699. local name="${1}"
  2700. local zone="${2}"
  2701. local ip="${3}"
  2702. 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';' -)
  2703. echo "Setting ${name}'s aliases to '${aliases}' (removed ${ip})"
  2704. # Detach ${MASTER_NAME}-internal-ip from ${name}
  2705. gcloud compute instances network-interfaces update "${name}" --project "${PROJECT}" --zone "${zone}" --aliases="${aliases}"
  2706. run-gcloud-command "${name}" "${zone}" 'sudo ip route del to local '${ip}'/32 dev $(ip route | grep default | awk '\''{print $5}'\'')' || true
  2707. return $?
  2708. }
  2709. # create-internal-loadbalancer creates an internal load balacer in front of existing master.
  2710. #
  2711. # Assumes:
  2712. # * MASTER_NAME
  2713. # * PROJECT
  2714. # * REGION
  2715. function create-internal-loadbalancer() {
  2716. if gcloud compute forwarding-rules describe "${MASTER_NAME}-internal" \
  2717. --project "${PROJECT}" --region ${REGION} > /dev/null 2>&1; then
  2718. echo "Load balancer already exists"
  2719. return
  2720. fi
  2721. local EXISTING_MASTER_NAME="$(get-all-replica-names)"
  2722. local EXISTING_MASTER_ZONE=$(gcloud compute instances list "${EXISTING_MASTER_NAME}" \
  2723. --project "${PROJECT}" --format="value(zone)")
  2724. echo "Detaching ${KUBE_MASTER_INTERNAL_IP} from ${EXISTING_MASTER_NAME}/${EXISTING_MASTER_ZONE}"
  2725. detach-internal-master-ip "${EXISTING_MASTER_NAME}" "${EXISTING_MASTER_ZONE}" "${KUBE_MASTER_INTERNAL_IP}"
  2726. echo "Creating internal load balancer with IP: ${KUBE_MASTER_INTERNAL_IP}"
  2727. gcloud compute health-checks --project "${PROJECT}" create tcp "${MASTER_NAME}-hc" --port=443
  2728. gcloud compute backend-services create "${MASTER_NAME}" \
  2729. --project "${PROJECT}" \
  2730. --region "${REGION}" \
  2731. --protocol tcp \
  2732. --region "${REGION}" \
  2733. --load-balancing-scheme internal \
  2734. --health-checks "${MASTER_NAME}-hc"
  2735. gcloud compute forwarding-rules create "${MASTER_NAME}-internal" \
  2736. --project "${PROJECT}" \
  2737. --region "${REGION}" \
  2738. --load-balancing-scheme internal \
  2739. --network "${NETWORK}" \
  2740. --subnet "${SUBNETWORK}" \
  2741. --address "${KUBE_MASTER_INTERNAL_IP}" \
  2742. --ip-protocol TCP \
  2743. --ports 443 \
  2744. --backend-service "${MASTER_NAME}" \
  2745. --backend-service-region "${REGION}"
  2746. echo "Adding ${EXISTING_MASTER_NAME}/${EXISTING_MASTER_ZONE} to the load balancer"
  2747. add-to-internal-loadbalancer "${EXISTING_MASTER_NAME}" "${EXISTING_MASTER_ZONE}"
  2748. }
  2749. # add-to-internal-loadbalancer adds an instance to ILB.
  2750. # Assumes:
  2751. # * MASTER_NAME
  2752. # * PROJECT
  2753. # * REGION
  2754. function add-to-internal-loadbalancer() {
  2755. local name="${1}"
  2756. local zone="${2}"
  2757. gcloud compute instance-groups unmanaged create "${name}" --project "${PROJECT}" --zone "${zone}"
  2758. gcloud compute instance-groups unmanaged add-instances "${name}" --project "${PROJECT}" --zone "${zone}" --instances "${name}"
  2759. gcloud compute backend-services add-backend "${MASTER_NAME}" \
  2760. --project "${PROJECT}" \
  2761. --region "${REGION}" \
  2762. --instance-group "${name}" \
  2763. --instance-group-zone "${zone}"
  2764. }
  2765. # remove-from-internal-loadbalancer removes an instance from ILB.
  2766. # Assumes:
  2767. # * MASTER_NAME
  2768. # * PROJECT
  2769. # * REGION
  2770. function remove-from-internal-loadbalancer() {
  2771. local name="${1}"
  2772. local zone="${2}"
  2773. if gcloud compute instance-groups unmanaged describe "${name}" --project "${PROJECT}" --zone "${zone}" &>/dev/null; then
  2774. gcloud compute backend-services remove-backend "${MASTER_NAME}" \
  2775. --project "${PROJECT}" \
  2776. --region "${REGION}" \
  2777. --instance-group "${name}" \
  2778. --instance-group-zone "${zone}"
  2779. gcloud compute instance-groups unmanaged delete "${name}" --project "${PROJECT}" --zone "${zone}" --quiet
  2780. fi
  2781. }
  2782. function delete-internal-loadbalancer() {
  2783. if gcloud compute forwarding-rules describe "${MASTER_NAME}-internal" --project "${PROJECT}" --region "${REGION}" &>/dev/null; then
  2784. gcloud compute forwarding-rules delete "${MASTER_NAME}-internal" --project "${PROJECT}" --region "${REGION}" --quiet
  2785. fi
  2786. if gcloud compute backend-services describe "${MASTER_NAME}" --project "${PROJECT}" --region "${REGION}" &>/dev/null; then
  2787. gcloud compute backend-services delete "${MASTER_NAME}" --project "${PROJECT}" --region "${REGION}" --quiet
  2788. fi
  2789. if gcloud compute health-checks describe "${MASTER_NAME}-gc" --project "${PROJECT}" &>/dev/null; then
  2790. gcloud compute health-checks delete "${MASTER_NAME}-gc" --project "${PROJECT}" --quiet
  2791. fi
  2792. }
  2793. function create-nodes-firewall() {
  2794. # Create a single firewall rule for all minions.
  2795. create-firewall-rule "${NODE_TAG}-all" "${CLUSTER_IP_RANGE}" "${NODE_TAG}" &
  2796. # Report logging choice (if any).
  2797. if [[ "${ENABLE_NODE_LOGGING-}" == "true" ]]; then
  2798. echo "+++ Logging using Fluentd to ${LOGGING_DESTINATION:-unknown}"
  2799. fi
  2800. # Wait for last batch of jobs
  2801. kube::util::wait-for-jobs || {
  2802. echo -e "${color_red}Some commands failed.${color_norm}" >&2
  2803. }
  2804. }
  2805. function get-scope-flags() {
  2806. local scope_flags=
  2807. if [[ -n "${NODE_SCOPES}" ]]; then
  2808. scope_flags="--scopes ${NODE_SCOPES}"
  2809. else
  2810. scope_flags="--no-scopes"
  2811. fi
  2812. echo "${scope_flags}"
  2813. }
  2814. function create-nodes-template() {
  2815. echo "Creating nodes."
  2816. local scope_flags=$(get-scope-flags)
  2817. write-linux-node-env
  2818. write-windows-node-env
  2819. # NOTE: these template names and their format must match
  2820. # create-[linux,windows]-nodes() as well as get-template()!
  2821. local linux_template_name="${NODE_INSTANCE_PREFIX}-template"
  2822. local windows_template_name="${WINDOWS_NODE_INSTANCE_PREFIX}-template"
  2823. create-linux-node-instance-template $linux_template_name
  2824. create-windows-node-instance-template $windows_template_name "${scope_flags[*]}"
  2825. if [[ -n "${ADDITIONAL_MACHINE_TYPE:-}" ]]; then
  2826. local linux_extra_template_name="${NODE_INSTANCE_PREFIX}-extra-template"
  2827. create-linux-node-instance-template $linux_extra_template_name "${ADDITIONAL_MACHINE_TYPE}"
  2828. fi
  2829. }
  2830. # Assumes:
  2831. # - MAX_INSTANCES_PER_MIG
  2832. # - NUM_NODES
  2833. # - NUM_WINDOWS_NODES
  2834. # exports:
  2835. # - NUM_MIGS
  2836. # - NUM_WINDOWS_MIGS
  2837. function set_num_migs() {
  2838. local defaulted_max_instances_per_mig=${MAX_INSTANCES_PER_MIG:-1000}
  2839. if [[ ${defaulted_max_instances_per_mig} -le "0" ]]; then
  2840. echo "MAX_INSTANCES_PER_MIG cannot be negative. Assuming default 1000"
  2841. defaulted_max_instances_per_mig=1000
  2842. fi
  2843. export NUM_MIGS=$(((${NUM_NODES} + ${defaulted_max_instances_per_mig} - 1) / ${defaulted_max_instances_per_mig}))
  2844. export NUM_WINDOWS_MIGS=$(((${NUM_WINDOWS_NODES} + ${defaulted_max_instances_per_mig} - 1) / ${defaulted_max_instances_per_mig}))
  2845. }
  2846. # Assumes:
  2847. # - NUM_MIGS
  2848. # - NODE_INSTANCE_PREFIX
  2849. # - NUM_NODES
  2850. # - PROJECT
  2851. # - ZONE
  2852. function create-linux-nodes() {
  2853. local template_name="${NODE_INSTANCE_PREFIX}-template"
  2854. local extra_template_name="${NODE_INSTANCE_PREFIX}-extra-template"
  2855. local nodes="${NUM_NODES}"
  2856. if [[ ! -z "${HEAPSTER_MACHINE_TYPE:-}" ]]; then
  2857. echo "Creating a special node for heapster with machine-type ${HEAPSTER_MACHINE_TYPE}"
  2858. create-heapster-node
  2859. nodes=$(( nodes - 1 ))
  2860. fi
  2861. if [[ -n "${ADDITIONAL_MACHINE_TYPE:-}" && "${NUM_ADDITIONAL_NODES:-}" -gt 0 ]]; then
  2862. local num_additional="${NUM_ADDITIONAL_NODES}"
  2863. if [[ "${NUM_ADDITIONAL_NODES:-}" -gt "${nodes}" ]]; then
  2864. echo "Capping NUM_ADDITIONAL_NODES to ${nodes}"
  2865. num_additional="${nodes}"
  2866. fi
  2867. if [[ "${num_additional:-}" -gt 0 ]]; then
  2868. echo "Creating ${num_additional} special nodes with machine-type ${ADDITIONAL_MACHINE_TYPE}"
  2869. local extra_group_name="${NODE_INSTANCE_PREFIX}-extra"
  2870. gcloud compute instance-groups managed \
  2871. create "${extra_group_name}" \
  2872. --project "${PROJECT}" \
  2873. --zone "${ZONE}" \
  2874. --base-instance-name "${extra_group_name}" \
  2875. --size "${num_additional}" \
  2876. --template "${extra_template_name}" || true;
  2877. gcloud compute instance-groups managed wait-until-stable \
  2878. "${extra_group_name}" \
  2879. --zone "${ZONE}" \
  2880. --project "${PROJECT}" \
  2881. --timeout "${MIG_WAIT_UNTIL_STABLE_TIMEOUT}" || true
  2882. nodes=$(( nodes - $num_additional ))
  2883. fi
  2884. fi
  2885. local instances_left=${nodes}
  2886. for ((i=1; i<=${NUM_MIGS}; i++)); do
  2887. local group_name="${NODE_INSTANCE_PREFIX}-group-$i"
  2888. if [[ $i == ${NUM_MIGS} ]]; then
  2889. # TODO: We don't add a suffix for the last group to keep backward compatibility when there's only one MIG.
  2890. # We should change it at some point, but note #18545 when changing this.
  2891. group_name="${NODE_INSTANCE_PREFIX}-group"
  2892. fi
  2893. # Spread the remaining number of nodes evenly
  2894. this_mig_size=$((${instances_left} / (${NUM_MIGS}-${i}+1)))
  2895. instances_left=$((instances_left-${this_mig_size}))
  2896. # Run instance-groups creation in parallel.
  2897. {
  2898. gcloud compute instance-groups managed \
  2899. create "${group_name}" \
  2900. --project "${PROJECT}" \
  2901. --zone "${ZONE}" \
  2902. --base-instance-name "${group_name}" \
  2903. --size "${this_mig_size}" \
  2904. --template "${template_name}" || true;
  2905. gcloud compute instance-groups managed wait-until-stable \
  2906. "${group_name}" \
  2907. --zone "${ZONE}" \
  2908. --project "${PROJECT}" \
  2909. --timeout "${MIG_WAIT_UNTIL_STABLE_TIMEOUT}" || true
  2910. } &
  2911. done
  2912. wait
  2913. }
  2914. # Assumes:
  2915. # - NUM_WINDOWS_MIGS
  2916. # - WINDOWS_NODE_INSTANCE_PREFIX
  2917. # - NUM_WINDOWS_NODES
  2918. # - PROJECT
  2919. # - ZONE
  2920. function create-windows-nodes() {
  2921. local template_name="${WINDOWS_NODE_INSTANCE_PREFIX}-template"
  2922. local -r nodes="${NUM_WINDOWS_NODES}"
  2923. local instances_left=${nodes}
  2924. for ((i=1; i<=${NUM_WINDOWS_MIGS}; i++)); do
  2925. local group_name="${WINDOWS_NODE_INSTANCE_PREFIX}-group-$i"
  2926. if [[ $i == ${NUM_WINDOWS_MIGS} ]]; then
  2927. # TODO: We don't add a suffix for the last group to keep backward compatibility when there's only one MIG.
  2928. # We should change it at some point, but note #18545 when changing this.
  2929. group_name="${WINDOWS_NODE_INSTANCE_PREFIX}-group"
  2930. fi
  2931. # Spread the remaining number of nodes evenly
  2932. this_mig_size=$((${instances_left} / (${NUM_WINDOWS_MIGS}-${i}+1)))
  2933. instances_left=$((instances_left-${this_mig_size}))
  2934. gcloud compute instance-groups managed \
  2935. create "${group_name}" \
  2936. --project "${PROJECT}" \
  2937. --zone "${ZONE}" \
  2938. --base-instance-name "${group_name}" \
  2939. --size "${this_mig_size}" \
  2940. --template "${template_name}" || true;
  2941. gcloud compute instance-groups managed wait-until-stable \
  2942. "${group_name}" \
  2943. --zone "${ZONE}" \
  2944. --project "${PROJECT}" \
  2945. --timeout "${MIG_WAIT_UNTIL_STABLE_TIMEOUT}" || true;
  2946. done
  2947. }
  2948. # Assumes:
  2949. # - NODE_INSTANCE_PREFIX
  2950. # - PROJECT
  2951. # - NETWORK_PROJECT
  2952. # - REGION
  2953. # - ZONE
  2954. # - HEAPSTER_MACHINE_TYPE
  2955. # - NODE_DISK_TYPE
  2956. # - NODE_DISK_SIZE
  2957. # - NODE_IMAGE_PROJECT
  2958. # - NODE_IMAGE
  2959. # - NODE_SERVICE_ACCOUNT
  2960. # - NODE_TAG
  2961. # - NETWORK
  2962. # - ENABLE_IP_ALIASES
  2963. # - SUBNETWORK
  2964. # - IP_ALIAS_SIZE
  2965. function create-heapster-node() {
  2966. local gcloud="gcloud"
  2967. local network=$(make-gcloud-network-argument \
  2968. "${NETWORK_PROJECT}" \
  2969. "${REGION}" \
  2970. "${NETWORK}" \
  2971. "${SUBNETWORK:-}" \
  2972. "" \
  2973. "${ENABLE_IP_ALIASES:-}" \
  2974. "${IP_ALIAS_SIZE:-}")
  2975. ${gcloud} compute instances \
  2976. create "${NODE_INSTANCE_PREFIX}-heapster" \
  2977. --project "${PROJECT}" \
  2978. --zone "${ZONE}" \
  2979. --machine-type="${HEAPSTER_MACHINE_TYPE}" \
  2980. --boot-disk-type "${NODE_DISK_TYPE}" \
  2981. --boot-disk-size "${NODE_DISK_SIZE}" \
  2982. --image-project="${NODE_IMAGE_PROJECT}" \
  2983. --image "${NODE_IMAGE}" \
  2984. --service-account "${NODE_SERVICE_ACCOUNT}" \
  2985. --tags "${NODE_TAG}" \
  2986. ${network} \
  2987. $(get-scope-flags) \
  2988. --metadata-from-file "$(get-node-instance-metadata-from-file "heapster-kube-env")"
  2989. }
  2990. # Assumes:
  2991. # - NUM_MIGS
  2992. # - NODE_INSTANCE_PREFIX
  2993. # - PROJECT
  2994. # - ZONE
  2995. # - AUTOSCALER_MAX_NODES
  2996. # - AUTOSCALER_MIN_NODES
  2997. # Exports
  2998. # - AUTOSCALER_MIG_CONFIG
  2999. function create-cluster-autoscaler-mig-config() {
  3000. # Each MIG must have at least one node, so the min number of nodes
  3001. # must be greater or equal to the number of migs.
  3002. if [[ ${AUTOSCALER_MIN_NODES} -lt 0 ]]; then
  3003. echo "AUTOSCALER_MIN_NODES must be greater or equal 0"
  3004. exit 2
  3005. fi
  3006. # Each MIG must have at least one node, so the min number of nodes
  3007. # must be greater or equal to the number of migs.
  3008. if [[ ${AUTOSCALER_MAX_NODES} -lt ${NUM_MIGS} ]]; then
  3009. echo "AUTOSCALER_MAX_NODES must be greater or equal ${NUM_MIGS}"
  3010. exit 2
  3011. fi
  3012. if [[ ${NUM_WINDOWS_MIGS} -gt 0 ]]; then
  3013. # TODO(pjh): implement Windows support in this function.
  3014. echo "Not implemented yet: autoscaler config for Windows MIGs"
  3015. exit 2
  3016. fi
  3017. # The code assumes that the migs were created with create-nodes
  3018. # function which tries to evenly spread nodes across the migs.
  3019. AUTOSCALER_MIG_CONFIG=""
  3020. local left_min=${AUTOSCALER_MIN_NODES}
  3021. local left_max=${AUTOSCALER_MAX_NODES}
  3022. for ((i=1; i<=${NUM_MIGS}; i++)); do
  3023. local group_name="${NODE_INSTANCE_PREFIX}-group-$i"
  3024. if [[ $i == ${NUM_MIGS} ]]; then
  3025. # TODO: We don't add a suffix for the last group to keep backward compatibility when there's only one MIG.
  3026. # We should change it at some point, but note #18545 when changing this.
  3027. group_name="${NODE_INSTANCE_PREFIX}-group"
  3028. fi
  3029. this_mig_min=$((${left_min}/(${NUM_MIGS}-${i}+1)))
  3030. this_mig_max=$((${left_max}/(${NUM_MIGS}-${i}+1)))
  3031. left_min=$((left_min-$this_mig_min))
  3032. left_max=$((left_max-$this_mig_max))
  3033. local mig_url="https://www.googleapis.com/compute/v1/projects/${PROJECT}/zones/${ZONE}/instanceGroups/${group_name}"
  3034. AUTOSCALER_MIG_CONFIG="${AUTOSCALER_MIG_CONFIG} --nodes=${this_mig_min}:${this_mig_max}:${mig_url}"
  3035. done
  3036. AUTOSCALER_MIG_CONFIG="${AUTOSCALER_MIG_CONFIG} --scale-down-enabled=${AUTOSCALER_ENABLE_SCALE_DOWN}"
  3037. }
  3038. # Assumes:
  3039. # - NUM_MIGS
  3040. # - NODE_INSTANCE_PREFIX
  3041. # - PROJECT
  3042. # - ZONE
  3043. # - ENABLE_CLUSTER_AUTOSCALER
  3044. # - AUTOSCALER_MAX_NODES
  3045. # - AUTOSCALER_MIN_NODES
  3046. function create-autoscaler-config() {
  3047. # Create autoscaler for nodes configuration if requested
  3048. if [[ "${ENABLE_CLUSTER_AUTOSCALER}" == "true" ]]; then
  3049. create-cluster-autoscaler-mig-config
  3050. echo "Using autoscaler config: ${AUTOSCALER_MIG_CONFIG} ${AUTOSCALER_EXPANDER_CONFIG}"
  3051. fi
  3052. }
  3053. function check-cluster() {
  3054. detect-node-names
  3055. detect-master
  3056. echo "Waiting up to ${KUBE_CLUSTER_INITIALIZATION_TIMEOUT} seconds for cluster initialization."
  3057. echo
  3058. echo " This will continually check to see if the API for kubernetes is reachable."
  3059. echo " This may time out if there was some uncaught error during start up."
  3060. echo
  3061. # curl in mavericks is borked.
  3062. secure=""
  3063. if which sw_vers >& /dev/null; then
  3064. if [[ $(sw_vers | grep ProductVersion | awk '{print $2}') = "10.9."* ]]; then
  3065. secure="--insecure"
  3066. fi
  3067. fi
  3068. local start_time=$(date +%s)
  3069. local curl_out=$(mktemp)
  3070. kube::util::trap_add "rm -f ${curl_out}" EXIT
  3071. until curl --cacert "${CERT_DIR}/pki/ca.crt" \
  3072. -H "Authorization: Bearer ${KUBE_BEARER_TOKEN}" \
  3073. ${secure} \
  3074. --max-time 5 --fail \
  3075. "https://${KUBE_MASTER_IP}/api/v1/pods?limit=100" > "${curl_out}" 2>&1; do
  3076. local elapsed=$(($(date +%s) - ${start_time}))
  3077. if [[ ${elapsed} -gt ${KUBE_CLUSTER_INITIALIZATION_TIMEOUT} ]]; then
  3078. echo -e "${color_red}Cluster failed to initialize within ${KUBE_CLUSTER_INITIALIZATION_TIMEOUT} seconds.${color_norm}" >&2
  3079. echo "Last output from querying API server follows:" >&2
  3080. echo "-----------------------------------------------------" >&2
  3081. cat "${curl_out}" >&2
  3082. echo "-----------------------------------------------------" >&2
  3083. exit 2
  3084. fi
  3085. printf "."
  3086. sleep 2
  3087. done
  3088. echo "Kubernetes cluster created."
  3089. export KUBE_CERT="${CERT_DIR}/pki/issued/kubecfg.crt"
  3090. export KUBE_KEY="${CERT_DIR}/pki/private/kubecfg.key"
  3091. export CA_CERT="${CERT_DIR}/pki/ca.crt"
  3092. export CONTEXT="${PROJECT}_${INSTANCE_PREFIX}"
  3093. (
  3094. umask 077
  3095. # Update the user's kubeconfig to include credentials for this apiserver.
  3096. create-kubeconfig
  3097. )
  3098. # ensures KUBECONFIG is set
  3099. get-kubeconfig-basicauth
  3100. if [[ ${GCE_UPLOAD_KUBCONFIG_TO_MASTER_METADATA:-} == "true" ]]; then
  3101. gcloud compute instances add-metadata "${MASTER_NAME}" --zone="${ZONE}" --metadata-from-file="kubeconfig=${KUBECONFIG}" || true
  3102. fi
  3103. echo
  3104. echo -e "${color_green}Kubernetes cluster is running. The master is running at:"
  3105. echo
  3106. echo -e "${color_yellow} https://${KUBE_MASTER_IP}"
  3107. echo
  3108. echo -e "${color_green}The user name and password to use is located in ${KUBECONFIG}.${color_norm}"
  3109. echo
  3110. }
  3111. # Removes master replica from etcd cluster.
  3112. #
  3113. # Assumed vars:
  3114. # REPLICA_NAME
  3115. # PROJECT
  3116. # EXISTING_MASTER_NAME
  3117. # EXISTING_MASTER_ZONE
  3118. #
  3119. # $1: etcd client port
  3120. # $2: whether etcd communication should use mtls
  3121. # returns the result of ssh command which removes replica
  3122. function remove-replica-from-etcd() {
  3123. local -r port="${1}"
  3124. local -r use_mtls="${2}"
  3125. TLSARG=""
  3126. PROTO="http://"
  3127. if [[ "${use_mtls}" == "true" ]]; then
  3128. # Keep in sync with ETCD_APISERVER_CA_CERT_PATH, ETCD_APISERVER_CLIENT_CERT_PATH and ETCD_APISERVER_CLIENT_KEY_PATH in configure-helper.sh.
  3129. 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"
  3130. PROTO="https://"
  3131. fi
  3132. [[ -n "${EXISTING_MASTER_NAME}" ]] || return
  3133. 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"
  3134. local -r res=$?
  3135. echo "Removing etcd replica, name: ${REPLICA_NAME}, port: ${port}, result: ${res}"
  3136. return "${res}"
  3137. }
  3138. # Delete a kubernetes cluster. This is called from test-teardown.
  3139. #
  3140. # Assumed vars:
  3141. # MASTER_NAME
  3142. # NODE_INSTANCE_PREFIX
  3143. # WINDOWS_NODE_INSTANCE_PREFIX
  3144. # ZONE
  3145. # This function tears down cluster resources 10 at a time to avoid issuing too many
  3146. # API calls and exceeding API quota. It is important to bring down the instances before bringing
  3147. # down the firewall rules and routes.
  3148. function kube-down() {
  3149. local -r batch=200
  3150. detect-project
  3151. detect-node-names # For INSTANCE_GROUPS and WINDOWS_INSTANCE_GROUPS
  3152. echo "Bringing down cluster"
  3153. set +e # Do not stop on error
  3154. if [[ "${KUBE_DELETE_NODES:-}" != "false" ]]; then
  3155. # Get the name of the managed instance group template before we delete the
  3156. # managed instance group. (The name of the managed instance group template may
  3157. # change during a cluster upgrade.)
  3158. local templates=$(get-template "${PROJECT}")
  3159. local all_instance_groups=(${INSTANCE_GROUPS[@]:-} ${WINDOWS_INSTANCE_GROUPS[@]:-})
  3160. for group in ${all_instance_groups[@]:-}; do
  3161. {
  3162. if gcloud compute instance-groups managed describe "${group}" --project "${PROJECT}" --zone "${ZONE}" &>/dev/null; then
  3163. gcloud compute instance-groups managed delete \
  3164. --project "${PROJECT}" \
  3165. --quiet \
  3166. --zone "${ZONE}" \
  3167. "${group}"
  3168. fi
  3169. } &
  3170. done
  3171. # Wait for last batch of jobs
  3172. kube::util::wait-for-jobs || {
  3173. echo -e "Failed to delete instance group(s)." >&2
  3174. }
  3175. for template in ${templates[@]:-}; do
  3176. {
  3177. if gcloud compute instance-templates describe --project "${PROJECT}" "${template}" &>/dev/null; then
  3178. gcloud compute instance-templates delete \
  3179. --project "${PROJECT}" \
  3180. --quiet \
  3181. "${template}"
  3182. fi
  3183. } &
  3184. done
  3185. # Wait for last batch of jobs
  3186. kube::util::wait-for-jobs || {
  3187. echo -e "Failed to delete instance template(s)." >&2
  3188. }
  3189. # Delete the special heapster node (if it exists).
  3190. if [[ -n "${HEAPSTER_MACHINE_TYPE:-}" ]]; then
  3191. local -r heapster_machine_name="${NODE_INSTANCE_PREFIX}-heapster"
  3192. if gcloud compute instances describe "${heapster_machine_name}" --zone "${ZONE}" --project "${PROJECT}" &>/dev/null; then
  3193. # Now we can safely delete the VM.
  3194. gcloud compute instances delete \
  3195. --project "${PROJECT}" \
  3196. --quiet \
  3197. --delete-disks all \
  3198. --zone "${ZONE}" \
  3199. "${heapster_machine_name}"
  3200. fi
  3201. fi
  3202. fi
  3203. local -r REPLICA_NAME="${KUBE_REPLICA_NAME:-$(get-replica-name)}"
  3204. set-existing-master
  3205. # Un-register the master replica from etcd and events etcd.
  3206. remove-replica-from-etcd 2379 true
  3207. remove-replica-from-etcd 4002 false
  3208. # Delete the master replica (if it exists).
  3209. if gcloud compute instances describe "${REPLICA_NAME}" --zone "${ZONE}" --project "${PROJECT}" &>/dev/null; then
  3210. # If there is a load balancer in front of apiservers we need to first update its configuration.
  3211. if gcloud compute target-pools describe "${MASTER_NAME}" --region "${REGION}" --project "${PROJECT}" &>/dev/null; then
  3212. gcloud compute target-pools remove-instances "${MASTER_NAME}" \
  3213. --project "${PROJECT}" \
  3214. --zone "${ZONE}" \
  3215. --instances "${REPLICA_NAME}"
  3216. fi
  3217. # Detach replica from LB if needed.
  3218. if [[ ${GCE_PRIVATE_CLUSTER:-} == "true" ]]; then
  3219. remove-from-internal-loadbalancer "${REPLICA_NAME}" "${ZONE}"
  3220. fi
  3221. # Now we can safely delete the VM.
  3222. gcloud compute instances delete \
  3223. --project "${PROJECT}" \
  3224. --quiet \
  3225. --delete-disks all \
  3226. --zone "${ZONE}" \
  3227. "${REPLICA_NAME}"
  3228. fi
  3229. # Delete the master replica pd (possibly leaked by kube-up if master create failed).
  3230. # TODO(jszczepkowski): remove also possibly leaked replicas' pds
  3231. local -r replica_pd="${REPLICA_NAME:-${MASTER_NAME}}-pd"
  3232. if gcloud compute disks describe "${replica_pd}" --zone "${ZONE}" --project "${PROJECT}" &>/dev/null; then
  3233. gcloud compute disks delete \
  3234. --project "${PROJECT}" \
  3235. --quiet \
  3236. --zone "${ZONE}" \
  3237. "${replica_pd}"
  3238. fi
  3239. # Check if this are any remaining master replicas.
  3240. local REMAINING_MASTER_COUNT=$(gcloud compute instances list \
  3241. --project "${PROJECT}" \
  3242. --filter="name ~ '$(get-replica-name-regexp)'" \
  3243. --format "value(zone)" | wc -l)
  3244. # In the replicated scenario, if there's only a single master left, we should also delete load balancer in front of it.
  3245. if [[ "${REMAINING_MASTER_COUNT}" -eq 1 ]]; then
  3246. if gcloud compute forwarding-rules describe "${MASTER_NAME}" --region "${REGION}" --project "${PROJECT}" &>/dev/null; then
  3247. detect-master
  3248. local REMAINING_REPLICA_NAME="$(get-all-replica-names)"
  3249. local REMAINING_REPLICA_ZONE=$(gcloud compute instances list "${REMAINING_REPLICA_NAME}" \
  3250. --project "${PROJECT}" --format="value(zone)")
  3251. gcloud compute forwarding-rules delete \
  3252. --project "${PROJECT}" \
  3253. --region "${REGION}" \
  3254. --quiet \
  3255. "${MASTER_NAME}"
  3256. attach-external-ip "${REMAINING_REPLICA_NAME}" "${REMAINING_REPLICA_ZONE}" "${KUBE_MASTER_IP}"
  3257. gcloud compute target-pools delete \
  3258. --project "${PROJECT}" \
  3259. --region "${REGION}" \
  3260. --quiet \
  3261. "${MASTER_NAME}"
  3262. fi
  3263. if [[ ${GCE_PRIVATE_CLUSTER:-} == "true" ]]; then
  3264. remove-from-internal-loadbalancer "${REMAINING_REPLICA_NAME}" "${REMAINING_REPLICA_ZONE}"
  3265. delete-internal-loadbalancer
  3266. attach-internal-master-ip "${REMAINING_REPLICA_NAME}" "${REMAINING_REPLICA_ZONE}" "${KUBE_MASTER_INTERNAL_IP}"
  3267. fi
  3268. fi
  3269. # If there are no more remaining master replicas, we should delete all remaining network resources.
  3270. if [[ "${REMAINING_MASTER_COUNT}" -eq 0 ]]; then
  3271. # Delete firewall rule for the master, etcd servers, and nodes.
  3272. delete-firewall-rules "${MASTER_NAME}-https" "${MASTER_NAME}-etcd" "${NODE_TAG}-all" "${MASTER_NAME}-konnectivity-server"
  3273. # Delete the master's reserved IP
  3274. if gcloud compute addresses describe "${MASTER_NAME}-ip" --region "${REGION}" --project "${PROJECT}" &>/dev/null; then
  3275. gcloud compute addresses delete \
  3276. --project "${PROJECT}" \
  3277. --region "${REGION}" \
  3278. --quiet \
  3279. "${MASTER_NAME}-ip"
  3280. fi
  3281. if gcloud compute addresses describe "${MASTER_NAME}-internal-ip" --region "${REGION}" --project "${PROJECT}" &>/dev/null; then
  3282. gcloud compute addresses delete \
  3283. --project "${PROJECT}" \
  3284. --region "${REGION}" \
  3285. --quiet \
  3286. "${MASTER_NAME}-internal-ip"
  3287. fi
  3288. fi
  3289. if [[ "${KUBE_DELETE_NODES:-}" != "false" ]]; then
  3290. # Find out what minions are running.
  3291. local -a minions
  3292. minions=( $(gcloud compute instances list \
  3293. --project "${PROJECT}" \
  3294. --filter="(name ~ '${NODE_INSTANCE_PREFIX}-.+' OR name ~ '${WINDOWS_NODE_INSTANCE_PREFIX}-.+') AND zone:(${ZONE})" \
  3295. --format='value(name)') )
  3296. # If any minions are running, delete them in batches.
  3297. while (( "${#minions[@]}" > 0 )); do
  3298. echo Deleting nodes "${minions[*]::${batch}}"
  3299. gcloud compute instances delete \
  3300. --project "${PROJECT}" \
  3301. --quiet \
  3302. --delete-disks boot \
  3303. --zone "${ZONE}" \
  3304. "${minions[@]::${batch}}"
  3305. minions=( "${minions[@]:${batch}}" )
  3306. done
  3307. fi
  3308. # If there are no more remaining master replicas: delete routes, pd for influxdb and update kubeconfig
  3309. if [[ "${REMAINING_MASTER_COUNT}" -eq 0 ]]; then
  3310. # Delete routes.
  3311. local -a routes
  3312. # Clean up all routes w/ names like "<cluster-name>-<node-GUID>"
  3313. # e.g. "kubernetes-12345678-90ab-cdef-1234-567890abcdef". The name is
  3314. # determined by the node controller on the master.
  3315. # Note that this is currently a noop, as synchronously deleting the node MIG
  3316. # first allows the master to cleanup routes itself.
  3317. local TRUNCATED_PREFIX="${INSTANCE_PREFIX:0:26}"
  3318. routes=( $(gcloud compute routes list --project "${NETWORK_PROJECT}" \
  3319. --filter="name ~ '${TRUNCATED_PREFIX}-.{8}-.{4}-.{4}-.{4}-.{12}'" \
  3320. --format='value(name)') )
  3321. while (( "${#routes[@]}" > 0 )); do
  3322. echo Deleting routes "${routes[*]::${batch}}"
  3323. gcloud compute routes delete \
  3324. --project "${NETWORK_PROJECT}" \
  3325. --quiet \
  3326. "${routes[@]::${batch}}"
  3327. routes=( "${routes[@]:${batch}}" )
  3328. done
  3329. # Delete persistent disk for influx-db.
  3330. if gcloud compute disks describe "${INSTANCE_PREFIX}"-influxdb-pd --zone "${ZONE}" --project "${PROJECT}" &>/dev/null; then
  3331. gcloud compute disks delete \
  3332. --project "${PROJECT}" \
  3333. --quiet \
  3334. --zone "${ZONE}" \
  3335. "${INSTANCE_PREFIX}"-influxdb-pd
  3336. fi
  3337. # Delete all remaining firewall rules and network.
  3338. delete-firewall-rules \
  3339. "${CLUSTER_NAME}-default-internal-master" \
  3340. "${CLUSTER_NAME}-default-internal-node" \
  3341. "${NETWORK}-default-ssh" \
  3342. "${NETWORK}-default-rdp" \
  3343. "${NETWORK}-default-internal" # Pre-1.5 clusters
  3344. if [[ "${KUBE_DELETE_NETWORK}" == "true" ]]; then
  3345. delete-cloud-nat-router
  3346. # Delete all remaining firewall rules in the network.
  3347. delete-all-firewall-rules || true
  3348. delete-subnetworks || true
  3349. delete-network || true # might fail if there are leaked resources that reference the network
  3350. fi
  3351. # If there are no more remaining master replicas, we should update kubeconfig.
  3352. export CONTEXT="${PROJECT}_${INSTANCE_PREFIX}"
  3353. clear-kubeconfig
  3354. else
  3355. # If some master replicas remain: cluster has been changed, we need to re-validate it.
  3356. echo "... calling validate-cluster" >&2
  3357. # Override errexit
  3358. (validate-cluster) && validate_result="$?" || validate_result="$?"
  3359. # We have two different failure modes from validate cluster:
  3360. # - 1: fatal error - cluster won't be working correctly
  3361. # - 2: weak error - something went wrong, but cluster probably will be working correctly
  3362. # We just print an error message in case 2).
  3363. if [[ "${validate_result}" -eq 1 ]]; then
  3364. exit 1
  3365. elif [[ "${validate_result}" -eq 2 ]]; then
  3366. echo "...ignoring non-fatal errors in validate-cluster" >&2
  3367. fi
  3368. fi
  3369. set -e
  3370. }
  3371. # Prints name of one of the master replicas in the current zone. It will be either
  3372. # just MASTER_NAME or MASTER_NAME with a suffix for a replica (see get-replica-name-regexp).
  3373. #
  3374. # Assumed vars:
  3375. # PROJECT
  3376. # ZONE
  3377. # MASTER_NAME
  3378. #
  3379. # NOTE: Must be in sync with get-replica-name-regexp and set-replica-name.
  3380. function get-replica-name() {
  3381. echo $(gcloud compute instances list \
  3382. --project "${PROJECT}" \
  3383. --filter="name ~ '$(get-replica-name-regexp)' AND zone:(${ZONE})" \
  3384. --format "value(name)" | head -n1)
  3385. }
  3386. # Prints comma-separated names of all of the master replicas in all zones.
  3387. #
  3388. # Assumed vars:
  3389. # PROJECT
  3390. # MASTER_NAME
  3391. #
  3392. # NOTE: Must be in sync with get-replica-name-regexp and set-replica-name.
  3393. function get-all-replica-names() {
  3394. echo $(gcloud compute instances list \
  3395. --project "${PROJECT}" \
  3396. --filter="name ~ '$(get-replica-name-regexp)'" \
  3397. --format "value(name)" | tr "\n" "," | sed 's/,$//')
  3398. }
  3399. # Prints the number of all of the master replicas in all zones.
  3400. #
  3401. # Assumed vars:
  3402. # MASTER_NAME
  3403. function get-master-replicas-count() {
  3404. detect-project
  3405. local num_masters=$(gcloud compute instances list \
  3406. --project "${PROJECT}" \
  3407. --filter="name ~ '$(get-replica-name-regexp)'" \
  3408. --format "value(zone)" | wc -l)
  3409. echo -n "${num_masters}"
  3410. }
  3411. # Prints regexp for full master machine name. In a cluster with replicated master,
  3412. # VM names may either be MASTER_NAME or MASTER_NAME with a suffix for a replica.
  3413. function get-replica-name-regexp() {
  3414. echo "^${MASTER_NAME}(-...)?"
  3415. }
  3416. # Sets REPLICA_NAME to a unique name for a master replica that will match
  3417. # expected regexp (see get-replica-name-regexp).
  3418. #
  3419. # Assumed vars:
  3420. # PROJECT
  3421. # ZONE
  3422. # MASTER_NAME
  3423. #
  3424. # Sets:
  3425. # REPLICA_NAME
  3426. function set-replica-name() {
  3427. local instances=$(gcloud compute instances list \
  3428. --project "${PROJECT}" \
  3429. --filter="name ~ '$(get-replica-name-regexp)'" \
  3430. --format "value(name)")
  3431. suffix=""
  3432. while echo "${instances}" | grep "${suffix}" &>/dev/null; do
  3433. suffix="$(date | md5sum | head -c3)"
  3434. done
  3435. REPLICA_NAME="${MASTER_NAME}-${suffix}"
  3436. }
  3437. # Gets the instance templates in use by the cluster. It echos the template names
  3438. # so that the function output can be used.
  3439. # Assumed vars:
  3440. # NODE_INSTANCE_PREFIX
  3441. # WINDOWS_NODE_INSTANCE_PREFIX
  3442. #
  3443. # $1: project
  3444. function get-template() {
  3445. local linux_filter="${NODE_INSTANCE_PREFIX}-(extra-)?template(-(${KUBE_RELEASE_VERSION_DASHED_REGEX}|${KUBE_CI_VERSION_DASHED_REGEX}))?"
  3446. local windows_filter="${WINDOWS_NODE_INSTANCE_PREFIX}-template(-(${KUBE_RELEASE_VERSION_DASHED_REGEX}|${KUBE_CI_VERSION_DASHED_REGEX}))?"
  3447. gcloud compute instance-templates list \
  3448. --filter="name ~ '${linux_filter}' OR name ~ '${windows_filter}'" \
  3449. --project="${1}" --format='value(name)'
  3450. }
  3451. # Checks if there are any present resources related kubernetes cluster.
  3452. #
  3453. # Assumed vars:
  3454. # MASTER_NAME
  3455. # NODE_INSTANCE_PREFIX
  3456. # WINDOWS_NODE_INSTANCE_PREFIX
  3457. # ZONE
  3458. # REGION
  3459. # Vars set:
  3460. # KUBE_RESOURCE_FOUND
  3461. function check-resources() {
  3462. detect-project
  3463. detect-node-names
  3464. echo "Looking for already existing resources"
  3465. KUBE_RESOURCE_FOUND=""
  3466. if [[ -n "${INSTANCE_GROUPS[@]:-}" ]]; then
  3467. KUBE_RESOURCE_FOUND="Managed instance groups ${INSTANCE_GROUPS[@]}"
  3468. return 1
  3469. fi
  3470. if [[ -n "${WINDOWS_INSTANCE_GROUPS[@]:-}" ]]; then
  3471. KUBE_RESOURCE_FOUND="Managed instance groups ${WINDOWS_INSTANCE_GROUPS[@]}"
  3472. return 1
  3473. fi
  3474. if gcloud compute instance-templates describe --project "${PROJECT}" "${NODE_INSTANCE_PREFIX}-template" &>/dev/null; then
  3475. KUBE_RESOURCE_FOUND="Instance template ${NODE_INSTANCE_PREFIX}-template"
  3476. return 1
  3477. fi
  3478. if gcloud compute instance-templates describe --project "${PROJECT}" "${WINDOWS_NODE_INSTANCE_PREFIX}-template" &>/dev/null; then
  3479. KUBE_RESOURCE_FOUND="Instance template ${WINDOWS_NODE_INSTANCE_PREFIX}-template"
  3480. return 1
  3481. fi
  3482. if gcloud compute instances describe --project "${PROJECT}" "${MASTER_NAME}" --zone "${ZONE}" &>/dev/null; then
  3483. KUBE_RESOURCE_FOUND="Kubernetes master ${MASTER_NAME}"
  3484. return 1
  3485. fi
  3486. if gcloud compute disks describe --project "${PROJECT}" "${MASTER_NAME}"-pd --zone "${ZONE}" &>/dev/null; then
  3487. KUBE_RESOURCE_FOUND="Persistent disk ${MASTER_NAME}-pd"
  3488. return 1
  3489. fi
  3490. # Find out what minions are running.
  3491. local -a minions
  3492. minions=( $(gcloud compute instances list \
  3493. --project "${PROJECT}" \
  3494. --filter="(name ~ '${NODE_INSTANCE_PREFIX}-.+' OR name ~ '${WINDOWS_NODE_INSTANCE_PREFIX}-.+') AND zone:(${ZONE})" \
  3495. --format='value(name)') )
  3496. if (( "${#minions[@]}" > 0 )); then
  3497. KUBE_RESOURCE_FOUND="${#minions[@]} matching ${NODE_INSTANCE_PREFIX}-.+ or ${WINDOWS_NODE_INSTANCE_PREFIX}-.+"
  3498. return 1
  3499. fi
  3500. if gcloud compute firewall-rules describe --project "${NETWORK_PROJECT}" "${MASTER_NAME}-https" &>/dev/null; then
  3501. KUBE_RESOURCE_FOUND="Firewall rules for ${MASTER_NAME}-https"
  3502. return 1
  3503. fi
  3504. if gcloud compute firewall-rules describe --project "${NETWORK_PROJECT}" "${NODE_TAG}-all" &>/dev/null; then
  3505. KUBE_RESOURCE_FOUND="Firewall rules for ${MASTER_NAME}-all"
  3506. return 1
  3507. fi
  3508. local -a routes
  3509. routes=( $(gcloud compute routes list --project "${NETWORK_PROJECT}" \
  3510. --filter="name ~ '${INSTANCE_PREFIX}-minion-.{4}'" --format='value(name)') )
  3511. if (( "${#routes[@]}" > 0 )); then
  3512. KUBE_RESOURCE_FOUND="${#routes[@]} routes matching ${INSTANCE_PREFIX}-minion-.{4}"
  3513. return 1
  3514. fi
  3515. if gcloud compute addresses describe --project "${PROJECT}" "${MASTER_NAME}-ip" --region "${REGION}" &>/dev/null; then
  3516. KUBE_RESOURCE_FOUND="Master's reserved IP"
  3517. return 1
  3518. fi
  3519. # No resources found.
  3520. return 0
  3521. }
  3522. # -----------------------------------------------------------------------------
  3523. # Cluster specific test helpers
  3524. # Execute prior to running tests to build a release if required for env.
  3525. #
  3526. # Assumed Vars:
  3527. # KUBE_ROOT
  3528. function test-build-release() {
  3529. # Make a release
  3530. "${KUBE_ROOT}/build/release.sh"
  3531. }
  3532. # Execute prior to running tests to initialize required structure.
  3533. #
  3534. # Assumed vars:
  3535. # Variables from config.sh
  3536. function test-setup() {
  3537. # Detect the project into $PROJECT if it isn't set
  3538. detect-project
  3539. if [[ ${MULTIZONE:-} == "true" && -n ${E2E_ZONES:-} ]]; then
  3540. for KUBE_GCE_ZONE in ${E2E_ZONES}; do
  3541. KUBE_GCE_ZONE="${KUBE_GCE_ZONE}" KUBE_USE_EXISTING_MASTER="${KUBE_USE_EXISTING_MASTER:-}" "${KUBE_ROOT}/cluster/kube-up.sh"
  3542. KUBE_USE_EXISTING_MASTER="true" # For subsequent zones we use the existing master
  3543. done
  3544. else
  3545. "${KUBE_ROOT}/cluster/kube-up.sh"
  3546. fi
  3547. # Open up port 80 & 8080 so common containers on minions can be reached
  3548. # TODO(roberthbailey): Remove this once we are no longer relying on hostPorts.
  3549. local start=`date +%s`
  3550. gcloud compute firewall-rules create \
  3551. --project "${NETWORK_PROJECT}" \
  3552. --target-tags "${NODE_TAG}" \
  3553. --allow tcp:80,tcp:8080 \
  3554. --network "${NETWORK}" \
  3555. "${NODE_TAG}-http-alt" 2> /dev/null || true
  3556. # As there is no simple way to wait longer for this operation we need to manually
  3557. # wait some additional time (20 minutes altogether).
  3558. while ! gcloud compute firewall-rules describe --project "${NETWORK_PROJECT}" "${NODE_TAG}-http-alt" 2> /dev/null; do
  3559. if [[ $(($start + 1200)) -lt `date +%s` ]]; then
  3560. echo -e "${color_red}Failed to create firewall ${NODE_TAG}-http-alt in ${NETWORK_PROJECT}" >&2
  3561. exit 1
  3562. fi
  3563. sleep 5
  3564. done
  3565. # Open up the NodePort range
  3566. # TODO(justinsb): Move to main setup, if we decide whether we want to do this by default.
  3567. start=`date +%s`
  3568. gcloud compute firewall-rules create \
  3569. --project "${NETWORK_PROJECT}" \
  3570. --target-tags "${NODE_TAG}" \
  3571. --allow tcp:30000-32767,udp:30000-32767 \
  3572. --network "${NETWORK}" \
  3573. "${NODE_TAG}-nodeports" 2> /dev/null || true
  3574. # As there is no simple way to wait longer for this operation we need to manually
  3575. # wait some additional time (20 minutes altogether).
  3576. while ! gcloud compute firewall-rules describe --project "${NETWORK_PROJECT}" "${NODE_TAG}-nodeports" 2> /dev/null; do
  3577. if [[ $(($start + 1200)) -lt `date +%s` ]]; then
  3578. echo -e "${color_red}Failed to create firewall ${NODE_TAG}-nodeports in ${PROJECT}" >&2
  3579. exit 1
  3580. fi
  3581. sleep 5
  3582. done
  3583. }
  3584. # Execute after running tests to perform any required clean-up.
  3585. function test-teardown() {
  3586. detect-project
  3587. echo "Shutting down test cluster in background."
  3588. delete-firewall-rules \
  3589. "${NODE_TAG}-http-alt" \
  3590. "${NODE_TAG}-nodeports"
  3591. if [[ ${MULTIZONE:-} == "true" && -n ${E2E_ZONES:-} ]]; then
  3592. local zones=( ${E2E_ZONES} )
  3593. # tear them down in reverse order, finally tearing down the master too.
  3594. for ((zone_num=${#zones[@]}-1; zone_num>0; zone_num--)); do
  3595. KUBE_GCE_ZONE="${zones[zone_num]}" KUBE_USE_EXISTING_MASTER="true" "${KUBE_ROOT}/cluster/kube-down.sh"
  3596. done
  3597. KUBE_GCE_ZONE="${zones[0]}" KUBE_USE_EXISTING_MASTER="false" "${KUBE_ROOT}/cluster/kube-down.sh"
  3598. else
  3599. "${KUBE_ROOT}/cluster/kube-down.sh"
  3600. fi
  3601. }
  3602. # SSH to a node by name ($1) and run a command ($2).
  3603. function ssh-to-node() {
  3604. local node="$1"
  3605. local cmd="$2"
  3606. # Loop until we can successfully ssh into the box
  3607. for try in {1..5}; do
  3608. 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
  3609. break
  3610. fi
  3611. sleep 5
  3612. done
  3613. # Then actually try the command.
  3614. gcloud compute ssh --ssh-flag="-o LogLevel=quiet" --ssh-flag="-o ConnectTimeout=30" --project "${PROJECT}" --zone="${ZONE}" "${node}" --command "${cmd}"
  3615. }
  3616. # Perform preparations required to run e2e tests
  3617. function prepare-e2e() {
  3618. detect-project
  3619. }
  3620. # Delete the image given by $1.
  3621. function delete-image() {
  3622. gcloud container images delete --quiet "$1"
  3623. }