Skip to content

Commit

Permalink
feat: format ipv6 address for service-cluster-ip-range
Browse files Browse the repository at this point in the history
Signed-off-by: baoyinghai_yewu <[email protected]>
  • Loading branch information
OrangeBao committed Sep 2, 2024
1 parent 68d7df8 commit 0d4b1c6
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 0 deletions.
10 changes: 10 additions & 0 deletions pkg/clusterlink/controllers/cluster/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/base64"
"encoding/json"
"fmt"
"net"
"strings"

"github.com/projectcalico/calico/libcalico-go/lib/apiconfig"
Expand Down Expand Up @@ -128,5 +129,14 @@ func ResolveServiceCIDRs(pod *corev1.Pod) ([]string, error) {
}
}
}

for i, cidr := range serviceCIDRS {
_, ipNet, err := net.ParseCIDR(cidr)
if err != nil {
return nil, fmt.Errorf("failed to parse service cidr %s, pod name is %s, err: %s", cidr, pod.Name, err.Error())
}
serviceCIDRS[i] = ipNet.String()
}

return serviceCIDRS, nil
}
79 changes: 79 additions & 0 deletions pkg/clusterlink/controllers/cluster/helper_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package cluster

import (
"fmt"
"strings"
"testing"

corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

func prepareData(crds string) *corev1.Pod {
return &corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: "test-pod",
Namespace: "test-namespace",
Labels: map[string]string{
"app": "test-app",
},
},
Spec: corev1.PodSpec{
Containers: []corev1.Container{
{
Name: "test-container",
Image: "test-image",
Command: []string{
"kube-apiserver",
fmt.Sprintf("--service-cluster-ip-range=%s", crds),
"--profiling=false",
},
},
},
},
}
}

func TestResolveServiceCIDRs(t *testing.T) {
tests := []struct {
name string
input *corev1.Pod
want []string
}{
{
name: "test ipv4 and ipv6",
input: prepareData("2409:8c2f:3800:0011::0a18:0000/114,10.237.6.0/18"),
want: []string{
"2409:8c2f:3800:11::a18:0/114",
"10.237.0.0/18",
},
},
{
name: "test ipv4",
input: prepareData("10.237.6.0/18"),
want: []string{
"10.237.0.0/18",
},
},
{
name: "test ipv6",
input: prepareData("2409:8c2f:3800:0011::0a18:0000/114"),
want: []string{
"2409:8c2f:3800:11::a18:0/114",
},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ret, err := ResolveServiceCIDRs(tt.input)
if err != nil {
t.Fatalf("ResolveServiceCIDRs err: %s", err.Error())
}

if strings.Join(ret, ",") != strings.Join(tt.want, ",") {
t.Fatalf("value is incorretc!")
}
})
}
}

0 comments on commit 0d4b1c6

Please sign in to comment.