-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge 'origin/main' into KU-864/contour
- Loading branch information
Showing
10 changed files
with
176 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package k8s | ||
|
||
import ( | ||
"context" | ||
"time" | ||
|
||
cmdutil "github.com/canonical/k8s/cmd/util" | ||
"github.com/canonical/k8s/pkg/k8sd/features" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func newXCleanupCmd(env cmdutil.ExecutionEnvironment) *cobra.Command { | ||
var opts struct { | ||
timeout time.Duration | ||
} | ||
|
||
cleanupNetworkCmd := &cobra.Command{ | ||
Use: "network", | ||
Short: "Cleanup left-over network resources", | ||
Run: func(cmd *cobra.Command, args []string) { | ||
ctx, cancel := context.WithTimeout(cmd.Context(), opts.timeout) | ||
defer cancel() | ||
|
||
if err := features.Cleanup.CleanupNetwork(ctx, env.Snap); err != nil { | ||
cmd.PrintErrf("Error: failed to cleanup network: %v\n", err) | ||
env.Exit(1) | ||
} | ||
}, | ||
} | ||
cleanupNetworkCmd.Flags().DurationVar(&opts.timeout, "timeout", 5*time.Minute, "the max time to wait for the command to execute") | ||
|
||
cmd := &cobra.Command{ | ||
Use: "x-cleanup", | ||
Short: "Cleanup left-over resources from the cluster's features", | ||
Hidden: true, | ||
} | ||
|
||
cmd.AddCommand(cleanupNetworkCmd) | ||
|
||
return cmd | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package calico | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net" | ||
"os" | ||
"os/exec" | ||
"path" | ||
"regexp" | ||
"strings" | ||
|
||
"github.com/canonical/k8s/pkg/snap" | ||
"golang.org/x/sys/unix" | ||
) | ||
|
||
func CleanupNetwork(ctx context.Context, snap snap.Snap) error { | ||
interfaces, err := net.Interfaces() | ||
if err != nil { | ||
return fmt.Errorf("failed to list network interfaces: %w", err) | ||
} | ||
|
||
// Find the interfaces created by Calico | ||
for _, iface := range interfaces { | ||
// Check if the interface name matches the regex pattern | ||
// Adapted from MicroK8s' link removal hook: | ||
// https://github.com/canonical/microk8s/blob/dff3627959d4774198000795a0a0afcaa003324b/microk8s-resources/default-hooks/remove.d/10-cni-link#L15 | ||
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) | ||
} | ||
if match { | ||
// Perform cleanup for Calico interface | ||
if err := exec.CommandContext(ctx, "ip", "link", "delete", iface.Name).Run(); err != nil { | ||
return fmt.Errorf("failed to delete interface %s: %w", iface.Name, err) | ||
} | ||
} | ||
} | ||
|
||
// Delete network namespaces that start with "cali-" | ||
netnsDir := "/run/netns" | ||
entries, err := os.ReadDir(netnsDir) | ||
if err != nil { | ||
return fmt.Errorf("failed to list files under %s: %w", netnsDir, err) | ||
} | ||
|
||
for _, entry := range entries { | ||
if strings.HasPrefix(entry.Name(), "cali-") { | ||
nsPath := path.Join(netnsDir, entry.Name()) | ||
|
||
if err := unix.Unmount(nsPath, unix.MNT_DETACH); err != nil { | ||
return fmt.Errorf("failed to unmount network namespace %s: %w", entry.Name(), err) | ||
} | ||
|
||
if err := os.Remove(nsPath); err != nil { | ||
return fmt.Errorf("failed to remove network namespace %s: %w", entry.Name(), err) | ||
} | ||
} | ||
} | ||
|
||
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(line, "cali") { | ||
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package cilium | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"os" | ||
"os/exec" | ||
|
||
"github.com/canonical/k8s/pkg/snap" | ||
) | ||
|
||
func CleanupNetwork(ctx context.Context, snap snap.Snap) error { | ||
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) | ||
} | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package features | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/canonical/k8s/pkg/snap" | ||
) | ||
|
||
type CleanupInterface interface { | ||
CleanupNetwork(context.Context, snap.Snap) error | ||
} | ||
|
||
type cleanup struct { | ||
cleanupNetwork func(context.Context, snap.Snap) error | ||
} | ||
|
||
func (c *cleanup) CleanupNetwork(ctx context.Context, snap snap.Snap) error { | ||
return c.cleanupNetwork(ctx, snap) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters