2
2
set -e
3
3
4
4
kubectl=kubectl
5
- version=1.5.5
5
+ version=1.7.0
6
6
generator=" "
7
7
node=" "
8
8
nodefaultctx=0
9
9
nodefaultns=0
10
- cmd=' [ "nsenter", "--target", "1", "--mount", "--uts", "--ipc", "--net", "--pid", "--"'
10
+ container_cpu=" ${KUBECTL_NODE_SHELL_POD_CPU:- 100m} "
11
+ container_memory=" ${KUBECTL_NODE_SHELL_POD_MEMORY:- 256Mi} "
12
+ labels=" ${KUBECTL_NODE_SHELL_LABELS} "
13
+
11
14
if [ -t 0 ]; then
12
15
tty=true
13
16
else
@@ -68,11 +71,42 @@ while [ $# -gt 0 ]; do
68
71
esac
69
72
done
70
73
74
+ if [ -z " $node " ]; then
75
+ echo " Please specify node name"
76
+ exit 1
77
+ fi
78
+
71
79
# Set the default context and namespace to avoid situations where the user switch them during the build process
72
80
[ " $nodefaultctx " = 1 ] || kubectl=" $kubectl --context=$( ${kubectl} config current-context) "
73
81
[ " $nodefaultns " = 1 ] || kubectl=" $kubectl --namespace=$( ${kubectl} config view --minify --output ' jsonpath={.contexts..namespace}' ) "
74
82
83
+ # Check the node and retrieve the node OS label
84
+ os=" $( $kubectl get node $node -o jsonpath=" {.metadata.labels.kubernetes\.io/os}" || exit 1) "
85
+
86
+ # Set pod configuration per operating system
87
+ if [ " $os " = " windows" ]; then
88
+ image=" ${KUBECTL_NODE_SHELL_IMAGE_WINDOWS:- mcr.microsoft.com/ powershell} "
89
+ name=" pwsh"
90
+ pod=" ${name} -$( env LC_ALL=C tr -dc a-z0-9 < /dev/urandom | head -c 6) "
91
+ # pwsh has to be launched via cmd.exe because of how containerd 1.6 handles the mount of the container filesystem
92
+ # see https://kubernetes.io/docs/tasks/configure-pod-container/create-hostprocess-pod/#volume-mounts
93
+ cmd_start=' "cmd.exe", "/c", "%CONTAINER_SANDBOX_MOUNT_POINT%\\Program Files\\PowerShell\\latest\\pwsh.exe", "-nol", "-wd", "C:\\"'
94
+ cmd_arg_prefix=' , "-Command"'
95
+ cmd_default=' '
96
+ security_context=' {"privileged":true,"windowsOptions":{"hostProcess":true,"runAsUserName":"NT AUTHORITY\\SYSTEM"}}'
97
+ else # If the OS isn't windows, assume linux
98
+ image=" ${KUBECTL_NODE_SHELL_IMAGE:- docker.io/ library/ alpine} "
99
+ name=" nsenter"
100
+ pod=" ${name} -$( env LC_ALL=C tr -dc a-z0-9 < /dev/urandom | head -c 6) "
101
+ cmd_start=' "nsenter", "--target", "1", "--mount", "--uts", "--ipc", "--net", "--pid"'
102
+ cmd_arg_prefix=' , "--"'
103
+ cmd_default=' , "bash", "-l"'
104
+ security_context=' {"privileged":true}'
105
+ fi
106
+
107
+ # Build the container command
75
108
if [ $# -gt 0 ]; then
109
+ cmd=" [ $cmd_start $cmd_arg_prefix "
76
110
while [ $# -gt 0 ]; do
77
111
cmd=" $cmd , \" $( echo " $1 " | \
78
112
awk ' {gsub(/["\\]/,"\\\\&");gsub(/\x1b/,"\\u001b");printf "%s",last;last=$0"\\n"} END{print $0}' \
@@ -81,63 +115,34 @@ if [ $# -gt 0 ]; then
81
115
done
82
116
cmd=" $cmd ]"
83
117
else
84
- cmd=" $cmd , \" bash\" , \" -l\" ]"
85
- fi
86
-
87
- if [ -z " $node " ]; then
88
- echo " Please specify node name"
89
- exit 1
118
+ cmd=" [ $cmd_start $cmd_default ]"
90
119
fi
91
120
92
- image=" ${KUBECTL_NODE_SHELL_IMAGE:- docker.io/ library/ alpine} "
93
- pod=" nsenter-$( env LC_ALL=C tr -dc a-z0-9 < /dev/urandom | head -c 6) "
94
-
95
- # Check the node
96
- $kubectl get node " $node " > /dev/null || exit 1
97
-
98
- container_cpu=" ${KUBECTL_NODE_SHELL_POD_CPU:- 100m} "
99
- container_memory=" ${KUBECTL_NODE_SHELL_POD_MEMORY:- 256Mi} "
100
- labels=" ${KUBECTL_NODE_SHELL_LABELS} "
101
-
102
121
overrides=" $(
103
- cat << EOT
122
+ cat << EOT
104
123
{
105
124
"spec": {
106
125
"nodeName": "$node ",
107
126
"hostPID": true,
108
127
"hostNetwork": true,
109
128
"containers": [
110
129
{
111
- "securityContext": {
112
- "privileged": true
113
- },
130
+ "securityContext": $security_context ,
114
131
"image": "$image ",
115
- "name": "nsenter ",
132
+ "name": "$name ",
116
133
"stdin": true,
117
134
"stdinOnce": true,
118
135
"tty": $tty ,
119
136
"command": $cmd ,
120
137
"resources": {
121
- "limits": {
122
- "cpu": "${container_cpu} ",
123
- "memory": "${container_memory} "
124
- },
125
- "requests": {
126
- "cpu": "${container_cpu} ",
127
- "memory": "${container_memory} "
128
- }
138
+ "limits": { "cpu": "${container_cpu} ", "memory": "${container_memory} " },
139
+ "requests": { "cpu": "${container_cpu} ", "memory": "${container_memory} " }
129
140
}
130
141
}
131
142
],
132
143
"tolerations": [
133
- {
134
- "key": "CriticalAddonsOnly",
135
- "operator": "Exists"
136
- },
137
- {
138
- "effect": "NoExecute",
139
- "operator": "Exists"
140
- }
144
+ { "key": "CriticalAddonsOnly", "operator": "Exists" },
145
+ { "effect": "NoExecute", "operator": "Exists" }
141
146
]
142
147
}
143
148
}
0 commit comments