-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwinnfs-flex-volume.yaml
475 lines (433 loc) · 14.4 KB
/
winnfs-flex-volume.yaml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
apiVersion: v1
kind: ConfigMap
metadata:
name: flexvolume-nfs-windows
namespace: kube-system
data:
flexvolume.ps1: |-
$debug_mode = $true
$exitCode = 0
$logName = "Application"
$logId = 1
if($logSource -eq $null)
{
throw "Define logSource before including this script"
}
#delete a symbolic link
Function DeleteSymLink($path)
{
$item = get-item $path
#should test that
if($item.linkType -eq "SymbolicLink")
{
$item.delete()
}
}
#allow you to expect an item and throw a useful message
Filter GetFirst
{
Param([string] $message)
Begin
{
$foundItem = $false;
}
Process
{
if(-not $foundItem)
{
$foundItem = $true
$_
}
}
End
{
if(-not $foundItem)
{
throw $message
}
}
}
Function IsSymLinkToPath($symLink, $path)
{
if($item.LinkType -eq $null)
{
return $false
}
$target = $item.target[0]
if($path.SubString(0, 2) -eq "\\")
{
# for nfs try
# UNC\machinename instead of \\machinename
if(("UNC" + $path.Substring(1)) -eq $target)
{
return $true
}
# for volumes try
# Voume instead of \\?\Volume
if($path.SubString(0, 4) -eq "\\?\")
{
if($target -eq $path.Substring(4))
{
return $true
}
}
}
if($target -eq $path)
{
return $true
}
return $false
}
#create the symbolic link for kubernetes
Function MakeSymLink($symlink, $remotepath)
{
Log "mklink $symlink $remotePath"
# flexvolume may have already created the folder at the location the symlink should be
# I think this is for bind mount in linux
# this does not work for windows, so lets delete the folder
if($(test-path $symlink -PathType Container))
{
$item = get-item $symlink
if($item.LinkType -eq $null)
{
Log "deleting folder $symlink"
#if not empty this will throw
$item.delete()
}
}
if($(test-path $symlink -PathType Container))
{
$item = get-item $symlink
if($item.LinkType -eq "SymbolicLink")
{
if(IsSymLinkToPath -symLink $item -path $remotePath)
{
Log "symlink already existed"
return
}
else
{
Log "stale symlink already existed to old path $($item.target[0])"
DeleteSymLink $symlink
}
}
else
{
throw "Cannot make symlink $symlink to $remotepath due to folder already existing"
}
}
elseif( $(test-path $symlink))
{
throw "Cannot make symlink $symlink to $remotepath due to file already existing"
}
else
{
}
#create a symbolic link using powershell (doesn't work for volumes)
$link = new-item -ItemType SymbolicLink -Path $symlink -Value $remoteP
#volume symbolic link workaround below
#cmd /c "mklink /D $symlink $remotepath" 2>&1 | Out-Null
#ensure symlink was created
$item = get-item $symlink
#should test that
if($item.linkType -ne "SymbolicLink")
{
throw "$symlink was not created"
}
Log $item
}
function Log([string] $s)
{
#if(($s -eq $null) -or $s.Trim() -eq "")
#{
#$s = <empty>
#}
# always prepending message as things are cleaner for empty lines
Write-EventLog -LogName $logName -Source $logSource -EntryType Information -EventId $logId -Message "log: $s"
}
function DebugLog([string] $s)
{
if($debug_mode)
{
Log $s
}
}
function Print([string] $s)
{
write-host $s
}
function LogAndPrint([string] $s)
{
Print $s
Log $s
}
function NormalizePath([string] $s)
{
# v1.5 $('c:' + $s.replace('/','\'))
$s.replace('/', '\')
}
function Base64Decode([string] $s)
{
[System.Text.Encoding]::ASCII.GetString([System.Convert]::FromBase64String($s))
}
function DoCommand ( [Parameter(Mandatory = $true)] [string] $command,
[bool] $throw = $false,
[object[]] $objectList = @() )
{
$scriptBlock = [scriptblock]::Create($command)
$b = Invoke-command -ErrorVariable err -ScriptBlock $scriptBlock -OutVariable output -ArgumentList $objectList
if($throw -And $err -ne $null)
{
throw $err[0]
}
return $output
}
function DoCommandValidateErrorCode([Parameter(Mandatory = $true)] [string] $command )
{
$output = DoCommand $command
$errorCode = $LASTEXITCODE
if($errorCode -ne 0)
{
$errorMessage = "Error $errorCode running command $command"
Log $errorMessage
throw $errorMessage
}
return $output
}
function GetParentPid($processId)
{
gwmi win32_process -Filter "ProcessId='$processId'" | % { $_.parentprocessid}
}
function GetCommandLine($processId)
{
gwmi win32_process -Filter "ProcessId='$processId'" | % { $_.commandline}
}
New-EventLog -LogName $logName -Source $logSource -ErrorAction Ignore
function RunFlexVolume()
{
$command = $env:flexvolume_command
$folder = $env:flexvolume_folder
$json = $null
$cmdline = GetCommandLine $(GetParentPid $pid)
DebugLog "command: $command"
DebugLog "folder: $folder"
DebugLog "cmdline: $cmdline"
if($folder -ne $null)
{
# No longer use flexvolume_all as it has problems with the sequence !@#
# I am also doing this because of batch rules and ',' escaping rules
#
# Take the hammer approach and get the commandline of the parent process and parse
# after the script file name
$firstDotCmdRemoved = $cmdline.substring($cmdline.IndexOf(".cmd") + 4)
$secondDotCmdRemoved = $firstDotCmdRemoved.substring($firstDotCmdRemoved.IndexOf(".cmd") + 4)
$all = $secondDotCmdRemoved.Trim('" ')
# remove the first 2 args by string length and removing white space inbetween
# this was the "best" work around for getting the json args
$escapedJson = $all.Substring($command.Length).Trim().SubString($folder.Length)
$json = $escapedJson.Replace('\"', '"')
DebugLog "json: $json"
}
Log "$command"
try
{
if($command -eq $null)
{
exit 0
}
if($command -eq "init")
{
init
$output = @{"status" = "Success"; "capabilities" = @{"attach" = $false}} | ConvertTo-Json -Compress
LogAndPrint $output
}
elseif($command -eq "mount")
{
$normPath = NormalizePath $folder
$makePath = $normPath + "\..\"
Log "Make dir $makePath"
if(-not $(test-path $makePath -PathType Container))
{
mkdir $makePath 2>&1 | Out-Null
}
$options = $json | convertfrom-json
DebugLog $options
mount_command -path $normPath -options $options
LogAndPrint '{"status": "Success"}'
}
elseif($command -eq "unmount")
{
$normPath = NormalizePath $folder
Log "unmount $normpath"
unmount_command -path $normPath
LogAndPrint '{"status": "Success"}'
}
elseif($command -eq "test")
{
test
}
else
{
$output = @{"status" = "Not supported"; "message" = "Unsupported command $command"} | ConvertTo-Json -Compress
Print $output
Log "Unsupported command $command"
$exitCode = 0
}
}
catch
{
$exception = $_
[string] $stack = $exception.ScriptStackTrace
$s = "Caught exception $exception with stack $stack"
Log $s
$output = @{"status" = "Failure"; "message" = "$s"} | ConvertTo-Json -Compress
Print $output
$exitCode = 1
}
Log " "
exit $exitCode
}
nfs.cmd: |-
@echo off
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
setlocal enabledelayedexpansion
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:: Get the drive, path, and file name of this file, minus the .CMD extension
set scriptname=%~dpn0
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:: using environment variables to avoid escaping pains
set flexvolume_command=%1
set flexvolume_folder=%2
:: Not trying to parse the json as it ended up being hard
:: script just gets the executed command line off the parent process (this script)
:: set flexvolume_json=%3
:: set flexvolume_all=%*
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:: launch powershell
PowerShell.exe -NoLogo -Sta -NoProfile -ExecutionPolicy "ByPass" -Command "%scriptname%.ps1"
goto :eof
nfs.ps1: |-
$global:ErrorActionPreference = [System.Management.Automation.ActionPreference]::Stop
$logSource = "KubeNFS"
. $PSScriptRoot\flexvolume.ps1
# Takes in \\servername\share\path or nfs://servername/share/path or //servername/share/path
# and returns \\servername\share\path
function MigrateLinuxCifsPathToWindows([string]$nfsPath)
{
if($nfsPath.StartsWith('nfs://','CurrentCultureIgnoreCase'))
{
$nfsPath = '//' + $nfsPath.SubString('nfs://'.Length)
}
if($nfsPath.StartsWith('//'))
{
$nfsPath = $nfsPath.replace('/', '\')
}
return $nfsPath
}
function init()
{
cmd /c "dism /online /enable-feature /featurename:ServicesForNFS-ServerAndClient" 2>&1 | Out-Null
cmd /c "dism /online /enable-feature /featurename:ClientForNFS-Infrastructure" 2>&1 | Out-Null
#Set-ItemProperty -Path HKLM:\SYSTEM\CurrentControlSet\Services\LanmanWorkstation\Parameters -Name 'AllowInsecureGuestAuth' -Value '1'
#Set-ItemProperty -Path HKLM:\SYSTEM\CurrentControlSet\Control\'Session Manager'\'Memory Management' -Name 'LargeSystemCache' -Value '1'
#Set-ItemProperty -Path HKLM:\SYSTEM\CurrentControlSet\Services\LanmanServer\Parameters -Name 'Size' -Value '3'
}
function mount_command([string]$path, $options)
{
$remoteP = $options.source
$remoteP = MigrateLinuxCifsPathToWindows -nfsPath $remoteP
Log $remoteP
MakeSymLink $path $remoteP
}
function unmount_command([string]$path)
{
Log "removing symlink for path $path"
#if there is no disk to disconnect then we don't care
try
{
DeleteSymLink $path
}
catch
{
Log "Did not do all steps of unmount, but will report success anyways"
}
}
RunFlexVolume
Readme.md: |2-
Driver has been installed.
Make sure /flexvolume-nfs from this container mounts to Kubernetes driver directory.
k8s 1.18.x
C:\usr\libexec\kubernetes\kubelet-plugins\volume\exec\
For Volume plugin directory path
C:\k\volumeplugins
This path may be different in your system due to kubelet parameter --volume-plugin-dir.
This driver depends on the following packages to be installed on the host:
## windows cmd
dism /online /enable-feature /featurename:ServicesForNFS-ServerAndClient
dism /online /enable-feature /featurename:ClientForNFS-Infrastructure
Make sure sufficient permission in NFS share (777 for UNIX)
Now you can delete this Daemonset.
---
apiVersion: apps/v1
kind: DaemonSet
metadata:
name: flexvolume-nfs-windows
namespace: kube-system
labels:
k8s-volume: flexvolume
spec:
selector:
matchLabels:
name: flexvolume-nfs-windows
template:
metadata:
labels:
name: flexvolume-nfs-windows
spec:
nodeSelector:
beta.kubernetes.io/os: windows
tolerations:
- key: "os"
operator: "Equal"
value: "windows"
effect: "NoSchedule"
- key: CriticalAddonsOnly
operator: Exists
- operator: Exists
containers:
- name: flexvolume-nfs-windows
image: mcr.microsoft.com/powershell:7.1.0-preview.5-nanoserver-1809
imagePullPolicy: Always
env:
- name: DRIVER
value: nfs-win
command:
- pwsh.exe
args:
- /Command
- mkdir -Force c:\host\$env:DRIVER~nfs.cmd;
- cp -Force c:\kubelet-plugins\flexvolume.ps1 c:\host\$env:DRIVER~nfs.cmd;
- cp -Force c:\kubelet-plugins\nfs.cmd c:\host\$env:DRIVER~nfs.cmd;
- cp -Force c:\kubelet-plugins\nfs.ps1 c:\host\$env:DRIVER~nfs.cmd;
- cat c:\kubelet-plugins\Readme.md;
- while ($true) {start-sleep -s 3600}
securityContext:
privileged: true
volumeMounts:
- name: kubelet-plugins-host
mountPath: C:\host
- name: flexvolume-nfs
mountPath: c:\kubelet-plugins
volumes:
- name: kubelet-plugins-host
hostPath:
# AKS Volume plugin directory
path: C:\k\volumeplugins
# Default directory for volume plugins
#path: C:\usr\libexec\kubernetes\kubelet-plugins\volume\exec
type: DirectoryOrCreate
- name: flexvolume-nfs
configMap:
defaultMode: 420
name: flexvolume-nfs-windows