in controllers/daemon/staticgatewayconfiguration_controller.go [376:417]
func (r *StaticGatewayConfigurationReconciler) ensureDeleteIP(ctx context.Context, gwns ns.NetNS, ip netlink.Addr) error {
log := log.FromContext(ctx)
if err := gwns.Do(func(nn ns.NetNS) error {
log.Info("Deleting IP from host0", "ip", ip.IP.String())
hostLink, err := r.Netlink.LinkByName(consts.HostLinkName)
if err != nil {
return fmt.Errorf("failed to get host link in gateway namespace: %w", err)
}
if err := r.Netlink.AddrDel(hostLink, &ip); err != nil {
return fmt.Errorf("failed to delete IP %s: %w", ip.IP.String(), err)
}
return nil
}); err != nil {
return err
}
routes, err := r.Netlink.RouteList(nil, nl.FAMILY_ALL)
if err != nil {
return fmt.Errorf("failed to list routes in host namespace: %w", err)
}
for _, route := range routes {
route := route
if route.Dst != nil && route.Dst.IP.Equal(ip.IP) {
log.Info("Deleting route in host namespace to vmSecondaryIP", "route", route)
if err := r.Netlink.RouteDel(&route); err != nil {
return fmt.Errorf("failed to delete route to %s: %w", ip.IP.String(), err)
}
}
}
log.Info("Deleting no-sNAT rule for vmSecondaryIP", "ip", ip.IP.String())
if err := r.removeIPTablesChains(
ctx,
utiliptables.TableNAT,
[]utiliptables.Chain{utiliptables.Chain(fmt.Sprintf("EGRESS-%s", strings.ReplaceAll(ip.IP.String(), ".", "-")))}, // target chain
[]utiliptables.Chain{utiliptables.Chain("EGRESS-GATEWAY-SNAT")}, // source chain
[]string{fmt.Sprintf("kube-egress-gateway no sNAT packet from ip %s", ip.IP.String())},
); err != nil {
return fmt.Errorf("failed to clean up no-sNAT rule for vmSecondaryIP %s: %w", ip.IP.String(), err)
}
return nil
}