From 511e15758f9ca224196f43da6100bca3bf1f1c69 Mon Sep 17 00:00:00 2001 From: Quang Nguyen Date: Fri, 4 Oct 2024 16:08:32 -0400 Subject: [PATCH] feat: make commands to test plugins locally (#767) # 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. --- test/plugin/Makefile | 44 ++++++++++++++++++++++ test/plugin/packetparser/deployment.yaml | 28 -------------- test/plugin/packetparser/main_linux.go | 48 ++++++------------------ 3 files changed, 55 insertions(+), 65 deletions(-) create mode 100644 test/plugin/Makefile delete mode 100644 test/plugin/packetparser/deployment.yaml diff --git a/test/plugin/Makefile b/test/plugin/Makefile new file mode 100644 index 0000000000..92fbc621d0 --- /dev/null +++ b/test/plugin/Makefile @@ -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 {} \; diff --git a/test/plugin/packetparser/deployment.yaml b/test/plugin/packetparser/deployment.yaml deleted file mode 100644 index 1b194ad02a..0000000000 --- a/test/plugin/packetparser/deployment.yaml +++ /dev/null @@ -1,28 +0,0 @@ -apiVersion: apps/v1 -kind: Deployment -metadata: - name: nginx-deployment - namespace: default - labels: - app: nginx -spec: - replicas: 1 - selector: - matchLabels: - app: nginx - template: - metadata: - labels: - app: nginx - spec: - containers: - - name: nginx - # image: nginx:1.14.2 - image: mcr.microsoft.com/mirror/docker/library/nginx:1.23 - # image: mcr.microsoft.com/mirror/docker/library/ubuntu:20.04 - # command: ["/bin/bash", "-c", "while true; do echo hello; sleep 10;done"] - ports: - - containerPort: 80 - nodeSelector: - kubernetes.io/hostname: aks-nodepool1-22492810-vmss000000 - # kubernetes.io/hostname: aks-arm64-36093013-vmss000000 diff --git a/test/plugin/packetparser/main_linux.go b/test/plugin/packetparser/main_linux.go index 96d09787c7..2596dd1fe7 100644 --- a/test/plugin/packetparser/main_linux.go +++ b/test/plugin/packetparser/main_linux.go @@ -14,7 +14,6 @@ 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" @@ -22,21 +21,6 @@ import ( "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" @@ -45,11 +29,6 @@ func main() { metrics.InitializeMetrics() - // test(l) - // return - - metrics.InitializeMetrics() - ctxTimeout, cancel := context.WithTimeout(context.Background(), 30*time.Second) // watcher manager @@ -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 { @@ -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 @@ -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) }