Skip to content

Commit 8aaef56

Browse files
authored
Merge pull request #506 from zylxjtu/gracefulshutdown
Support the transition from nssm to windows-service manager
2 parents a553e9b + 5af636f commit 8aaef56

File tree

6 files changed

+217
-42
lines changed

6 files changed

+217
-42
lines changed

capz/templates/gmsa-ci.yaml

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -75,13 +75,42 @@ spec:
7575
permissions: "0744"
7676
- content: |
7777
$ErrorActionPreference = "Stop"
78+
$FileContent = Get-Content -Path "$env:SYSTEMDRIVE/var/lib/kubelet/kubeadm-flags.env"
79+
$kubeAdmArgs = $FileContent.TrimStart('KUBELET_KUBEADM_ARGS=').Trim('"')
80+
81+
# Set the path to your kubelet config file
82+
$configFile = "$env:SYSTEMDRIVE/var/lib/kubelet/config.yaml"
83+
$content = Get-Content -Path $configFile
7884
# Check if NodeLogQuery feature gate was specified for the cluster and if so, also enable it via kubelet config
79-
$kubeletEnvContents = Get-Content -Path "/var/lib/kubelet/kubeadm-flags.env"
80-
if ($kubeletEnvContents.Contains("NodeLogQuery=true")) {
81-
Add-Content -Path "$env:SYSTEMDRIVE/var/lib/kubelet/config.yaml" -Value "enableSystemLogQuery: true" -Encoding ascii -NoNewLine
82-
nssm restart kubelet
85+
if ($kubeAdmArgs.Contains("NodeLogQuery=true")) {
86+
$content += "enableSystemLogQuery: true"
87+
}
88+
if ($kubeAdmArgs.Contains("WindowsGracefulNodeShutdown=true")) {
89+
# Remove existing lines that match the settings
90+
$content = $content | Where-Object { $_ -notmatch "^shutdownGracePeriod:" -and $_ -notmatch "^shutdownGracePeriodCriticalPods:" }
91+
# Append new values at the end
92+
$content += "shutdownGracePeriod: 1m"
93+
$content += "shutdownGracePeriodCriticalPods: 20s"
94+
}
95+
96+
# Write the updated content back to the file
97+
$content | Set-Content -Path $configFile -Encoding ascii
98+
path: C:/updateKubeletConfig.ps1
99+
permissions: "0744"
100+
- content: |
101+
# This script restarts the kubelet service.
102+
# It supports both nssm and Windows service configurations,
103+
# ensuring a smooth transition between service managers without interruption.
104+
# This script will eventually be removed once nssm is no longer used.
105+
$scriptPath = "$env:SYSTEMDRIVE/k/RestartKubelet.ps1"
106+
if (Test-Path $scriptPath) {
107+
Write-Host "Found RestartKubelet.ps1, executing..."
108+
& $scriptPath
109+
} else {
110+
Write-Host "RestartKubelet.ps1 not found. Running fallback command..."
111+
nssm set kubelet start SERVICE_AUTO_START
83112
}
84-
path: C:/NodeLogQueryKubeletConfig.ps1
113+
path: C:/KubeletRestart_nssm_sc.ps1
85114
permissions: "0744"
86115
- content: |
87116
mkdir -Force c:/localdumps
@@ -146,8 +175,8 @@ spec:
146175
windows-priorityclass: ABOVE_NORMAL_PRIORITY_CLASS
147176
name: '{{ ds.meta_data["local_hostname"] }}'
148177
postKubeadmCommands:
149-
- powershell C:/NodeLogQueryKubeletConfig.ps1
150-
- nssm set kubelet start SERVICE_AUTO_START
178+
- powershell C:/updateKubeletConfig.ps1
179+
- powershell C:/KubeletRestart_nssm_sc.ps1
151180
- powershell C:/defender-exclude-calico.ps1
152181
preKubeadmCommands:
153182
- powershell C:/create-temp-folder.ps1

capz/templates/gmsa-pr.yaml

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -75,13 +75,42 @@ spec:
7575
permissions: "0744"
7676
- content: |
7777
$ErrorActionPreference = "Stop"
78+
$FileContent = Get-Content -Path "$env:SYSTEMDRIVE/var/lib/kubelet/kubeadm-flags.env"
79+
$kubeAdmArgs = $FileContent.TrimStart('KUBELET_KUBEADM_ARGS=').Trim('"')
80+
81+
# Set the path to your kubelet config file
82+
$configFile = "$env:SYSTEMDRIVE/var/lib/kubelet/config.yaml"
83+
$content = Get-Content -Path $configFile
7884
# Check if NodeLogQuery feature gate was specified for the cluster and if so, also enable it via kubelet config
79-
$kubeletEnvContents = Get-Content -Path "/var/lib/kubelet/kubeadm-flags.env"
80-
if ($kubeletEnvContents.Contains("NodeLogQuery=true")) {
81-
Add-Content -Path "$env:SYSTEMDRIVE/var/lib/kubelet/config.yaml" -Value "enableSystemLogQuery: true" -Encoding ascii -NoNewLine
82-
nssm restart kubelet
85+
if ($kubeAdmArgs.Contains("NodeLogQuery=true")) {
86+
$content += "enableSystemLogQuery: true"
87+
}
88+
if ($kubeAdmArgs.Contains("WindowsGracefulNodeShutdown=true")) {
89+
# Remove existing lines that match the settings
90+
$content = $content | Where-Object { $_ -notmatch "^shutdownGracePeriod:" -and $_ -notmatch "^shutdownGracePeriodCriticalPods:" }
91+
# Append new values at the end
92+
$content += "shutdownGracePeriod: 1m"
93+
$content += "shutdownGracePeriodCriticalPods: 20s"
94+
}
95+
96+
# Write the updated content back to the file
97+
$content | Set-Content -Path $configFile -Encoding ascii
98+
path: C:/updateKubeletConfig.ps1
99+
permissions: "0744"
100+
- content: |
101+
# This script restarts the kubelet service.
102+
# It supports both nssm and Windows service configurations,
103+
# ensuring a smooth transition between service managers without interruption.
104+
# This script will eventually be removed once nssm is no longer used.
105+
$scriptPath = "$env:SYSTEMDRIVE/k/RestartKubelet.ps1"
106+
if (Test-Path $scriptPath) {
107+
Write-Host "Found RestartKubelet.ps1, executing..."
108+
& $scriptPath
109+
} else {
110+
Write-Host "RestartKubelet.ps1 not found. Running fallback command..."
111+
nssm set kubelet start SERVICE_AUTO_START
83112
}
84-
path: C:/NodeLogQueryKubeletConfig.ps1
113+
path: C:/KubeletRestart_nssm_sc.ps1
85114
permissions: "0744"
86115
- content: |
87116
mkdir -Force c:/localdumps
@@ -141,8 +170,8 @@ spec:
141170
windows-priorityclass: ABOVE_NORMAL_PRIORITY_CLASS
142171
name: '{{ ds.meta_data["local_hostname"] }}'
143172
postKubeadmCommands:
144-
- powershell C:/NodeLogQueryKubeletConfig.ps1
145-
- nssm set kubelet start SERVICE_AUTO_START
173+
- powershell C:/updateKubeletConfig.ps1
174+
- powershell C:/KubeletRestart_nssm_sc.ps1
146175
- powershell C:/defender-exclude-calico.ps1
147176
preKubeadmCommands:
148177
- powershell C:/create-temp-folder.ps1

capz/templates/shared-image-gallery-ci.yaml

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -75,13 +75,42 @@ spec:
7575
permissions: "0744"
7676
- content: |
7777
$ErrorActionPreference = "Stop"
78+
$FileContent = Get-Content -Path "$env:SYSTEMDRIVE/var/lib/kubelet/kubeadm-flags.env"
79+
$kubeAdmArgs = $FileContent.TrimStart('KUBELET_KUBEADM_ARGS=').Trim('"')
80+
81+
# Set the path to your kubelet config file
82+
$configFile = "$env:SYSTEMDRIVE/var/lib/kubelet/config.yaml"
83+
$content = Get-Content -Path $configFile
7884
# Check if NodeLogQuery feature gate was specified for the cluster and if so, also enable it via kubelet config
79-
$kubeletEnvContents = Get-Content -Path "/var/lib/kubelet/kubeadm-flags.env"
80-
if ($kubeletEnvContents.Contains("NodeLogQuery=true")) {
81-
Add-Content -Path "$env:SYSTEMDRIVE/var/lib/kubelet/config.yaml" -Value "enableSystemLogQuery: true" -Encoding ascii -NoNewLine
82-
nssm restart kubelet
85+
if ($kubeAdmArgs.Contains("NodeLogQuery=true")) {
86+
$content += "enableSystemLogQuery: true"
87+
}
88+
if ($kubeAdmArgs.Contains("WindowsGracefulNodeShutdown=true")) {
89+
# Remove existing lines that match the settings
90+
$content = $content | Where-Object { $_ -notmatch "^shutdownGracePeriod:" -and $_ -notmatch "^shutdownGracePeriodCriticalPods:" }
91+
# Append new values at the end
92+
$content += "shutdownGracePeriod: 1m"
93+
$content += "shutdownGracePeriodCriticalPods: 20s"
94+
}
95+
96+
# Write the updated content back to the file
97+
$content | Set-Content -Path $configFile -Encoding ascii
98+
path: C:/updateKubeletConfig.ps1
99+
permissions: "0744"
100+
- content: |
101+
# This script restarts the kubelet service.
102+
# It supports both nssm and Windows service configurations,
103+
# ensuring a smooth transition between service managers without interruption.
104+
# This script will eventually be removed once nssm is no longer used.
105+
$scriptPath = "$env:SYSTEMDRIVE/k/RestartKubelet.ps1"
106+
if (Test-Path $scriptPath) {
107+
Write-Host "Found RestartKubelet.ps1, executing..."
108+
& $scriptPath
109+
} else {
110+
Write-Host "RestartKubelet.ps1 not found. Running fallback command..."
111+
nssm set kubelet start SERVICE_AUTO_START
83112
}
84-
path: C:/NodeLogQueryKubeletConfig.ps1
113+
path: C:/KubeletRestart_nssm_sc.ps1
85114
permissions: "0744"
86115
- content: |
87116
mkdir -Force c:/localdumps
@@ -151,8 +180,8 @@ spec:
151180
windows-priorityclass: ABOVE_NORMAL_PRIORITY_CLASS
152181
name: '{{ ds.meta_data["local_hostname"] }}'
153182
postKubeadmCommands:
154-
- powershell C:/NodeLogQueryKubeletConfig.ps1
155-
- nssm set kubelet start SERVICE_AUTO_START
183+
- powershell C:/updateKubeletConfig.ps1
184+
- powershell C:/KubeletRestart_nssm_sc.ps1
156185
- powershell C:/defender-exclude-calico.ps1
157186
preKubeadmCommands:
158187
- powershell C:/create-temp-folder.ps1

capz/templates/windows-base.yaml

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -75,13 +75,43 @@ spec:
7575
permissions: "0744"
7676
- content: |
7777
$ErrorActionPreference = "Stop"
78+
$FileContent = Get-Content -Path "$env:SYSTEMDRIVE/var/lib/kubelet/kubeadm-flags.env"
79+
$kubeAdmArgs = $FileContent.TrimStart('KUBELET_KUBEADM_ARGS=').Trim('"')
80+
81+
# Set the path to your kubelet config file
82+
$configFile = "$env:SYSTEMDRIVE/var/lib/kubelet/config.yaml"
83+
$content = Get-Content -Path $configFile
7884
# Check if NodeLogQuery feature gate was specified for the cluster and if so, also enable it via kubelet config
79-
$kubeletEnvContents = Get-Content -Path "/var/lib/kubelet/kubeadm-flags.env"
80-
if ($kubeletEnvContents.Contains("NodeLogQuery=true")) {
81-
Add-Content -Path "$env:SYSTEMDRIVE/var/lib/kubelet/config.yaml" -Value "enableSystemLogQuery: true" -Encoding ascii -NoNewLine
82-
nssm restart kubelet
85+
if ($kubeAdmArgs.Contains("NodeLogQuery=true")) {
86+
$content += "enableSystemLogQuery: true"
87+
}
88+
if ($kubeAdmArgs.Contains("WindowsGracefulNodeShutdown=true")) {
89+
# Remove existing lines that match the settings
90+
$content = $content | Where-Object { $_ -notmatch "^shutdownGracePeriod:" -and $_ -notmatch "^shutdownGracePeriodCriticalPods:" }
91+
# Append new values at the end
92+
$content += "shutdownGracePeriod: 1m"
93+
$content += "shutdownGracePeriodCriticalPods: 20s"
94+
}
95+
96+
# Write the updated content back to the file
97+
$content | Set-Content -Path $configFile -Encoding ascii
98+
99+
path: C:/updateKubeletConfig.ps1
100+
permissions: "0744"
101+
- content: |
102+
# This script restarts the kubelet service.
103+
# It supports both nssm and Windows service configurations,
104+
# ensuring a smooth transition between service managers without interruption.
105+
# This script will eventually be removed once nssm is no longer used.
106+
$scriptPath = "$env:SYSTEMDRIVE/k/RestartKubelet.ps1"
107+
if (Test-Path $scriptPath) {
108+
Write-Host "Found RestartKubelet.ps1, executing..."
109+
& $scriptPath
110+
} else {
111+
Write-Host "RestartKubelet.ps1 not found. Running fallback command..."
112+
nssm set kubelet start SERVICE_AUTO_START
83113
}
84-
path: C:/NodeLogQueryKubeletConfig.ps1
114+
path: C:/KubeletRestart_nssm_sc.ps1
85115
permissions: "0744"
86116
- content: |
87117
mkdir -Force c:/localdumps
@@ -113,8 +143,8 @@ spec:
113143
windows-priorityclass: ABOVE_NORMAL_PRIORITY_CLASS
114144
name: '{{ ds.meta_data["local_hostname"] }}'
115145
postKubeadmCommands:
116-
- powershell C:/NodeLogQueryKubeletConfig.ps1
117-
- nssm set kubelet start SERVICE_AUTO_START
146+
- powershell C:/updateKubeletConfig.ps1
147+
- powershell C:/KubeletRestart_nssm_sc.ps1
118148
- powershell C:/defender-exclude-calico.ps1
119149
preKubeadmCommands:
120150
- powershell C:/create-temp-folder.ps1

capz/templates/windows-ci.yaml

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -75,13 +75,42 @@ spec:
7575
permissions: "0744"
7676
- content: |
7777
$ErrorActionPreference = "Stop"
78+
$FileContent = Get-Content -Path "$env:SYSTEMDRIVE/var/lib/kubelet/kubeadm-flags.env"
79+
$kubeAdmArgs = $FileContent.TrimStart('KUBELET_KUBEADM_ARGS=').Trim('"')
80+
81+
# Set the path to your kubelet config file
82+
$configFile = "$env:SYSTEMDRIVE/var/lib/kubelet/config.yaml"
83+
$content = Get-Content -Path $configFile
7884
# Check if NodeLogQuery feature gate was specified for the cluster and if so, also enable it via kubelet config
79-
$kubeletEnvContents = Get-Content -Path "/var/lib/kubelet/kubeadm-flags.env"
80-
if ($kubeletEnvContents.Contains("NodeLogQuery=true")) {
81-
Add-Content -Path "$env:SYSTEMDRIVE/var/lib/kubelet/config.yaml" -Value "enableSystemLogQuery: true" -Encoding ascii -NoNewLine
82-
nssm restart kubelet
85+
if ($kubeAdmArgs.Contains("NodeLogQuery=true")) {
86+
$content += "enableSystemLogQuery: true"
87+
}
88+
if ($kubeAdmArgs.Contains("WindowsGracefulNodeShutdown=true")) {
89+
# Remove existing lines that match the settings
90+
$content = $content | Where-Object { $_ -notmatch "^shutdownGracePeriod:" -and $_ -notmatch "^shutdownGracePeriodCriticalPods:" }
91+
# Append new values at the end
92+
$content += "shutdownGracePeriod: 1m"
93+
$content += "shutdownGracePeriodCriticalPods: 20s"
94+
}
95+
96+
# Write the updated content back to the file
97+
$content | Set-Content -Path $configFile -Encoding ascii
98+
path: C:/updateKubeletConfig.ps1
99+
permissions: "0744"
100+
- content: |
101+
# This script restarts the kubelet service.
102+
# It supports both nssm and Windows service configurations,
103+
# ensuring a smooth transition between service managers without interruption.
104+
# This script will eventually be removed once nssm is no longer used.
105+
$scriptPath = "$env:SYSTEMDRIVE/k/RestartKubelet.ps1"
106+
if (Test-Path $scriptPath) {
107+
Write-Host "Found RestartKubelet.ps1, executing..."
108+
& $scriptPath
109+
} else {
110+
Write-Host "RestartKubelet.ps1 not found. Running fallback command..."
111+
nssm set kubelet start SERVICE_AUTO_START
83112
}
84-
path: C:/NodeLogQueryKubeletConfig.ps1
113+
path: C:/KubeletRestart_nssm_sc.ps1
85114
permissions: "0744"
86115
- content: |
87116
mkdir -Force c:/localdumps
@@ -146,8 +175,8 @@ spec:
146175
windows-priorityclass: ABOVE_NORMAL_PRIORITY_CLASS
147176
name: '{{ ds.meta_data["local_hostname"] }}'
148177
postKubeadmCommands:
149-
- powershell C:/NodeLogQueryKubeletConfig.ps1
150-
- nssm set kubelet start SERVICE_AUTO_START
178+
- powershell C:/updateKubeletConfig.ps1
179+
- powershell C:/KubeletRestart_nssm_sc.ps1
151180
- powershell C:/defender-exclude-calico.ps1
152181
preKubeadmCommands:
153182
- powershell C:/create-temp-folder.ps1

capz/templates/windows-pr.yaml

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -75,13 +75,42 @@ spec:
7575
permissions: "0744"
7676
- content: |
7777
$ErrorActionPreference = "Stop"
78+
$FileContent = Get-Content -Path "$env:SYSTEMDRIVE/var/lib/kubelet/kubeadm-flags.env"
79+
$kubeAdmArgs = $FileContent.TrimStart('KUBELET_KUBEADM_ARGS=').Trim('"')
80+
81+
# Set the path to your kubelet config file
82+
$configFile = "$env:SYSTEMDRIVE/var/lib/kubelet/config.yaml"
83+
$content = Get-Content -Path $configFile
7884
# Check if NodeLogQuery feature gate was specified for the cluster and if so, also enable it via kubelet config
79-
$kubeletEnvContents = Get-Content -Path "/var/lib/kubelet/kubeadm-flags.env"
80-
if ($kubeletEnvContents.Contains("NodeLogQuery=true")) {
81-
Add-Content -Path "$env:SYSTEMDRIVE/var/lib/kubelet/config.yaml" -Value "enableSystemLogQuery: true" -Encoding ascii -NoNewLine
82-
nssm restart kubelet
85+
if ($kubeAdmArgs.Contains("NodeLogQuery=true")) {
86+
$content += "enableSystemLogQuery: true"
87+
}
88+
if ($kubeAdmArgs.Contains("WindowsGracefulNodeShutdown=true")) {
89+
# Remove existing lines that match the settings
90+
$content = $content | Where-Object { $_ -notmatch "^shutdownGracePeriod:" -and $_ -notmatch "^shutdownGracePeriodCriticalPods:" }
91+
# Append new values at the end
92+
$content += "shutdownGracePeriod: 1m"
93+
$content += "shutdownGracePeriodCriticalPods: 20s"
94+
}
95+
96+
# Write the updated content back to the file
97+
$content | Set-Content -Path $configFile -Encoding ascii
98+
path: C:/updateKubeletConfig.ps1
99+
permissions: "0744"
100+
- content: |
101+
# This script restarts the kubelet service.
102+
# It supports both nssm and Windows service configurations,
103+
# ensuring a smooth transition between service managers without interruption.
104+
# This script will eventually be removed once nssm is no longer used.
105+
$scriptPath = "$env:SYSTEMDRIVE/k/RestartKubelet.ps1"
106+
if (Test-Path $scriptPath) {
107+
Write-Host "Found RestartKubelet.ps1, executing..."
108+
& $scriptPath
109+
} else {
110+
Write-Host "RestartKubelet.ps1 not found. Running fallback command..."
111+
nssm set kubelet start SERVICE_AUTO_START
83112
}
84-
path: C:/NodeLogQueryKubeletConfig.ps1
113+
path: C:/KubeletRestart_nssm_sc.ps1
85114
permissions: "0744"
86115
- content: |
87116
mkdir -Force c:/localdumps
@@ -141,8 +170,8 @@ spec:
141170
windows-priorityclass: ABOVE_NORMAL_PRIORITY_CLASS
142171
name: '{{ ds.meta_data["local_hostname"] }}'
143172
postKubeadmCommands:
144-
- powershell C:/NodeLogQueryKubeletConfig.ps1
145-
- nssm set kubelet start SERVICE_AUTO_START
173+
- powershell C:/updateKubeletConfig.ps1
174+
- powershell C:/KubeletRestart_nssm_sc.ps1
146175
- powershell C:/defender-exclude-calico.ps1
147176
preKubeadmCommands:
148177
- powershell C:/create-temp-folder.ps1

0 commit comments

Comments
 (0)