forked from kosmos-io/kosmos
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request kosmos-io#699 from OrangeBao/main
feat: format ipv6 address for service-cluster-ip-range
- Loading branch information
Showing
6 changed files
with
225 additions
and
2 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
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,104 @@ | ||
package agent | ||
|
||
import ( | ||
"testing" | ||
|
||
kosmosv1alpha1 "github.com/kosmos.io/kosmos/pkg/apis/kosmos/v1alpha1" | ||
) | ||
|
||
func TestFormatNodeConfig(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
input *kosmosv1alpha1.NodeConfig | ||
want *kosmosv1alpha1.NodeConfig | ||
}{ | ||
{ | ||
name: "test ipv4 and ipv6", | ||
input: &kosmosv1alpha1.NodeConfig{ | ||
Spec: kosmosv1alpha1.NodeConfigSpec{ | ||
Routes: []kosmosv1alpha1.Route{ | ||
{ | ||
CIDR: "2409:8c2f:3800:0011::0a18:0000/114", | ||
}, | ||
{ | ||
CIDR: "10.237.6.0/18", | ||
}, | ||
}, | ||
}, | ||
}, | ||
want: &kosmosv1alpha1.NodeConfig{ | ||
Spec: kosmosv1alpha1.NodeConfigSpec{ | ||
Routes: []kosmosv1alpha1.Route{ | ||
{ | ||
CIDR: "2409:8c2f:3800:11::a18:0/114", | ||
}, | ||
{ | ||
CIDR: "10.237.0.0/18", | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "test ipv6", | ||
input: &kosmosv1alpha1.NodeConfig{ | ||
Spec: kosmosv1alpha1.NodeConfigSpec{ | ||
Routes: []kosmosv1alpha1.Route{ | ||
{ | ||
CIDR: "2409:8c2f:3800:0011::0a18:0000/114", | ||
}, | ||
}, | ||
}, | ||
}, | ||
want: &kosmosv1alpha1.NodeConfig{ | ||
Spec: kosmosv1alpha1.NodeConfigSpec{ | ||
Routes: []kosmosv1alpha1.Route{ | ||
{ | ||
CIDR: "2409:8c2f:3800:11::a18:0/114", | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "test ipv4", | ||
input: &kosmosv1alpha1.NodeConfig{ | ||
Spec: kosmosv1alpha1.NodeConfigSpec{ | ||
Routes: []kosmosv1alpha1.Route{ | ||
{ | ||
CIDR: "10.237.6.0/18", | ||
}, | ||
}, | ||
}, | ||
}, | ||
want: &kosmosv1alpha1.NodeConfig{ | ||
Spec: kosmosv1alpha1.NodeConfigSpec{ | ||
Routes: []kosmosv1alpha1.Route{ | ||
{ | ||
CIDR: "10.237.0.0/18", | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
nodeconfig, err := formatNodeConfig(tt.input) | ||
|
||
if err != nil { | ||
t.Errorf("formatNodeConfig() error = %v", err) | ||
} | ||
|
||
if len(nodeconfig.Spec.Routes) != len(tt.want.Spec.Routes) { | ||
t.Errorf("formatNodeConfig() = %v, want %v", nodeconfig.Spec.Routes, tt.want.Spec.Routes) | ||
} | ||
|
||
for i := range nodeconfig.Spec.Routes { | ||
if nodeconfig.Spec.Routes[i].CIDR != tt.want.Spec.Routes[i].CIDR { | ||
t.Errorf("formatNodeConfig() = %v, want %v", nodeconfig.Spec.Routes[i].CIDR, tt.want.Spec.Routes[i].CIDR) | ||
} | ||
} | ||
}) | ||
} | ||
} |
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,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!") | ||
} | ||
}) | ||
} | ||
} |
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