Skip to content

Commit 546d2c7

Browse files
committed
Add support for deployment processing
Signed-off-by: Kevin Grigorenko <kevin.grigorenko@us.ibm.com>
1 parent 10ce36e commit 546d2c7

File tree

1 file changed

+66
-14
lines changed

1 file changed

+66
-14
lines changed

containerdiag.bat

Lines changed: 66 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@ set "SCRIPTNAME=%~nx0"
88
goto :start
99

1010
:usage
11-
echo usage: !SCRIPTNAME! [options] [/d DEPLOYMENT] [/p POD] COMMANDS...
11+
echo usage: !SCRIPTNAME! [options] [-d DEPLOYMENT] [-p POD] COMMANDS...
1212
echo(
13-
echo( /d DEPLOYMENT: Run COMMANDS on all pods in the specified DEPLOYMENT
14-
echo( /i IMAGE: The image used for the debug pod (default quay.io/ibm/containerdiag)
15-
echo( /k: By default, this script uses oc if available. This options forces the use of kubectl
16-
echo( /n NAMESPACE: Namespace (optional; defaults to current namespace)
17-
echo( /p POD: Run COMMANDS on the specified POD
18-
echo( /q: Do not append the pod name to COMMANDS
19-
echo( /v: verbose output to stderr
13+
echo( -d DEPLOYMENT: Run COMMANDS on all pods in the specified DEPLOYMENT
14+
echo( -i IMAGE: The image used for the debug pod (default quay.io/ibm/containerdiag)
15+
echo( -k: By default, this script uses oc if available. This options forces the use of kubectl
16+
echo( -n NAMESPACE: Namespace (optional; defaults to current namespace)
17+
echo( -p POD: Run COMMANDS on the specified POD
18+
echo( -q: Do not append the pod name to COMMANDS
19+
echo( -v: verbose output to stderr
2020
echo(
2121
echo( COMMANDS will be passed to oc debug node along with the pod name at the end
2222
echo( (unless -q is specified in which case the pod name is not appended)
@@ -105,7 +105,7 @@ for /L %%J in (!REMAININGINDEX!,1,!I!) do (
105105

106106
if "!TARGETDEPLOYMENT!" == "" (
107107
if "!TARGETPOD!" == "" (
108-
echo ERROR: Either /d DEPLOYMENT or /p POD must be specified
108+
echo ERROR: Either -d DEPLOYMENT or -p POD must be specified
109109
goto :usage
110110
)
111111
)
@@ -117,23 +117,75 @@ call :printInfo Script started with !CTL! and !IMAGE!
117117
if "!VERBOSE!" == "1" call :printVerbose Commands: !REMAININGARGS!
118118

119119
if "!NAMESPACE!" == "" (
120-
for /F "tokens=* USEBACKQ" %%g in (`!CTL! config view --minify --output "jsonpath={..namespace}"`) do set "NAMESPACE=%%g"
120+
for /F "tokens=* USEBACKQ" %%x in (`!CTL! config view --minify --output "jsonpath={..namespace}"`) do set "NAMESPACE=%%x"
121121
if "!NAMESPACE!" == "" set "NAMESPACE=default"
122122
)
123123

124124
if not "!TARGETDEPLOYMENT!" == "" (
125125
call :printInfo Querying available replicas for deployment !TARGETDEPLOYMENT! in namespace !NAMESPACE!
126126

127-
echo ERROR: Only -p POD is implemented
128-
goto :usage
127+
for /F "tokens=* USEBACKQ" %%x in (`!CTL! get deployment !TARGETDEPLOYMENT! "--namespace=!NAMESPACE!" "--output=jsonpath={.status.availableReplicas}"`) do set "AVAILABLEREPLICAS=%%x"
128+
if "!AVAILABLEREPLICAS!" == "" (
129+
call :printInfo Error getting deployment information. Ensure you specify the right namespace with -n NAMESPACE
130+
exit /B 1
131+
)
132+
133+
if "!AVAILABLEREPLICAS!" == "0" (
134+
call :printInfo There are 0 available replicas for this deployment
135+
exit /B 1
136+
)
137+
138+
call :printInfo There are !AVAILABLEREPLICAS! available replicas
139+
140+
call :printInfo Querying deployment selector label
141+
142+
for /F "tokens=* USEBACKQ" %%x in (`!CTL! get --raw "/apis/apps/v1/namespaces/!NAMESPACE!/deployments/!TARGETDEPLOYMENT!/scale"`) do set "SELECTORRAW=%%x"
143+
if "!SELECTORRAW!" == "" (
144+
call :printInfo Error getting scale information. See previous output.
145+
exit /B 1
146+
)
147+
148+
set "SELECTOR=!SELECTORRAW:*selector":"=!"
149+
for /f delims^=^"^ tokens^=1 %%a in ("!SELECTOR!") do set "SELECTOR=%%a"
150+
if "!SELECTOR!" == "" (
151+
call :printInfo Error getting scale selector from !SELECTORRAW!
152+
exit /B 1
153+
)
154+
155+
call :printInfo Selector labels are !SELECTOR!
156+
157+
call :printInfo Getting pods by selector
158+
159+
for /F "tokens=* USEBACKQ" %%x in (`!CTL! get pods --namespace !NAMESPACE! --selector !SELECTOR! --output "jsonpath={range .items[*]}{.metadata.name}{' '}{.spec.nodeName}{'\n'}{end}"`) do (
160+
call :printInfo Found pod: %%x
161+
set "TARGETPOD="
162+
set "WORKER="
163+
for /F "tokens=1,2 delims= " %%y in ("%%x") do (
164+
set "TARGETPOD=%%y"
165+
set "WORKER=%%z"
166+
)
167+
168+
if "!TARGETPOD!" == "" (
169+
call :printInfo Could not find pod name from %%x
170+
exit /B 1
171+
)
172+
173+
if "!WORKER!" == "" (
174+
call :printInfo Could not find worker from %%x
175+
exit /B 1
176+
)
177+
178+
call :processPod !REMAININGARGS!
179+
)
180+
129181
) else (
130182
if not "!TARGETPOD!" == "" (
131183
call :printInfo Querying worker node for pod !TARGETPOD! in namespace !NAMESPACE!
132184

133-
for /F "tokens=* USEBACKQ" %%g in (`!CTL! get pod !TARGETPOD! --namespace !NAMESPACE! --output "jsonpath={.spec.nodeName}"`) do set "WORKER=%%g"
185+
for /F "tokens=* USEBACKQ" %%x in (`!CTL! get pod !TARGETPOD! --namespace !NAMESPACE! --output "jsonpath={.spec.nodeName}"`) do set "WORKER=%%x"
134186

135187
if "!WORKER!" == "" (
136-
call :printInfo Error getting pod information. See previous output. Ensure you specify the right namespace with /n NAMESPACE
188+
call :printInfo Error getting pod information. See previous output. Ensure you specify the right namespace with -n NAMESPACE
137189
exit /B 1
138190
)
139191

0 commit comments

Comments
 (0)