Skip to content

Commit 3436bfe

Browse files
committed
update
Signed-off-by: Hang Yan <[email protected]>
1 parent 18cc669 commit 3436bfe

File tree

4 files changed

+33
-30
lines changed

4 files changed

+33
-30
lines changed

pkg/antctl/antctl.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -753,8 +753,8 @@ $ antctl get podmulticaststats pod -n namespace`,
753753
},
754754
{
755755
cobraCommand: packetcapture.Command,
756-
supportAgent: true,
757-
supportController: true,
756+
supportAgent: false,
757+
supportController: false,
758758
},
759759
{
760760
cobraCommand: proxy.Command,

pkg/antctl/command_list_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,12 @@ func TestGetDebugCommands(t *testing.T) {
6565
{
6666
name: "Antctl running against controller mode",
6767
mode: "controller",
68-
expected: [][]string{{"version"}, {"get", "networkpolicy"}, {"get", "appliedtogroup"}, {"get", "addressgroup"}, {"get", "controllerinfo"}, {"supportbundle"}, {"traceflow"}, {"packetcapture"}, {"get", "featuregates"}},
68+
expected: [][]string{{"version"}, {"get", "networkpolicy"}, {"get", "appliedtogroup"}, {"get", "addressgroup"}, {"get", "controllerinfo"}, {"supportbundle"}, {"traceflow"}, {"get", "featuregates"}},
6969
},
7070
{
7171
name: "Antctl running against agent mode",
7272
mode: "agent",
73-
expected: [][]string{{"version"}, {"get", "podmulticaststats"}, {"log-level"}, {"get", "networkpolicy"}, {"get", "appliedtogroup"}, {"get", "addressgroup"}, {"get", "agentinfo"}, {"get", "podinterface"}, {"get", "ovsflows"}, {"trace-packet"}, {"get", "serviceexternalip"}, {"get", "memberlist"}, {"get", "bgppolicy"}, {"get", "bgppeers"}, {"get", "bgproutes"}, {"supportbundle"}, {"traceflow"}, {"packetcapture"}, {"get", "featuregates"}},
73+
expected: [][]string{{"version"}, {"get", "podmulticaststats"}, {"log-level"}, {"get", "networkpolicy"}, {"get", "appliedtogroup"}, {"get", "addressgroup"}, {"get", "agentinfo"}, {"get", "podinterface"}, {"get", "ovsflows"}, {"trace-packet"}, {"get", "serviceexternalip"}, {"get", "memberlist"}, {"get", "bgppolicy"}, {"get", "bgppeers"}, {"get", "bgproutes"}, {"supportbundle"}, {"traceflow"}, {"get", "featuregates"}},
7474
},
7575
{
7676
name: "Antctl running against flow-aggregator mode",

pkg/antctl/raw/packetcapture/command.go

+26-24
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ var (
4848
getCopier = getPodFile
4949
defaultFS = afero.NewOsFs()
5050
)
51+
5152
var option = &struct {
5253
source string
5354
dest string
@@ -59,11 +60,11 @@ var option = &struct {
5960
}{}
6061

6162
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
6364
$ 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
6566
$ 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
6768
$ antctl packetcapture -S pod1 -D pod2 -f udp,udp_dst=1234
6869
Save the packets file to a specified directory
6970
$ 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) {
137138
}, nil
138139
}
139140

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+
140152
func packetCaptureRunE(cmd *cobra.Command, args []string) error {
141153
option.timeout, _ = cmd.Flags().GetDuration("timeout")
142-
if option.timeout > time.Hour {
154+
if option.timeout > 300*time.Second {
143155
return errors.New("timeout cannot be longer than 1 hour")
144156
}
145157
if option.timeout == 0 {
@@ -157,7 +169,7 @@ func packetCaptureRunE(cmd *cobra.Command, args []string) error {
157169
if err != nil {
158170
return fmt.Errorf("error when constructing a PacketCapture CR: %w", err)
159171
}
160-
createCtx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
172+
createCtx, cancel := context.WithTimeout(cmd.Context(), 5*time.Second)
161173
defer cancel()
162174

163175
if _, err := antreaClient.CrdV1alpha1().PacketCaptures().Create(createCtx, pc, metav1.CreateOptions{}); err != nil {
@@ -189,7 +201,6 @@ func packetCaptureRunE(cmd *cobra.Command, args []string) error {
189201
}
190202
}
191203
return false, nil
192-
193204
})
194205

195206
if wait.Interrupted(err) {
@@ -204,14 +215,16 @@ func packetCaptureRunE(cmd *cobra.Command, args []string) error {
204215
splits := strings.Split(latestPC.Status.FilePath, ":")
205216
fileName := filepath.Base(splits[1])
206217
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
210220
}
211-
return err
221+
fmt.Fprintf(cmd.OutOrStdout(), "Captured packets file: %s\n", filepath.Join(option.outputDir, fileName))
222+
return nil
212223
}
213224

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
215228
parsedIP := net.ParseIP(endpoint)
216229
if parsedIP != nil && parsedIP.To4() != nil {
217230
ip = ptr.To(parsedIP.String())
@@ -229,23 +242,12 @@ func parseEndpoint(endpoint string) (pod *v1alpha1.PodReference, ip *string) {
229242
}
230243
}
231244
}
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
244246
}
245247

246248
func parseFlow() (*v1alpha1.Packet, error) {
247249
trimFlow := strings.ReplaceAll(option.flow, " ", "")
248-
fields, err := getFlowFields(cleanFlow)
250+
fields, err := getFlowFields(trimFlow)
249251
if err != nil {
250252
return nil, fmt.Errorf("error when parsing the flow: %w", err)
251253
}

pkg/antctl/raw/packetcapture/command_test.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -110,14 +110,14 @@ func TestRun(t *testing.T) {
110110
src: srcPod,
111111
dst: dstPod,
112112
flow: "icmp",
113-
expectErr: "timeout waiting for PacketCapture done",
113+
expectErr: "timeout while waiting for PacketCapture to complete",
114114
},
115115
{
116116
name: "invalid-packetcapture",
117117
src: ipv4,
118118
dst: ipv4,
119119
flow: "icmp",
120-
expectErr: "error when filling up PacketCapture config: one of source and destination must be a Pod",
120+
expectErr: "error when constructing a PacketCapture CR: one of source and destination must be a Pod",
121121
},
122122
}
123123
for _, tt := range tcs {
@@ -159,6 +159,7 @@ func TestRun(t *testing.T) {
159159
Command.SetOutput(buf)
160160
Command.SetOut(buf)
161161
Command.SetErr(buf)
162+
Command.SetContext(context.Background())
162163

163164
err := packetCaptureRunE(Command, nil)
164165
if tt.expectErr == "" {

0 commit comments

Comments
 (0)