48
48
getCopier = getPodFile
49
49
defaultFS = afero .NewOsFs ()
50
50
)
51
+
51
52
var option = & struct {
52
53
source string
53
54
dest string
@@ -59,11 +60,11 @@ var option = &struct {
59
60
}{}
60
61
61
62
var packetCaptureExample = strings .TrimSpace (`
62
- Start capture packets from pod1 to pod2, both Pods are in Namespace default
63
+ Start capturing packets from pod1 to pod2, both Pods are in Namespace default
63
64
$ antctl packetcaputre -S pod1 -D pod2
64
- Start capture packets from pod1 in Namespace ns1 to a destination IP
65
+ Start capturing packets from pod1 in Namespace ns1 to a destination IP
65
66
$ antctl packetcapture -S ns1/pod1 -D 192.168.123.123
66
- Start capture UDP packets from pod1 to pod2, with destination port 1234
67
+ Start capturing UDP packets from pod1 to pod2, with destination port 1234
67
68
$ antctl packetcapture -S pod1 -D pod2 -f udp,udp_dst=1234
68
69
Save the packets file to a specified directory
69
70
$ antctl packetcapture -S 192.168.123.123 -D pod2 -f tcp,tcp_dst=80 -o /tmp
@@ -137,9 +138,20 @@ func getPodFile(cmd *cobra.Command) (PodFileCopy, error) {
137
138
}, nil
138
139
}
139
140
141
+ func getPCName (src , dest string ) string {
142
+ replace := func (s string ) string {
143
+ return strings .ReplaceAll (s , "/" , "-" )
144
+ }
145
+ prefix := fmt .Sprintf ("%s-%s" , replace (src ), replace (dest ))
146
+ if option .nowait {
147
+ return prefix
148
+ }
149
+ return fmt .Sprintf ("%s-%s" , prefix , rand .String (8 ))
150
+ }
151
+
140
152
func packetCaptureRunE (cmd * cobra.Command , args []string ) error {
141
153
option .timeout , _ = cmd .Flags ().GetDuration ("timeout" )
142
- if option .timeout > time .Hour {
154
+ if option .timeout > 300 * time .Second {
143
155
return errors .New ("timeout cannot be longer than 1 hour" )
144
156
}
145
157
if option .timeout == 0 {
@@ -157,7 +169,7 @@ func packetCaptureRunE(cmd *cobra.Command, args []string) error {
157
169
if err != nil {
158
170
return fmt .Errorf ("error when constructing a PacketCapture CR: %w" , err )
159
171
}
160
- createCtx , cancel := context .WithTimeout (context . Background (), 5 * time .Second )
172
+ createCtx , cancel := context .WithTimeout (cmd . Context (), 5 * time .Second )
161
173
defer cancel ()
162
174
163
175
if _ , err := antreaClient .CrdV1alpha1 ().PacketCaptures ().Create (createCtx , pc , metav1.CreateOptions {}); err != nil {
@@ -189,7 +201,6 @@ func packetCaptureRunE(cmd *cobra.Command, args []string) error {
189
201
}
190
202
}
191
203
return false , nil
192
-
193
204
})
194
205
195
206
if wait .Interrupted (err ) {
@@ -204,14 +215,16 @@ func packetCaptureRunE(cmd *cobra.Command, args []string) error {
204
215
splits := strings .Split (latestPC .Status .FilePath , ":" )
205
216
fileName := filepath .Base (splits [1 ])
206
217
copier , _ := getCopier (cmd )
207
- err = copier .CopyFromPod (context .TODO (), env .GetAntreaNamespace (), splits [0 ], "antrea-agent" , splits [1 ], option .outputDir )
208
- if err == nil {
209
- fmt .Fprintf (cmd .OutOrStdout (), "Captured packets file: %s\n " , filepath .Join (option .outputDir , fileName ))
218
+ if err := copier .CopyFromPod (context .TODO (), env .GetAntreaNamespace (), splits [0 ], "antrea-agent" , splits [1 ], option .outputDir ); err == nil {
219
+ return err
210
220
}
211
- return err
221
+ fmt .Fprintf (cmd .OutOrStdout (), "Captured packets file: %s\n " , filepath .Join (option .outputDir , fileName ))
222
+ return nil
212
223
}
213
224
214
- func parseEndpoint (endpoint string ) (pod * v1alpha1.PodReference , ip * string ) {
225
+ func parseEndpoint (endpoint string ) (* v1alpha1.PodReference , * string ) {
226
+ var pod * v1alpha1.PodReference
227
+ var ip * string
215
228
parsedIP := net .ParseIP (endpoint )
216
229
if parsedIP != nil && parsedIP .To4 () != nil {
217
230
ip = ptr .To (parsedIP .String ())
@@ -229,23 +242,12 @@ func parseEndpoint(endpoint string) (pod *v1alpha1.PodReference, ip *string) {
229
242
}
230
243
}
231
244
}
232
- return
233
- }
234
-
235
- func getPCName (src , dest string ) string {
236
- replace := func (s string ) string {
237
- return strings .ReplaceAll (s , "/" , "-" )
238
- }
239
- prefix := fmt .Sprintf ("%s-%s" , replace (src ), replace (dest ))
240
- if option .nowait {
241
- return prefix
242
- }
243
- return fmt .Sprintf ("%s-%s" , prefix , rand .String (8 ))
245
+ return pod , ip
244
246
}
245
247
246
248
func parseFlow () (* v1alpha1.Packet , error ) {
247
249
trimFlow := strings .ReplaceAll (option .flow , " " , "" )
248
- fields , err := getFlowFields (cleanFlow )
250
+ fields , err := getFlowFields (trimFlow )
249
251
if err != nil {
250
252
return nil , fmt .Errorf ("error when parsing the flow: %w" , err )
251
253
}
0 commit comments