Skip to content

Commit

Permalink
Update egress agent test
Browse files Browse the repository at this point in the history
  • Loading branch information
lou-lan committed Jul 30, 2023
1 parent 75589ad commit 4f2c8c5
Show file tree
Hide file tree
Showing 17 changed files with 450 additions and 103 deletions.
2 changes: 1 addition & 1 deletion charts/crds/egressgateway.spidernet.io_egresstunnels.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ spec:
group: egressgateway.spidernet.io
names:
categories:
- egressnode
- egresstunnel
kind: EgressTunnel
listKind: EgressTunnelList
plural: egresstunnels
Expand Down
8 changes: 2 additions & 6 deletions pkg/agent/vxlan.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ func (r *vxlanReconciler) updateEgressNodeStatus(node *egressv1.EgressTunnel, ve
ctx := context.Background()
err = r.client.Get(ctx, types.NamespacedName{Name: r.cfg.NodeName}, node)
if err != nil {
if !errors.IsNotFound(err) {
if errors.IsNotFound(err) {

Check warning on line 188 in pkg/agent/vxlan.go

View check run for this annotation

Codecov / codecov/patch

pkg/agent/vxlan.go#L188

Added line #L188 was not covered by tests
return nil
}
return err
Expand Down Expand Up @@ -283,11 +283,7 @@ func (r *vxlanReconciler) parseVTEP(status egressv1.EgressNodeStatus) *vxlan.Pee
if !ready {
return nil
}
return &vxlan.Peer{
IPv4: ipv4,
IPv6: ipv6,
MAC: mac,
}
return &vxlan.Peer{IPv4: ipv4, IPv6: ipv6, MAC: mac}

Check warning on line 286 in pkg/agent/vxlan.go

View check run for this annotation

Codecov / codecov/patch

pkg/agent/vxlan.go#L286

Added line #L286 was not covered by tests
}

func (r *vxlanReconciler) version() int {
Expand Down
152 changes: 146 additions & 6 deletions pkg/agent/vxlan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ type TestReqVXLAN struct {

func TestReconcilerEgressNode(t *testing.T) {
cases := map[string]TestCaseVXLAN{
//"caseAddEgressNode": caseAddEgressNode(),
"caseAddEgressNodeIPv4": caseAddEgressNodeIPv4(),
"caseAddEgressNodeDual": caseAddEgressNodeDual(),
}

getParent := vxlan.GetParentByDefaultRoute(vxlan.NetLink{
Expand All @@ -56,6 +57,7 @@ func TestReconcilerEgressNode(t *testing.T) {
builder := fake.NewClientBuilder()
builder.WithScheme(schema.GetScheme())
builder.WithObjects(c.initialObjects...)
builder.WithStatusSubresource(c.initialObjects...)
ctx := context.Background()
ruleRoute := route.NewRuleRoute(log)
reconciler := vxlanReconciler{
Expand All @@ -65,6 +67,8 @@ func TestReconcilerEgressNode(t *testing.T) {
getParent: getParent,
ruleRoute: ruleRoute,
ruleRouteCache: utils.NewSyncMap[string, []net.IP](),
peerMap: utils.NewSyncMap[string, vxlan.Peer](),
vxlan: vxlan.New(),
}

for _, req := range c.reqs {
Expand All @@ -78,8 +82,7 @@ func TestReconcilerEgressNode(t *testing.T) {
}
}

func caseAddEgressNode() TestCaseVXLAN {

func caseAddEgressNodeIPv4() TestCaseVXLAN {
_, ipn, err := net.ParseCIDR("192.168.100.1/24")
if err != nil {
panic(err)
Expand All @@ -90,9 +93,8 @@ func caseAddEgressNode() TestCaseVXLAN {
NodeName: "workstation1",
},
FileConfig: config.FileConfig{
EnableIPv4: true,
EnableIPv6: false,

EnableIPv4: true,
EnableIPv6: false,
TunnelIPv4Net: ipn,
TunnelIPv6Net: nil,
},
Expand All @@ -105,6 +107,29 @@ func caseAddEgressNode() TestCaseVXLAN {
Spec: egressv1.EgressNodeSpec{},
Status: egressv1.EgressNodeStatus{},
},
&egressv1.EgressTunnel{
ObjectMeta: metav1.ObjectMeta{Name: "workstation2"},
Spec: egressv1.EgressNodeSpec{},
Status: egressv1.EgressNodeStatus{},
},
&egressv1.EgressTunnel{
ObjectMeta: metav1.ObjectMeta{Name: "workstation3"},
Spec: egressv1.EgressNodeSpec{},
Status: egressv1.EgressNodeStatus{
Tunnel: egressv1.Tunnel{
IPv4: "172.16.25.128",
IPv6: "",
MAC: "00:0c:29:bb:8b:31",
Parent: egressv1.Parent{
Name: "ens160",
IPv4: "10.6.1.21",
IPv6: "",
},
},
Phase: egressv1.EgressNodeSucceeded,
Mark: "",
},
},
},
reqs: []TestReqVXLAN{
{
Expand All @@ -115,6 +140,121 @@ func caseAddEgressNode() TestCaseVXLAN {
expErr: false,
expRequeue: false,
},
{
nn: types.NamespacedName{
Namespace: "EgressTunnel/",
Name: "workstation2",
},
expErr: false,
expRequeue: false,
},
{
nn: types.NamespacedName{
Namespace: "EgressTunnel/",
Name: "workstation3",
},
expErr: false,
expRequeue: false,
},
{
nn: types.NamespacedName{
Namespace: "EgressTunnel/",
Name: "workstation4-not-found",
},
expErr: true,
expRequeue: false,
},
},
config: cfg,
}
}

func caseAddEgressNodeDual() TestCaseVXLAN {
_, tunnelIPv4CIDR, err := net.ParseCIDR("192.168.100.1/24")
if err != nil {
panic(err)
}
_, tunnelIPv6CIDR, err := net.ParseCIDR("fd00::/64")
if err != nil {
panic(err)
}

cfg := &config.Config{
EnvConfig: config.EnvConfig{
NodeName: "workstation1",
},
FileConfig: config.FileConfig{
EnableIPv4: true,
EnableIPv6: true,
TunnelIPv4Net: tunnelIPv4CIDR,
TunnelIPv6Net: tunnelIPv6CIDR,
},
}

return TestCaseVXLAN{
initialObjects: []client.Object{
&egressv1.EgressTunnel{
ObjectMeta: metav1.ObjectMeta{Name: "workstation1"},
Spec: egressv1.EgressNodeSpec{},
Status: egressv1.EgressNodeStatus{},
},
&egressv1.EgressTunnel{
ObjectMeta: metav1.ObjectMeta{Name: "workstation2"},
Spec: egressv1.EgressNodeSpec{},
Status: egressv1.EgressNodeStatus{},
},
&egressv1.EgressTunnel{
ObjectMeta: metav1.ObjectMeta{Name: "workstation3"},
Spec: egressv1.EgressNodeSpec{},
Status: egressv1.EgressNodeStatus{
Tunnel: egressv1.Tunnel{
IPv4: "192.168.100.2",
IPv6: "fd00::1",
MAC: "00:0c:29:bb:8b:31",
Parent: egressv1.Parent{
Name: "ens160",
IPv4: "10.6.1.21",
IPv6: "",
},
},
Phase: egressv1.EgressNodeSucceeded,
Mark: "",
},
},
},
reqs: []TestReqVXLAN{
{
nn: types.NamespacedName{
Namespace: "EgressTunnel/",
Name: "workstation1",
},
expErr: false,
expRequeue: false,
},
{
nn: types.NamespacedName{
Namespace: "EgressTunnel/",
Name: "workstation2",
},
expErr: false,
expRequeue: false,
},
{
nn: types.NamespacedName{
Namespace: "EgressTunnel/",
Name: "workstation3",
},
expErr: false,
expRequeue: false,
},
{
nn: types.NamespacedName{
Namespace: "EgressTunnel/",
Name: "workstation4-not-found",
},
expErr: true,
expRequeue: false,
},
},
config: cfg,
}
Expand Down
9 changes: 5 additions & 4 deletions pkg/controller/egress_cluster_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package controller
import (
"context"
"fmt"
"github.com/spidernet-io/egressgateway/pkg/utils/ip"
"strings"
"sync"

Expand Down Expand Up @@ -144,11 +145,11 @@ func (r *eciReconciler) reconcileCalicoIPPool(ctx context.Context, req reconcile
log.Info("update event")

// check if cidr about ippools changed
isv4Cidr, err := utils.IsIPv4Cidr(ippool.Spec.CIDR)
isv4Cidr, err := ip.IsIPv4Cidr(ippool.Spec.CIDR)

Check warning on line 148 in pkg/controller/egress_cluster_info.go

View check run for this annotation

Codecov / codecov/patch

pkg/controller/egress_cluster_info.go#L148

Added line #L148 was not covered by tests
if err != nil {
return reconcile.Result{Requeue: true}, err
}
isv6Cidr, err := utils.IsIPv6Cidr(ippool.Spec.CIDR)
isv6Cidr, err := ip.IsIPv6Cidr(ippool.Spec.CIDR)

Check warning on line 152 in pkg/controller/egress_cluster_info.go

View check run for this annotation

Codecov / codecov/patch

pkg/controller/egress_cluster_info.go#L152

Added line #L152 was not covered by tests
if err != nil {
return reconcile.Result{Requeue: true}, err
}
Expand Down Expand Up @@ -547,11 +548,11 @@ func getCidr(pod *corev1.Pod, param string) (ipv4Range, ipv6Range []string, err
// get cidr
ipRanges := strings.Split(ipRange, ",")
if len(ipRanges) == 1 {
if isV4, _ := utils.IsIPv4Cidr(ipRanges[0]); isV4 {
if isV4, _ := ip.IsIPv4Cidr(ipRanges[0]); isV4 {

Check warning on line 551 in pkg/controller/egress_cluster_info.go

View check run for this annotation

Codecov / codecov/patch

pkg/controller/egress_cluster_info.go#L551

Added line #L551 was not covered by tests
ipv4Range = ipRanges
ipv6Range = []string{}
}
if isV6, _ := utils.IsIPv6Cidr(ipRanges[0]); isV6 {
if isV6, _ := ip.IsIPv6Cidr(ipRanges[0]); isV6 {

Check warning on line 555 in pkg/controller/egress_cluster_info.go

View check run for this annotation

Codecov / codecov/patch

pkg/controller/egress_cluster_info.go#L555

Added line #L555 was not covered by tests
ipv6Range = ipRanges
ipv4Range = []string{}

Expand Down
17 changes: 9 additions & 8 deletions pkg/egressgateway/egress_gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package egressgateway
import (
"context"
"fmt"
"github.com/spidernet-io/egressgateway/pkg/utils/ip"
"math/rand"
"net"
"time"
Expand Down Expand Up @@ -657,10 +658,10 @@ func (r egnReconciler) allocatorEIP(selEipLolicy string, nodeName string, pi pol
var useIpv4s []net.IP
var useIpv4sByNode []net.IP

ipv4Ranges, _ := utils.MergeIPRanges(constant.IPv4, eg.Spec.Ippools.IPv4)
ipv4Ranges, _ := ip.MergeIPRanges(constant.IPv4, eg.Spec.Ippools.IPv4)
perIpv4 = pi.ipv4
if len(perIpv4) != 0 {
result, err := utils.IsIPIncludedRange(constant.IPv4, perIpv4, ipv4Ranges)
result, err := ip.IsIPIncludedRange(constant.IPv4, perIpv4, ipv4Ranges)
if err != nil {
return "", "", err
}
Expand All @@ -676,8 +677,8 @@ func (r egnReconciler) allocatorEIP(selEipLolicy string, nodeName string, pi pol
}
}

ipv4s, _ := utils.ParseIPRanges(constant.IPv4, ipv4Ranges)
freeIpv4s := utils.IPsDiffSet(ipv4s, useIpv4s, false)
ipv4s, _ := ip.ParseIPRanges(constant.IPv4, ipv4Ranges)
freeIpv4s := ip.IPsDiffSet(ipv4s, useIpv4s, false)

if len(freeIpv4s) == 0 {
for _, node := range eg.Status.NodeList {
Expand Down Expand Up @@ -709,11 +710,11 @@ func (r egnReconciler) allocatorEIP(selEipLolicy string, nodeName string, pi pol
var useIpv6s []net.IP
var useIpv6sByNode []net.IP

ipv6Ranges, _ := utils.MergeIPRanges(constant.IPv6, eg.Spec.Ippools.IPv6)
ipv6Ranges, _ := ip.MergeIPRanges(constant.IPv6, eg.Spec.Ippools.IPv6)

perIpv6 = pi.ipv6
if len(perIpv6) != 0 {
result, err := utils.IsIPIncludedRange(constant.IPv6, perIpv6, ipv6Ranges)
result, err := ip.IsIPIncludedRange(constant.IPv6, perIpv6, ipv6Ranges)
if err != nil {
return "", "", err
}
Expand All @@ -729,8 +730,8 @@ func (r egnReconciler) allocatorEIP(selEipLolicy string, nodeName string, pi pol
}
}

ipv6s, _ := utils.ParseIPRanges(constant.IPv6, ipv6Ranges)
freeIpv6s := utils.IPsDiffSet(ipv6s, useIpv6s, false)
ipv6s, _ := ip.ParseIPRanges(constant.IPv6, ipv6Ranges)
freeIpv6s := ip.IPsDiffSet(ipv6s, useIpv6s, false)

if len(freeIpv6s) == 0 {
for _, node := range eg.Status.NodeList {
Expand Down
Loading

0 comments on commit 4f2c8c5

Please sign in to comment.