Skip to content

Commit

Permalink
feat: lb switch
Browse files Browse the repository at this point in the history
  • Loading branch information
fanriming committed Jun 22, 2021
1 parent 872340c commit 744e657
Show file tree
Hide file tree
Showing 9 changed files with 128 additions and 21 deletions.
2 changes: 2 additions & 0 deletions dist/images/install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ ENABLE_VLAN=${ENABLE_VLAN:-false}
ENABLE_MIRROR=${ENABLE_MIRROR:-false}
VLAN_NIC=${VLAN_NIC:-}
HW_OFFLOAD=${HW_OFFLOAD:-false}
ENABLE_LB=${ENABLE_LB:-true}
IFACE="" # The nic to support container network can be a nic name or a group of regex separated by comma, if empty will use the nic that the default route use

CNI_CONF_DIR="/etc/cni/net.d"
Expand Down Expand Up @@ -1582,6 +1583,7 @@ spec:
- --default-interface-name=$VLAN_INTERFACE_NAME
- --default-vlan-id=$VLAN_ID
- --pod-nic-type=$POD_NIC_TYPE
- --enable-lb=$ENABLE_LB
env:
- name: ENABLE_SSL
value: "$ENABLE_SSL"
Expand Down
4 changes: 4 additions & 0 deletions pkg/controller/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ type Configuration struct {
DefaultVlanName string
DefaultVlanRange string
DefaultVlanID int

EnableLb bool
}

// ParseFlags parses cmd args then init kubeclient and conf
Expand Down Expand Up @@ -92,6 +94,7 @@ func ParseFlags() (*Configuration, error) {
argsDefaultVlanID = pflag.Int("default-vlan-id", 1, "The default vlan id, default: 1")
argsDefaultVlanRange = pflag.String("default-vlan-range", "1,4095", "The default vlan range, default: 1-4095")
argsPodNicType = pflag.String("pod-nic-type", "veth-pair", "The default pod network nic implementation type, default: veth-pair")
argsEnableLb = pflag.Bool("enable-lb", true, "Enable load balancer, default: true")
)

klogFlags := flag.NewFlagSet("klog", flag.ExitOnError)
Expand Down Expand Up @@ -140,6 +143,7 @@ func ParseFlags() (*Configuration, error) {
PodName: os.Getenv("POD_NAME"),
PodNamespace: os.Getenv("KUBE_NAMESPACE"),
PodNicType: *argsPodNicType,
EnableLb: *argsEnableLb,
}

if util.IsNetworkVlan(config.NetworkType) && config.DefaultHostInterface == "" {
Expand Down
12 changes: 8 additions & 4 deletions pkg/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -456,8 +456,10 @@ func (c *Controller) startWorkers(stopCh <-chan struct{}) {
go wait.Until(c.runDelVpcWorker, time.Second, stopCh)
go wait.Until(c.runUpdateVpcStatusWorker, time.Second, stopCh)

// run in a single worker to avoid delete the last vip, which will lead ovn to delete the loadbalancer
go wait.Until(c.runDeleteServiceWorker, time.Second, stopCh)
if c.config.EnableLb {
// run in a single worker to avoid delete the last vip, which will lead ovn to delete the loadbalancer
go wait.Until(c.runDeleteServiceWorker, time.Second, stopCh)
}
for i := 0; i < c.config.WorkerNum; i++ {
go wait.Until(c.runAddPodWorker, time.Second, stopCh)
go wait.Until(c.runDeletePodWorker, time.Second, stopCh)
Expand All @@ -467,8 +469,10 @@ func (c *Controller) startWorkers(stopCh <-chan struct{}) {
go wait.Until(c.runDeleteRouteWorker, time.Second, stopCh)
go wait.Until(c.runUpdateSubnetStatusWorker, time.Second, stopCh)

go wait.Until(c.runUpdateServiceWorker, time.Second, stopCh)
go wait.Until(c.runUpdateEndpointWorker, time.Second, stopCh)
if c.config.EnableLb {
go wait.Until(c.runUpdateServiceWorker, time.Second, stopCh)
go wait.Until(c.runUpdateEndpointWorker, time.Second, stopCh)
}

go wait.Until(c.runUpdateNpWorker, time.Second, stopCh)
go wait.Until(c.runDeleteNpWorker, time.Second, stopCh)
Expand Down
51 changes: 51 additions & 0 deletions pkg/controller/gc.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
k8serrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/types"
"k8s.io/klog"

"github.com/kubeovn/kube-ovn/pkg/ovs"
Expand Down Expand Up @@ -251,6 +252,56 @@ func (c *Controller) markAndCleanLSP() error {

func (c *Controller) gcLoadBalancer() error {
klog.Infof("start to gc loadbalancers")
if !c.config.EnableLb {
// remove lb from logical switch
vpcs, err := c.vpcsLister.List(labels.Everything())
if err != nil {
return err
}
for _, vpc := range vpcs {
for _, subnetName := range vpc.Status.Subnets {
_, err := c.subnetsLister.Get(subnetName)
if err != nil && !k8serrors.IsNotFound(err) {
return err
}
err = c.ovnClient.RemoveLbFromLogicalSwitch(
vpc.Status.TcpLoadBalancer,
vpc.Status.TcpSessionLoadBalancer,
vpc.Status.UdpLoadBalancer,
vpc.Status.UdpSessionLoadBalancer,
subnetName)
if err != nil {
return err
}
}

vpc.Status.TcpLoadBalancer = ""
vpc.Status.TcpSessionLoadBalancer = ""
vpc.Status.UdpLoadBalancer = ""
vpc.Status.UdpSessionLoadBalancer = ""
bytes, err := vpc.Status.Bytes()
if err != nil {
return err
}
_, err = c.config.KubeOvnClient.KubeovnV1().Vpcs().Patch(context.Background(), vpc.Name, types.MergePatchType, bytes, metav1.PatchOptions{}, "status")
if err != nil {
return err
}
}

// delete
ovnLbs, err := c.ovnClient.ListLoadBalancer()
if err != nil {
klog.Errorf("failed to list load balancer, %v", err)
return err
}
if err = c.ovnClient.DeleteLoadBalancer(ovnLbs...); err != nil {
klog.Errorf("failed to delete load balancer, %v", err)
return err
}
return nil
}

svcs, err := c.servicesLister.List(labels.Everything())
if err != nil {
klog.Errorf("failed to list svc, %v", err)
Expand Down
18 changes: 11 additions & 7 deletions pkg/controller/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@ func (c *Controller) InitOVN() error {
return err
}

if err := c.initLoadBalancer(); err != nil {
klog.Errorf("init load balancer failed %v", err)
return err
if c.config.EnableLb {
if err := c.initLoadBalancer(); err != nil {
klog.Errorf("init load balancer failed %v", err)
return err
}
}

if err := c.initDefaultVlan(); err != nil {
Expand Down Expand Up @@ -58,10 +60,12 @@ func (c *Controller) InitDefaultVpc() error {

vpc.Status.DefaultLogicalSwitch = c.config.DefaultLogicalSwitch
vpc.Status.Router = c.config.ClusterRouter
vpc.Status.TcpLoadBalancer = c.config.ClusterTcpLoadBalancer
vpc.Status.TcpSessionLoadBalancer = c.config.ClusterTcpSessionLoadBalancer
vpc.Status.UdpLoadBalancer = c.config.ClusterUdpLoadBalancer
vpc.Status.UdpSessionLoadBalancer = c.config.ClusterUdpSessionLoadBalancer
if c.config.EnableLb {
vpc.Status.TcpLoadBalancer = c.config.ClusterTcpLoadBalancer
vpc.Status.TcpSessionLoadBalancer = c.config.ClusterTcpSessionLoadBalancer
vpc.Status.UdpLoadBalancer = c.config.ClusterUdpLoadBalancer
vpc.Status.UdpSessionLoadBalancer = c.config.ClusterUdpSessionLoadBalancer
}
vpc.Status.Standby = true
vpc.Status.Default = true

Expand Down
2 changes: 1 addition & 1 deletion pkg/controller/subnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -591,7 +591,7 @@ func (c *Controller) handleAddOrUpdateSubnet(key string) error {
}
}

if subnet.Name != c.config.NodeSwitch {
if c.config.EnableLb && subnet.Name != c.config.NodeSwitch {
if err := c.ovnClient.AddLbToLogicalSwitch(vpc.Status.TcpLoadBalancer, vpc.Status.TcpSessionLoadBalancer, vpc.Status.UdpLoadBalancer, vpc.Status.UdpSessionLoadBalancer, subnet.Name); err != nil {
c.patchSubnetStatus(subnet, "AddLbToLogicalSwitchFailed", err.Error())
return err
Expand Down
19 changes: 10 additions & 9 deletions pkg/controller/vpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,11 +264,6 @@ func (c *Controller) handleAddOrUpdateVpc(key string) error {
return err
}

vpcLb, err := c.addLoadBalancer(key)
if err != nil {
return err
}

if vpc.Name != util.DefaultVpc {
// handle route
existRoute, err := c.ovnClient.GetStaticRouteList(vpc.Name)
Expand Down Expand Up @@ -299,10 +294,16 @@ func (c *Controller) handleAddOrUpdateVpc(key string) error {

vpc.Status.Router = key
vpc.Status.Standby = true
vpc.Status.TcpLoadBalancer = vpcLb.TcpLoadBalancer
vpc.Status.TcpSessionLoadBalancer = vpcLb.TcpSessLoadBalancer
vpc.Status.UdpLoadBalancer = vpcLb.UdpLoadBalancer
vpc.Status.UdpSessionLoadBalancer = vpcLb.UdpSessLoadBalancer
if c.config.EnableLb {
vpcLb, err := c.addLoadBalancer(key)
if err != nil {
return err
}
vpc.Status.TcpLoadBalancer = vpcLb.TcpLoadBalancer
vpc.Status.TcpSessionLoadBalancer = vpcLb.TcpSessLoadBalancer
vpc.Status.UdpLoadBalancer = vpcLb.UdpLoadBalancer
vpc.Status.UdpSessionLoadBalancer = vpcLb.UdpSessLoadBalancer
}
bytes, err := vpc.Status.Bytes()
if err != nil {
return err
Expand Down
40 changes: 40 additions & 0 deletions pkg/ovs/ovn-nbctl.go
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,30 @@ func (c Client) AddLbToLogicalSwitch(tcpLb, tcpSessLb, udpLb, udpSessLb, ls stri
return nil
}

func (c Client) RemoveLbFromLogicalSwitch(tcpLb, tcpSessLb, udpLb, udpSessLb, ls string) error {
if err := c.removeLoadBalancerFromLogicalSwitch(tcpLb, ls); err != nil {
klog.Errorf("failed to add tcp lb to %s, %v", ls, err)
return err
}

if err := c.removeLoadBalancerFromLogicalSwitch(udpLb, ls); err != nil {
klog.Errorf("failed to add udp lb to %s, %v", ls, err)
return err
}

if err := c.removeLoadBalancerFromLogicalSwitch(tcpSessLb, ls); err != nil {
klog.Errorf("failed to add tcp session lb to %s, %v", ls, err)
return err
}

if err := c.removeLoadBalancerFromLogicalSwitch(udpSessLb, ls); err != nil {
klog.Errorf("failed to add udp session lb to %s, %v", ls, err)
return err
}

return nil
}

// DeleteLoadBalancer delete loadbalancer in ovn
func (c Client) DeleteLoadBalancer(lbs ...string) error {
for _, lb := range lbs {
Expand Down Expand Up @@ -780,6 +804,22 @@ func (c Client) addLoadBalancerToLogicalSwitch(lb, ls string) error {
return err
}

func (c Client) removeLoadBalancerFromLogicalSwitch(lb, ls string) error {
if lb == "" {
return nil
}
lbUuid, err := c.FindLoadbalancer(lb)
if err != nil {
return err
}
if lbUuid == "" {
return nil
}

_, err = c.ovnNbCommand(IfExists, "ls-lb-del", ls, lb)
return err
}

// DeleteLoadBalancerVip delete a vip rule from loadbalancer
func (c Client) DeleteLoadBalancerVip(vip, lb string) error {
lbUuid, err := c.FindLoadbalancer(lb)
Expand Down
1 change: 1 addition & 0 deletions yamls/kube-ovn.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ spec:
- --default-interface-name=
- --default-vlan-id=100
- --pod-nic-type=veth-pair
- --enable-lb=true
env:
- name: ENABLE_SSL
value: "false"
Expand Down

0 comments on commit 744e657

Please sign in to comment.