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

Cleanup left-over iptables rules from kubeproxy and cilium #788

Merged
merged 2 commits into from
Nov 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
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
berkayoz marked this conversation as resolved.
Show resolved Hide resolved
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
}
Loading