Skip to content
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

feat: add support to upload tshark to s3 #409

Merged
merged 22 commits into from
Jun 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
8ee088b
feat: add support to upload tshark to s3
smuu May 30, 2024
7636aa8
feat: use tshark from ghcr
smuu May 30, 2024
d6f8d0e
feat: improvemtns from review
smuu Jun 5, 2024
da47cd9
Merge branch 'main' into smuu/collect-tshark-upload-s3
smuu Jun 5, 2024
fa4c769
feat: improvements from review
smuu Jun 5, 2024
bf2c3e8
feat: improvements from review
smuu Jun 5, 2024
9dcab5e
Merge branch 'main' into smuu/collect-tshark-upload-s3
mojtaba-esk Jun 6, 2024
657bd0c
add scope name to prefix
smuu Jun 7, 2024
952406d
Revert "add scope name to prefix"
smuu Jun 7, 2024
3df8569
feat: bump tshark-s3 container
smuu Jun 7, 2024
7a05b68
feat: dump tshark-s3 container
smuu Jun 7, 2024
84aee86
fix: tshark e2e test
mojtaba-esk Jun 7, 2024
63d8307
Merge branch 'mojtaba/collect-tshark-upload-s3' into smuu/collect-tsh…
mojtaba-esk Jun 7, 2024
155b50b
fix: integrate new tshark-uploader changes
mojtaba-esk Jun 10, 2024
05d4691
fix: add extra check to make sure tshark config is not nil
mojtaba-esk Jun 10, 2024
6b72272
fix: applied feedback
mojtaba-esk Jun 12, 2024
7b2640d
fix: expanded the pattern to include other s3 like minio as well
mojtaba-esk Jun 12, 2024
656f877
Merge branch 'main' into smuu/collect-tshark-upload-s3
MSevey Jun 12, 2024
9a2c5b2
Merge branch 'main' into smuu/collect-tshark-upload-s3
mojtaba-esk Jun 13, 2024
8f592da
fix: adopt the new knuu obj change
mojtaba-esk Jun 13, 2024
c904db7
Merge branch 'main' into smuu/collect-tshark-upload-s3
smuu Jun 14, 2024
ace7012
fix: linter complain
mojtaba-esk Jun 14, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
113 changes: 113 additions & 0 deletions e2e/tshark/tshark_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
package basic

import (
"context"
"io"
"net/http"
"path/filepath"
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/celestiaorg/knuu/pkg/instance"
"github.com/celestiaorg/knuu/pkg/knuu"
)

const (
s3BucketName = "tshark-test-bucket"
s3Location = "eu-east-1"
)
mojtaba-esk marked this conversation as resolved.
Show resolved Hide resolved

func TestTshark(t *testing.T) {
t.Parallel()
// Setup

ctx := context.Background()

kn, err := knuu.New(ctx, knuu.Options{})
require.NoError(t, err, "error creating knuu")

defer func() {
if err := kn.CleanUp(ctx); err != nil {
t.Logf("error cleaning up knuu: %v", err)
}
}()

scope := kn.Scope()
t.Logf("Test scope: %s", scope)

target, err := kn.NewInstance("busybox")
require.NoError(t, err, "error creating instance")

err = target.SetImage(ctx, "busybox")
require.NoError(t, err, "error setting image")

err = target.SetCommand("sleep", "infinity")
require.NoError(t, err, "error setting command")

t.Log("deploying minio as s3 backend")
err = kn.MinioClient.DeployMinio(ctx)
require.NoError(t, err, "error deploying minio")

t.Log("getting minio configs")
minioConf, err := kn.MinioClient.GetConfigs(ctx)
require.NoError(t, err, "error getting S3 (minio) configs")

var (
filename = target.K8sName() + instance.TsharkCaptureFileExtension
keyPrefix = "tshark/" + scope
fileKey = filepath.Join(keyPrefix, filename)
)

err = target.EnableTsharkCollector(
instance.TsharkCollectorConfig{
VolumeSize: "10Gi",
S3AccessKey: minioConf.AccessKeyID,
S3SecretKey: minioConf.SecretAccessKey,
S3Region: s3Location,
S3Bucket: s3BucketName,
CreateBucket: true, // Since we fire up a fresh minio server, we need to create the bucket
S3KeyPrefix: keyPrefix,
S3Endpoint: minioConf.Endpoint,
UploadInterval: 1 * time.Second, // for sake of the test we keep this short
},
)
smuu marked this conversation as resolved.
Show resolved Hide resolved
require.NoError(t, err, "error enabling tshark collector")

err = target.Commit()
require.NoError(t, err, "error committing instance")

t.Cleanup(func() {
if err := kn.CleanUp(ctx); err != nil {
t.Logf("error cleaning up knuu: %v", err)
}
})

// Test logic

t.Log("starting target instance")
err = target.Start(ctx)
require.NoError(t, err, "error starting instance")

err = target.WaitInstanceIsRunning(ctx)
require.NoError(t, err, "error waiting for instance to be running")

// Perform a ping to do generate network traffic to allow tshark to capture it
_, err = target.ExecuteCommand(ctx, "ping", "-c", "4", "google.com")
require.NoError(t, err, "error executing command")
smuu marked this conversation as resolved.
Show resolved Hide resolved

url, err := kn.MinioClient.GetMinioURL(ctx, fileKey, s3BucketName)
require.NoError(t, err, "error getting minio url")

resp, err := http.Get(url)
require.NoError(t, err, "error downloading from minio URL")
defer resp.Body.Close()

assert.Equal(t, http.StatusOK, resp.StatusCode, "URL does not exist or is not accessible")

bodyBytes, err := io.ReadAll(resp.Body)
require.NoError(t, err, "error reading response body")
assert.NotEmpty(t, bodyBytes, "downloaded log file is empty")
}
9 changes: 9 additions & 0 deletions pkg/instance/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,4 +198,13 @@ var (
ErrAddingToProxy = errors.New("AddingToProxy", "error adding '%s' to traefik proxy for service '%s'")
ErrGettingProxyURL = errors.New("GettingProxyURL", "error getting proxy URL for service '%s'")
ErrProxyNotInitialized = errors.New("ProxyNotInitialized", "proxy not initialized")
ErrTsharkCollectorAlreadyEnabled = errors.New("TsharkCollectorAlreadyEnabled", "tshark collector already enabled for instance '%s'")
ErrCreatingTsharkCollectorInstance = errors.New("CreatingTsharkCollectorInstance", "error creating tshark collector instance")
ErrAddingTsharkCollectorSidecar = errors.New("AddingTsharkCollectorSidecar", "error adding tshark collector sidecar for instance '%s'")
ErrTsharkCollectorConfigNotSet = errors.New("TsharkCollectorConfigNotSet", "tshark collector config not set for instance '%s'. volumeSize: %s, s3AccessKey: %s, s3SecretKey: %s, s3Region: %s, s3BucketName: %s, s3KeyPrefix: %s")
ErrTsharkCollectorInvalidVolumeSize = errors.New("TsharkCollectorInvalidVolumeSize", "invalid volume size format for tshark collector: %s")
ErrTsharkCollectorInvalidS3AccessKey = errors.New("TsharkCollectorInvalidS3AccessKey", "invalid S3 access key format for tshark collector: %s")
ErrTsharkCollectorInvalidS3SecretKey = errors.New("TsharkCollectorInvalidS3SecretKey", "invalid S3 secret key format for tshark collector: %s")
ErrTsharkCollectorS3RegionOrBucketEmpty = errors.New("TsharkCollectorS3RegionOrBucketEmpty", "S3 region or bucket cannot be empty for tshark collector: %s, %s")
ErrRegexpCompile = errors.New("RegexpCompile", "error compiling regexp for %s")
)
68 changes: 40 additions & 28 deletions pkg/instance/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -361,34 +361,35 @@ func (i *Instance) cloneWithSuffix(suffix string) *Instance {
clonedBitTwister.SetClient(nil) // reset client to avoid reusing the same client

return &Instance{
name: i.name + suffix,
k8sName: i.k8sName + suffix,
imageName: i.imageName,
state: i.state,
instanceType: i.instanceType,
kubernetesService: i.kubernetesService,
builderFactory: i.builderFactory,
kubernetesReplicaSet: i.kubernetesReplicaSet,
portsTCP: i.portsTCP,
portsUDP: i.portsUDP,
command: i.command,
args: i.args,
env: i.env,
volumes: i.volumes,
memoryRequest: i.memoryRequest,
memoryLimit: i.memoryLimit,
cpuRequest: i.cpuRequest,
policyRules: i.policyRules,
livenessProbe: i.livenessProbe,
readinessProbe: i.readinessProbe,
startupProbe: i.startupProbe,
isSidecar: false,
parentInstance: nil,
sidecars: clonedSidecars,
obsyConfig: i.obsyConfig,
securityContext: &clonedSecurityContext,
BitTwister: &clonedBitTwister,
SystemDependencies: i.SystemDependencies,
name: i.name + suffix,
k8sName: i.k8sName + suffix,
imageName: i.imageName,
state: i.state,
instanceType: i.instanceType,
kubernetesService: i.kubernetesService,
builderFactory: i.builderFactory,
kubernetesReplicaSet: i.kubernetesReplicaSet,
portsTCP: i.portsTCP,
portsUDP: i.portsUDP,
command: i.command,
args: i.args,
env: i.env,
volumes: i.volumes,
memoryRequest: i.memoryRequest,
memoryLimit: i.memoryLimit,
cpuRequest: i.cpuRequest,
policyRules: i.policyRules,
livenessProbe: i.livenessProbe,
readinessProbe: i.readinessProbe,
startupProbe: i.startupProbe,
isSidecar: false,
parentInstance: nil,
sidecars: clonedSidecars,
obsyConfig: i.obsyConfig,
tsharkCollectorConfig: i.tsharkCollectorConfig,
securityContext: &clonedSecurityContext,
BitTwister: &clonedBitTwister,
SystemDependencies: i.SystemDependencies,
}
}

Expand Down Expand Up @@ -594,6 +595,17 @@ func (i *Instance) addOtelCollectorSidecar(ctx context.Context) error {
return nil
}

func (i *Instance) addTsharkCollectorSidecar(ctx context.Context) error {
tsharkSidecar, err := i.createTsharkCollectorInstance(ctx)
if err != nil {
return ErrCreatingTsharkCollectorInstance.WithParams(i.k8sName).Wrap(err)
}
if err := i.AddSidecar(tsharkSidecar); err != nil {
return ErrAddingTsharkCollectorSidecar.WithParams(i.k8sName).Wrap(err)
}
return nil
}

func (i *Instance) createBitTwisterInstance(ctx context.Context) (*Instance, error) {
bt, err := New("bit-twister", i.SystemDependencies)
if err != nil {
Expand Down
Loading
Loading