configure.ps1 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. # Returns true if the ENABLE_STACKDRIVER_WINDOWS or ENABLE_NODE_LOGGING field in kube_env is true.
  78. # $KubeEnv is a hash table containing the kube-env metadata keys+values.
  79. # ENABLE_NODE_LOGGING is used for legacy Stackdriver Logging, and will be deprecated (always set to False)
  80. # soon. ENABLE_STACKDRIVER_WINDOWS is added to indicate whether logging is enabled for windows nodes.
  81. function IsLoggingEnabled {
  82. param (
  83. [parameter(Mandatory=$true)] [hashtable]$KubeEnv
  84. )
  85. if ($KubeEnv.Contains('ENABLE_STACKDRIVER_WINDOWS') -and `
  86. ($KubeEnv['ENABLE_STACKDRIVER_WINDOWS'] -eq 'true')) {
  87. return $true
  88. } elseif ($KubeEnv.Contains('ENABLE_NODE_LOGGING') -and `
  89. ($KubeEnv['ENABLE_NODE_LOGGING'] -eq 'true')) {
  90. return $true
  91. }
  92. return $false
  93. }
  94. try {
  95. # Don't use FetchAndImport-ModuleFromMetadata for common.psm1 - the common
  96. # module includes variables and functions that any other function may depend
  97. # on.
  98. $module = Get-InstanceMetadataAttribute 'common-psm1'
  99. New-Item -ItemType file -Force C:\common.psm1 | Out-Null
  100. Set-Content C:\common.psm1 $module
  101. Import-Module -Force C:\common.psm1
  102. # TODO(pjh): update the function to set $Filename automatically from the key,
  103. # then put these calls into a loop over a list of XYZ-psm1 keys.
  104. FetchAndImport-ModuleFromMetadata 'k8s-node-setup-psm1' 'k8s-node-setup.psm1'
  105. Dump-DebugInfoToConsole
  106. Set-PrerequisiteOptions
  107. $kube_env = Fetch-KubeEnv
  108. if (Test-IsTestCluster $kube_env) {
  109. Log-Output 'Test cluster detected, installing OpenSSH.'
  110. FetchAndImport-ModuleFromMetadata 'install-ssh-psm1' 'install-ssh.psm1'
  111. InstallAndStart-OpenSsh
  112. StartProcess-WriteSshKeys
  113. }
  114. Set-EnvironmentVars
  115. Create-Directories
  116. Download-HelperScripts
  117. DownloadAndInstall-Crictl
  118. Configure-Crictl
  119. Setup-ContainerRuntime
  120. DownloadAndInstall-AuthPlugin
  121. DownloadAndInstall-KubernetesBinaries
  122. Create-NodePki
  123. Create-KubeletKubeconfig
  124. Create-KubeproxyKubeconfig
  125. Set-PodCidr
  126. Configure-HostNetworkingService
  127. Prepare-CniNetworking
  128. Configure-HostDnsConf
  129. Configure-GcePdTools
  130. Configure-Kubelet
  131. # Even if Stackdriver is already installed, the function will still [re]start the service.
  132. if (IsLoggingEnabled $kube_env) {
  133. Install-LoggingAgent
  134. Configure-LoggingAgent
  135. Restart-LoggingAgent
  136. }
  137. Start-WorkerServices
  138. Log-Output 'Waiting 15 seconds for node to join cluster.'
  139. Start-Sleep 15
  140. Verify-WorkerServices
  141. $config = New-FileRotationConfig
  142. # TODO(random-liu): Generate containerd log into the log directory.
  143. Schedule-LogRotation -Pattern '.*\.log$' -Path ${env:LOGS_DIR} -RepetitionInterval $(New-Timespan -Hour 1) -Config $config
  144. Pull-InfraContainer
  145. }
  146. catch {
  147. Write-Host 'Exception caught in script:'
  148. Write-Host $_.InvocationInfo.PositionMessage
  149. Write-Host "Kubernetes Windows node setup failed: $($_.Exception.Message)"
  150. exit 1
  151. }