Skip to content

Commit

Permalink
utils: improve unittest coverage
Browse files Browse the repository at this point in the history
Signed-off-by: Cyclinder Kuo <[email protected]>
  • Loading branch information
cyclinder committed Dec 31, 2024
1 parent 415359e commit c59b57c
Show file tree
Hide file tree
Showing 5 changed files with 155 additions and 110 deletions.
102 changes: 2 additions & 100 deletions pkg/coordinatormanager/coordinator_informer.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"fmt"
"net"
"reflect"
"regexp"
"sort"
"strings"
"time"
Expand Down Expand Up @@ -473,7 +472,7 @@ func (cc *CoordinatorController) updatePodAndServerCIDR(ctx context.Context, log

if err == nil {
logger.Sugar().Info("Trying to fetch the ClusterCIDR from kube-system/kubeadm-config")
k8sPodCIDR, k8sServiceCIDR, err = ExtractK8sCIDRFromKubeadmConfigMap(&cm)
k8sPodCIDR, k8sServiceCIDR, err = utils.ExtractK8sCIDRFromKubeadmConfigMap(&cm)
if err == nil {
// Success to get ClusterCIDR from kubeadm-config
logger.Sugar().Infof("Success get CIDR from kubeadm-config: PodCIDR=%v, ServiceCIDR=%v", k8sPodCIDR, k8sServiceCIDR)
Expand Down Expand Up @@ -507,7 +506,7 @@ func (cc *CoordinatorController) updatePodAndServerCIDR(ctx context.Context, log
return coordCopy
}

k8sPodCIDR, k8sServiceCIDR = ExtractK8sCIDRFromKCMPod(&podList.Items[0])
k8sPodCIDR, k8sServiceCIDR = utils.ExtractK8sCIDRFromKCMPod(&podList.Items[0])
logger.Sugar().Infof("kube-controller-manager k8sPodCIDR %v, k8sServiceCIDR %v", k8sPodCIDR, k8sServiceCIDR)
}

Expand Down Expand Up @@ -778,103 +777,6 @@ func (cc *CoordinatorController) updateServiceCIDR(logger *zap.Logger, coordCopy
return nil
}

func ExtractK8sCIDRFromKubeadmConfigMap(cm *corev1.ConfigMap) ([]string, []string, error) {
if cm == nil {
return nil, nil, fmt.Errorf("kubeadm configmap is unexpected to nil")
}
var podCIDR, serviceCIDR []string

clusterConfig, exists := cm.Data["ClusterConfiguration"]
if !exists {
return podCIDR, serviceCIDR, fmt.Errorf("unable to get kubeadm configmap ClusterConfiguration")
}

podReg := regexp.MustCompile(`podSubnet:\s*(\S+)`)
serviceReg := regexp.MustCompile(`serviceSubnet:\s*(\S+)`)

podSubnets := podReg.FindStringSubmatch(clusterConfig)
serviceSubnets := serviceReg.FindStringSubmatch(clusterConfig)

if len(podSubnets) > 1 {
for _, cidr := range strings.Split(podSubnets[1], ",") {
cidr = strings.TrimSpace(cidr)
_, _, err := net.ParseCIDR(cidr)
if err != nil {
continue
}
podCIDR = append(podCIDR, cidr)
}
}

if len(serviceSubnets) > 1 {
for _, cidr := range strings.Split(serviceSubnets[1], ",") {
cidr = strings.TrimSpace(cidr)
_, _, err := net.ParseCIDR(cidr)
if err != nil {
continue
}
serviceCIDR = append(serviceCIDR, cidr)
}
}

return podCIDR, serviceCIDR, nil
}

func ExtractK8sCIDRFromKCMPod(kcm *corev1.Pod) ([]string, []string) {
var podCIDR, serviceCIDR []string

podReg := regexp.MustCompile(`--cluster-cidr=(.*)`)
serviceReg := regexp.MustCompile(`--service-cluster-ip-range=(.*)`)

var podSubnets, serviceSubnets []string
findSubnets := func(l string) {
if len(podSubnets) == 0 {
podSubnets = podReg.FindStringSubmatch(l)
}
if len(serviceSubnets) == 0 {
serviceSubnets = serviceReg.FindStringSubmatch(l)
}
}

for _, l := range kcm.Spec.Containers[0].Command {
findSubnets(l)
if len(podSubnets) != 0 && len(serviceSubnets) != 0 {
break
}
}

if len(podSubnets) == 0 || len(serviceSubnets) == 0 {
for _, l := range kcm.Spec.Containers[0].Args {
findSubnets(l)
if len(podSubnets) != 0 && len(serviceSubnets) != 0 {
break
}
}
}

if len(podSubnets) != 0 {
for _, cidr := range strings.Split(podSubnets[1], ",") {
_, _, err := net.ParseCIDR(cidr)
if err != nil {
continue
}
podCIDR = append(podCIDR, cidr)
}
}

if len(serviceSubnets) != 0 {
for _, cidr := range strings.Split(serviceSubnets[1], ",") {
_, _, err := net.ParseCIDR(cidr)
if err != nil {
continue
}
serviceCIDR = append(serviceCIDR, cidr)
}
}

return podCIDR, serviceCIDR
}

func fetchType(cniDir string) (string, error) {
defaultCniName, err := utils.GetDefaultCniName(cniDir)
if err != nil {
Expand Down
100 changes: 100 additions & 0 deletions pkg/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@ package utils

import (
"fmt"
"net"
"regexp"
"sort"
"strings"

"github.com/containernetworking/cni/libcni"
corev1 "k8s.io/api/core/v1"
)

// GetDefaultCNIConfPath according to the provided CNI file path (default is /etc/cni/net.d),
Expand Down Expand Up @@ -68,3 +71,100 @@ func fetchCniNameFromPath(cniPath string) (string, error) {
}
return conf.Network.Name, nil
}

func ExtractK8sCIDRFromKubeadmConfigMap(cm *corev1.ConfigMap) ([]string, []string, error) {
if cm == nil {
return nil, nil, fmt.Errorf("kubeadm configmap is unexpected to nil")
}
var podCIDR, serviceCIDR []string

clusterConfig, exists := cm.Data["ClusterConfiguration"]
if !exists {
return podCIDR, serviceCIDR, fmt.Errorf("unable to get kubeadm configmap ClusterConfiguration")
}

Check warning on line 84 in pkg/utils/utils.go

View check run for this annotation

Codecov / codecov/patch

pkg/utils/utils.go#L75-L84

Added lines #L75 - L84 were not covered by tests

podReg := regexp.MustCompile(`podSubnet:\s*(\S+)`)
serviceReg := regexp.MustCompile(`serviceSubnet:\s*(\S+)`)

podSubnets := podReg.FindStringSubmatch(clusterConfig)
serviceSubnets := serviceReg.FindStringSubmatch(clusterConfig)

if len(podSubnets) > 1 {
for _, cidr := range strings.Split(podSubnets[1], ",") {
cidr = strings.TrimSpace(cidr)
_, _, err := net.ParseCIDR(cidr)
if err != nil {
continue

Check warning on line 97 in pkg/utils/utils.go

View check run for this annotation

Codecov / codecov/patch

pkg/utils/utils.go#L86-L97

Added lines #L86 - L97 were not covered by tests
}
podCIDR = append(podCIDR, cidr)

Check warning on line 99 in pkg/utils/utils.go

View check run for this annotation

Codecov / codecov/patch

pkg/utils/utils.go#L99

Added line #L99 was not covered by tests
}
}

if len(serviceSubnets) > 1 {
for _, cidr := range strings.Split(serviceSubnets[1], ",") {
cidr = strings.TrimSpace(cidr)
_, _, err := net.ParseCIDR(cidr)
if err != nil {
continue

Check warning on line 108 in pkg/utils/utils.go

View check run for this annotation

Codecov / codecov/patch

pkg/utils/utils.go#L103-L108

Added lines #L103 - L108 were not covered by tests
}
serviceCIDR = append(serviceCIDR, cidr)

Check warning on line 110 in pkg/utils/utils.go

View check run for this annotation

Codecov / codecov/patch

pkg/utils/utils.go#L110

Added line #L110 was not covered by tests
}
}

return podCIDR, serviceCIDR, nil

Check warning on line 114 in pkg/utils/utils.go

View check run for this annotation

Codecov / codecov/patch

pkg/utils/utils.go#L114

Added line #L114 was not covered by tests
}

func ExtractK8sCIDRFromKCMPod(kcm *corev1.Pod) ([]string, []string) {
var podCIDR, serviceCIDR []string

podReg := regexp.MustCompile(`--cluster-cidr=(.*)`)
serviceReg := regexp.MustCompile(`--service-cluster-ip-range=(.*)`)

var podSubnets, serviceSubnets []string
findSubnets := func(l string) {
if len(podSubnets) == 0 {
podSubnets = podReg.FindStringSubmatch(l)
}
if len(serviceSubnets) == 0 {
serviceSubnets = serviceReg.FindStringSubmatch(l)
}

Check warning on line 130 in pkg/utils/utils.go

View check run for this annotation

Codecov / codecov/patch

pkg/utils/utils.go#L117-L130

Added lines #L117 - L130 were not covered by tests
}

for _, l := range kcm.Spec.Containers[0].Command {
findSubnets(l)
if len(podSubnets) != 0 && len(serviceSubnets) != 0 {
break

Check warning on line 136 in pkg/utils/utils.go

View check run for this annotation

Codecov / codecov/patch

pkg/utils/utils.go#L133-L136

Added lines #L133 - L136 were not covered by tests
}
}

if len(podSubnets) == 0 || len(serviceSubnets) == 0 {
for _, l := range kcm.Spec.Containers[0].Args {
findSubnets(l)
if len(podSubnets) != 0 && len(serviceSubnets) != 0 {
break

Check warning on line 144 in pkg/utils/utils.go

View check run for this annotation

Codecov / codecov/patch

pkg/utils/utils.go#L140-L144

Added lines #L140 - L144 were not covered by tests
}
}
}

if len(podSubnets) != 0 {
for _, cidr := range strings.Split(podSubnets[1], ",") {
_, _, err := net.ParseCIDR(cidr)
if err != nil {
continue

Check warning on line 153 in pkg/utils/utils.go

View check run for this annotation

Codecov / codecov/patch

pkg/utils/utils.go#L149-L153

Added lines #L149 - L153 were not covered by tests
}
podCIDR = append(podCIDR, cidr)

Check warning on line 155 in pkg/utils/utils.go

View check run for this annotation

Codecov / codecov/patch

pkg/utils/utils.go#L155

Added line #L155 was not covered by tests
}
}

if len(serviceSubnets) != 0 {
for _, cidr := range strings.Split(serviceSubnets[1], ",") {
_, _, err := net.ParseCIDR(cidr)
if err != nil {
continue

Check warning on line 163 in pkg/utils/utils.go

View check run for this annotation

Codecov / codecov/patch

pkg/utils/utils.go#L159-L163

Added lines #L159 - L163 were not covered by tests
}
serviceCIDR = append(serviceCIDR, cidr)

Check warning on line 165 in pkg/utils/utils.go

View check run for this annotation

Codecov / codecov/patch

pkg/utils/utils.go#L165

Added line #L165 was not covered by tests
}
}

return podCIDR, serviceCIDR

Check warning on line 169 in pkg/utils/utils.go

View check run for this annotation

Codecov / codecov/patch

pkg/utils/utils.go#L169

Added line #L169 was not covered by tests
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Copyright 2022 Authors of spidernet-io
// Copyright 2024 Authors of spidernet-io
// SPDX-License-Identifier: Apache-2.0

package coordinatormanager
package utils

import (
"testing"
Expand All @@ -16,9 +15,9 @@ var (
noCIDRJson string
)

func TestCoordinatorManager(t *testing.T) {
func TestUtils(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "CoordinatorManager Suite")
RunSpecs(t, "Utils Suite")
}

var _ = BeforeSuite(func() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
// Copyright 2022 Authors of spidernet-io
// Copyright 2024 Authors of spidernet-io
// SPDX-License-Identifier: Apache-2.0

package coordinatormanager
package utils

import (
"encoding/json"
"os"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
corev1 "k8s.io/api/core/v1"
)

var _ = Describe("Coordinator Manager", Label("coordinatorinformer", "informer_test"), Serial, func() {
var _ = Describe("Utils", Label("utils"), Serial, func() {
DescribeTable("should extract CIDRs correctly",
func(testName, cmStr string, expectedPodCIDR, expectedServiceCIDR []string, expectError bool) {
var cm corev1.ConfigMap
Expand Down Expand Up @@ -51,4 +53,45 @@ var _ = Describe("Coordinator Manager", Label("coordinatorinformer", "informer_t
),
)

Describe("GetDefaultCniName", func() {
var tempCniDir string
BeforeEach(func() {
tempCniDir = "/tmp/etc/cni/net.d"

tempDir, err := os.MkdirTemp(tempCniDir, "test-")
Expect(err).NotTo(HaveOccurred())

tempFilePath := tempDir + "/10-calico.conflist"
err = os.WriteFile(tempFilePath, []byte(`{
"name": "calico",
"cniVersion": "0.4.0",
"plugins": [
{
"type": "calico",
"etcd_endpoints": "http://127.0.0.1:2379",
"log_level": "info",
"ipam": {
"type": "calico-ipam"
},
"policy": {
"type": "k8s"
}
}
]
}`), 0644)
Expect(err).NotTo(HaveOccurred())

DeferCleanup(func() {
Expect(os.RemoveAll(tempDir)).NotTo(HaveOccurred())
})
})

It("Test GetDefaultCniName", func() {
cniName, err := GetDefaultCniName(tempCniDir)
Expect(err).NotTo(HaveOccurred())

Expect(cniName).To(Equal("calico"))
})
})

})
5 changes: 3 additions & 2 deletions test/e2e/spidercoordinator/spidercoordinator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"github.com/spidernet-io/spiderpool/pkg/coordinatormanager"
"github.com/spidernet-io/spiderpool/pkg/ip"
spiderpoolv2beta1 "github.com/spidernet-io/spiderpool/pkg/k8s/apis/spiderpool.spidernet.io/v2beta1"
"github.com/spidernet-io/spiderpool/pkg/utils"
"github.com/spidernet-io/spiderpool/test/e2e/common"
)

Expand Down Expand Up @@ -464,7 +465,7 @@ var _ = Describe("SpiderCoordinator", Label("spidercoordinator", "overlay"), Ser
It("Prioritize getting ClusterCIDR from kubeadm-config", Label("V00009"), func() {
GinkgoWriter.Printf("podCIDR and serviceCIDR from spidercoordinator: %v,%v\n", spc.Status.OverlayPodCIDR, spc.Status.ServiceCIDR)

podCIDR, serviceCIDr, err := coordinatormanager.ExtractK8sCIDRFromKubeadmConfigMap(cm)
podCIDR, serviceCIDr, err := utils.ExtractK8sCIDRFromKubeadmConfigMap(cm)
Expect(err).NotTo(HaveOccurred(), "Failed to extract k8s CIDR from Kubeadm configMap, error is %v", err)
GinkgoWriter.Printf("podCIDR and serviceCIDR from kubeadm-config : %v,%v\n", podCIDR, serviceCIDr)

Expand Down Expand Up @@ -500,7 +501,7 @@ var _ = Describe("SpiderCoordinator", Label("spidercoordinator", "overlay"), Ser
allPods, err := frame.GetPodList(client.MatchingLabels{"component": "kube-controller-manager"})
Expect(err).NotTo(HaveOccurred())

kcmPodCIDR, kcmServiceCIDR := coordinatormanager.ExtractK8sCIDRFromKCMPod(&allPods.Items[0])
kcmPodCIDR, kcmServiceCIDR := utils.ExtractK8sCIDRFromKCMPod(&allPods.Items[0])
GinkgoWriter.Printf("podCIDR and serviceCIDR from kube-controller-manager pod : %v,%v\n", kcmPodCIDR, kcmServiceCIDR)

Eventually(func() bool {
Expand Down

0 comments on commit c59b57c

Please sign in to comment.