From 3fb825c8f723c06f4ad20033f9c6dee1cb506146 Mon Sep 17 00:00:00 2001 From: hzma Date: Fri, 23 Dec 2022 16:37:31 +0800 Subject: [PATCH] reserve pod eip static route when update vpc (#2185) --- pkg/controller/vpc.go | 34 ++++++++++++++++++++++++++++++++++ pkg/ovs/ovn-nbctl-legacy.go | 30 ++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+) diff --git a/pkg/controller/vpc.go b/pkg/controller/vpc.go index 1fbc6d3d681..c451b3eb2c8 100644 --- a/pkg/controller/vpc.go +++ b/pkg/controller/vpc.go @@ -400,6 +400,40 @@ func (c *Controller) handleAddOrUpdateVpc(key string) error { NextHopIP: gatewayV6, }) } + + if c.config.EnableEipSnat { + cm, err := c.configMapsLister.ConfigMaps(c.config.ExternalGatewayConfigNS).Get(util.ExternalGatewayConfig) + if err == nil { + nextHop := cm.Data["external-gw-addr"] + if nextHop == "" { + klog.Errorf("no available gateway nic address") + return fmt.Errorf("no available gateway nic address") + } + if strings.Contains(nextHop, "/") { + nextHop = strings.Split(nextHop, "/")[0] + } + + nats, err := c.ovnLegacyClient.GetRouterNat(vpc.Name) + if err != nil { + klog.Errorf("failed to get nat for vpc %s, %v", vpc.Name, err) + return err + } + for _, nat := range nats { + logical_ip, err := c.ovnLegacyClient.GetNatIPInfo(nat) + if err != nil { + klog.Errorf("failed to get nat ip info for vpc %s, %v", vpc.Name, err) + return err + } + if logical_ip != "" { + targetRoutes = append(targetRoutes, &kubeovnv1.StaticRoute{ + Policy: kubeovnv1.PolicySrc, + CIDR: logical_ip, + NextHopIP: nextHop, + }) + } + } + } + } } routeNeedDel, routeNeedAdd, err := diffStaticRoute(existRoute, targetRoutes) diff --git a/pkg/ovs/ovn-nbctl-legacy.go b/pkg/ovs/ovn-nbctl-legacy.go index 2320607ef56..dd221b170bc 100644 --- a/pkg/ovs/ovn-nbctl-legacy.go +++ b/pkg/ovs/ovn-nbctl-legacy.go @@ -2989,3 +2989,33 @@ func (c LegacyClient) SetAclLog(pgName string, logEnable, isIngress bool) error return nil } + +func (c *LegacyClient) GetRouterNat(routerName string) ([]string, error) { + var nat []string + results, err := c.CustomFindEntity("logical-router", []string{"nat"}, fmt.Sprintf("name=%s", routerName)) + if err != nil { + klog.Errorf("customFindEntity failed, %v", err) + return nat, err + } + if len(results) == 0 { + return nat, nil + } + + return results[0]["nat"], nil +} + +func (c *LegacyClient) GetNatIPInfo(uuid string) (string, error) { + var logical_ip string + + output, err := c.ovnNbCommand("--data=bare", "--format=csv", "--no-heading", "--columns=logical_ip", "list", "nat", uuid) + if err != nil { + klog.Errorf("failed to list nat, %v", err) + return logical_ip, err + } + lines := strings.Split(output, "\n") + + if len(lines) > 0 { + logical_ip = strings.TrimSpace(lines[0]) + } + return logical_ip, nil +}