Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

try fix fip not work for default vpc #3666

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions pkg/controller/ovn_fip.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@
import (
"context"
"encoding/json"
"fmt"

Check failure on line 6 in pkg/controller/ovn_fip.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gofumpt`-ed with `-extra` (gofumpt)
"strconv"

"github.com/kubeovn/kube-ovn/pkg/ovs"

Check failure on line 7 in pkg/controller/ovn_fip.go

View workflow job for this annotation

GitHub Actions / lint

File is not `goimports`-ed with -local github.com/kubeovn/kube-ovn (goimports)
k8serrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
Expand All @@ -14,6 +13,7 @@
"k8s.io/client-go/tools/cache"
"k8s.io/klog/v2"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
"strconv"

Check failure on line 16 in pkg/controller/ovn_fip.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gofumpt`-ed with `-extra` (gofumpt)

kubeovnv1 "github.com/kubeovn/kube-ovn/pkg/apis/kubeovn/v1"
"github.com/kubeovn/kube-ovn/pkg/ovsdb/ovnnb"
Expand Down Expand Up @@ -421,6 +421,15 @@
return err
}
// ovn add fip
if vpcName == c.config.ClusterRouter {
// Default vpc
defaultVpcToDefaultExternalPortGroupName := ovs.VpcSubnetToPortGroupName(vpcName, c.config.ExternalGatewaySwitch)

if err := c.OVNNbClient.PortGroupAddPorts(defaultVpcToDefaultExternalPortGroupName, cachedFip.Spec.IPName); err != nil {
klog.Errorf("failed to add port %s to port group %s, %v", cachedFip.Spec.IPName, defaultVpcToDefaultExternalPortGroupName, err)
return err
}
}
options := map[string]string{"staleless": strconv.FormatBool(c.ExternalGatewayType == kubeovnv1.GWDistributedType)}
if err = c.OVNNbClient.AddNat(vpcName, ovnnb.NATTypeDNATAndSNAT, cachedEip.Status.V4Ip,
internalV4Ip, mac, cachedFip.Spec.IPName, options); err != nil {
Expand Down Expand Up @@ -457,6 +466,13 @@
}
// ovn delete fip nat
if cachedFip.Status.Vpc != "" && cachedFip.Status.V4Eip != "" && cachedFip.Status.V4Ip != "" {
if cachedFip.Status.Vpc == c.config.ClusterRouter {
defaultVpcToDefaultExternalPortGroupName := ovs.VpcSubnetToPortGroupName(c.config.ClusterRouter, c.config.ExternalGatewaySwitch)
if err := c.OVNNbClient.PortGroupRemovePorts(defaultVpcToDefaultExternalPortGroupName, cachedFip.Spec.IPName); err != nil {
klog.Errorf("failed to remove port %s from port group %s, %v", cachedFip.Spec.IPName, defaultVpcToDefaultExternalPortGroupName, err)
return err
}
}
if err = c.OVNNbClient.DeleteNat(cachedFip.Status.Vpc, ovnnb.NATTypeDNATAndSNAT, cachedFip.Status.V4Eip, cachedFip.Status.V4Ip); err != nil {
klog.Errorf("failed to delete fip %s, %v", key, err)
return err
Expand Down
26 changes: 26 additions & 0 deletions pkg/controller/vpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@
"context"
"encoding/json"
"fmt"
"github.com/kubeovn/kube-ovn/pkg/ovs"

Check failure on line 7 in pkg/controller/vpc.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gofumpt`-ed with `-extra` (gofumpt)
"net"
"reflect"
"slices"
"sort"
"strings"

Check failure on line 12 in pkg/controller/vpc.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gofumpt`-ed with `-extra` (gofumpt)

Check failure on line 13 in pkg/controller/vpc.go

View workflow job for this annotation

GitHub Actions / lint

File is not `goimports`-ed with -local github.com/kubeovn/kube-ovn (goimports)
v1 "k8s.io/api/core/v1"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -418,6 +419,31 @@
}
}
}

defaultVpcToDefaultExternalPortGroupName := ovs.VpcSubnetToPortGroupName(vpc.Name, c.config.ExternalGatewaySwitch)
snatMatch := "ip4.src == $" + defaultVpcToDefaultExternalPortGroupName
if externalSubnetExist {
if err := c.OVNNbClient.CreatePortGroup(defaultVpcToDefaultExternalPortGroupName, nil); err != nil {
klog.Errorf("failed to create port group %s for vpc %s, %v", defaultVpcToDefaultExternalPortGroupName, vpc.Name, err)
return err
}
if policies, err := c.OVNNbClient.GetLogicalRouterPolicy(vpc.Name, util.DefaultVpcToExternalNetworkSnatPolicyPriority, snatMatch, true); err != nil {
klog.Errorf("failed to get snat policy route for vpc %s to external network, %v", vpc.Name, err)
} else if len(policies) == 0 {
if err := c.OVNNbClient.AddLogicalRouterPolicy(vpc.Name, util.DefaultVpcToExternalNetworkSnatPolicyPriority, snatMatch,
ovnnb.LogicalRouterPolicyActionReroute, []string{externalSubnet.Spec.Gateway}, nil); err != nil {
klog.Errorf("failed to add snat policy route for vpc %s to external network, %v", vpc.Name, err)
}
}
} else {
if err := c.OVNNbClient.DeleteLogicalRouterPolicy(vpc.Name, util.DefaultVpcToExternalNetworkSnatPolicyPriority, snatMatch); err != nil {
klog.Errorf("failed to delete snat policy route for vpc %s to external network, %v", vpc.Name, err)
}
if err := c.OVNNbClient.DeletePortGroup(defaultVpcToDefaultExternalPortGroupName); err != nil {
klog.Errorf("failed to delete port group %s for vpc %s, %v", defaultVpcToDefaultExternalPortGroupName, vpc.Name, err)
return err
}
}
}

routeNeedDel, routeNeedAdd, err := diffStaticRoute(staticExistedRoutes, staticTargetRoutes)
Expand Down
6 changes: 6 additions & 0 deletions pkg/ovs/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ func PodNameToPortName(pod, namespace, provider string) string {
return fmt.Sprintf("%s.%s.%s", pod, namespace, provider)
}

func VpcSubnetToPortGroupName(vpc, subnet string) string {
portGroupName := fmt.Sprintf("%s.%s", vpc, subnet)
portGroupName = strings.ReplaceAll(portGroupName, "-", ".")
return portGroupName
}

func GetLocalnetName(subnet string) string {
return fmt.Sprintf("localnet.%s", subnet)
}
Expand Down
13 changes: 7 additions & 6 deletions pkg/util/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,12 +199,13 @@ const (
OvnFip = "ovn"
IptablesFip = "iptables"

U2OSubnetPolicyPriority = 29400
GatewayRouterPolicyPriority = 29000
OvnICPolicyPriority = 29500
NodeRouterPolicyPriority = 30000
NodeLocalDNSPolicyPriority = 30100
SubnetRouterPolicyPriority = 31000
GatewayRouterPolicyPriority = 29000
DefaultVpcToExternalNetworkSnatPolicyPriority = 29200
U2OSubnetPolicyPriority = 29400
OvnICPolicyPriority = 29500
NodeRouterPolicyPriority = 30000
NodeLocalDNSPolicyPriority = 30100
SubnetRouterPolicyPriority = 31000

OffloadType = "offload-port"
InternalType = "internal-port"
Expand Down
Loading