de-dupe clusterHasIPv6 and clusterHasImplicitLoadBalancer in providers

This commit is contained in:
Benjamin Elder
2021-11-23 14:21:03 -08:00
parent 15cade4b1a
commit 4a18b42d96
4 changed files with 176 additions and 34 deletions

View File

@@ -44,7 +44,7 @@ func planCreation(cfg *config.Cluster, networkName string) (createContainerFuncs
name := nodeNamer(string(node.Role)) // name the node
names[i] = name
}
haveLoadbalancer := clusterHasImplicitLoadBalancer(cfg)
haveLoadbalancer := config.ClusterHasImplicitLoadBalancer(cfg)
if haveLoadbalancer {
names = append(names, nodeNamer(constants.ExternalLoadBalancerNodeRoleValue))
}
@@ -129,21 +129,6 @@ func planCreation(cfg *config.Cluster, networkName string) (createContainerFuncs
return createContainerFuncs, nil
}
func clusterIsIPv6(cfg *config.Cluster) bool {
return cfg.Networking.IPFamily == config.IPv6Family || cfg.Networking.IPFamily == config.DualStackFamily
}
func clusterHasImplicitLoadBalancer(cfg *config.Cluster) bool {
controlPlanes := 0
for _, configNode := range cfg.Nodes {
role := string(configNode.Role)
if role == constants.ControlPlaneNodeRoleValue {
controlPlanes++
}
}
return controlPlanes > 1
}
// commonArgs computes static arguments that apply to all containers
func commonArgs(cluster string, cfg *config.Cluster, networkName string, nodeNames []string) ([]string, error) {
// standard arguments all nodes containers need, computed once
@@ -185,7 +170,7 @@ func commonArgs(cluster string, cfg *config.Cluster, networkName string, nodeNam
}
// enable IPv6 if necessary
if clusterIsIPv6(cfg) {
if config.ClusterHasIPv6(cfg) {
args = append(args, "--sysctl=net.ipv6.conf.all.disable_ipv6=0", "--sysctl=net.ipv6.conf.all.forwarding=1")
}

View File

@@ -45,7 +45,7 @@ func planCreation(cfg *config.Cluster, networkName string) (createContainerFuncs
// only the external LB should reflect the port if we have multiple control planes
apiServerPort := cfg.Networking.APIServerPort
apiServerAddress := cfg.Networking.APIServerAddress
if clusterHasImplicitLoadBalancer(cfg) {
if config.ClusterHasImplicitLoadBalancer(cfg) {
// TODO: picking ports locally is less than ideal with a remote runtime
// (does podman have this?)
// but this is supposed to be an implementation detail and NOT picking
@@ -115,21 +115,6 @@ func planCreation(cfg *config.Cluster, networkName string) (createContainerFuncs
return createContainerFuncs, nil
}
func clusterIsIPv6(cfg *config.Cluster) bool {
return cfg.Networking.IPFamily == config.IPv6Family || cfg.Networking.IPFamily == config.DualStackFamily
}
func clusterHasImplicitLoadBalancer(cfg *config.Cluster) bool {
controlPlanes := 0
for _, configNode := range cfg.Nodes {
role := string(configNode.Role)
if role == constants.ControlPlaneNodeRoleValue {
controlPlanes++
}
}
return controlPlanes > 1
}
// commonArgs computes static arguments that apply to all containers
func commonArgs(cfg *config.Cluster, networkName string) ([]string, error) {
// standard arguments all nodes containers need, computed once
@@ -144,7 +129,7 @@ func commonArgs(cfg *config.Cluster, networkName string) ([]string, error) {
}
// enable IPv6 if necessary
if clusterIsIPv6(cfg) {
if config.ClusterHasIPv6(cfg) {
args = append(args, "--sysctl=net.ipv6.conf.all.disable_ipv6=0", "--sysctl=net.ipv6.conf.all.forwarding=1")
}

View File

@@ -0,0 +1,34 @@
/*
Copyright 2021 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package config
// ClusterHasIPv6 returns true if the cluster should have IPv6 enabled due to either
// being IPv6 cluster family or Dual Stack
func ClusterHasIPv6(c *Cluster) bool {
return c.Networking.IPFamily == IPv6Family || c.Networking.IPFamily == DualStackFamily
}
// ClusterHasImplicitLoadBalancer returns true if this cluster has an implicit api-server LoadBalancer
func ClusterHasImplicitLoadBalancer(c *Cluster) bool {
controlPlanes := 0
for _, node := range c.Nodes {
if node.Role == ControlPlaneRole {
controlPlanes++
}
}
return controlPlanes > 1
}

View File

@@ -0,0 +1,138 @@
/*
Copyright 2021 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package config
import (
"testing"
"sigs.k8s.io/kind/pkg/internal/assert"
)
func TestClusterHasIPv6(t *testing.T) {
cases := []struct {
Name string
c *Cluster
expected bool
}{
{
Name: "IPv6",
c: &Cluster{
Networking: Networking{
IPFamily: IPv6Family,
},
},
expected: true,
},
{
Name: "IPv4",
c: &Cluster{
Networking: Networking{
IPFamily: IPv4Family,
},
},
expected: false,
},
{
Name: "DualStack",
c: &Cluster{
Networking: Networking{
IPFamily: DualStackFamily,
},
},
expected: true,
},
}
for _, tc := range cases {
tc := tc // capture loop var
t.Run(tc.Name, func(t *testing.T) {
r := ClusterHasIPv6(tc.c)
assert.BoolEqual(t, tc.expected, r)
})
}
}
func TestClusterHasImplicitLoadBalancer(t *testing.T) {
cases := []struct {
Name string
c *Cluster
expected bool
}{
{
Name: "One Node",
c: &Cluster{
Nodes: []Node{
{Role: ControlPlaneRole},
},
},
expected: false,
},
{
Name: "Two Control Planes",
c: &Cluster{
Nodes: []Node{
{Role: ControlPlaneRole},
{Role: ControlPlaneRole},
},
},
expected: true,
},
{
Name: "Three Control Planes",
c: &Cluster{
Nodes: []Node{
{Role: ControlPlaneRole},
{Role: ControlPlaneRole},
{Role: ControlPlaneRole},
},
},
expected: true,
},
{
Name: "One Control Plane, Multiple Workers",
c: &Cluster{
Nodes: []Node{
{Role: ControlPlaneRole},
{Role: WorkerRole},
{Role: WorkerRole},
{Role: WorkerRole},
},
},
expected: false,
},
{
Name: "Multiple Control Planes, Multiple Workers",
c: &Cluster{
Nodes: []Node{
{Role: ControlPlaneRole},
{Role: ControlPlaneRole},
{Role: ControlPlaneRole},
{Role: WorkerRole},
{Role: WorkerRole},
{Role: WorkerRole},
},
},
expected: true,
},
}
for _, tc := range cases {
tc := tc // capture loop var
t.Run(tc.Name, func(t *testing.T) {
r := ClusterHasImplicitLoadBalancer(tc.c)
assert.BoolEqual(t, tc.expected, r)
})
}
}