Skip to content

Commit

Permalink
Merge pull request kosmos-io#662 from OrangeBao/release-0.4.0
Browse files Browse the repository at this point in the history
Cherry-Pick: Support automatic configuration of the IP family for API server…
  • Loading branch information
duanmengkk authored Aug 15, 2024
2 parents d84e895 + 5e46d4f commit 91471b0
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 0 deletions.
4 changes: 4 additions & 0 deletions pkg/kubenest/controlplane/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/kosmos.io/kosmos/pkg/kubenest/manifest/controlplane/apiserver"
"github.com/kosmos.io/kosmos/pkg/kubenest/manifest/controlplane/etcd"
"github.com/kosmos.io/kosmos/pkg/kubenest/util"
"github.com/kosmos.io/kosmos/pkg/utils"
)

func EnsureVirtualClusterService(client clientset.Interface, name, namespace string, portMap map[string]int32) error {
Expand Down Expand Up @@ -47,14 +48,17 @@ func DeleteVirtualClusterService(client clientset.Interface, name, namespace str
}

func createServerService(client clientset.Interface, name, namespace string, portMap map[string]int32) error {
ipFamilies := utils.IPFamilyGenerator(constants.ApiServerServiceSubnet)
apiserverServiceBytes, err := util.ParseTemplate(apiserver.ApiserverService, struct {
ServiceName, Namespace, ServiceType string
ServicePort int32
IPFamilies []corev1.IPFamily
}{
ServiceName: fmt.Sprintf("%s-%s", name, "apiserver"),
Namespace: namespace,
ServiceType: constants.ApiServerServiceType,
ServicePort: portMap[constants.ApiServerPortKey],
IPFamilies: ipFamilies,
})
if err != nil {
return fmt.Errorf("error when parsing virtualClusterApiserver serive template: %w", err)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ metadata:
name: {{ .ServiceName }}
namespace: {{ .Namespace }}
spec:
ipFamilies:
{{- range .IPFamilies }}
- {{ . }}
{{- end }}
ports:
- name: client
port: {{ .ServicePort }}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package apiserver

import (
"fmt"
"testing"

corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/util/yaml"

"github.com/kosmos.io/kosmos/pkg/kubenest/constants"
"github.com/kosmos.io/kosmos/pkg/kubenest/util"
"github.com/kosmos.io/kosmos/pkg/utils"
)

func ParseServerTemplate(apiServerServiceSubnet string) (*corev1.Service, error) {
ipFamilies := utils.IPFamilyGenerator(apiServerServiceSubnet)
apiserverServiceBytes, err := util.ParseTemplate(ApiserverService, struct {
ServiceName, Namespace, ServiceType string
ServicePort int32
IPFamilies []corev1.IPFamily
}{
ServiceName: fmt.Sprintf("%s-%s", "test", "apiserver"),
Namespace: "test-namespace",
ServiceType: constants.ApiServerServiceType,
ServicePort: 40010,
IPFamilies: ipFamilies,
})

if err != nil {
return nil, fmt.Errorf("error when parsing virtualClusterApiserver serive template: %s", err)
}

apiserverService := &corev1.Service{}
if err := yaml.Unmarshal([]byte(apiserverServiceBytes), apiserverService); err != nil {
return nil, fmt.Errorf("error when decoding virtual cluster apiserver service: %s", err)
}
return apiserverService, nil
}

func CompareIPFamilies(a []corev1.IPFamily, b []corev1.IPFamily) bool {
if len(a) != len(b) {
return false
}
for i, v := range a {
if v != b[i] {
return false
}
}
return true
}

func TestSyncIPPool(t *testing.T) {
tests := []struct {
name string
input string
want []corev1.IPFamily
}{
{
name: "ipv4 only",
input: "10.237.6.0/18",
want: []corev1.IPFamily{corev1.IPv4Protocol},
},
{
name: "ipv6 only",
input: "2409:8c2f:3800:0011::0a18:0000/114",
want: []corev1.IPFamily{corev1.IPv6Protocol},
},
{
name: "ipv4 first",
input: "10.237.6.0/18,2409:8c2f:3800:0011::0a18:0000/114",
want: []corev1.IPFamily{corev1.IPv4Protocol, corev1.IPv6Protocol},
},
{
name: "ipv6 first",
input: "2409:8c2f:3800:0011::0a18:0000/114,10.237.6.0/18",
want: []corev1.IPFamily{corev1.IPv6Protocol, corev1.IPv4Protocol},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
svc, err := ParseServerTemplate(tt.input)
if err != nil {
t.Fatalf("happen error: %s", err)
}

if !CompareIPFamilies(svc.Spec.IPFamilies, tt.want) {
t.Errorf("ParseServerTemplate()=%v, want %v", svc.Spec.IPFamilies, tt.want)
}
})
}
}
15 changes: 15 additions & 0 deletions pkg/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"fmt"
"os"
"strings"

corev1 "k8s.io/api/core/v1"
)

func ContainsString(arr []string, s string) bool {
Expand Down Expand Up @@ -42,3 +44,16 @@ func GenerateAddrStr(addr string, port string) string {
}
return fmt.Sprintf("%s:%s", addr, port)
}

func IPFamilyGenerator(apiServerServiceSubnet string) []corev1.IPFamily {
ipNetStrArray := strings.Split(apiServerServiceSubnet, ",")
ipFamilies := []corev1.IPFamily{}
for _, ipstr := range ipNetStrArray {
if IsIPv6(ipstr) {
ipFamilies = append(ipFamilies, corev1.IPv6Protocol)
} else {
ipFamilies = append(ipFamilies, corev1.IPv4Protocol)
}
}
return ipFamilies
}

0 comments on commit 91471b0

Please sign in to comment.