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 556f33e
Show file tree
Hide file tree
Showing 7 changed files with 324 additions and 217 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
54 changes: 0 additions & 54 deletions pkg/coordinatormanager/coordinator_informer_test.go

This file was deleted.

61 changes: 0 additions & 61 deletions pkg/coordinatormanager/coordinatormanager_suite_test.go

This file was deleted.

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
}
15 changes: 15 additions & 0 deletions pkg/utils/utils_suite_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Copyright 2024 Authors of spidernet-io
// SPDX-License-Identifier: Apache-2.0
package utils

import (
"testing"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)

func TestUtils(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Utils Suite", Label("utils", "unittest"))
}
Loading

0 comments on commit 556f33e

Please sign in to comment.