Skip to content

Commit

Permalink
Added legacy code back to handle upgrades
Browse files Browse the repository at this point in the history
Signed-off-by: Aswin Suryanarayanan <[email protected]>
  • Loading branch information
aswinsuryan committed Aug 10, 2023
1 parent 1c1d8d3 commit e1c0ee4
Show file tree
Hide file tree
Showing 5 changed files with 200 additions and 47 deletions.
2 changes: 2 additions & 0 deletions pkg/routeagent_driver/handlers/ovn/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ func (c *ConnectionHandler) createLibovsdbClient(dbAddress string, tlsConfig *tl
defer cancel()

err = client.Connect(ctx)

err = errors.Wrap(err, "error connecting to ovsdb")
if err != nil {
if dbModel.Name() == "OVN_Northbound" {
Expand All @@ -170,6 +171,7 @@ func (c *ConnectionHandler) createLibovsdbClient(dbAddress string, tlsConfig *tl
err = errors.Wrap(err, "error monitoring chassis table in OVN SBDB")
}
}

if err != nil {
client.Close()
return nil, err
Expand Down
66 changes: 38 additions & 28 deletions pkg/routeagent_driver/handlers/ovn/gateway_route_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,23 +88,13 @@ func (g *GatewayRouteController) gatewayRouteCreatedorUpdated(obj runtime.Object
defer g.mutex.Unlock()

subMGWRoute := obj.(*submarinerv1.GatewayRoute)
if subMGWRoute.RoutePolicySpec.NextHops != nil && subMGWRoute.RoutePolicySpec.NextHops[0] == g.mgmtIP {
for _, subnet := range subMGWRoute.RoutePolicySpec.RemoteCIDRs {
g.remoteSubnets.Insert(subnet)
}

err := g.connectionHandler.reconcileSubOvnLogicalRouterPolicies(g.remoteSubnets, g.mgmtIP)
if err != nil {
logger.Errorf(err, "error reconciling router policies for remote subnet %q", g.remoteSubnets)
return true
}

err = g.connectionHandler.reconcileOvnLogicalRouterStaticRoutes(g.remoteSubnets, g.mgmtIP)
if err != nil {
logger.Errorf(err, "error reconciling static routes for remote subnet %q", g.remoteSubnets)
return true
}
err := g.reconcileRemoteSubnets(subMGWRoute, true)
if err != nil {
logger.Errorf(err, "error creating or updating router policies and static routes for remote subnet %q", g.remoteSubnets)
return true
}

return false
}

Expand All @@ -113,25 +103,45 @@ func (g *GatewayRouteController) gatewayRouteDeleted(obj runtime.Object, _ int)
defer g.mutex.Unlock()

subMGWRoute := obj.(*submarinerv1.GatewayRoute)
if subMGWRoute.RoutePolicySpec.NextHops != nil && subMGWRoute.RoutePolicySpec.NextHops[0] == g.mgmtIP {
for _, subnet := range subMGWRoute.RoutePolicySpec.RemoteCIDRs {

err := g.reconcileRemoteSubnets(subMGWRoute, false)
if err != nil {
logger.Errorf(err, "error deleting router policies and static routes for remote subnet %q", g.remoteSubnets)
return true
}

return false
}

func (g *GatewayRouteController) reconcileRemoteSubnets(subMGWRoute *submarinerv1.GatewayRoute, addSubnet bool) error {
if len(subMGWRoute.RoutePolicySpec.NextHops) == 0 {
logger.Warningf("The GatewayRoute does not have next hop %v", subMGWRoute)
return nil
}

if subMGWRoute.RoutePolicySpec.NextHops[0] != g.mgmtIP {
return nil
}

for _, subnet := range subMGWRoute.RoutePolicySpec.RemoteCIDRs {
if addSubnet {
g.remoteSubnets.Insert(subnet)
} else {
g.remoteSubnets.Delete(subnet)
}
}

err := g.connectionHandler.reconcileSubOvnLogicalRouterPolicies(g.remoteSubnets, g.mgmtIP)
if err != nil {
logger.Errorf(err, "error reconciling router policies for remote subnet %q", g.remoteSubnets)
return true
}
err := g.connectionHandler.reconcileSubOvnLogicalRouterPolicies(g.remoteSubnets, g.mgmtIP)
if err != nil {
return err
}

err = g.connectionHandler.reconcileOvnLogicalRouterStaticRoutes(g.remoteSubnets, g.mgmtIP)
if err != nil {
logger.Errorf(err, "error reconciling static routes for remote subnet %q", g.remoteSubnets)
return true
}
err = g.connectionHandler.reconcileOvnLogicalRouterStaticRoutes(g.remoteSubnets, g.mgmtIP)
if err != nil {
return err
}

return false
return nil
}

func (g *GatewayRouteController) stop() {
Expand Down
50 changes: 32 additions & 18 deletions pkg/routeagent_driver/handlers/ovn/non_gateway_route_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,17 +103,12 @@ func (g *NonGatewayRouteController) nonGatewayRouteCreatedorUpdated(obj runtime.
g.mutex.Lock()
defer g.mutex.Unlock()

submGWRoute := obj.(*submarinerv1.NonGatewayRoute)
if submGWRoute.RoutePolicySpec.NextHops != nil && submGWRoute.RoutePolicySpec.NextHops[0] != g.transitSwitchIP {
for _, subnet := range submGWRoute.RoutePolicySpec.RemoteCIDRs {
g.remoteSubnets.Insert(subnet)
}
submNonGWRoute := obj.(*submarinerv1.NonGatewayRoute)

err := g.connectionHandler.reconcileSubOvnLogicalRouterPolicies(g.remoteSubnets, submGWRoute.RoutePolicySpec.NextHops[0])
if err != nil {
logger.Errorf(err, "error reconciling router policies for remote subnet %q", g.remoteSubnets)
return true
}
err := g.reconcileRemoteSubnets(submNonGWRoute, true)
if err != nil {
logger.Errorf(err, "error creating or updating router policies for remote subnet %q", g.remoteSubnets)
return true
}

return false
Expand All @@ -123,20 +118,39 @@ func (g *NonGatewayRouteController) nonGatewayRouteDeleted(obj runtime.Object, _
g.mutex.Lock()
defer g.mutex.Unlock()

submGWRoute := obj.(*submarinerv1.NonGatewayRoute)
if submGWRoute.RoutePolicySpec.NextHops != nil && submGWRoute.RoutePolicySpec.NextHops[0] != g.transitSwitchIP {
for _, subnet := range submGWRoute.RoutePolicySpec.RemoteCIDRs {
g.remoteSubnets.Delete(subnet)
submNonGWRoute := obj.(*submarinerv1.NonGatewayRoute)

err := g.reconcileRemoteSubnets(submNonGWRoute, false)
if err != nil {
logger.Errorf(err, "error deleting policies for remote subnet %q", g.remoteSubnets)
return true
}

return false
}

func (g *NonGatewayRouteController) reconcileRemoteSubnets(submNonGWRoute *submarinerv1.NonGatewayRoute, addSubnet bool) error {
if len(submNonGWRoute.RoutePolicySpec.NextHops) == 0 {
logger.Warningf("The NonGatewayRoute does not have next hop %v", submNonGWRoute)
return nil
}

if submNonGWRoute.RoutePolicySpec.NextHops[0] != g.transitSwitchIP {
for _, subnet := range submNonGWRoute.RoutePolicySpec.RemoteCIDRs {
if addSubnet {
g.remoteSubnets.Insert(subnet)
} else {
g.remoteSubnets.Delete(subnet)
}
}

err := g.connectionHandler.reconcileSubOvnLogicalRouterPolicies(g.remoteSubnets, submGWRoute.RoutePolicySpec.NextHops[0])
err := g.connectionHandler.reconcileSubOvnLogicalRouterPolicies(g.remoteSubnets, submNonGWRoute.RoutePolicySpec.NextHops[0])
if err != nil {
logger.Errorf(err, "error reconciling router policies for remote subnet %q", g.remoteSubnets)
return true
return err
}
}

return false
return nil
}

func (g *NonGatewayRouteController) stop() {
Expand Down
15 changes: 14 additions & 1 deletion pkg/routeagent_driver/handlers/ovn/uninstall.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ package ovn
import (
"github.com/pkg/errors"
"github.com/submariner-io/submariner/pkg/routeagent_driver/constants"
"github.com/submariner-io/submariner/pkg/routeagent_driver/handlers/ovn/vsctl"
"github.com/vishvananda/netlink"
)

func (ovn *Handler) Stop(uninstall bool) error {
ovn.gatewayRouteController.stop()
ovn.nonGatewayRouteController.stop()

close(ovn.stopCh)

if !uninstall {
Expand Down Expand Up @@ -97,3 +97,16 @@ func (ovn *Handler) flushAndDeleteIPTableChains(table, tableChain, submarinerCha
logger.Errorf(err, "Error deleting iptable chain %q of table %q", submarinerChain, table)
}
}

// TODO need to be removed when the clusters are fully upgraded to new implementation.
func (ovn *Handler) LegacyCleanup() {
err := vsctl.DelInternalPort(ovnK8sSubmarinerBridge, ovnK8sSubmarinerInterface)
if err != nil {
logger.Errorf(err, "Error deleting Submariner port %q", ovnK8sSubmarinerInterface)
}

err = vsctl.DelBridge(ovnK8sSubmarinerBridge)
if err != nil {
logger.Errorf(err, "Error deleting Submariner bridge %q", ovnK8sSubmarinerBridge)
}
}
114 changes: 114 additions & 0 deletions pkg/routeagent_driver/handlers/ovn/vsctl/vsctl.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
/*
SPDX-License-Identifier: Apache-2.0
Copyright Contributors to the Submariner project.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package vsctl

import (
"bytes"
"fmt"
"os/exec"
"strings"

"github.com/pkg/errors"
"github.com/submariner-io/admiral/pkg/log"
logf "sigs.k8s.io/controller-runtime/pkg/log"
)

const ovsCommandTimeout = 15

var logger = log.Logger{Logger: logf.Log.WithName("ovs-vsctl")}

func vsctlCmd(parameters ...string) (output string, err error) {
allParameters := []string{fmt.Sprintf("--timeout=%d", ovsCommandTimeout)}
allParameters = append(allParameters, parameters...)

cmd := exec.Command("/usr/bin/ovs-vsctl", allParameters...)

logger.V(log.TRACE).Infof("Running ovs-vsctl %v", allParameters)

var stderr bytes.Buffer
cmd.Stderr = &stderr

out, err := cmd.Output()

stdout := string(out)

if err != nil {
stdErrOut := stderr.String()
logger.Errorf(err, "Error running ovs-vsctl, output:\n%s", stdErrOut)

return stdErrOut, errors.Wrap(err, "error running ovs-vsctl")
}

return stdout, nil
}

func AddBridge(bridgeName string) error {
_, err := vsctlCmd("--may-exist", "add-br", bridgeName)

return err
}

func DelBridge(bridgeName string) error {
_, err := vsctlCmd("del-br", bridgeName)

return err
}

func AddInternalPort(bridgeName, portName, macAddress string, mtu int) error {
_, err := vsctlCmd("--may-exist", "add-port", bridgeName, portName, "--",
"set", "interface", portName, "type=internal", "mtu_request="+fmt.Sprintf("%d", mtu),
fmt.Sprintf("mac=%s", strings.ReplaceAll(macAddress, ":", "\\:")))

return err
}

func DelInternalPort(bridgeName, portName string) error {
_, err := vsctlCmd("--if-exists", "del-port", bridgeName, portName)

return err
}

func AddOVNBridgeMapping(netName, bridgeName string) error {
stdout, err := vsctlCmd("--if-exists", "get", "Open_vSwitch", ".",
"external_ids:ovn-bridge-mappings")
if err != nil {
return errors.Wrap(err, "failed to get ovn-bridge-mappings")
}

// cleanup the ovs-vsctl output, and remove quotes
bridgeMappings := strings.Trim(strings.TrimSpace(stdout), "\"")

locnetMapping := fmt.Sprintf("%s:%s", netName, bridgeName)
if strings.Contains(bridgeMappings, locnetMapping) {
return nil // it was already set
}

if len(bridgeMappings) > 0 {
bridgeMappings = bridgeMappings + "," + locnetMapping
} else {
bridgeMappings = locnetMapping
}

if _, err = vsctlCmd("set", "Open_vSwitch", ".",
fmt.Sprintf("external_ids:ovn-bridge-mappings=%s", bridgeMappings)); err != nil {
return errors.Wrap(err, "failed to set ovn-bridge-mappings")
}

return nil
}

0 comments on commit e1c0ee4

Please sign in to comment.