Setting the file. One moment.
Run Ig · Azure Diagnostics · microsoft/azure-skills · Skills Docs
ContentsBack to the top of the page ·
scripts/run-ig.ps1
scripts/ run-ig.ps1
PowerShell · 184 lines · 6 KB
15
16 . PARAMETER Gadget
17 Gadget to run, e.g. trace_dns, snapshot_socket, tcpdump (required).
18
19 . PARAMETER Pod
20 Pod name; the node is resolved automatically.
21
22 . PARAMETER Namespace
23 Namespace of the pod (required with -Pod).
24
25 . PARAMETER Node
26 Run directly against a node (node-wide scope).
27
28 . PARAMETER Container
29 Scope to a specific container.
30
31 . PARAMETER Timeout
32 Override the gadget-type default timeout (seconds).
33
34 . PARAMETER Filter
35 Extra IG flags, passed through verbatim (e.g. -Filter --max-entries,20).
36
37 . PARAMETER Pf
38 tcpdump packet filter (tcpdump gadget only, e.g. "port 80").
39
40 . PARAMETER IgVersion
41 Override the pinned IG image tag.
42
43 . PARAMETER DryRun
44 Print the assembled command; do not execute.
45
46 . EXAMPLE
47 ./run-ig.ps1 -Gadget trace_dns -Pod web-0 -Namespace default
48
49 . EXAMPLE
50 ./run-ig.ps1 -Gadget snapshot_process -Node aks-nodepool1-1234
51
52 . EXAMPLE
53 ./run-ig.ps1 -Gadget tcpdump -Pod web-0 -Namespace default -Pf "port 80"
54
55 . EXAMPLE
56 ./run-ig.ps1 -Gadget traceloop -Pod web-0 -Namespace default -Filter --syscall-filters,open,connect
57
58 . EXAMPLE
59 ./run-ig.ps1 -Gadget trace_dns -Pod web-0 -Namespace default -DryRun
60 #>
61 [ CmdletBinding ()]
62 param (
63 [ string ]$Gadget ,
64 [ string ]$Pod ,
65 [ Alias ( 'Ns' )]
66 [ string ]$Namespace ,
67 [ string ]$Node ,
68 [ string ]$Container ,
69 [ int ]$Timeout ,
70 [ string []]$Filter ,
71 [ string ]$Pf ,
72 # Pinned IG image tag. Bump this default (and run-ig.sh) to update the IG version.
73 [ string ]$IgVersion = 'v0.51.0' ,
74 [ switch ]$DryRun
75 )
76
77 $IgImageRepo = 'mcr.microsoft.com/oss/v2/inspektor-gadget/ig'
78
79 if ( -not $Gadget) {
80 Write-Error 'Provide -Gadget <name> (e.g. trace_dns, snapshot_socket, tcpdump).'
81 exit 2
82 }
83 if ( -not $Node -and -not $Pod) {
84 Write-Error 'Provide either -Node <node> or -Pod <pod> -Namespace <namespace>.'
85 exit 2
86 }
87 if ($Pod -and -not $Namespace) {
88 Write-Error '-Pod requires -Namespace <namespace>.'
89 exit 2
90 }
91 if ($Pf -and $Gadget -ne 'tcpdump' ) {
92 Write-Error '-Pf is only valid for the tcpdump gadget.'
93 exit 2
94 }
95
96 # Default timeout by gadget type, inferred from the gadget name prefix.
97 # snapshot_* / top_* -> 5s (point-in-time / quick aggregate)
98 # trace_* / profile_* / tcpdump -> 30s (streaming / sampling)
99 function Get-DefaultTimeout ([ string ]$g) {
100 switch - Wildcard ($g) {
101 'snapshot_*' { return 5 }
102 'top_*' { return 5 }
103 'trace_*' { return 30 }
104 'profile_*' { return 30 }
105 'tcpdump' { return 30 }
106 default { return 30 } # unknown gadget: use the safer streaming default
107 }
108 }
109
110 if ( -not $PSBoundParameters .ContainsKey( 'Timeout' ) -or $Timeout -le 0 ) {
111 $Timeout = Get-DefaultTimeout $Gadget
112 }
113
114 # Resolve the node name from the pod when not given directly.
115 if ( -not $Node) {
116 $Node = (( & kubectl get pod $Pod - n $Namespace - o "jsonpath={.spec.nodeName}" 2> $null ) | Out-String ).Trim()
117 if ( -not $Node) {
118 Write-Error "Could not resolve node for pod ' $Pod ' in namespace ' $Namespace '."
119 exit 1
120 }
121 }
122
123 $IgImage = " ${IgImageRepo} : ${IgVersion} "
124
125 # Assemble the k8s scoping filters.
126 $filters = @ ()
127 if ($Namespace) { $filters += @ ( '--k8s-namespace' , $Namespace) }
128 if ($Pod) { $filters += @ ( '--k8s-podname' , $Pod) }
129 if ($Container) { $filters += @ ( '--k8s-containername' , $Container) }
130
131 # Base kubectl debug invocation.
132 $debug = @ ( 'debug' , '--profile=sysadmin' , "node/ $Node " , '--attach' , '--quiet' , "--image= $IgImage " , '--' )
133
134 if ($Gadget -eq 'tcpdump' ) {
135 # tcpdump emits raw pcap-ng; pipe through tcpdump for readable output when available.
136 $igCmd = @ ( 'ig' , 'run' , "tcpdump: $IgVersion " , '-o' , 'pcap-ng' ) + $filters + @ ( '--timeout' , " $Timeout " )
137 if ($Pf) { $igCmd += @ ( '--pf' , $Pf) }
138 if ($Filter) { $igCmd += $Filter }
139 }
140 else {
141 $igCmd = @ ( 'ig' , 'run' , " ${Gadget} : $IgVersion " , '-o' , 'json' ) + $filters + @ ( '--timeout' , " $Timeout " )
142 if ($Filter) { $igCmd += $Filter }
143 }
144
145 $fullArgs = $debug + $igCmd
146
147 # Pretty-print a shell-quoted version of the command for display.
148 function Format-Cmd ([ string []]$parts) {
149 ($parts | ForEach-Object {
150 if ( $_ -match '\s' ) { '"' + $_ + '"' } else { $_ }
151 }) -join ' '
152 }
153
154 $displayCmd = 'kubectl ' + ( Format-Cmd $fullArgs)
155
156 # The tcpdump gadget is only piped through `tcpdump` when that binary is present.
157 # Reflect the real behavior in the displayed command so -DryRun does not mislead.
158 $tcpdumpAvail = $Gadget -eq 'tcpdump' -and [ bool ]( Get-Command tcpdump - ErrorAction SilentlyContinue)
159 if ($tcpdumpAvail) {
160 $displayCmd = " $displayCmd | tcpdump -nvr -"
161 }
162
163 Write-Host "Gadget: $Gadget "
164 Write-Host "Node: $Node "
165 Write-Host "Timeout: ${Timeout} s"
166 Write-Host "Image: $IgImage "
167 Write-Host "Command: $displayCmd "
168 if ($Gadget -eq 'tcpdump' -and -not $tcpdumpAvail) {
169 Write-Host 'Note: tcpdump not found; emitting raw pcap-ng to stdout.'
170 }
171
172 if ($DryRun) {
173 Write-Host '(dry-run: command not executed)'
174 exit 0
175 }
176
177 Write-Host "Ran gadget $Gadget on node $Node (timeout ${Timeout} s)"
178
179 if ($tcpdumpAvail) {
180 & kubectl @fullArgs | & tcpdump - nvr -
181 }
182 else {
183 & kubectl @fullArgs
184 }