Skip to content

Commit

Permalink
feat: distributed eip
Browse files Browse the repository at this point in the history
  • Loading branch information
oilbeater committed Dec 10, 2020
1 parent c4d4c34 commit ddda633
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 6 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ dist/images/kube-ovn-webhook
dist/images/kube-ovn-pinger
dist/images/kube-ovn-speaker
dist/images/kube-ovn-monitor
dist/images/kube-ovn-cmd
kube-ovn.yaml
kube-ovn-crd.yaml
ovn.yaml
1 change: 1 addition & 0 deletions docs/snat-and-eip.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ metadata:
namespace: kube-system
data:
enable-external-gw: "true"
type: "centralized" # centralized or distributed, when centralized external-gw-nodes config below will take effect. When distributed, every node in cluster must have a same nic and eip function will perform in distributed way.
external-gw-nodes: "kube-ovn-worker" # NodeName in kubernetes which will act the overlay to underlay gateway functions
external-gw-nic: "eth1" # The nic that will be bridged into ovs and act as overlay to underlay gateway
nic-ip: "172.56.0.1/16" # The ip and mask of the underlay physical gateway
Expand Down
15 changes: 14 additions & 1 deletion pkg/controller/external-gw.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/alauda/kube-ovn/pkg/util"
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"
"reflect"
Expand Down Expand Up @@ -48,6 +49,7 @@ func (c *Controller) resyncExternalGateway() {
}
exGwEnabled = "true"
lastExGwCM = cm.Data
c.ovnClient.ExternalGatewayType = cm.Data["type"]
klog.Info("finish establishing ovn external gw")
}
}
Expand Down Expand Up @@ -89,7 +91,18 @@ func (c *Controller) removeExternalGateway() error {

func (c *Controller) establishExternalGateway(config map[string]string) error {
chassises := []string{}
gwNodes := strings.Split(config["external-gw-nodes"], ",")
nodes, err := c.nodesLister.List(labels.Everything())
if err != nil {
klog.Errorf("failed to list nodes, %v", err)
return err
}
gwNodes := make([]string, 0, len(nodes))
for _, node := range nodes {
gwNodes = append(gwNodes, node.Name)
}
if config["type"] != "distributed" {
gwNodes = strings.Split(config["external-gw-nodes"], ",")
}
for _, gw := range gwNodes {
gw = strings.TrimSpace(gw)
node, err := c.nodesLister.Get(gw)
Expand Down
4 changes: 2 additions & 2 deletions pkg/controller/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -563,12 +563,12 @@ func (c *Controller) handleUpdatePod(key string) error {
}

for _, ipStr := range strings.Split(podIP, ",") {
if err := c.ovnClient.UpdateNatRule("dnat_and_snat", ipStr, pod.Annotations[util.EipAnnotation], c.config.ClusterRouter); err != nil {
if err := c.ovnClient.UpdateNatRule("dnat_and_snat", ipStr, pod.Annotations[util.EipAnnotation], c.config.ClusterRouter, pod.Annotations[util.MacAddressAnnotation], fmt.Sprintf("%s.%s", pod.Name, pod.Namespace)); err != nil {
klog.Errorf("failed to add nat rules, %v", err)
return err
}

if err := c.ovnClient.UpdateNatRule("snat", ipStr, pod.Annotations[util.SnatAnnotation], c.config.ClusterRouter); err != nil {
if err := c.ovnClient.UpdateNatRule("snat", ipStr, pod.Annotations[util.SnatAnnotation], c.config.ClusterRouter, "", ""); err != nil {
klog.Errorf("failed to add nat rules, %v", err)
return err
}
Expand Down
11 changes: 8 additions & 3 deletions pkg/ovs/ovn-nbctl.go
Original file line number Diff line number Diff line change
Expand Up @@ -507,7 +507,7 @@ func parseLrRouteListOutput(output string) (routeList []*StaticRoute, err error)
return routeList, nil
}

func (c Client) UpdateNatRule(policy, logicalIP, externalIP, router string) error {
func (c Client) UpdateNatRule(policy, logicalIP, externalIP, router, logicalMac, port string) error {
if policy == "snat" {
if externalIP == "" {
_, err := c.ovnNbCommand(IfExists, "lr-nat-del", router, "snat", logicalIP)
Expand All @@ -534,8 +534,13 @@ func (c Client) UpdateNatRule(policy, logicalIP, externalIP, router string) erro
}
}
if externalIP != "" {
_, err = c.ovnNbCommand(MayExist, "lr-nat-add", router, policy, externalIP, logicalIP)
return err
if c.ExternalGatewayType == "distributed" {
_, err = c.ovnNbCommand(MayExist, "--stateless", "lr-nat-add", router, policy, externalIP, logicalIP, port, logicalMac)
return err
} else {
_, err = c.ovnNbCommand(MayExist, "lr-nat-add", router, policy, externalIP, logicalIP)
return err
}
}
}
return nil
Expand Down
1 change: 1 addition & 0 deletions pkg/ovs/ovn.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ type Client struct {
ClusterUdpSessionLoadBalancer string
NodeSwitch string
NodeSwitchCIDR string
ExternalGatewayType string
}

const (
Expand Down

0 comments on commit ddda633

Please sign in to comment.