Skip to content

Commit

Permalink
Cleanup left-over iptables rules from kubeproxy and cilium (#788)
Browse files Browse the repository at this point in the history
  • Loading branch information
berkayoz authored Nov 15, 2024
1 parent e1dd58e commit a8e140b
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 0 deletions.
2 changes: 2 additions & 0 deletions k8s/lib.sh
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ k8s::common::is_strict() {
# Cleanup configuration left by the network feature
k8s::remove::network() {
k8s::common::setup_env

"${SNAP}/bin/kube-proxy" --cleanup || true

k8s::cmd::k8s x-cleanup network || true
}
Expand Down
1 change: 1 addition & 0 deletions snap/snapcraft.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ parts:
- ethtool
- hostname
- iproute2
- ipset
- kmod
- libatm1
- libnss-resolve
Expand Down
21 changes: 21 additions & 0 deletions src/k8s/pkg/k8sd/features/cilium/cleanup.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"os"
"os/exec"
"strings"

"github.com/canonical/k8s/pkg/snap"
)
Expand All @@ -18,5 +19,25 @@ func CleanupNetwork(ctx context.Context, snap snap.Snap) error {
}
}

for _, cmd := range []string{"iptables", "ip6tables", "iptables-legacy", "ip6tables-legacy"} {
out, err := exec.Command(fmt.Sprintf("%s-save", cmd)).Output()
if err != nil {
return fmt.Errorf("failed to read iptables rules: %w", err)
}

lines := strings.Split(string(out), "\n")
for i, line := range lines {
if strings.Contains(strings.ToLower(line), "cilium") {
lines[i] = ""
}
}

restore := exec.Command(fmt.Sprintf("%s-restore", cmd))
restore.Stdin = strings.NewReader(strings.Join(lines, "\n"))
if err := restore.Run(); err != nil {
return fmt.Errorf("failed to restore iptables rules: %w", err)
}
}

return nil
}

0 comments on commit a8e140b

Please sign in to comment.