Skip to content

Commit c095cd1

Browse files
Frank MaiAlena Prokharchyk
authored andcommitted
Windows refactor
- Store the Kubernetes execution binary - Keep the same behavior like Linux: bootstrapping script will be stored in sidekick image - Support 1903 **Issue:** rancher/rancher#16460
1 parent 1a73745 commit c095cd1

File tree

5 files changed

+166
-43
lines changed

5 files changed

+166
-43
lines changed

.drone.yml

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,41 @@ steps:
7676
event:
7777
- tag
7878

79+
volumes:
80+
- name: docker_pipe
81+
host:
82+
path: \\\\.\\pipe\\docker_engine
83+
---
84+
kind: pipeline
85+
name: windows-1903
86+
87+
platform:
88+
os: windows
89+
arch: amd64
90+
version: 1903
91+
92+
steps:
93+
- name: publish-hyperkube-windows-1903
94+
image: plugins/docker
95+
settings:
96+
username:
97+
from_secret: docker_username
98+
password:
99+
from_secret: docker_password
100+
dockerfile: Dockerfile.windows
101+
repo: rancher/hyperkube
102+
tag: "${DRONE_TAG}-windows-1903"
103+
build_args:
104+
- SERVERCORE_VERSION=1903
105+
volumes:
106+
- name: docker_pipe
107+
path: \\\\.\\pipe\\docker_engine
108+
when:
109+
instance:
110+
- drone-publish.rancher.io
111+
event:
112+
- tag
113+
79114
volumes:
80115
- name: docker_pipe
81116
host:
@@ -103,3 +138,5 @@ depends_on:
103138
- linux-amd64
104139
- linux-arm64
105140
- windows-1809
141+
- windows-1903
142+

Dockerfile.windows

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
ARG SERVERCORE_VERSION
22

3-
FROM mcr.microsoft.com/windows/servercore:${SERVERCORE_VERSION} as download
3+
FROM mcr.microsoft.com/windows/servercore:${SERVERCORE_VERSION} as builder
44
SHELL ["powershell", "-NoLogo", "-Command", "$ErrorActionPreference = 'Stop'; $ProgressPreference = 'SilentlyContinue';"]
55
RUN if (-not (Get-Command Expand-7Zip -ErrorAction Ignore)) { \
66
Install-PackageProvider -Name NuGet -Force -Verbose; \
@@ -38,19 +38,18 @@ RUN $URL = ('https://dl.k8s.io/{0}/kubernetes-node-windows-amd64.tar.gz' -f $env
3838
}; \
3939
\
4040
Write-Host ('Downloading Kubernetes from {0} ...' -f $URL); \
41-
\
4241
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12; \
4342
Invoke-WebRequest -UseBasicParsing -OutFile c:\kubernetes.tar.gz -Uri $URL; \
4443
\
4544
Write-Host 'Expanding ...'; \
46-
\
4745
Expand-GZip c:\kubernetes.tar.gz c:\kubernetes.tar; \
4846
Expand-7Zip c:\kubernetes.tar c:\; \
4947
\
50-
Write-Host 'Complete.';
48+
Write-Host 'Complete.'
5149

5250
FROM mcr.microsoft.com/powershell:nanoserver-${SERVERCORE_VERSION}
53-
COPY ["windows/*", "c:/Program Files/runtime/"]
54-
COPY --from=download ["/kubernetes/node/bin/kubelet.exe", "/kubernetes/node/bin/kubectl.exe", "/kubernetes/node/bin/kube-proxy.exe", "c:/Program Files/kubernetes/bin/"]
55-
WORKDIR "c:\\Program Files\\runtime"
56-
CMD ["pwsh", "-f", "copy.ps1"]
51+
USER ContainerAdministrator
52+
COPY --from=builder /Windows/System32/netapi32.dll /Windows/System32/
53+
COPY --from=builder /kubernetes/node/bin/kubectl.exe /Windows
54+
COPY --from=builder /kubernetes/node/bin/kubelet.exe /kubernetes/node/bin/kube-proxy.exe /etc/kubernetes/bin/
55+
COPY windows/entrypoint.ps1 /usr/bin/

manifest.tmpl

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,10 @@ manifests:
2020
platform:
2121
architecture: amd64
2222
os: windows
23-
version: 1809
23+
version: 1809
24+
-
25+
image: rancher/hyperkube:{{build.tag}}-windows-1903
26+
platform:
27+
architecture: amd64
28+
os: windows
29+
version: 1903

windows/copy.ps1

Lines changed: 0 additions & 34 deletions
This file was deleted.

windows/entrypoint.ps1

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
<#
2+
watch the sentinel `c:\opt\rke-tools\entrypoint.ps1` is ready or not
3+
#>
4+
5+
$ErrorActionPreference = 'Stop'
6+
$WarningPreference = 'SilentlyContinue'
7+
$VerbosePreference = 'SilentlyContinue'
8+
$DebugPreference = 'SilentlyContinue'
9+
$InformationPreference = 'SilentlyContinue'
10+
11+
function Log-Info
12+
{
13+
Write-Host -NoNewline -ForegroundColor Blue "INFO: "
14+
Write-Host -ForegroundColor Gray ("{0,-44}" -f ($args -join " "))
15+
}
16+
17+
function Log-Fatal
18+
{
19+
Write-Host -NoNewline -ForegroundColor DarkRed "FATA: "
20+
Write-Host -ForegroundColor Gray ("{0,-44}" -f ($args -join " "))
21+
22+
exit 1
23+
}
24+
25+
function Wait-Ready
26+
{
27+
param(
28+
[parameter(Mandatory = $true)] $Path
29+
)
30+
31+
$count = 600
32+
while ($count -gt 0)
33+
{
34+
Start-Sleep -s 1
35+
36+
if (Test-Path $Path -ErrorAction Ignore)
37+
{
38+
Start-Sleep -s 5
39+
break
40+
}
41+
42+
Start-Sleep -s 1
43+
$count -= 1
44+
}
45+
46+
if ($count -le 0)
47+
{
48+
Log-Fatal "Timeout, could not found $Path"
49+
}
50+
}
51+
52+
function Fix-LegacyArgument
53+
{
54+
param (
55+
[parameter(Mandatory = $false)] [string[]]$ArgumentList
56+
)
57+
58+
$argList = @()
59+
$legacy = $null
60+
for ($i = $ArgumentList.Length; $i -ge 0; $i--) {
61+
$arg = $ArgumentList[$i]
62+
switch -regex ($arg)
63+
{
64+
"^-{2}.*" {
65+
if ($legacy) {
66+
$arg = '{0}:{1}' -f $arg, $legacy
67+
$legacy = $null
68+
}
69+
$argList += $arg
70+
}
71+
default {
72+
$legacy = $arg
73+
}
74+
}
75+
}
76+
77+
return $argList
78+
}
79+
80+
function Run-PowerShell
81+
{
82+
param (
83+
[parameter(Mandatory = $false)] [string[]]$ArgumentList
84+
)
85+
86+
try {
87+
if ($ArgumentList) {
88+
Start-Process -NoNewWindow -Wait -FilePath "powershell" -ArgumentList $ArgumentList
89+
} else {
90+
Start-Process -NoNewWindow -Wait -FilePath "powershell"
91+
}
92+
} catch {
93+
$errMsg = $_.Exception.Message
94+
if ($errMsg -like "*This command cannot be run due to the error: *") {
95+
if ($ArgumentList) {
96+
Start-Process -NoNewWindow -Wait -FilePath "pwsh" -ArgumentList $ArgumentList
97+
} else {
98+
Start-Process -NoNewWindow -Wait -FilePath "pwsh"
99+
}
100+
} else {
101+
throw $_
102+
}
103+
}
104+
}
105+
106+
# the bootstrap arguments for kubernetes components are formatted by `component-name --key1=val1 --key2=val2 ...`,
107+
# when passing `--pod-infra-container-image=rancher/kubelet-pause:v0.1.2` via windows docker,
108+
# powershell would recognize it as `--pod-infra-container-image=rancher/kubelet-pause` and `v0.0.2`
109+
$argList = Fix-LegacyArgument -ArgumentList $args[1..$args.Length]
110+
111+
# waiting for the sentinel ready, which provided by servcie-sidekick
112+
Log-Info "Waiting for servcie-sidekick to prepare the entrypoint.ps1 ..."
113+
Wait-Ready -Path "c:\opt\rke-tools\entrypoint.ps1"
114+
$entryArgs = @("-NoLogo", "-NonInteractive", "-File", "c:\opt\rke-tools\entrypoint.ps1", $args[0]) + $argList
115+
Run-PowerShell -ArgumentList $entryArgs

0 commit comments

Comments
 (0)