-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Federico Paolinelli <[email protected]>
- Loading branch information
Showing
4 changed files
with
308 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package address | ||
|
||
import ( | ||
"net" | ||
|
||
"go.universe.tf/e2etest/pkg/ipfamily" | ||
) | ||
|
||
func FilterForFamily(ips []string, family ipfamily.Family) []string { | ||
if family == ipfamily.DualStack { | ||
return ips | ||
} | ||
var res []string | ||
for _, ip := range ips { | ||
parsedIP, _, _ := net.ParseCIDR(ip) | ||
if parsedIP == nil { | ||
panic("invalid ip") // it's a test after all, should never happen | ||
} | ||
isV4 := (parsedIP.To4() != nil) | ||
if family == ipfamily.IPv4 && isV4 { | ||
res = append(res, ip) | ||
continue | ||
} | ||
if family == ipfamily.IPv6 && !isV4 { | ||
res = append(res, ip) | ||
continue | ||
} | ||
} | ||
return res | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,257 @@ | ||
// SPDX-License-Identifier:Apache-2.0 | ||
|
||
package tests | ||
|
||
import ( | ||
"github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
"go.universe.tf/e2etest/pkg/frr/container" | ||
frrcontainer "go.universe.tf/e2etest/pkg/frr/container" | ||
|
||
frrk8sv1beta1 "github.com/metallb/frr-k8s/api/v1beta1" | ||
"github.com/metallb/frrk8stests/pkg/address" | ||
"github.com/metallb/frrk8stests/pkg/config" | ||
"github.com/metallb/frrk8stests/pkg/dump" | ||
"github.com/metallb/frrk8stests/pkg/infra" | ||
"github.com/metallb/frrk8stests/pkg/k8s" | ||
"github.com/metallb/frrk8stests/pkg/k8sclient" | ||
frrconfig "go.universe.tf/e2etest/pkg/frr/config" | ||
"go.universe.tf/e2etest/pkg/ipfamily" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
clientset "k8s.io/client-go/kubernetes" | ||
) | ||
|
||
var _ = ginkgo.Describe("Advertising and Receiving routes", func() { | ||
var cs clientset.Interface | ||
|
||
defer ginkgo.GinkgoRecover() | ||
updater, err := config.NewUpdater() | ||
Expect(err).NotTo(HaveOccurred()) | ||
reporter := dump.NewK8sReporter(k8s.FRRK8sNamespace) | ||
|
||
ginkgo.AfterEach(func() { | ||
if ginkgo.CurrentSpecReport().Failed() { | ||
testName := ginkgo.CurrentSpecReport().LeafNodeText | ||
dump.K8sInfo(testName, reporter) | ||
dump.BGPInfo(testName, infra.FRRContainers, cs) | ||
} | ||
}) | ||
|
||
ginkgo.BeforeEach(func() { | ||
ginkgo.By("Clearing any previous configuration") | ||
|
||
for _, c := range infra.FRRContainers { | ||
err := c.UpdateBGPConfigFile(frrconfig.Empty) | ||
Expect(err).NotTo(HaveOccurred()) | ||
} | ||
err := updater.Clean() | ||
Expect(err).NotTo(HaveOccurred()) | ||
|
||
cs = k8sclient.New() | ||
}) | ||
|
||
initNeighbors := func(useVrf bool, ipFamily ipfamily.Family) ([]*frrcontainer.FRR, config.PeersConfig, []frrk8sv1beta1.Neighbor) { | ||
frrs := config.ContainersForVRF(infra.FRRContainers, "") | ||
if useVrf { | ||
frrs = config.ContainersForVRF(infra.FRRContainers, infra.VRFName) | ||
} | ||
peersConfig := config.PeersForContainers(frrs, ipFamily) | ||
neighbors := config.NeighborsFromPeers(peersConfig.PeersV4, peersConfig.PeersV6) | ||
return frrs, peersConfig, neighbors | ||
} | ||
|
||
pairWithNodes := func(frrs []*frrcontainer.FRR, family ipfamily.Family, toAdvertise []string) { | ||
for _, c := range frrs { | ||
err := container.PairWithNodes(cs, c, family, func(frr *container.FRR) { | ||
frr.NeighborConfig.ToAdvertiseV4 = address.FilterForFamily(toAdvertise, ipfamily.IPv4) | ||
frr.NeighborConfig.ToAdvertiseV6 = address.FilterForFamily(toAdvertise, ipfamily.IPv6) | ||
}) | ||
Expect(err).NotTo(HaveOccurred()) | ||
} | ||
} | ||
|
||
updateAndCheckPeered := func(config frrk8sv1beta1.FRRConfiguration, peersDefault, peersVRF config.PeersConfig, frrsDefault, frrsVRF []*frrcontainer.FRR, family ipfamily.Family) { | ||
err := updater.Update(peersDefault.Secrets, config) | ||
Expect(err).NotTo(HaveOccurred()) | ||
|
||
err = updater.Update(peersVRF.Secrets, config) | ||
Expect(err).NotTo(HaveOccurred()) | ||
|
||
nodes, err := k8s.Nodes(cs) | ||
Expect(err).NotTo(HaveOccurred()) | ||
|
||
for _, c := range frrsDefault { | ||
ValidateFRRPeeredWithNodes(nodes, c, family) | ||
} | ||
for _, c := range frrsVRF { | ||
ValidateFRRPeeredWithNodes(nodes, c, family) | ||
} | ||
} | ||
|
||
baseConfig := frrk8sv1beta1.FRRConfiguration{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "test", | ||
Namespace: k8s.FRRK8sNamespace, | ||
}, | ||
Spec: frrk8sv1beta1.FRRConfigurationSpec{ | ||
BGP: frrk8sv1beta1.BGPConfig{ | ||
Routers: []frrk8sv1beta1.Router{ | ||
{ | ||
ASN: infra.FRRK8sASN, | ||
}, | ||
{ | ||
ASN: infra.FRRK8sASNVRF, | ||
VRF: infra.VRFName, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
ginkgo.Context("Receiving IPs", func() { | ||
type params struct { | ||
ipFamily ipfamily.Family | ||
toAdvertise []string // advertised from outside | ||
toAdvertiseVRF []string | ||
} | ||
|
||
ginkgo.DescribeTable("When the default VRF imports a vrf", func(p params) { | ||
|
||
frrsDefault, peersDefault, neighborsDefault := initNeighbors(false, p.ipFamily) | ||
frrsVRF, peersVRF, neighborsVRF := initNeighbors(true, p.ipFamily) | ||
|
||
ginkgo.By("pairing with nodes") | ||
pairWithNodes(frrsDefault, p.ipFamily, p.toAdvertise) | ||
pairWithNodes(frrsVRF, p.ipFamily, p.toAdvertiseVRF) | ||
|
||
config := baseConfig.DeepCopy() | ||
config.Spec.BGP.Routers[0].Neighbors = neighborsDefault | ||
config.Spec.BGP.Routers[0].Imports = []frrk8sv1beta1.Import{{VRF: infra.VRFName}} | ||
config.Spec.BGP.Routers[1].Neighbors = neighborsVRF | ||
|
||
for i := range config.Spec.BGP.Routers[0].Neighbors { | ||
config.Spec.BGP.Routers[0].Neighbors[i].ToReceive.Allowed.Mode = frrk8sv1beta1.AllowAll | ||
} | ||
for i := range config.Spec.BGP.Routers[1].Neighbors { | ||
config.Spec.BGP.Routers[0].Neighbors[i].ToReceive.Allowed.Mode = frrk8sv1beta1.AllowAll | ||
} | ||
updateAndCheckPeered(*config, peersDefault, peersVRF, frrsDefault, frrsVRF, p.ipFamily) | ||
|
||
ginkgo.By("validating") | ||
pods, err := k8s.FRRK8sPods(cs) | ||
Expect(err).NotTo(HaveOccurred()) | ||
|
||
for _, frr := range frrsDefault { | ||
ValidateNodesHaveRoutes(pods, *frr, p.toAdvertise...) | ||
} | ||
for _, frr := range frrsVRF { | ||
// The routes advertised from the peers through red must appear in the default VRF too | ||
ValidateNodesHaveRoutesVRF(pods, *frr, "default", p.toAdvertiseVRF...) | ||
ValidateNodesHaveRoutes(pods, *frr, p.toAdvertiseVRF...) | ||
} | ||
}, | ||
ginkgo.Entry("IPV4 - default imports red", params{ | ||
ipFamily: ipfamily.IPv4, | ||
toAdvertise: []string{"192.168.2.0/24"}, | ||
toAdvertiseVRF: []string{"192.169.2.0/24"}, | ||
}), | ||
) | ||
|
||
ginkgo.DescribeTable("When the red VRF imports the default VRF", func(p params) { | ||
|
||
frrsDefault, peersDefault, neighborsDefault := initNeighbors(false, p.ipFamily) | ||
frrsVRF, peersVRF, neighborsVRF := initNeighbors(true, p.ipFamily) | ||
|
||
ginkgo.By("pairing with nodes") | ||
pairWithNodes(frrsDefault, p.ipFamily, p.toAdvertise) | ||
pairWithNodes(frrsVRF, p.ipFamily, p.toAdvertiseVRF) | ||
|
||
config := baseConfig.DeepCopy() | ||
config.Spec.BGP.Routers[0].Neighbors = neighborsDefault | ||
config.Spec.BGP.Routers[1].Neighbors = neighborsVRF | ||
config.Spec.BGP.Routers[1].Imports = []frrk8sv1beta1.Import{{VRF: "default"}} | ||
for i := range config.Spec.BGP.Routers[0].Neighbors { | ||
config.Spec.BGP.Routers[0].Neighbors[i].ToReceive.Allowed.Mode = frrk8sv1beta1.AllowAll | ||
} | ||
for i := range config.Spec.BGP.Routers[1].Neighbors { | ||
config.Spec.BGP.Routers[0].Neighbors[i].ToReceive.Allowed.Mode = frrk8sv1beta1.AllowAll | ||
} | ||
|
||
updateAndCheckPeered(*config, peersDefault, peersVRF, frrsDefault, frrsVRF, p.ipFamily) | ||
|
||
ginkgo.By("validating") | ||
pods, err := k8s.FRRK8sPods(cs) | ||
Expect(err).NotTo(HaveOccurred()) | ||
|
||
for _, frr := range frrsDefault { | ||
ValidateNodesHaveRoutesVRF(pods, *frr, infra.VRFName, p.toAdvertise...) | ||
ValidateNodesHaveRoutes(pods, *frr, p.toAdvertise...) | ||
} | ||
for _, frr := range frrsVRF { | ||
ValidateNodesHaveRoutes(pods, *frr, p.toAdvertiseVRF...) | ||
} | ||
}, | ||
ginkgo.Entry("IPV4 - default imports red", params{ | ||
ipFamily: ipfamily.IPv4, | ||
toAdvertise: []string{"192.168.2.0/24"}, | ||
toAdvertiseVRF: []string{"192.169.2.0/24"}, | ||
}), | ||
) | ||
|
||
}) | ||
|
||
ginkgo.Context("Advertising IPs", func() { | ||
type params struct { | ||
ipFamily ipfamily.Family | ||
toAdvertise []string // advertised from outside | ||
toAdvertiseVRF []string | ||
} | ||
|
||
ginkgo.DescribeTable("When the default VRF imports a vrf", func(p params) { | ||
frrsDefault, peersDefault, neighborsDefault := initNeighbors(false, p.ipFamily) | ||
frrsVRF, peersVRF, neighborsVRF := initNeighbors(true, p.ipFamily) | ||
|
||
ginkgo.By("pairing with nodes") | ||
pairWithNodes(frrsDefault, p.ipFamily, p.toAdvertise) | ||
pairWithNodes(frrsVRF, p.ipFamily, p.toAdvertiseVRF) | ||
|
||
config := baseConfig.DeepCopy() | ||
config.Spec.BGP.Routers[0].Neighbors = neighborsDefault | ||
config.Spec.BGP.Routers[0].Prefixes = p.toAdvertise | ||
config.Spec.BGP.Routers[0].Imports = []frrk8sv1beta1.Import{{VRF: infra.VRFName}} | ||
config.Spec.BGP.Routers[1].Neighbors = neighborsVRF | ||
config.Spec.BGP.Routers[1].Prefixes = p.toAdvertiseVRF | ||
|
||
for i := range config.Spec.BGP.Routers[0].Neighbors { | ||
config.Spec.BGP.Routers[0].Neighbors[i].ToAdvertise.Allowed.Mode = frrk8sv1beta1.AllowRestricted | ||
config.Spec.BGP.Routers[0].Neighbors[i].ToAdvertise.Allowed.Prefixes = append(p.toAdvertise, p.toAdvertiseVRF...) | ||
} | ||
for i := range config.Spec.BGP.Routers[1].Neighbors { | ||
config.Spec.BGP.Routers[1].Neighbors[i].ToAdvertise.Allowed.Mode = frrk8sv1beta1.AllowRestricted | ||
config.Spec.BGP.Routers[1].Neighbors[i].ToAdvertise.Allowed.Prefixes = p.toAdvertiseVRF | ||
} | ||
|
||
updateAndCheckPeered(*config, peersDefault, peersVRF, frrsDefault, frrsVRF, p.ipFamily) | ||
|
||
ginkgo.By("validating") | ||
|
||
nodes, err := k8s.Nodes(cs) | ||
Expect(err).NotTo(HaveOccurred()) | ||
|
||
for _, frr := range frrsDefault { | ||
ValidatePrefixesForNeighbor(*frr, nodes, p.toAdvertise...) | ||
ValidatePrefixesForNeighborVRF(*frr, nodes, infra.VRFName, p.toAdvertiseVRF...) | ||
} | ||
for _, frr := range frrsVRF { | ||
ValidatePrefixesForNeighbor(*frr, nodes, p.toAdvertiseVRF...) | ||
} | ||
}, | ||
ginkgo.Entry("IPV4 - default imports red", params{ | ||
ipFamily: ipfamily.IPv4, | ||
toAdvertise: []string{"192.168.2.0/24"}, | ||
toAdvertiseVRF: []string{"192.169.2.0/24"}, | ||
}), | ||
) | ||
}) | ||
|
||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters