Skip to content

Commit

Permalink
feat: make commands to test plugins locally (#767)
Browse files Browse the repository at this point in the history
# Description

Provides several make commands to make local testings for plugins that
use eBPF less tedious.

## Related Issue

If this pull request is related to any issue, please mention it here.
Additionally, make sure that the issue is assigned to you before
submitting this pull request.

## Checklist

- [x] I have read the [contributing
documentation](https://retina.sh/docs/contributing).
- [x] I signed and signed-off the commits (`git commit -S -s ...`). See
[this
documentation](https://docs.github.com/en/authentication/managing-commit-signature-verification/about-commit-signature-verification)
on signing commits.
- [x] I have correctly attributed the author(s) of the code.
- [x] I have tested the changes locally.
- [x] I have followed the project's style guidelines.
- [x] I have updated the documentation, if necessary.
- [x] I have added tests, if applicable.

## Screenshots (if applicable) or Testing Completed

Please add any relevant screenshots or GIFs to showcase the changes
made.

## Additional Notes

Add any additional notes or context about the pull request here.

---

Please refer to the [CONTRIBUTING.md](../CONTRIBUTING.md) file for more
information on how to contribute to this project.
  • Loading branch information
nddq authored Oct 4, 2024
1 parent db7b16e commit 511e157
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 65 deletions.
44 changes: 44 additions & 0 deletions test/plugin/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
REPO_ROOT = $(shell git rev-parse --show-toplevel)
PLUGIN_DIR = $(REPO_ROOT)/pkg/plugin
INIT_DIR = $(REPO_ROOT)/init/retina
CONFIG_DIR = /retina/config
PACKETPARSER_DIR = $(REPO_ROOT)/test/plugin/packetparser
PACKETFORWARD_DIR = $(REPO_ROOT)/test/plugin/packetforward
DROPREASON_DIR = $(REPO_ROOT)/test/plugin/dropreason

.PHONY : generate-map-bpf-obj
generate-map-bpf-obj:
go generate $(PLUGIN_DIR)/conntrack && go generate $(PLUGIN_DIR)/filter

.PHONY : build-init
build-init:
cd $(INIT_DIR) && go build .

.PHONY : create-config
create-config:
sudo mkdir -p $(CONFIG_DIR) && echo "enableTelemetry: false" | sudo tee $(CONFIG_DIR)/config.yaml > /dev/null

.PHONY : run-init
run-init: create-config generate-map-bpf-obj build-init
sudo $(INIT_DIR)/retina

.PHONY : test-packetparser
test-packetparser: run-init
trap 'rm -f $(PACKETPARSER_DIR)/packetparser' INT TERM EXIT; \
go build -o $(PACKETPARSER_DIR)/packetparser $(PACKETPARSER_DIR) && sudo $(PACKETPARSER_DIR)/packetparser

.PHONY : test-packetforward
test-packetforward: run-init
trap 'rm -f $(PACKETFORWARD_DIR)/packetforward' INT TERM EXIT; \
go build -o $(PACKETFORWARD_DIR)/packetforward $(PACKETFORWARD_DIR) && sudo $(PACKETFORWARD_DIR)/packetforward

.PHONY : test-dropreason
test-dropreason: run-init
trap 'rm -f $(DROPREASON_DIR)/dropreason' INT TERM EXIT; \
go build -o $(DROPREASON_DIR)/dropreason $(DROPREASON_DIR) && sudo $(DROPREASON_DIR)/dropreason

.PHONY : clean
clean:
sudo rm -rf $(CONFIG_DIR); \
rm -f $(INIT_DIR)/retina; \
cd $(REPO_ROOT) && sudo find . -type f -name "*.o" -exec truncate -s 0 {} \;
28 changes: 0 additions & 28 deletions test/plugin/packetparser/deployment.yaml

This file was deleted.

48 changes: 11 additions & 37 deletions test/plugin/packetparser/main_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,29 +14,13 @@ import (
"github.com/microsoft/retina/pkg/log"
"github.com/microsoft/retina/pkg/managers/filtermanager"
"github.com/microsoft/retina/pkg/managers/watchermanager"
"github.com/microsoft/retina/pkg/plugin/api"
"github.com/microsoft/retina/pkg/plugin/packetparser"
"github.com/microsoft/retina/pkg/watchers/endpoint"
"go.uber.org/zap"

"github.com/microsoft/retina/pkg/metrics"
)

var tt api.Plugin

// func test(l *log.ZapLogger) {
// m := &dto.Metric{}
// metrics.NodeApiServerTcpHandshakeLatencyMs.Write(m)

// h := m.Histogram.GetBucket()
// var last uint64
// last = 0
// for _, b := range h {
// l.Info("Hist", zap.Any("Bucket", b.UpperBound), zap.Any("Count", *b.CumulativeCount-last))
// last = *b.CumulativeCount
// }
// }

func main() {
opts := log.GetDefaultLogOpts()
opts.Level = "debug"
Expand All @@ -45,11 +29,6 @@ func main() {

metrics.InitializeMetrics()

// test(l)
// return

metrics.InitializeMetrics()

ctxTimeout, cancel := context.WithTimeout(context.Background(), 30*time.Second)

// watcher manager
Expand Down Expand Up @@ -78,9 +57,6 @@ func main() {
}
}()

// Add IPs to filtermanager.
// ipsToAdd := []string{"10.224.0.106", "10.224.0.101"}
// ipsToAdd := []string{"20.69.116.85", "10.224.0.6"}
ipsToAdd := []string{"20.69.116.85"}
ips := []net.IP{}
for _, ip := range ipsToAdd {
Expand All @@ -96,41 +72,37 @@ func main() {
return
}

// time.Sleep(30 * time.Second)
// f.DeleteIPs(ips, "packetparser-test")
// return

// Start packetparser plugin.
cfg := &kcfg.Config{
MetricsInterval: 1 * time.Second,
EnablePodLevel: true,
}
tt = packetparser.New(cfg)
if err = tt.Stop(); err != nil {
p := packetparser.New(cfg)
if err = p.Stop(); err != nil {
l.Error("Stop packetparser plugin failed", zap.Error(err))
return
}

defer cancel()
err = tt.Generate(ctxTimeout)
err = p.Generate(ctxTimeout)
if err != nil {
l.Error("Generate failed", zap.Error(err))
return
}

err = tt.Compile(ctxTimeout)
err = p.Compile(ctxTimeout)
if err != nil {
l.Error("Compile failed", zap.Error(err))
return
}

err = tt.Init()
err = p.Init()
if err != nil {
l.Error("Init failed", zap.Error(err))
return
}

err = tt.Start(ctxTimeout)
err = p.Start(ctxTimeout)
if err != nil {
l.Error("Start failed", zap.Error(err))
return
Expand All @@ -142,8 +114,10 @@ func main() {
time.Sleep(1 * time.Second)
}

tt.Stop()
err = p.Stop()
if err != nil {
l.Error("Stop failed", zap.Error(err))
return
}
l.Info("Stopping packetparser")

// test(l)
}

0 comments on commit 511e157

Please sign in to comment.