diff --git a/pkg/agent/controller/packetsampling/packetin_test.go b/pkg/agent/controller/packetsampling/packetin_test.go index 91086999754..d86df2fba7c 100644 --- a/pkg/agent/controller/packetsampling/packetin_test.go +++ b/pkg/agent/controller/packetsampling/packetin_test.go @@ -27,6 +27,7 @@ import ( "github.com/google/gopacket/pcapgo" "github.com/spf13/afero" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" "golang.org/x/crypto/ssh" "golang.org/x/time/rate" v1 "k8s.io/api/core/v1" diff --git a/pkg/agent/controller/packetsampling/packetsampling_controller.go b/pkg/agent/controller/packetsampling/packetsampling_controller.go index b0cf5f1d89a..dbe29a5c3d7 100644 --- a/pkg/agent/controller/packetsampling/packetsampling_controller.go +++ b/pkg/agent/controller/packetsampling/packetsampling_controller.go @@ -348,18 +348,18 @@ func (c *Controller) startPacketSampling(ps *crdv1alpha1.PacketSampling, psState c.runningPacketSamplingsMutex.Lock() psState.maxNumCapturedPackets = ps.Spec.FirstNSamplingConfig.Number - var file afero.File + var file afero.File filePath := uidToPath(string(ps.UID)) if _, err := os.Stat(filePath); err == nil { - return fmt.Errorf("packet file already exists. this may be due to an unexpected termination") - } else if os.IsNotExist(err) { - file, err = defaultFS.Create(filePath) - if err != nil { - return fmt.Errorf("failed to create pcapng file: %w", err) - } - } else { - return fmt.Errorf("couldn't check if the file exists: %w", err) - } + return fmt.Errorf("packet file already exists. this may be due to an unexpected termination") + } else if os.IsNotExist(err) { + file, err = defaultFS.Create(filePath) + if err != nil { + return fmt.Errorf("failed to create pcapng file: %w", err) + } + } else { + return fmt.Errorf("couldn't check if the file exists: %w", err) + } writer, err := pcapgo.NewNgWriter(file, layers.LinkTypeEthernet) if err != nil { return fmt.Errorf("couldn't init pcap writer: %w", err) @@ -383,19 +383,6 @@ func (c *Controller) startPacketSampling(ps *crdv1alpha1.PacketSampling, psState return err } -func fileExists(path string) (bool, error) { - _, err := defaultFS.Stat(path) - if err == nil { - return true, nil - } else { - if os.IsNotExist(err) { - return false, nil - } else { - return false, err - } - } -} - // genEndpointMatchPackets generates match packets (with destination Endpoint's IP/port info) besides the normal match packet. // these match packets will help the pipeline to capture the pod -> svc traffic. // TODO: 1. support name based port name 2. dual-stack support