Skip to content

Commit

Permalink
add-metallb-nodeselector-tc
Browse files Browse the repository at this point in the history
  • Loading branch information
Gregory Kopels committed Dec 28, 2024
1 parent ec23344 commit 7dcf454
Show file tree
Hide file tree
Showing 8 changed files with 410 additions and 42 deletions.
37 changes: 35 additions & 2 deletions tests/cnf/core/network/metallb/internal/frr/frr.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package frr

import (
"bytes"
"encoding/json"
"fmt"

"strings"

"github.com/golang/glog"
Expand Down Expand Up @@ -30,6 +32,7 @@ type (
PathFrom string `json:"pathFrom"`
Prefix string `json:"prefix"`
PrefixLen int `json:"prefixLen"`
LocalPref uint32 `json:"locPrf"`
Network string `json:"network"`
Metric int `json:"metric"`
Weight int `json:"weight"`
Expand Down Expand Up @@ -280,10 +283,10 @@ func GetBGPStatus(frrPod *pod.Builder, protocolVersion string, containerName ...
}

// GetBGPCommunityStatus returns bgp community status from frr pod.
func GetBGPCommunityStatus(frrPod *pod.Builder, ipProtocolVersion string) (*bgpStatus, error) {
func GetBGPCommunityStatus(frrPod *pod.Builder, communityString, ipProtocolVersion string) (*bgpStatus, error) {
glog.V(90).Infof("Getting bgp community status from container on pod: %s", frrPod.Definition.Name)

return getBgpStatus(frrPod, fmt.Sprintf("show bgp %s community %s json", ipProtocolVersion, "65535:65282"))
return getBgpStatus(frrPod, fmt.Sprintf("show bgp %s community %s json", ipProtocolVersion, communityString))
}

// FetchBGPConnectTimeValue fetches and returns the ConnectRetryTimer value for the specified BGP peer.
Expand Down Expand Up @@ -499,3 +502,33 @@ func ResetBGPConnection(frrPod *pod.Builder) error {

return err
}

// ValidateLocalPref verifies local pref from FRR is equal to configured Local Pref.
func ValidateLocalPref(frrPod *pod.Builder, localPref uint32, ipFamily string) error {
var (
res bytes.Buffer
err error
)

res, err = frrPod.ExecCommand(append(netparam.VtySh,
fmt.Sprintf("show ip bgp %s json", ipFamily)))

if err != nil {
return fmt.Errorf("failed to query routes %w", err)
}

toParse := bgpStatus{}
err = json.Unmarshal(res.Bytes(), &toParse)

if err != nil {
return fmt.Errorf("failed to parse route local preference %w", err)
}

for _, locPref := range toParse.Routes {
if locPref[0].LocalPref != localPref {
return fmt.Errorf("incorrect localPref: %d", localPref)
}
}

return err
}
4 changes: 4 additions & 0 deletions tests/cnf/core/network/metallb/internal/tsparams/mlbvars.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ var (
FRRK8sDefaultLabel = "component=frr-k8s"
// FRRK8sNodeLabel represents the default metalLb FRRK8S node label.
FRRK8sNodeLabel = "app=frr-k8s"
// LabelHostName contains the key for the hostname label.
LabelHostName = "kubernetes.io/hostname"
// LabelRoleName contains the key for the hostname label.
LabelRoleName = "node-role.kubernetes.io/workercnf"
// ExternalMacVlanNADName represents default external NetworkAttachmentDefinition name.
ExternalMacVlanNADName = "external"
// HubMacVlanNADName represents default external NetworkAttachmentDefinition name.
Expand Down
2 changes: 1 addition & 1 deletion tests/cnf/core/network/metallb/tests/bfd-test.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ var _ = Describe("BFD", Ordered, Label(tsparams.LabelBFDTestCases), ContinueOnFa
ipAddressPool := setupBgpAdvertisement(addressPool, prefixLen)

By("Creating a MetalLB service")
setupMetalLbService(ipStack, ipAddressPool, externalTrafficPolicy)
setupMetalLbService("service-1", ipStack, ipAddressPool, externalTrafficPolicy)

By("Creating nginx test pod on worker node")
setupNGNXPod(workerNodeList[0].Definition.Name)
Expand Down
36 changes: 1 addition & 35 deletions tests/cnf/core/network/metallb/tests/bgp-tests.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@ package tests

import (
"fmt"
"net"
"strings"
"time"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
Expand All @@ -18,7 +15,6 @@ import (
"github.com/openshift-kni/eco-goinfra/pkg/service"
. "github.com/openshift-kni/eco-gotests/tests/cnf/core/network/internal/netinittools"
"github.com/openshift-kni/eco-gotests/tests/cnf/core/network/internal/netparam"
"github.com/openshift-kni/eco-gotests/tests/cnf/core/network/metallb/internal/frr"
"github.com/openshift-kni/eco-gotests/tests/cnf/core/network/metallb/internal/metallbenv"
"github.com/openshift-kni/eco-gotests/tests/cnf/core/network/metallb/internal/tsparams"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -133,7 +129,7 @@ var _ = Describe("BGP", Ordered, Label(tsparams.LabelBGPTestCases), ContinueOnFa
ipAddressPool := setupBgpAdvertisement(addressPool, int32(prefixLen))

By("Creating a MetalLB service")
setupMetalLbService(ipStack, ipAddressPool, "Cluster")
setupMetalLbService("service-1", ipStack, ipAddressPool, "Cluster")

By("Creating nginx test pod on worker node")
setupNGNXPod(workerNodeList[0].Definition.Name)
Expand Down Expand Up @@ -224,33 +220,3 @@ var _ = Describe("BGP", Ordered, Label(tsparams.LabelBGPTestCases), ContinueOnFa
})
})
})

func validatePrefix(
masterNodeFRRPod *pod.Builder, ipProtoVersion string, workerNodesAddresses, addressPool []string, prefixLength int) {
Eventually(
frr.GetBGPStatus, time.Minute, tsparams.DefaultRetryInterval).
WithArguments(masterNodeFRRPod, strings.ToLower(ipProtoVersion), "test").ShouldNot(BeNil())

bgpStatus, err := frr.GetBGPStatus(masterNodeFRRPod, strings.ToLower(ipProtoVersion), "test")
Expect(err).ToNot(HaveOccurred(), "Failed to verify bgp status")
_, subnet, err := net.ParseCIDR(fmt.Sprintf("%s/%d", addressPool[0], prefixLength))
Expect(err).ToNot(HaveOccurred(), "Failed to parse CIDR")
Expect(bgpStatus.Routes).To(HaveKey(subnet.String()), "Failed to verify subnet in bgp status output")

var nextHopAddresses []string

for _, nextHop := range bgpStatus.Routes[subnet.String()] {
Expect(nextHop.PrefixLen).To(BeNumerically("==", prefixLength),
"Failed prefix length is not in expected value")

for _, nHop := range nextHop.Nexthops {
nextHopAddresses = append(nextHopAddresses, nHop.IP)
}
}

Expect(workerNodesAddresses).To(ContainElements(nextHopAddresses),
"Failed next hop address in not in node addresses list")

_, err = frr.GetBGPCommunityStatus(masterNodeFRRPod, strings.ToLower(ipProtoVersion))
Expect(err).ToNot(HaveOccurred(), "Failed to collect bgp community status")
}
54 changes: 53 additions & 1 deletion tests/cnf/core/network/metallb/tests/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package tests

import (
"fmt"
"net"
"strings"
"time"

. "github.com/onsi/ginkgo/v2"
Expand Down Expand Up @@ -172,6 +174,28 @@ func setupBgpAdvertisement(addressPool []string, prefixLen int32) *metallb.IPAdd
return ipAddressPool
}

func setupBgpAdvertisementWithNodeSelector(name, communities string, localPreference uint32, bgpPeers []string,
ipAddressPool *metallb.IPAddressPoolBuilder, nodeSelectors []metav1.LabelSelector,
nodeSelector bool) {
var bgpAdv *metallb.BGPAdvertisementBuilder
if nodeSelector {
bgpAdv = metallb.
NewBGPAdvertisementBuilder(APIClient, name, NetConfig.MlbOperatorNamespace).
WithIPAddressPools([]string{ipAddressPool.Definition.Name}).WithPeers(bgpPeers).
WithCommunities([]string{communities}).
WithLocalPref(localPreference).
WithAggregationLength4(int32(32)).WithNodeSelector(nodeSelectors)
} else {
bgpAdv = metallb.
NewBGPAdvertisementBuilder(APIClient, name, NetConfig.MlbOperatorNamespace).
WithIPAddressPools([]string{ipAddressPool.Definition.Name}).
WithCommunities([]string{communities}).WithLocalPref(localPreference)
}

_, err := bgpAdv.Create()
Expect(err).ToNot(HaveOccurred(), "Failed to create BGPAdvertisement")
}

func setupL2Advertisement(addressPool []string) *metallb.IPAddressPoolBuilder {
ipAddressPool, err := metallb.NewIPAddressPoolBuilder(
APIClient,
Expand Down Expand Up @@ -273,13 +297,14 @@ func createFrrHubPod(name, nodeName, configmapName string, defaultCMD []string,
}

func setupMetalLbService(
name string,
ipStack string,
ipAddressPool *metallb.IPAddressPoolBuilder,
extTrafficPolicy corev1.ServiceExternalTrafficPolicyType) {
servicePort, err := service.DefineServicePort(80, 80, "TCP")
Expect(err).ToNot(HaveOccurred(), "Failed to define service port")

_, err = service.NewBuilder(APIClient, "service-mlb", tsparams.TestNamespaceName,
_, err = service.NewBuilder(APIClient, name, tsparams.TestNamespaceName,
map[string]string{"app": "nginx1"}, *servicePort).
WithExternalTrafficPolicy(extTrafficPolicy).
WithIPFamily([]corev1.IPFamily{corev1.IPFamily(ipStack)}, corev1.IPFamilyPolicySingleStack).
Expand Down Expand Up @@ -377,3 +402,30 @@ func resetOperatorAndTestNS() {
nad.GetGVR())
Expect(err).ToNot(HaveOccurred(), "Failed to clean test namespace")
}

func validatePrefix(
masterNodeFRRPod *pod.Builder, ipProtoVersion string, workerNodesAddresses, addressPool []string, prefixLength int) {
Eventually(
frr.GetBGPStatus, time.Minute, tsparams.DefaultRetryInterval).
WithArguments(masterNodeFRRPod, strings.ToLower(ipProtoVersion), "test").ShouldNot(BeNil())

bgpStatus, err := frr.GetBGPStatus(masterNodeFRRPod, strings.ToLower(ipProtoVersion), "test")
Expect(err).ToNot(HaveOccurred(), "Failed to verify bgp status")
_, subnet, err := net.ParseCIDR(fmt.Sprintf("%s/%d", addressPool[0], prefixLength))
Expect(err).ToNot(HaveOccurred(), "Failed to parse CIDR")
Expect(bgpStatus.Routes).To(HaveKey(subnet.String()), "Failed to verify subnet in bgp status output")

var nextHopAddresses []string

for _, nextHop := range bgpStatus.Routes[subnet.String()] {
Expect(nextHop.PrefixLen).To(BeNumerically("==", prefixLength),
"Failed prefix length is not in expected value")

for _, nHop := range nextHop.Nexthops {
nextHopAddresses = append(nextHopAddresses, nHop.IP)
}
}

Expect(workerNodesAddresses).To(ContainElements(nextHopAddresses),
"Failed next hop address in not in node addresses list")
}
4 changes: 2 additions & 2 deletions tests/cnf/core/network/metallb/tests/frrk8-tests.go
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,7 @@ var _ = Describe("FRR", Ordered, Label(tsparams.LabelFRRTestCases), ContinueOnFa
ipAddressPool := setupBgpAdvertisement(addressPool, int32(32))

By("Creating a MetalLB service")
setupMetalLbService(netparam.IPV4Family, ipAddressPool, "Cluster")
setupMetalLbService("service-1", netparam.IPV4Family, ipAddressPool, "Cluster")

By("Creating nginx test pod on worker node")
setupNGNXPod(workerNodeList[0].Definition.Name)
Expand Down Expand Up @@ -764,7 +764,7 @@ func deployTestPods(addressPool, hubIPAddresses, externalAdvertisedIPv4Routes,
ipAddressPool := setupBgpAdvertisement(addressPool, int32(32))

By("Creating a MetalLB service")
setupMetalLbService(netparam.IPV4Family, ipAddressPool, "Cluster")
setupMetalLbService("service-1", netparam.IPV4Family, ipAddressPool, "Cluster")

By("Creating nginx test pod on worker node")
setupNGNXPod(workerNodeList[0].Definition.Name)
Expand Down
2 changes: 1 addition & 1 deletion tests/cnf/core/network/metallb/tests/layer2-test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ var _ = Describe("Layer2", Ordered, Label(tsparams.LabelLayer2TestCases), Contin
ipAddressPool := setupL2Advertisement(ipv4metalLbIPList)

By("Creating a MetalLB service")
setupMetalLbService(netparam.IPV4Family, ipAddressPool, "Cluster")
setupMetalLbService("service-1", netparam.IPV4Family, ipAddressPool, "Cluster")

By("Creating external Network Attachment Definition")
createExternalNad(tsparams.ExternalMacVlanNADName)
Expand Down
Loading

0 comments on commit 7dcf454

Please sign in to comment.