From 74aab3871d4ccbab4a4b891e96d4972d28e15456 Mon Sep 17 00:00:00 2001 From: Berkay Tekin Oz Date: Thu, 13 Jun 2024 13:32:14 +0000 Subject: [PATCH] Addressing more comments --- src/k8s/pkg/k8sd/features/calico/cleanup.go | 15 +++++++-------- src/k8s/pkg/k8sd/features/cilium/cleanup.go | 6 +----- 2 files changed, 8 insertions(+), 13 deletions(-) diff --git a/src/k8s/pkg/k8sd/features/calico/cleanup.go b/src/k8s/pkg/k8sd/features/calico/cleanup.go index fb55f5def..9a69a19f6 100644 --- a/src/k8s/pkg/k8sd/features/calico/cleanup.go +++ b/src/k8s/pkg/k8sd/features/calico/cleanup.go @@ -1,7 +1,6 @@ package calico import ( - "bytes" "context" "fmt" "net" @@ -24,7 +23,7 @@ func CleanupNetwork(ctx context.Context, snap snap.Snap) error { // Find the interfaces created by Calico for _, iface := range interfaces { // Check if the interface name matches the regex pattern - match, err := regexp.MatchString("^vxlan[-v6]*.calico|cali[a-f0-9]*$", iface.Name) + match, err := regexp.MatchString("^vxlan[-v6]*.calico|cali[a-f0-9]*|tunl[0-9]*$", iface.Name) if err != nil { return fmt.Errorf("failed to match regex pattern: %w", err) } @@ -63,15 +62,15 @@ func CleanupNetwork(ctx context.Context, snap snap.Snap) error { return fmt.Errorf("failed to read iptables rules: %w", err) } - grep := exec.Command("grep", "-iv", "cali") - grep.Stdin = bytes.NewReader(out) - grepOut, err := grep.Output() - if err != nil { - return fmt.Errorf("failed to filter calico iptables rules: %w", err) + lines := strings.Split(string(out), "\n") + for i, line := range lines { + if strings.Contains(line, "cali") { + lines[i] = "" + } } restore := exec.Command(fmt.Sprintf("%s-restore", cmd)) - restore.Stdin = bytes.NewReader(grepOut) + restore.Stdin = strings.NewReader(strings.Join(lines, "\n")) if err := restore.Run(); err != nil { return fmt.Errorf("failed to restore iptables rules: %w", err) } diff --git a/src/k8s/pkg/k8sd/features/cilium/cleanup.go b/src/k8s/pkg/k8sd/features/cilium/cleanup.go index 11a1bfaf0..bb97321e8 100644 --- a/src/k8s/pkg/k8sd/features/cilium/cleanup.go +++ b/src/k8s/pkg/k8sd/features/cilium/cleanup.go @@ -10,16 +10,12 @@ import ( ) func CleanupNetwork(ctx context.Context, snap snap.Snap) error { - if err := os.Remove("/var/run/cilium/cilium.pid"); err != nil { - return fmt.Errorf("failed to remove cilium pid file: %w", err) - } + os.Remove("/var/run/cilium/cilium.pid") if _, err := os.Stat("/opt/cni/bin/cilium-dbg"); err == nil { if err := exec.CommandContext(ctx, "/opt/cni/bin/cilium-dbg", "cleanup", "--all-state", "--force").Run(); err != nil { return fmt.Errorf("cilium-dbg cleanup failed: %w", err) } - } else { - return fmt.Errorf("cilium-dbg can't be accessed: %w", err) } return nil