From 25f89737827c66666b9317b5b4392b9071dcc251 Mon Sep 17 00:00:00 2001 From: lut777 Date: Mon, 9 Jan 2023 16:41:50 +0800 Subject: [PATCH] fix: now route with connected/static will all be sync (#2231) --- pkg/controller/ovn-ic.go | 80 +++++++++++++++++++++------------------- pkg/util/const.go | 5 ++- 2 files changed, 45 insertions(+), 40 deletions(-) diff --git a/pkg/controller/ovn-ic.go b/pkg/controller/ovn-ic.go index 59460300b12..030d2c83738 100644 --- a/pkg/controller/ovn-ic.go +++ b/pkg/controller/ovn-ic.go @@ -4,6 +4,7 @@ import ( "context" "encoding/json" "fmt" + "github.com/kubeovn/kube-ovn/pkg/ovsdb/ovnnb" "os" "os/exec" "reflect" @@ -16,7 +17,6 @@ import ( "k8s.io/apimachinery/pkg/types" "k8s.io/klog/v2" - "github.com/kubeovn/kube-ovn/pkg/ovsdb/ovnnb" "github.com/kubeovn/kube-ovn/pkg/util" ) @@ -396,20 +396,57 @@ func genHostAddress(host string, port string) (hostAddress string) { } func (c *Controller) SynRouteToPolicy() { + c.syncOneRouteToPolicy(util.OvnICKey, util.OvnICConnected) + c.syncOneRouteToPolicy(util.OvnICKey, util.OvnICStatic) +} + +func (c *Controller) RemoveOldChassisInSbDB() error { + azUUID, err := c.ovnLegacyClient.GetAzUUID(lastIcCm["az-name"]) + if err != nil { + klog.Errorf("failed to get UUID of AZ %s: %v", lastIcCm["az-name"], err) + } + gateways, err := c.ovnLegacyClient.GetGatewayUUIDsInOneAZ(azUUID) + if err != nil { + klog.Errorf("failed to get gateway UUIDs in AZ %s: %v", azUUID, err) + } + + routes, err := c.ovnLegacyClient.GetRouteUUIDsInOneAZ(azUUID) + if err != nil { + klog.Errorf("failed to get route UUIDs in AZ %s: %v", azUUID, err) + } + + c.ovnLegacyClient.DestroyGateways(gateways) + c.ovnLegacyClient.DestroyRoutes(routes) + if err := c.ovnLegacyClient.DestroyChassis(azUUID); err != nil { + return err + } + return nil +} + +func stripPrefix(policyMatch string) (string, error) { + matches := strings.Split(policyMatch, "==") + if strings.Trim(matches[0], " ") == util.MatchV4Dst { + return strings.Trim(matches[1], " "), nil + } else { + return "", fmt.Errorf("policy %s is mismatched", policyMatch) + } +} + +func (c *Controller) syncOneRouteToPolicy(key, value string) { lr, err := c.ovnClient.GetLogicalRouter(util.DefaultVpc, false) if err != nil { klog.Errorf("logical router does not exist %v at %v", err, time.Now()) return } - lrRouteList, err := c.ovnClient.GetLogicalRouterRouteByOpts(util.OvnICKey, util.OvnICValue) + lrRouteList, err := c.ovnClient.GetLogicalRouterRouteByOpts(key, value) if err != nil { klog.Errorf("failed to list lr ovn-ic route %v", err) return } if len(lrRouteList) == 0 { klog.V(5).Info(" lr ovn-ic route does not exist") - lrPolicyList, err := c.ovnClient.GetLogicalRouterPoliciesByExtID(util.OvnICKey, util.OvnICValue) + lrPolicyList, err := c.ovnClient.GetLogicalRouterPoliciesByExtID(key, value) if err != nil { klog.Errorf("failed to list ovn-ic lr policy ", err) return @@ -423,7 +460,7 @@ func (c *Controller) SynRouteToPolicy() { } policyMap := map[string]string{} - lrPolicyList, err := c.ovnClient.GetLogicalRouterPoliciesByExtID(util.OvnICKey, util.OvnICValue) + lrPolicyList, err := c.ovnClient.GetLogicalRouterPoliciesByExtID(key, value) if err != nil { klog.Errorf("failed to list ovn-ic lr policy ", err) return @@ -443,7 +480,7 @@ func (c *Controller) SynRouteToPolicy() { matchFiled := util.MatchV4Dst + " == " + lrRoute.IPPrefix if err := c.ovnClient.AddRouterPolicy(lr, matchFiled, ovnnb.LogicalRouterPolicyActionAllow, map[string]string{}, - map[string]string{util.OvnICKey: util.OvnICValue, "vendor": util.CniTypeName}, + map[string]string{key: value, "vendor": util.CniTypeName}, util.OvnICPolicyPriority); err != nil { klog.Errorf("adding router policy failed %v", err) } @@ -455,36 +492,3 @@ func (c *Controller) SynRouteToPolicy() { } } } - -func (c *Controller) RemoveOldChassisInSbDB() error { - azUUID, err := c.ovnLegacyClient.GetAzUUID(lastIcCm["az-name"]) - if err != nil { - klog.Errorf("failed to get UUID of AZ %s: %v", lastIcCm["az-name"], err) - } - - gateways, err := c.ovnLegacyClient.GetGatewayUUIDsInOneAZ(azUUID) - if err != nil { - klog.Errorf("failed to get gateway UUIDs in AZ %s: %v", azUUID, err) - } - - routes, err := c.ovnLegacyClient.GetRouteUUIDsInOneAZ(azUUID) - if err != nil { - klog.Errorf("failed to get route UUIDs in AZ %s: %v", azUUID, err) - } - - c.ovnLegacyClient.DestroyGateways(gateways) - c.ovnLegacyClient.DestroyRoutes(routes) - if err := c.ovnLegacyClient.DestroyChassis(azUUID); err != nil { - return err - } - return nil -} - -func stripPrefix(policyMatch string) (string, error) { - matches := strings.Split(policyMatch, "==") - if strings.Trim(matches[0], " ") == util.MatchV4Dst { - return strings.Trim(matches[1], " "), nil - } else { - return "", fmt.Errorf("policy %s is mismatched", policyMatch) - } -} diff --git a/pkg/util/const.go b/pkg/util/const.go index 6bab1051b8d..8a0bce567dd 100644 --- a/pkg/util/const.go +++ b/pkg/util/const.go @@ -197,8 +197,9 @@ const ( AttachmentProvider = "ovn.kubernetes.io/attachmentprovider" LbSvcPodImg = "ovn.kubernetes.io/lb_svc_img" - OvnICKey = "origin" - OvnICValue = "connected" + OvnICKey = "origin" + OvnICConnected = "connected" + OvnICStatic = "static" MatchV4Src = "ip4.src" MatchV4Dst = "ip4.dst"