-
Notifications
You must be signed in to change notification settings - Fork 388
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support antctl command for packetcapture #6884
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -33,6 +33,7 @@ running in three different modes: | |||||
- [Dumping OVS flows](#dumping-ovs-flows) | ||||||
- [OVS packet tracing](#ovs-packet-tracing) | ||||||
- [Traceflow](#traceflow) | ||||||
- [PacketCapture](#packetcapture) | ||||||
- [Antctl Proxy](#antctl-proxy) | ||||||
- [Flow Aggregator commands](#flow-aggregator-commands) | ||||||
- [Dumping flow records](#dumping-flow-records) | ||||||
|
@@ -571,6 +572,47 @@ $ antctl traceflow -S pod1 -D svc1 -f tcp --live-traffic -t 1m | |||||
$ antctl traceflow -D pod1 -f tcp,tcp_dst=80 --live-traffic --dropped-only -t 10m | ||||||
``` | ||||||
|
||||||
### PacketCapture | ||||||
|
||||||
`antctl packetcapture` (or `antctl pc`) command is used to start a `PacketCapture` | ||||||
and retrieve the captured result. After the result packet file is copied out, | ||||||
the PacketCapture will be deleted. Users can also create a PacketCapture with `kubectl`, | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. maybe add this sentence here:
|
||||||
but `antctl` provide a simpler way. For more information about PacketCapture, | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. s/provide a simpler way/makes it easier |
||||||
refer to [PacketCapture guide](packetcapture-guide.md). | ||||||
|
||||||
To start a PacketCapture, users must provide the following arguments: | ||||||
|
||||||
* `--source` (or `-S`) | ||||||
* `--destination` (or `-D`) | ||||||
* `--number` (or `-n`) | ||||||
|
||||||
Note: one of `--source` and `--destination` must be a pod. | ||||||
|
||||||
The `--flow` (or `-f`) argument can be used to specify the PacketCapture packet | ||||||
headers with the [ovs-ofctl](http://www.openvswitch.org//support/dist-docs/ovs-ofctl.8.txt) | ||||||
flow syntax(This argument works similar as Traceflow). The supported flow fields | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
include: IP protocol (`icmp`, `tcp`, `udp`), source and destination ports | ||||||
(`tcp_src`, `tcp_dst`, `udp_src`, `udp_dst`). | ||||||
|
||||||
By default, the command will wait for the PacketCapture to succeed or fail, or | ||||||
timeout. The default timeout is 10 seconds, but can be changed with the | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. or to timeout There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. wrong default timeout value, it is 60s |
||||||
`--timeout` (or `-t`) argument. Add the `--no-wait` flag to start a PacketCapture | ||||||
without waiting for its results. In this case, the command will not delete the | ||||||
PacketCapture resource. | ||||||
|
||||||
More examples of `antctl packetcapture`: | ||||||
|
||||||
```bash | ||||||
Start capturing packets from pod1 to pod2, both Pods are in Namespace default | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if you use the |
||||||
$ antctl packetcaputre -S pod1 -D pod2 | ||||||
Start capturing packets from pod1 in Namespace ns1 to a destination IP | ||||||
$ antctl packetcapture -S ns1/pod1 -D 192.168.123.123 | ||||||
Start capturing UDP packets from pod1 to pod2, with destination port 1234 | ||||||
$ antctl packetcapture -S pod1 -D pod2 -f udp,udp_dst=1234 | ||||||
Save the packets file to a specified directory | ||||||
$ antctl packetcapture -S 192.168.123.123 -D pod2 -f tcp,tcp_dst=80 -o /tmp | ||||||
``` | ||||||
|
||||||
### Antctl Proxy | ||||||
|
||||||
antctl can run as a reverse proxy for the Antrea API (Controller or arbitrary | ||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,8 +18,11 @@ import ( | |
"context" | ||
"fmt" | ||
"net" | ||
"path/filepath" | ||
"strconv" | ||
"strings" | ||
|
||
"github.com/spf13/afero" | ||
"github.com/spf13/cobra" | ||
"golang.org/x/mod/semver" | ||
corev1 "k8s.io/api/core/v1" | ||
|
@@ -28,11 +31,13 @@ import ( | |
"k8s.io/client-go/kubernetes/scheme" | ||
"k8s.io/client-go/rest" | ||
|
||
"antrea.io/antrea/pkg/antctl/raw/check" | ||
"antrea.io/antrea/pkg/antctl/runtime" | ||
"antrea.io/antrea/pkg/apis" | ||
"antrea.io/antrea/pkg/apis/crd/v1beta1" | ||
antrea "antrea.io/antrea/pkg/client/clientset/versioned" | ||
antreascheme "antrea.io/antrea/pkg/client/clientset/versioned/scheme" | ||
"antrea.io/antrea/pkg/util/compress" | ||
"antrea.io/antrea/pkg/util/ip" | ||
"antrea.io/antrea/pkg/util/k8s" | ||
) | ||
|
@@ -220,3 +225,26 @@ func CreateControllerClientCfg( | |
cfg.Host = fmt.Sprintf("https://%s", net.JoinHostPort(nodeIP, fmt.Sprint(controllerInfo.APIPort))) | ||
return cfg, nil | ||
} | ||
|
||
type PodFileCopy interface { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: s/PodFileCopy/PodFileCopier |
||
CopyFromPod(ctx context.Context, fs afero.Fs, namespace, name, containerName, srcPath, dstDir string) error | ||
} | ||
|
||
type PodFile struct { | ||
RestConfig *rest.Config | ||
Client kubernetes.Interface | ||
} | ||
Comment on lines
+233
to
+236
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. make the struct |
||
|
||
func (p *PodFile) CopyFromPod(ctx context.Context, fs afero.Fs, namespace, name, containerName, srcPath, dstDir string) error { | ||
dir, fileName := filepath.Split(srcPath) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. technically you should be using |
||
cmd := fmt.Sprintf("cd %s; tar cf - %s", dir, fileName) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. instead of For example: cmd := ["tar"]
if dir != "" {
cmd = append(cmd, "-C", dir)
}
cmd = append(cmd, "cf", "-", fileName) This way, you should be able to skip the shell wrapping and exec the |
||
if dir == "" { | ||
cmd = fmt.Sprintf("tar cf - %s", fileName) | ||
} | ||
cmdArr := []string{"/bin/sh", "-c", cmd} | ||
output, _, err := check.ExecInPod(ctx, p.Client, p.RestConfig, namespace, name, containerName, cmdArr) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please move the |
||
if err != nil { | ||
return err | ||
} | ||
return compress.UnpackReader(fs, strings.NewReader(output), false, dstDir) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
After the result packet file (in pcapng format)