From b63889f2fbc7833eb2dd85f92a5ec10cdcd5c633 Mon Sep 17 00:00:00 2001 From: Mitali Paygude Date: Wed, 21 Feb 2024 11:29:04 -0800 Subject: [PATCH] Override pod and service subnet for kind cluster (#7654) --- pkg/executables/config/kind.yaml | 3 ++ pkg/executables/kind.go | 13 ++++++++ pkg/executables/kind_test.go | 32 +++++++++++++++++++ pkg/executables/testdata/kind_config.yaml | 3 ++ .../kind_config_docker_mount_networking.yaml | 3 ++ .../kind_config_extra_port_mappings.yaml | 3 ++ .../kind_config_registry_mirror_insecure.yaml | 3 ++ ...kind_config_registry_mirror_with_auth.yaml | 3 ++ .../kind_config_registry_mirror_with_ca.yaml | 3 ++ 9 files changed, 66 insertions(+) diff --git a/pkg/executables/config/kind.yaml b/pkg/executables/config/kind.yaml index fe5343a4c79b..4fcebd2083ba 100644 --- a/pkg/executables/config/kind.yaml +++ b/pkg/executables/config/kind.yaml @@ -1,5 +1,8 @@ kind: Cluster apiVersion: kind.x-k8s.io/v1alpha4 +networking: + podSubnet: {{ .PodSubnet }} + serviceSubnet: {{ .ServiceSubnet }} kubeadmConfigPatches: - | kind: ClusterConfiguration diff --git a/pkg/executables/kind.go b/pkg/executables/kind.go index e4726a9a1938..ebe2edf75ecd 100644 --- a/pkg/executables/kind.go +++ b/pkg/executables/kind.go @@ -55,6 +55,8 @@ type kindExecConfig struct { ExtraPortMappings []int DockerExtraMounts bool DisableDefaultCNI bool + PodSubnet string + ServiceSubnet string } func NewKind(executable Executable, writer filewriter.FileWriter) *Kind { @@ -76,6 +78,17 @@ func (k *Kind) CreateBootstrapCluster(ctx context.Context, clusterSpec *cluster. return "", err } + serviceCidrs := clusterSpec.Cluster.Spec.ClusterNetwork.Services.CidrBlocks + podCidrs := clusterSpec.Cluster.Spec.ClusterNetwork.Pods.CidrBlocks + + if len(serviceCidrs) != 0 { + k.execConfig.ServiceSubnet = serviceCidrs[0] + } + + if len(podCidrs) != 0 { + k.execConfig.PodSubnet = podCidrs[0] + } + err = k.buildConfigFile() if err != nil { return "", err diff --git a/pkg/executables/kind_test.go b/pkg/executables/kind_test.go index 8e2fd83b7b14..1aa75c46fa9d 100644 --- a/pkg/executables/kind_test.go +++ b/pkg/executables/kind_test.go @@ -32,6 +32,14 @@ func TestKindCreateBootstrapClusterSuccess(t *testing.T) { clusterSpec := test.NewClusterSpec(func(s *cluster.Spec) { s.Cluster.Name = clusterName s.VersionsBundles["1.19"] = versionBundle + s.Cluster.Spec.ClusterNetwork = v1alpha1.ClusterNetwork{ + Pods: v1alpha1.Pods{ + CidrBlocks: []string{"1.1.1.1"}, + }, + Services: v1alpha1.Services{ + CidrBlocks: []string{"2.2.2.2"}, + }, + } }) eksClusterName := "test_cluster-eks-a-cluster" kubeConfigFile := "test_cluster.kind.kubeconfig" @@ -176,6 +184,14 @@ func TestKindCreateBootstrapClusterSuccessWithRegistryMirror(t *testing.T) { }, }, } + s.Cluster.Spec.ClusterNetwork = v1alpha1.ClusterNetwork{ + Pods: v1alpha1.Pods{ + CidrBlocks: []string{"1.1.1.1"}, + }, + Services: v1alpha1.Services{ + CidrBlocks: []string{"2.2.2.2"}, + }, + } }), env: map[string]string{}, wantKindConfig: "testdata/kind_config_registry_mirror_insecure.yaml", @@ -191,6 +207,14 @@ func TestKindCreateBootstrapClusterSuccessWithRegistryMirror(t *testing.T) { Port: constants.DefaultHttpsPort, CACertContent: "test", } + s.Cluster.Spec.ClusterNetwork = v1alpha1.ClusterNetwork{ + Pods: v1alpha1.Pods{ + CidrBlocks: []string{"1.1.1.1"}, + }, + Services: v1alpha1.Services{ + CidrBlocks: []string{"2.2.2.2"}, + }, + } }), env: map[string]string{}, wantKindConfig: "testdata/kind_config_registry_mirror_with_ca.yaml", @@ -216,6 +240,14 @@ func TestKindCreateBootstrapClusterSuccessWithRegistryMirror(t *testing.T) { }, Authenticate: true, } + s.Cluster.Spec.ClusterNetwork = v1alpha1.ClusterNetwork{ + Pods: v1alpha1.Pods{ + CidrBlocks: []string{"1.1.1.1"}, + }, + Services: v1alpha1.Services{ + CidrBlocks: []string{"2.2.2.2"}, + }, + } }), env: map[string]string{}, wantKindConfig: "testdata/kind_config_registry_mirror_with_auth.yaml", diff --git a/pkg/executables/testdata/kind_config.yaml b/pkg/executables/testdata/kind_config.yaml index 39c571127213..9a400a9c66cc 100644 --- a/pkg/executables/testdata/kind_config.yaml +++ b/pkg/executables/testdata/kind_config.yaml @@ -1,5 +1,8 @@ kind: Cluster apiVersion: kind.x-k8s.io/v1alpha4 +networking: + podSubnet: 1.1.1.1 + serviceSubnet: 2.2.2.2 kubeadmConfigPatches: - | kind: ClusterConfiguration diff --git a/pkg/executables/testdata/kind_config_docker_mount_networking.yaml b/pkg/executables/testdata/kind_config_docker_mount_networking.yaml index 04b64b22b444..c4f5b600b026 100644 --- a/pkg/executables/testdata/kind_config_docker_mount_networking.yaml +++ b/pkg/executables/testdata/kind_config_docker_mount_networking.yaml @@ -1,5 +1,8 @@ kind: Cluster apiVersion: kind.x-k8s.io/v1alpha4 +networking: + podSubnet: 1.1.1.1 + serviceSubnet: 2.2.2.2 kubeadmConfigPatches: - | kind: ClusterConfiguration diff --git a/pkg/executables/testdata/kind_config_extra_port_mappings.yaml b/pkg/executables/testdata/kind_config_extra_port_mappings.yaml index 4416f51d102d..4fb0932db63a 100644 --- a/pkg/executables/testdata/kind_config_extra_port_mappings.yaml +++ b/pkg/executables/testdata/kind_config_extra_port_mappings.yaml @@ -1,5 +1,8 @@ kind: Cluster apiVersion: kind.x-k8s.io/v1alpha4 +networking: + podSubnet: 1.1.1.1 + serviceSubnet: 2.2.2.2 kubeadmConfigPatches: - | kind: ClusterConfiguration diff --git a/pkg/executables/testdata/kind_config_registry_mirror_insecure.yaml b/pkg/executables/testdata/kind_config_registry_mirror_insecure.yaml index f04f2ebcdb84..658f15c07ed6 100644 --- a/pkg/executables/testdata/kind_config_registry_mirror_insecure.yaml +++ b/pkg/executables/testdata/kind_config_registry_mirror_insecure.yaml @@ -1,5 +1,8 @@ kind: Cluster apiVersion: kind.x-k8s.io/v1alpha4 +networking: + podSubnet: 1.1.1.1 + serviceSubnet: 2.2.2.2 kubeadmConfigPatches: - | kind: ClusterConfiguration diff --git a/pkg/executables/testdata/kind_config_registry_mirror_with_auth.yaml b/pkg/executables/testdata/kind_config_registry_mirror_with_auth.yaml index 20a30b00d4d6..c732d38a7fc3 100644 --- a/pkg/executables/testdata/kind_config_registry_mirror_with_auth.yaml +++ b/pkg/executables/testdata/kind_config_registry_mirror_with_auth.yaml @@ -1,5 +1,8 @@ kind: Cluster apiVersion: kind.x-k8s.io/v1alpha4 +networking: + podSubnet: 1.1.1.1 + serviceSubnet: 2.2.2.2 kubeadmConfigPatches: - | kind: ClusterConfiguration diff --git a/pkg/executables/testdata/kind_config_registry_mirror_with_ca.yaml b/pkg/executables/testdata/kind_config_registry_mirror_with_ca.yaml index 280c8a870ebf..d58d2b5c1f6f 100644 --- a/pkg/executables/testdata/kind_config_registry_mirror_with_ca.yaml +++ b/pkg/executables/testdata/kind_config_registry_mirror_with_ca.yaml @@ -1,5 +1,8 @@ kind: Cluster apiVersion: kind.x-k8s.io/v1alpha4 +networking: + podSubnet: 1.1.1.1 + serviceSubnet: 2.2.2.2 kubeadmConfigPatches: - | kind: ClusterConfiguration