@@ -137,9 +137,20 @@ func getPodFile(cmd *cobra.Command) (PodFileCopy, error) {
137
137
}, nil
138
138
}
139
139
140
+ func getPCName (src , dest string ) string {
141
+ replace := func (s string ) string {
142
+ return strings .ReplaceAll (s , "/" , "-" )
143
+ }
144
+ prefix := fmt .Sprintf ("%s-%s" , replace (src ), replace (dest ))
145
+ if option .nowait {
146
+ return prefix
147
+ }
148
+ return fmt .Sprintf ("%s-%s" , prefix , rand .String (8 ))
149
+ }
150
+
140
151
func packetCaptureRunE (cmd * cobra.Command , args []string ) error {
141
152
option .timeout , _ = cmd .Flags ().GetDuration ("timeout" )
142
- if option .timeout > time .Hour {
153
+ if option .timeout > 300 * time .Second {
143
154
return errors .New ("timeout cannot be longer than 1 hour" )
144
155
}
145
156
if option .timeout == 0 {
@@ -157,7 +168,7 @@ func packetCaptureRunE(cmd *cobra.Command, args []string) error {
157
168
if err != nil {
158
169
return fmt .Errorf ("error when constructing a PacketCapture CR: %w" , err )
159
170
}
160
- createCtx , cancel := context .WithTimeout (context . Background (), 5 * time .Second )
171
+ createCtx , cancel := context .WithTimeout (cmd . Context (), 5 * time .Second )
161
172
defer cancel ()
162
173
163
174
if _ , err := antreaClient .CrdV1alpha1 ().PacketCaptures ().Create (createCtx , pc , metav1.CreateOptions {}); err != nil {
@@ -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