configure.ps1 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. # Copyright 2019 The Kubernetes Authors.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. <#
  15. .SYNOPSIS
  16. Top-level script that runs on Windows nodes to join them to the K8s cluster.
  17. #>
  18. # IMPORTANT PLEASE NOTE:
  19. # Any time the file structure in the `windows` directory changes, `windows/BUILD`
  20. # and `k8s.io/release/lib/releaselib.sh` must be manually updated with the changes.
  21. # We HIGHLY recommend not changing the file structure, because consumers of
  22. # Kubernetes releases depend on the release structure remaining stable.
  23. $ErrorActionPreference = 'Stop'
  24. # Turn on tracing to debug
  25. # Set-PSDebug -Trace 1
  26. # Update TLS setting to enable Github downloads and disable progress bar to
  27. # increase download speed.
  28. [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
  29. $ProgressPreference = 'SilentlyContinue'
  30. # Returns the GCE instance metadata value for $Key where key is an "attribute"
  31. # of the instance. If the key is not present in the instance metadata returns
  32. # $Default if set, otherwise returns $null.
  33. function Get-InstanceMetadataAttribute {
  34. param (
  35. [parameter(Mandatory=$true)] [string]$Key,
  36. [parameter(Mandatory=$false)] [string]$Default
  37. )
  38. $url = ("http://metadata.google.internal/computeMetadata/v1/instance/" +
  39. "attributes/$Key")
  40. try {
  41. $client = New-Object Net.WebClient
  42. $client.Headers.Add('Metadata-Flavor', 'Google')
  43. return ($client.DownloadString($url)).Trim()
  44. }
  45. catch [System.Net.WebException] {
  46. if ($Default) {
  47. return $Default
  48. }
  49. else {
  50. Write-Host "Failed to retrieve value for $Key."
  51. return $null
  52. }
  53. }
  54. }
  55. # Fetches the value of $MetadataKey, saves it to C:\$Filename and imports it as
  56. # a PowerShell module.
  57. #
  58. # Note: this function depends on common.psm1.
  59. function FetchAndImport-ModuleFromMetadata {
  60. param (
  61. [parameter(Mandatory=$true)] [string]$MetadataKey,
  62. [parameter(Mandatory=$true)] [string]$Filename
  63. )
  64. $module = Get-InstanceMetadataAttribute $MetadataKey
  65. if (Test-Path C:\$Filename) {
  66. if (-not $REDO_STEPS) {
  67. Log-Output "Skip: C:\$Filename already exists, not overwriting"
  68. Import-Module -Force C:\$Filename
  69. return
  70. }
  71. Log-Output "Warning: C:\$Filename already exists, will overwrite it."
  72. }
  73. New-Item -ItemType file -Force C:\$Filename | Out-Null
  74. Set-Content C:\$Filename $module
  75. Import-Module -Force C:\$Filename
  76. }
  77. try {
  78. # Don't use FetchAndImport-ModuleFromMetadata for common.psm1 - the common
  79. # module includes variables and functions that any other function may depend
  80. # on.
  81. $module = Get-InstanceMetadataAttribute 'common-psm1'
  82. New-Item -ItemType file -Force C:\common.psm1 | Out-Null
  83. Set-Content C:\common.psm1 $module
  84. Import-Module -Force C:\common.psm1
  85. # TODO(pjh): update the function to set $Filename automatically from the key,
  86. # then put these calls into a loop over a list of XYZ-psm1 keys.
  87. FetchAndImport-ModuleFromMetadata 'k8s-node-setup-psm1' 'k8s-node-setup.psm1'
  88. Dump-DebugInfoToConsole
  89. Set-PrerequisiteOptions
  90. $kube_env = Fetch-KubeEnv
  91. Disable-WindowsDefender
  92. if (Test-IsTestCluster $kube_env) {
  93. Log-Output 'Test cluster detected, installing OpenSSH.'
  94. FetchAndImport-ModuleFromMetadata 'install-ssh-psm1' 'install-ssh.psm1'
  95. InstallAndStart-OpenSsh
  96. StartProcess-WriteSshKeys
  97. }
  98. Set-EnvironmentVars
  99. Create-Directories
  100. Download-HelperScripts
  101. InstallAndStart-LoggingAgent
  102. Create-DockerRegistryKey
  103. Configure-Dockerd
  104. Pull-InfraContainer
  105. DownloadAndInstall-KubernetesBinaries
  106. Create-NodePki
  107. Create-KubeletKubeconfig
  108. Create-KubeproxyKubeconfig
  109. Set-PodCidr
  110. Configure-HostNetworkingService
  111. Configure-CniNetworking
  112. Configure-GcePdTools
  113. Configure-Kubelet
  114. Start-WorkerServices
  115. Log-Output 'Waiting 15 seconds for node to join cluster.'
  116. Start-Sleep 15
  117. Verify-WorkerServices
  118. $config = New-FileRotationConfig
  119. Schedule-LogRotation -Pattern '.*\.log$' -Path ${env:LOGS_DIR} -RepetitionInterval $(New-Timespan -Hour 1) -Config $config
  120. }
  121. catch {
  122. Write-Host 'Exception caught in script:'
  123. Write-Host $_.InvocationInfo.PositionMessage
  124. Write-Host "Kubernetes Windows node setup failed: $($_.Exception.Message)"
  125. exit 1
  126. }