Unverified Commit f1feecb5 authored by Kubernetes Submit Queue's avatar Kubernetes Submit Queue Committed by GitHub

Merge pull request #67830 from fabriziopandini/kubeadm-config-ControlPlaneEndpoint

Automatic merge from submit-queue (batch tested with PRs 67776, 67503, 67679, 67786, 67830). If you want to cherry-pick this change to another branch, please follow the instructions <a href="https://github.com/kubernetes/community/blob/master/contributors/devel/cherry-picks.md">here</a>. kubeadm config move ControlPlaneEndpoint to ClusterConfiguration **What this PR does / why we need it**: This PR moves `ControlPlaneEndpoint` from the `API` config struct to `ClusterConfiguration`. This change is required as initial step for enabling management of more than one control plane instances in kubeadm **Which issue(s) this PR fixes** : refs https://github.com/kubernetes/kubeadm/issues/911, refs https://github.com/kubernetes/kubeadm/issues/963 **Special notes for your reviewer**: just an appetizer, the main dish will be the next PR... **Release note**: ```release-note kubeadm: ControlPlaneEndpoint was moved from the API config struct to ClusterConfiguration ``` /cc @kubernetes/sig-cluster-lifecycle-pr-reviews /sig cluster-lifecycle /area kubeadm /kind api-change /kind enhancement /assign @luxas /assign @timothysc /cc @chuckha @rosti @neolit123 @liztio
parents 078961f3 960ef7bf
...@@ -63,11 +63,25 @@ type ClusterConfiguration struct { ...@@ -63,11 +63,25 @@ type ClusterConfiguration struct {
API API API API
// Etcd holds configuration for etcd. // Etcd holds configuration for etcd.
Etcd Etcd Etcd Etcd
// Networking holds configuration for the networking topology of the cluster. // Networking holds configuration for the networking topology of the cluster.
Networking Networking Networking Networking
// KubernetesVersion is the target version of the control plane. // KubernetesVersion is the target version of the control plane.
KubernetesVersion string KubernetesVersion string
// ControlPlaneEndpoint sets a stable IP address or DNS name for the control plane; it
// can be a valid IP address or a RFC-1123 DNS subdomain, both with optional TCP port.
// In case the ControlPlaneEndpoint is not specified, the AdvertiseAddress + BindPort
// are used; in case the ControlPlaneEndpoint is specified but without a TCP port,
// the BindPort is used.
// Possible usages are:
// e.g. In an cluster with more than one control plane instances, this field should be
// assigned the address of the external load balancer in front of the
// control plane instances.
// e.g. in environments with enforced node recycling, the ControlPlaneEndpoint
// could be used for assigning a stable DNS to the control plane.
ControlPlaneEndpoint string
// APIServerExtraArgs is a set of extra flags to pass to the API Server or override // APIServerExtraArgs is a set of extra flags to pass to the API Server or override
// default ones in form of <flagname>=<value>. // default ones in form of <flagname>=<value>.
// TODO: This is temporary and ideally we would like to switch all components to // TODO: This is temporary and ideally we would like to switch all components to
...@@ -138,18 +152,7 @@ func (cc ComponentConfigs) Fuzz(c fuzz.Continue) {} ...@@ -138,18 +152,7 @@ func (cc ComponentConfigs) Fuzz(c fuzz.Continue) {}
type API struct { type API struct {
// AdvertiseAddress sets the IP address for the API server to advertise. // AdvertiseAddress sets the IP address for the API server to advertise.
AdvertiseAddress string AdvertiseAddress string
// ControlPlaneEndpoint sets a stable IP address or DNS name for the control plane; it
// can be a valid IP address or a RFC-1123 DNS subdomain, both with optional TCP port.
// In case the ControlPlaneEndpoint is not specified, the AdvertiseAddress + BindPort
// are used; in case the ControlPlaneEndpoint is specified but without a TCP port,
// the BindPort is used.
// Possible usages are:
// e.g. In an cluster with more than one control plane instances, this field should be
// assigned the address of the external load balancer in front of the
// control plane instances.
// e.g. in environments with enforced node recycling, the ControlPlaneEndpoint
// could be used for assigning a stable DNS to the control plane.
ControlPlaneEndpoint string
// BindPort sets the secure port for the API Server to bind to. // BindPort sets the secure port for the API Server to bind to.
// Defaults to 6443. // Defaults to 6443.
BindPort int32 BindPort int32
......
...@@ -157,11 +157,21 @@ func RegisterConversions(s *runtime.Scheme) error { ...@@ -157,11 +157,21 @@ func RegisterConversions(s *runtime.Scheme) error {
}); err != nil { }); err != nil {
return err return err
} }
if err := s.AddConversionFunc((*kubeadm.API)(nil), (*API)(nil), func(a, b interface{}, scope conversion.Scope) error {
return Convert_kubeadm_API_To_v1alpha2_API(a.(*kubeadm.API), b.(*API), scope)
}); err != nil {
return err
}
if err := s.AddConversionFunc((*kubeadm.InitConfiguration)(nil), (*InitConfiguration)(nil), func(a, b interface{}, scope conversion.Scope) error { if err := s.AddConversionFunc((*kubeadm.InitConfiguration)(nil), (*InitConfiguration)(nil), func(a, b interface{}, scope conversion.Scope) error {
return Convert_kubeadm_InitConfiguration_To_v1alpha2_InitConfiguration(a.(*kubeadm.InitConfiguration), b.(*InitConfiguration), scope) return Convert_kubeadm_InitConfiguration_To_v1alpha2_InitConfiguration(a.(*kubeadm.InitConfiguration), b.(*InitConfiguration), scope)
}); err != nil { }); err != nil {
return err return err
} }
if err := s.AddConversionFunc((*API)(nil), (*kubeadm.API)(nil), func(a, b interface{}, scope conversion.Scope) error {
return Convert_v1alpha2_API_To_kubeadm_API(a.(*API), b.(*kubeadm.API), scope)
}); err != nil {
return err
}
if err := s.AddConversionFunc((*InitConfiguration)(nil), (*kubeadm.InitConfiguration)(nil), func(a, b interface{}, scope conversion.Scope) error { if err := s.AddConversionFunc((*InitConfiguration)(nil), (*kubeadm.InitConfiguration)(nil), func(a, b interface{}, scope conversion.Scope) error {
return Convert_v1alpha2_InitConfiguration_To_kubeadm_InitConfiguration(a.(*InitConfiguration), b.(*kubeadm.InitConfiguration), scope) return Convert_v1alpha2_InitConfiguration_To_kubeadm_InitConfiguration(a.(*InitConfiguration), b.(*kubeadm.InitConfiguration), scope)
}); err != nil { }); err != nil {
...@@ -172,28 +182,17 @@ func RegisterConversions(s *runtime.Scheme) error { ...@@ -172,28 +182,17 @@ func RegisterConversions(s *runtime.Scheme) error {
func autoConvert_v1alpha2_API_To_kubeadm_API(in *API, out *kubeadm.API, s conversion.Scope) error { func autoConvert_v1alpha2_API_To_kubeadm_API(in *API, out *kubeadm.API, s conversion.Scope) error {
out.AdvertiseAddress = in.AdvertiseAddress out.AdvertiseAddress = in.AdvertiseAddress
out.ControlPlaneEndpoint = in.ControlPlaneEndpoint // WARNING: in.ControlPlaneEndpoint requires manual conversion: does not exist in peer-type
out.BindPort = in.BindPort out.BindPort = in.BindPort
return nil return nil
} }
// Convert_v1alpha2_API_To_kubeadm_API is an autogenerated conversion function.
func Convert_v1alpha2_API_To_kubeadm_API(in *API, out *kubeadm.API, s conversion.Scope) error {
return autoConvert_v1alpha2_API_To_kubeadm_API(in, out, s)
}
func autoConvert_kubeadm_API_To_v1alpha2_API(in *kubeadm.API, out *API, s conversion.Scope) error { func autoConvert_kubeadm_API_To_v1alpha2_API(in *kubeadm.API, out *API, s conversion.Scope) error {
out.AdvertiseAddress = in.AdvertiseAddress out.AdvertiseAddress = in.AdvertiseAddress
out.ControlPlaneEndpoint = in.ControlPlaneEndpoint
out.BindPort = in.BindPort out.BindPort = in.BindPort
return nil return nil
} }
// Convert_kubeadm_API_To_v1alpha2_API is an autogenerated conversion function.
func Convert_kubeadm_API_To_v1alpha2_API(in *kubeadm.API, out *API, s conversion.Scope) error {
return autoConvert_kubeadm_API_To_v1alpha2_API(in, out, s)
}
func autoConvert_v1alpha2_AuditPolicyConfiguration_To_kubeadm_AuditPolicyConfiguration(in *AuditPolicyConfiguration, out *kubeadm.AuditPolicyConfiguration, s conversion.Scope) error { func autoConvert_v1alpha2_AuditPolicyConfiguration_To_kubeadm_AuditPolicyConfiguration(in *AuditPolicyConfiguration, out *kubeadm.AuditPolicyConfiguration, s conversion.Scope) error {
out.Path = in.Path out.Path = in.Path
out.LogDir = in.LogDir out.LogDir = in.LogDir
......
...@@ -56,12 +56,26 @@ type ClusterConfiguration struct { ...@@ -56,12 +56,26 @@ type ClusterConfiguration struct {
API API `json:"api"` API API `json:"api"`
// Etcd holds configuration for etcd. // Etcd holds configuration for etcd.
Etcd Etcd `json:"etcd"` Etcd Etcd `json:"etcd"`
// Networking holds configuration for the networking topology of the cluster. // Networking holds configuration for the networking topology of the cluster.
Networking Networking `json:"networking"` Networking Networking `json:"networking"`
// KubernetesVersion is the target version of the control plane. // KubernetesVersion is the target version of the control plane.
KubernetesVersion string `json:"kubernetesVersion"` KubernetesVersion string `json:"kubernetesVersion"`
// ControlPlaneEndpoint sets a stable IP address or DNS name for the control plane; it
// can be a valid IP address or a RFC-1123 DNS subdomain, both with optional TCP port.
// In case the ControlPlaneEndpoint is not specified, the AdvertiseAddress + BindPort
// are used; in case the ControlPlaneEndpoint is specified but without a TCP port,
// the BindPort is used.
// Possible usages are:
// e.g. In an cluster with more than one control plane instances, this field should be
// assigned the address of the external load balancer in front of the
// control plane instances.
// e.g. in environments with enforced node recycling, the ControlPlaneEndpoint
// could be used for assigning a stable DNS to the control plane.
ControlPlaneEndpoint string `json:"controlPlaneEndpoint"`
// APIServerExtraArgs is a set of extra flags to pass to the API Server or override // APIServerExtraArgs is a set of extra flags to pass to the API Server or override
// default ones in form of <flagname>=<value>. // default ones in form of <flagname>=<value>.
// TODO: This is temporary and ideally we would like to switch all components to // TODO: This is temporary and ideally we would like to switch all components to
...@@ -111,18 +125,7 @@ type ClusterConfiguration struct { ...@@ -111,18 +125,7 @@ type ClusterConfiguration struct {
type API struct { type API struct {
// AdvertiseAddress sets the IP address for the API server to advertise. // AdvertiseAddress sets the IP address for the API server to advertise.
AdvertiseAddress string `json:"advertiseAddress"` AdvertiseAddress string `json:"advertiseAddress"`
// ControlPlaneEndpoint sets a stable IP address or DNS name for the control plane; it
// can be a valid IP address or a RFC-1123 DNS subdomain, both with optional TCP port.
// In case the ControlPlaneEndpoint is not specified, the AdvertiseAddress + BindPort
// are used; in case the ControlPlaneEndpoint is specified but without a TCP port,
// the BindPort is used.
// Possible usages are:
// e.g. In an cluster with more than one control plane instances, this field should be
// assigned the address of the external load balancer in front of the
// control plane instances.
// e.g. in environments with enforced node recycling, the ControlPlaneEndpoint
// could be used for assigning a stable DNS to the control plane.
ControlPlaneEndpoint string `json:"controlPlaneEndpoint"`
// BindPort sets the secure port for the API Server to bind to. // BindPort sets the secure port for the API Server to bind to.
// Defaults to 6443. // Defaults to 6443.
BindPort int32 `json:"bindPort"` BindPort int32 `json:"bindPort"`
......
...@@ -177,7 +177,6 @@ func RegisterConversions(s *runtime.Scheme) error { ...@@ -177,7 +177,6 @@ func RegisterConversions(s *runtime.Scheme) error {
func autoConvert_v1alpha3_API_To_kubeadm_API(in *API, out *kubeadm.API, s conversion.Scope) error { func autoConvert_v1alpha3_API_To_kubeadm_API(in *API, out *kubeadm.API, s conversion.Scope) error {
out.AdvertiseAddress = in.AdvertiseAddress out.AdvertiseAddress = in.AdvertiseAddress
out.ControlPlaneEndpoint = in.ControlPlaneEndpoint
out.BindPort = in.BindPort out.BindPort = in.BindPort
return nil return nil
} }
...@@ -189,7 +188,6 @@ func Convert_v1alpha3_API_To_kubeadm_API(in *API, out *kubeadm.API, s conversion ...@@ -189,7 +188,6 @@ func Convert_v1alpha3_API_To_kubeadm_API(in *API, out *kubeadm.API, s conversion
func autoConvert_kubeadm_API_To_v1alpha3_API(in *kubeadm.API, out *API, s conversion.Scope) error { func autoConvert_kubeadm_API_To_v1alpha3_API(in *kubeadm.API, out *API, s conversion.Scope) error {
out.AdvertiseAddress = in.AdvertiseAddress out.AdvertiseAddress = in.AdvertiseAddress
out.ControlPlaneEndpoint = in.ControlPlaneEndpoint
out.BindPort = in.BindPort out.BindPort = in.BindPort
return nil return nil
} }
...@@ -286,6 +284,7 @@ func autoConvert_v1alpha3_ClusterConfiguration_To_kubeadm_ClusterConfiguration(i ...@@ -286,6 +284,7 @@ func autoConvert_v1alpha3_ClusterConfiguration_To_kubeadm_ClusterConfiguration(i
return err return err
} }
out.KubernetesVersion = in.KubernetesVersion out.KubernetesVersion = in.KubernetesVersion
out.ControlPlaneEndpoint = in.ControlPlaneEndpoint
out.APIServerExtraArgs = *(*map[string]string)(unsafe.Pointer(&in.APIServerExtraArgs)) out.APIServerExtraArgs = *(*map[string]string)(unsafe.Pointer(&in.APIServerExtraArgs))
out.ControllerManagerExtraArgs = *(*map[string]string)(unsafe.Pointer(&in.ControllerManagerExtraArgs)) out.ControllerManagerExtraArgs = *(*map[string]string)(unsafe.Pointer(&in.ControllerManagerExtraArgs))
out.SchedulerExtraArgs = *(*map[string]string)(unsafe.Pointer(&in.SchedulerExtraArgs)) out.SchedulerExtraArgs = *(*map[string]string)(unsafe.Pointer(&in.SchedulerExtraArgs))
...@@ -316,6 +315,7 @@ func autoConvert_kubeadm_ClusterConfiguration_To_v1alpha3_ClusterConfiguration(i ...@@ -316,6 +315,7 @@ func autoConvert_kubeadm_ClusterConfiguration_To_v1alpha3_ClusterConfiguration(i
return err return err
} }
out.KubernetesVersion = in.KubernetesVersion out.KubernetesVersion = in.KubernetesVersion
out.ControlPlaneEndpoint = in.ControlPlaneEndpoint
out.APIServerExtraArgs = *(*map[string]string)(unsafe.Pointer(&in.APIServerExtraArgs)) out.APIServerExtraArgs = *(*map[string]string)(unsafe.Pointer(&in.APIServerExtraArgs))
out.ControllerManagerExtraArgs = *(*map[string]string)(unsafe.Pointer(&in.ControllerManagerExtraArgs)) out.ControllerManagerExtraArgs = *(*map[string]string)(unsafe.Pointer(&in.ControllerManagerExtraArgs))
out.SchedulerExtraArgs = *(*map[string]string)(unsafe.Pointer(&in.SchedulerExtraArgs)) out.SchedulerExtraArgs = *(*map[string]string)(unsafe.Pointer(&in.SchedulerExtraArgs))
......
...@@ -22,6 +22,7 @@ import ( ...@@ -22,6 +22,7 @@ import (
"net/url" "net/url"
"os" "os"
"path/filepath" "path/filepath"
"strconv"
"strings" "strings"
"github.com/spf13/pflag" "github.com/spf13/pflag"
...@@ -57,7 +58,8 @@ func ValidateClusterConfiguration(c *kubeadm.ClusterConfiguration) field.ErrorLi ...@@ -57,7 +58,8 @@ func ValidateClusterConfiguration(c *kubeadm.ClusterConfiguration) field.ErrorLi
allErrs = append(allErrs, ValidateCertSANs(c.APIServerCertSANs, field.NewPath("apiServerCertSANs"))...) allErrs = append(allErrs, ValidateCertSANs(c.APIServerCertSANs, field.NewPath("apiServerCertSANs"))...)
allErrs = append(allErrs, ValidateAbsolutePath(c.CertificatesDir, field.NewPath("certificatesDir"))...) allErrs = append(allErrs, ValidateAbsolutePath(c.CertificatesDir, field.NewPath("certificatesDir"))...)
allErrs = append(allErrs, ValidateFeatureGates(c.FeatureGates, field.NewPath("featureGates"))...) allErrs = append(allErrs, ValidateFeatureGates(c.FeatureGates, field.NewPath("featureGates"))...)
allErrs = append(allErrs, ValidateAPIEndpoint(&c.API, field.NewPath("api"))...) allErrs = append(allErrs, ValidateAPI(&c.API, field.NewPath("api"))...)
allErrs = append(allErrs, ValidateHostPort(c.ControlPlaneEndpoint, field.NewPath("controlPlaneEndpoint"))...)
allErrs = append(allErrs, ValidateEtcd(&c.Etcd, field.NewPath("etcd"))...) allErrs = append(allErrs, ValidateEtcd(&c.Etcd, field.NewPath("etcd"))...)
allErrs = append(allErrs, componentconfigs.Known.Validate(c)...) allErrs = append(allErrs, componentconfigs.Known.Validate(c)...)
return allErrs return allErrs
...@@ -312,6 +314,24 @@ func ValidateIPFromString(ipaddr string, fldPath *field.Path) field.ErrorList { ...@@ -312,6 +314,24 @@ func ValidateIPFromString(ipaddr string, fldPath *field.Path) field.ErrorList {
return allErrs return allErrs
} }
// ValidatePort validates port numbers
func ValidatePort(port int32, fldPath *field.Path) field.ErrorList {
allErrs := field.ErrorList{}
if _, err := kubeadmutil.ParsePort(strconv.Itoa(int(port))); err != nil {
allErrs = append(allErrs, field.Invalid(fldPath, port, "port number is not valid"))
}
return allErrs
}
// ValidateHostPort validates host[:port] endpoints
func ValidateHostPort(endpoint string, fldPath *field.Path) field.ErrorList {
allErrs := field.ErrorList{}
if _, _, err := kubeadmutil.ParseHostPort(endpoint); endpoint != "" && err != nil {
allErrs = append(allErrs, field.Invalid(fldPath, endpoint, "endpoint is not valid"))
}
return allErrs
}
// ValidateIPNetFromString validates network portion of ip address // ValidateIPNetFromString validates network portion of ip address
func ValidateIPNetFromString(subnet string, minAddrs int64, fldPath *field.Path) field.ErrorList { func ValidateIPNetFromString(subnet string, minAddrs int64, fldPath *field.Path) field.ErrorList {
allErrs := field.ErrorList{} allErrs := field.ErrorList{}
...@@ -385,14 +405,11 @@ func ValidateFeatureGates(featureGates map[string]bool, fldPath *field.Path) fie ...@@ -385,14 +405,11 @@ func ValidateFeatureGates(featureGates map[string]bool, fldPath *field.Path) fie
return allErrs return allErrs
} }
// ValidateAPIEndpoint validates API server's endpoint // ValidateAPI validates API configuration
func ValidateAPIEndpoint(c *kubeadm.API, fldPath *field.Path) field.ErrorList { func ValidateAPI(c *kubeadm.API, fldPath *field.Path) field.ErrorList {
allErrs := field.ErrorList{} allErrs := field.ErrorList{}
allErrs = append(allErrs, ValidateIPFromString(c.AdvertiseAddress, fldPath.Child("advertiseAddress"))...)
endpoint, err := kubeadmutil.GetMasterEndpoint(c) allErrs = append(allErrs, ValidatePort(c.BindPort, fldPath.Child("bindPort"))...)
if err != nil {
allErrs = append(allErrs, field.Invalid(fldPath, endpoint, err.Error()))
}
return allErrs return allErrs
} }
......
...@@ -218,202 +218,125 @@ func TestValidateIPNetFromString(t *testing.T) { ...@@ -218,202 +218,125 @@ func TestValidateIPNetFromString(t *testing.T) {
} }
} }
func TestValidateAPIEndpoint(t *testing.T) { func TestValidateHostPort(t *testing.T) {
var tests = []struct { var tests = []struct {
name string name string
s *kubeadm.InitConfiguration s string
expected bool expected bool
}{ }{
{ {
name: "Missing configuration", name: "Valid DNS address / port",
s: &kubeadm.InitConfiguration{}, s: "cp.k8s.io:8081",
expected: false,
},
{
name: "Valid DNS ControlPlaneEndpoint (with port), AdvertiseAddress and default port",
s: &kubeadm.InitConfiguration{
ClusterConfiguration: kubeadm.ClusterConfiguration{
API: kubeadm.API{
ControlPlaneEndpoint: "cp.k8s.io:8081",
AdvertiseAddress: "4.5.6.7",
BindPort: 6443,
},
},
},
expected: true, expected: true,
}, },
{ {
name: "Valid IPv4 ControlPlaneEndpoint (with port), AdvertiseAddress and default port", name: "Valid DNS address",
s: &kubeadm.InitConfiguration{ s: "cp.k8s.io",
ClusterConfiguration: kubeadm.ClusterConfiguration{
API: kubeadm.API{
ControlPlaneEndpoint: "1.2.3.4:8081",
AdvertiseAddress: "4.5.6.7",
BindPort: 6443,
},
},
},
expected: true, expected: true,
}, },
{ {
name: "Valid IPv6 ControlPlaneEndpoint (with port), ControlPlaneEndpoint and port", name: "Valid IPv4 address / port",
s: &kubeadm.InitConfiguration{ s: "1.2.3.4:8081",
ClusterConfiguration: kubeadm.ClusterConfiguration{
API: kubeadm.API{
ControlPlaneEndpoint: "[2001:db7::1]:8081",
AdvertiseAddress: "2001:db7::2",
BindPort: 6443,
},
},
},
expected: true, expected: true,
}, },
{ {
name: "Valid DNS ControlPlaneEndpoint (without port), AdvertiseAddress and default port", name: "Valid IPv4 address",
s: &kubeadm.InitConfiguration{ s: "1.2.3.4",
ClusterConfiguration: kubeadm.ClusterConfiguration{
API: kubeadm.API{
ControlPlaneEndpoint: "cp.k8s.io",
AdvertiseAddress: "4.5.6.7",
BindPort: 6443,
},
},
},
expected: true, expected: true,
}, },
{ {
name: "Valid IPv4 ControlPlaneEndpoint (without port), AdvertiseAddress and default port", name: "Valid IPv6 address / port",
s: &kubeadm.InitConfiguration{ s: "[2001:db7::1]:8081",
ClusterConfiguration: kubeadm.ClusterConfiguration{
API: kubeadm.API{
ControlPlaneEndpoint: "1.2.3.4",
AdvertiseAddress: "4.5.6.7",
BindPort: 6443,
},
},
},
expected: true, expected: true,
}, },
{ {
name: "Valid IPv6 ControlPlaneEndpoint (without port), ControlPlaneEndpoint and port", name: "Valid IPv6 address",
s: &kubeadm.InitConfiguration{ s: "2001:db7::1",
ClusterConfiguration: kubeadm.ClusterConfiguration{
API: kubeadm.API{
ControlPlaneEndpoint: "2001:db7::1",
AdvertiseAddress: "2001:db7::2",
BindPort: 6443,
},
},
},
expected: true, expected: true,
}, },
{ {
name: "Valid IPv4 AdvertiseAddress and default port", name: "Invalid IPv4 address, but valid DNS",
s: &kubeadm.InitConfiguration{ s: "1.2.34",
ClusterConfiguration: kubeadm.ClusterConfiguration{
API: kubeadm.API{
AdvertiseAddress: "1.2.3.4",
BindPort: 6443,
},
},
},
expected: true, expected: true,
}, },
{ {
name: "Valid IPv6 AdvertiseAddress and port", name: "Invalid DNS",
s: &kubeadm.InitConfiguration{ s: "a.B.c.d.e",
ClusterConfiguration: kubeadm.ClusterConfiguration{ expected: false,
API: kubeadm.API{
AdvertiseAddress: "2001:db7::1",
BindPort: 3446,
},
},
},
expected: true,
}, },
{ {
name: "Invalid IPv4 AdvertiseAddress", name: "Invalid IPv6 address",
s: &kubeadm.InitConfiguration{ s: "2001:db7:1",
ClusterConfiguration: kubeadm.ClusterConfiguration{
API: kubeadm.API{
AdvertiseAddress: "1.2.34",
BindPort: 6443,
},
},
},
expected: false, expected: false,
}, },
{ {
name: "Invalid IPv6 AdvertiseAddress", name: "Invalid BindPort",
s: &kubeadm.InitConfiguration{ s: "1.2.3.4:0",
ClusterConfiguration: kubeadm.ClusterConfiguration{
API: kubeadm.API{
AdvertiseAddress: "2001:db7:1",
BindPort: 3446,
},
},
},
expected: false, expected: false,
}, },
}
for _, rt := range tests {
actual := ValidateHostPort(rt.s, nil)
if (len(actual) == 0) != rt.expected {
t.Errorf(
"%s test case failed:\n\texpected: %t\n\t actual: %t",
rt.name,
rt.expected,
(len(actual) == 0),
)
}
}
}
func TestValidateAPI(t *testing.T) {
var tests = []struct {
name string
s *kubeadm.API
expected bool
}{
{ {
name: "Invalid BindPort", name: "Valid IPv4 address / port",
s: &kubeadm.InitConfiguration{ s: &kubeadm.API{
ClusterConfiguration: kubeadm.ClusterConfiguration{ AdvertiseAddress: "4.5.6.7",
API: kubeadm.API{ BindPort: 6443,
AdvertiseAddress: "1.2.3.4",
BindPort: 0,
},
},
}, },
expected: false, expected: true,
}, },
{ {
name: "Invalid DNS ControlPlaneEndpoint", name: "Valid IPv6 address / port",
s: &kubeadm.InitConfiguration{ s: &kubeadm.API{
ClusterConfiguration: kubeadm.ClusterConfiguration{ AdvertiseAddress: "2001:db7::2",
API: kubeadm.API{ BindPort: 6443,
ControlPlaneEndpoint: "bad!!.k8s.io",
},
},
}, },
expected: false, expected: true,
}, },
{ {
name: "Invalid ipv4 ControlPlaneEndpoint", name: "Invalid IPv4 address",
s: &kubeadm.InitConfiguration{ s: &kubeadm.API{
ClusterConfiguration: kubeadm.ClusterConfiguration{ AdvertiseAddress: "1.2.34",
API: kubeadm.API{ BindPort: 6443,
ControlPlaneEndpoint: "1..3.4",
},
},
}, },
expected: false, expected: false,
}, },
{ {
name: "Invalid ipv6 ControlPlaneEndpoint", name: "Invalid IPv6 address",
s: &kubeadm.InitConfiguration{ s: &kubeadm.API{
ClusterConfiguration: kubeadm.ClusterConfiguration{ AdvertiseAddress: "2001:db7:1",
API: kubeadm.API{ BindPort: 6443,
ControlPlaneEndpoint: "1200::AB00:1234::2552:7777:1313",
},
},
}, },
expected: false, expected: false,
}, },
{ {
name: "Invalid ControlPlaneEndpoint port", name: "Invalid BindPort",
s: &kubeadm.InitConfiguration{ s: &kubeadm.API{
ClusterConfiguration: kubeadm.ClusterConfiguration{ AdvertiseAddress: "4.5.6.7",
API: kubeadm.API{ BindPort: 0,
ControlPlaneEndpoint: "1.2.3.4:0",
},
},
}, },
expected: false, expected: false,
}, },
} }
for _, rt := range tests { for _, rt := range tests {
actual := ValidateAPIEndpoint(&rt.s.API, nil) actual := ValidateAPI(rt.s, nil)
if (len(actual) == 0) != rt.expected { if (len(actual) == 0) != rt.expected {
t.Errorf( t.Errorf(
"%s test case failed:\n\texpected: %t\n\t actual: %t", "%s test case failed:\n\texpected: %t\n\t actual: %t",
...@@ -425,6 +348,7 @@ func TestValidateAPIEndpoint(t *testing.T) { ...@@ -425,6 +348,7 @@ func TestValidateAPIEndpoint(t *testing.T) {
} }
} }
//TODO: Create a separated test for ValidateClusterConfiguration
func TestValidateInitConfiguration(t *testing.T) { func TestValidateInitConfiguration(t *testing.T) {
nodename := "valid-nodename" nodename := "valid-nodename"
var tests = []struct { var tests = []struct {
......
...@@ -72,7 +72,7 @@ var ( ...@@ -72,7 +72,7 @@ var (
{{.Error}} {{.Error}}
Please ensure that: Please ensure that:
* The cluster has a stable api.controlPlaneEndpoint address. * The cluster has a stable controlPlaneEndpoint address.
* The cluster uses an external etcd. * The cluster uses an external etcd.
* The certificates that must be shared among control plane instances are provided. * The certificates that must be shared among control plane instances are provided.
...@@ -388,8 +388,8 @@ func (j *Join) FetchInitClusterConfiguration(tlsBootstrapCfg *clientcmdapi.Confi ...@@ -388,8 +388,8 @@ func (j *Join) FetchInitClusterConfiguration(tlsBootstrapCfg *clientcmdapi.Confi
// joining an additional control plane instance and if the node is ready to join // joining an additional control plane instance and if the node is ready to join
func (j *Join) CheckIfReadyForAdditionalControlPlane(clusterConfiguration *kubeadmapi.InitConfiguration) error { func (j *Join) CheckIfReadyForAdditionalControlPlane(clusterConfiguration *kubeadmapi.InitConfiguration) error {
// blocks if the cluster was created without a stable control plane endpoint // blocks if the cluster was created without a stable control plane endpoint
if clusterConfiguration.API.ControlPlaneEndpoint == "" { if clusterConfiguration.ControlPlaneEndpoint == "" {
return fmt.Errorf("unable to add a new control plane instance a cluster that doesn't have a stable api.controlPlaneEndpoint address") return fmt.Errorf("unable to add a new control plane instance a cluster that doesn't have a stable controlPlaneEndpoint address")
} }
// blocks if the cluster was created without an external etcd cluster // blocks if the cluster was created without an external etcd cluster
......
...@@ -46,12 +46,12 @@ func TestPrintConfiguration(t *testing.T) { ...@@ -46,12 +46,12 @@ func TestPrintConfiguration(t *testing.T) {
api: api:
advertiseAddress: "" advertiseAddress: ""
bindPort: 0 bindPort: 0
controlPlaneEndpoint: ""
apiVersion: kubeadm.k8s.io/v1alpha3 apiVersion: kubeadm.k8s.io/v1alpha3
auditPolicy: auditPolicy:
logDir: "" logDir: ""
path: "" path: ""
certificatesDir: "" certificatesDir: ""
controlPlaneEndpoint: ""
etcd: etcd:
local: local:
dataDir: /some/path dataDir: /some/path
...@@ -82,12 +82,12 @@ func TestPrintConfiguration(t *testing.T) { ...@@ -82,12 +82,12 @@ func TestPrintConfiguration(t *testing.T) {
api: api:
advertiseAddress: "" advertiseAddress: ""
bindPort: 0 bindPort: 0
controlPlaneEndpoint: ""
apiVersion: kubeadm.k8s.io/v1alpha3 apiVersion: kubeadm.k8s.io/v1alpha3
auditPolicy: auditPolicy:
logDir: "" logDir: ""
path: "" path: ""
certificatesDir: "" certificatesDir: ""
controlPlaneEndpoint: ""
etcd: etcd:
external: external:
caFile: "" caFile: ""
......
...@@ -52,7 +52,7 @@ func EnsureProxyAddon(cfg *kubeadmapi.InitConfiguration, client clientset.Interf ...@@ -52,7 +52,7 @@ func EnsureProxyAddon(cfg *kubeadmapi.InitConfiguration, client clientset.Interf
} }
// Generate Master Enpoint kubeconfig file // Generate Master Enpoint kubeconfig file
masterEndpoint, err := kubeadmutil.GetMasterEndpoint(&cfg.API) masterEndpoint, err := kubeadmutil.GetMasterEndpoint(cfg)
if err != nil { if err != nil {
return err return err
} }
......
...@@ -284,16 +284,16 @@ func GetAPIServerAltNames(cfg *kubeadmapi.InitConfiguration) (*certutil.AltNames ...@@ -284,16 +284,16 @@ func GetAPIServerAltNames(cfg *kubeadmapi.InitConfiguration) (*certutil.AltNames
}, },
} }
// add api server controlPlaneEndpoint if present (dns or ip) // add cluster controlPlaneEndpoint if present (dns or ip)
if len(cfg.API.ControlPlaneEndpoint) > 0 { if len(cfg.ControlPlaneEndpoint) > 0 {
if host, _, err := kubeadmutil.ParseHostPort(cfg.API.ControlPlaneEndpoint); err == nil { if host, _, err := kubeadmutil.ParseHostPort(cfg.ControlPlaneEndpoint); err == nil {
if ip := net.ParseIP(host); ip != nil { if ip := net.ParseIP(host); ip != nil {
altNames.IPs = append(altNames.IPs, ip) altNames.IPs = append(altNames.IPs, ip)
} else { } else {
altNames.DNSNames = append(altNames.DNSNames, host) altNames.DNSNames = append(altNames.DNSNames, host)
} }
} else { } else {
return nil, fmt.Errorf("error parsing API api.controlPlaneEndpoint %q: %s", cfg.API.ControlPlaneEndpoint, err) return nil, fmt.Errorf("error parsing cluster controlPlaneEndpoint %q: %s", cfg.ControlPlaneEndpoint, err)
} }
} }
......
...@@ -447,9 +447,10 @@ func TestGetAPIServerAltNames(t *testing.T) { ...@@ -447,9 +447,10 @@ func TestGetAPIServerAltNames(t *testing.T) {
name: "ControlPlaneEndpoint DNS", name: "ControlPlaneEndpoint DNS",
cfg: &kubeadmapi.InitConfiguration{ cfg: &kubeadmapi.InitConfiguration{
ClusterConfiguration: kubeadmapi.ClusterConfiguration{ ClusterConfiguration: kubeadmapi.ClusterConfiguration{
API: kubeadmapi.API{AdvertiseAddress: "1.2.3.4", ControlPlaneEndpoint: "api.k8s.io:6443"}, API: kubeadmapi.API{AdvertiseAddress: "1.2.3.4"},
Networking: kubeadmapi.Networking{ServiceSubnet: "10.96.0.0/12", DNSDomain: "cluster.local"}, ControlPlaneEndpoint: "api.k8s.io:6443",
APIServerCertSANs: []string{"10.1.245.94", "10.1.245.95", "1.2.3.L", "invalid,commas,in,DNS"}, Networking: kubeadmapi.Networking{ServiceSubnet: "10.96.0.0/12", DNSDomain: "cluster.local"},
APIServerCertSANs: []string{"10.1.245.94", "10.1.245.95", "1.2.3.L", "invalid,commas,in,DNS"},
}, },
NodeRegistration: kubeadmapi.NodeRegistrationOptions{Name: "valid-hostname"}, NodeRegistration: kubeadmapi.NodeRegistrationOptions{Name: "valid-hostname"},
}, },
...@@ -460,9 +461,10 @@ func TestGetAPIServerAltNames(t *testing.T) { ...@@ -460,9 +461,10 @@ func TestGetAPIServerAltNames(t *testing.T) {
name: "ControlPlaneEndpoint IP", name: "ControlPlaneEndpoint IP",
cfg: &kubeadmapi.InitConfiguration{ cfg: &kubeadmapi.InitConfiguration{
ClusterConfiguration: kubeadmapi.ClusterConfiguration{ ClusterConfiguration: kubeadmapi.ClusterConfiguration{
API: kubeadmapi.API{AdvertiseAddress: "1.2.3.4", ControlPlaneEndpoint: "4.5.6.7:6443"}, API: kubeadmapi.API{AdvertiseAddress: "1.2.3.4"},
Networking: kubeadmapi.Networking{ServiceSubnet: "10.96.0.0/12", DNSDomain: "cluster.local"}, ControlPlaneEndpoint: "4.5.6.7:6443",
APIServerCertSANs: []string{"10.1.245.94", "10.1.245.95", "1.2.3.L", "invalid,commas,in,DNS"}, Networking: kubeadmapi.Networking{ServiceSubnet: "10.96.0.0/12", DNSDomain: "cluster.local"},
APIServerCertSANs: []string{"10.1.245.94", "10.1.245.95", "1.2.3.L", "invalid,commas,in,DNS"},
}, },
NodeRegistration: kubeadmapi.NodeRegistrationOptions{Name: "valid-hostname"}, NodeRegistration: kubeadmapi.NodeRegistrationOptions{Name: "valid-hostname"},
}, },
......
...@@ -156,7 +156,7 @@ func getKubeConfigSpecs(cfg *kubeadmapi.InitConfiguration) (map[string]*kubeConf ...@@ -156,7 +156,7 @@ func getKubeConfigSpecs(cfg *kubeadmapi.InitConfiguration) (map[string]*kubeConf
return nil, fmt.Errorf("couldn't create a kubeconfig; the CA files couldn't be loaded: %v", err) return nil, fmt.Errorf("couldn't create a kubeconfig; the CA files couldn't be loaded: %v", err)
} }
masterEndpoint, err := kubeadmutil.GetMasterEndpoint(&cfg.API) masterEndpoint, err := kubeadmutil.GetMasterEndpoint(cfg)
if err != nil { if err != nil {
return nil, err return nil, err
} }
...@@ -293,7 +293,7 @@ func WriteKubeConfigWithClientCert(out io.Writer, cfg *kubeadmapi.InitConfigurat ...@@ -293,7 +293,7 @@ func WriteKubeConfigWithClientCert(out io.Writer, cfg *kubeadmapi.InitConfigurat
return fmt.Errorf("couldn't create a kubeconfig; the CA files couldn't be loaded: %v", err) return fmt.Errorf("couldn't create a kubeconfig; the CA files couldn't be loaded: %v", err)
} }
masterEndpoint, err := kubeadmutil.GetMasterEndpoint(&cfg.API) masterEndpoint, err := kubeadmutil.GetMasterEndpoint(cfg)
if err != nil { if err != nil {
return err return err
} }
...@@ -320,7 +320,7 @@ func WriteKubeConfigWithToken(out io.Writer, cfg *kubeadmapi.InitConfiguration, ...@@ -320,7 +320,7 @@ func WriteKubeConfigWithToken(out io.Writer, cfg *kubeadmapi.InitConfiguration,
return fmt.Errorf("couldn't create a kubeconfig; the CA files couldn't be loaded: %v", err) return fmt.Errorf("couldn't create a kubeconfig; the CA files couldn't be loaded: %v", err)
} }
masterEndpoint, err := kubeadmutil.GetMasterEndpoint(&cfg.API) masterEndpoint, err := kubeadmutil.GetMasterEndpoint(cfg)
if err != nil { if err != nil {
return err return err
} }
......
...@@ -74,29 +74,33 @@ func TestGetKubeConfigSpecs(t *testing.T) { ...@@ -74,29 +74,33 @@ func TestGetKubeConfigSpecs(t *testing.T) {
}, },
{ {
ClusterConfiguration: kubeadmapi.ClusterConfiguration{ ClusterConfiguration: kubeadmapi.ClusterConfiguration{
API: kubeadmapi.API{AdvertiseAddress: "1.2.3.4", ControlPlaneEndpoint: "api.k8s.io", BindPort: 1234}, API: kubeadmapi.API{AdvertiseAddress: "1.2.3.4", BindPort: 1234},
CertificatesDir: pkidir, ControlPlaneEndpoint: "api.k8s.io",
CertificatesDir: pkidir,
}, },
NodeRegistration: kubeadmapi.NodeRegistrationOptions{Name: "valid-node-name"}, NodeRegistration: kubeadmapi.NodeRegistrationOptions{Name: "valid-node-name"},
}, },
{ {
ClusterConfiguration: kubeadmapi.ClusterConfiguration{ ClusterConfiguration: kubeadmapi.ClusterConfiguration{
API: kubeadmapi.API{AdvertiseAddress: "1.2.3.4", ControlPlaneEndpoint: "api.k8s.io:4321", BindPort: 1234}, API: kubeadmapi.API{AdvertiseAddress: "1.2.3.4", BindPort: 1234},
CertificatesDir: pkidir, ControlPlaneEndpoint: "api.k8s.io:4321",
CertificatesDir: pkidir,
}, },
NodeRegistration: kubeadmapi.NodeRegistrationOptions{Name: "valid-node-name"}, NodeRegistration: kubeadmapi.NodeRegistrationOptions{Name: "valid-node-name"},
}, },
{ {
ClusterConfiguration: kubeadmapi.ClusterConfiguration{ ClusterConfiguration: kubeadmapi.ClusterConfiguration{
API: kubeadmapi.API{AdvertiseAddress: "1.2.3.4", ControlPlaneEndpoint: "api.k8s.io", BindPort: 1234}, API: kubeadmapi.API{AdvertiseAddress: "1.2.3.4", BindPort: 1234},
CertificatesDir: pkidir, ControlPlaneEndpoint: "api.k8s.io",
CertificatesDir: pkidir,
}, },
NodeRegistration: kubeadmapi.NodeRegistrationOptions{Name: "valid-node-name"}, NodeRegistration: kubeadmapi.NodeRegistrationOptions{Name: "valid-node-name"},
}, },
{ {
ClusterConfiguration: kubeadmapi.ClusterConfiguration{ ClusterConfiguration: kubeadmapi.ClusterConfiguration{
API: kubeadmapi.API{AdvertiseAddress: "1.2.3.4", ControlPlaneEndpoint: "api.k8s.io:4321", BindPort: 1234}, API: kubeadmapi.API{AdvertiseAddress: "1.2.3.4", BindPort: 1234},
CertificatesDir: pkidir, ControlPlaneEndpoint: "api.k8s.io:4321",
CertificatesDir: pkidir,
}, },
NodeRegistration: kubeadmapi.NodeRegistrationOptions{Name: "valid-node-name"}, NodeRegistration: kubeadmapi.NodeRegistrationOptions{Name: "valid-node-name"},
}, },
...@@ -155,7 +159,7 @@ func TestGetKubeConfigSpecs(t *testing.T) { ...@@ -155,7 +159,7 @@ func TestGetKubeConfigSpecs(t *testing.T) {
} }
// Asserts InitConfiguration values injected into spec // Asserts InitConfiguration values injected into spec
masterEndpoint, err := kubeadmutil.GetMasterEndpoint(&cfg.API) masterEndpoint, err := kubeadmutil.GetMasterEndpoint(cfg)
if err != nil { if err != nil {
t.Error(err) t.Error(err)
} }
......
API: API:
AdvertiseAddress: 192.168.2.2 AdvertiseAddress: 192.168.2.2
BindPort: 6443 BindPort: 6443
ControlPlaneEndpoint: ""
APIServerCertSANs: null APIServerCertSANs: null
APIServerExtraArgs: APIServerExtraArgs:
authorization-mode: Node,RBAC,Webhook authorization-mode: Node,RBAC,Webhook
...@@ -157,6 +156,7 @@ ComponentConfigs: ...@@ -157,6 +156,7 @@ ComponentConfigs:
TLSMinVersion: "" TLSMinVersion: ""
TLSPrivateKeyFile: "" TLSPrivateKeyFile: ""
VolumeStatsAggPeriod: 1m0s VolumeStatsAggPeriod: 1m0s
ControlPlaneEndpoint: ""
ControllerManagerExtraArgs: null ControllerManagerExtraArgs: null
ControllerManagerExtraVolumes: null ControllerManagerExtraVolumes: null
Etcd: Etcd:
......
...@@ -18,7 +18,6 @@ nodeRegistration: ...@@ -18,7 +18,6 @@ nodeRegistration:
api: api:
advertiseAddress: 192.168.2.2 advertiseAddress: 192.168.2.2
bindPort: 6443 bindPort: 6443
controlPlaneEndpoint: ""
apiServerExtraArgs: apiServerExtraArgs:
authorization-mode: Node,RBAC,Webhook authorization-mode: Node,RBAC,Webhook
apiVersion: kubeadm.k8s.io/v1alpha3 apiVersion: kubeadm.k8s.io/v1alpha3
...@@ -28,6 +27,7 @@ auditPolicy: ...@@ -28,6 +27,7 @@ auditPolicy:
path: "" path: ""
certificatesDir: /etc/kubernetes/pki certificatesDir: /etc/kubernetes/pki
clusterName: kubernetes clusterName: kubernetes
controlPlaneEndpoint: ""
etcd: etcd:
local: local:
dataDir: /var/lib/etcd dataDir: /var/lib/etcd
......
...@@ -18,7 +18,6 @@ nodeRegistration: ...@@ -18,7 +18,6 @@ nodeRegistration:
api: api:
advertiseAddress: 192.168.2.2 advertiseAddress: 192.168.2.2
bindPort: 6443 bindPort: 6443
controlPlaneEndpoint: ""
apiVersion: kubeadm.k8s.io/v1alpha3 apiVersion: kubeadm.k8s.io/v1alpha3
auditPolicy: auditPolicy:
logDir: /var/log/kubernetes/audit logDir: /var/log/kubernetes/audit
...@@ -26,6 +25,7 @@ auditPolicy: ...@@ -26,6 +25,7 @@ auditPolicy:
path: "" path: ""
certificatesDir: /var/lib/kubernetes/pki certificatesDir: /var/lib/kubernetes/pki
clusterName: kubernetes clusterName: kubernetes
controlPlaneEndpoint: ""
etcd: etcd:
local: local:
dataDir: /var/lib/etcd dataDir: /var/lib/etcd
......
...@@ -27,42 +27,42 @@ import ( ...@@ -27,42 +27,42 @@ import (
) )
// GetMasterEndpoint returns a properly formatted endpoint for the control plane built according following rules: // GetMasterEndpoint returns a properly formatted endpoint for the control plane built according following rules:
// - If the api.ControlPlaneEndpoint is defined, use it. // - If the ControlPlaneEndpoint is defined, use it.
// - if the api.ControlPlaneEndpoint is defined but without a port number, use the api.ControlPlaneEndpoint + api.BindPort is used. // - if the ControlPlaneEndpoint is defined but without a port number, use the ControlPlaneEndpoint + api.BindPort is used.
// - Otherwise, in case the api.ControlPlaneEndpoint is not defined, use the api.AdvertiseAddress + the api.BindPort. // - Otherwise, in case the ControlPlaneEndpoint is not defined, use the api.AdvertiseAddress + the api.BindPort.
func GetMasterEndpoint(api *kubeadmapi.API) (string, error) { func GetMasterEndpoint(cfg *kubeadmapi.InitConfiguration) (string, error) {
// parse the bind port // parse the bind port
var bindPort = strconv.Itoa(int(api.BindPort)) bindPortString := strconv.Itoa(int(cfg.API.BindPort))
if _, err := parsePort(bindPort); err != nil { if _, err := ParsePort(bindPortString); err != nil {
return "", fmt.Errorf("invalid value %q given for api.bindPort: %s", api.BindPort, err) return "", fmt.Errorf("invalid value %q given for api.bindPort: %s", cfg.API.BindPort, err)
} }
// parse the AdvertiseAddress // parse the AdvertiseAddress
var ip = net.ParseIP(api.AdvertiseAddress) var ip = net.ParseIP(cfg.API.AdvertiseAddress)
if ip == nil { if ip == nil {
return "", fmt.Errorf("invalid value `%s` given for api.advertiseAddress", api.AdvertiseAddress) return "", fmt.Errorf("invalid value `%s` given for api.advertiseAddress", cfg.API.AdvertiseAddress)
} }
// set the master url using cfg.API.AdvertiseAddress + the cfg.API.BindPort // set the master url using cfg.API.AdvertiseAddress + the cfg.API.BindPort
masterURL := &url.URL{ masterURL := &url.URL{
Scheme: "https", Scheme: "https",
Host: net.JoinHostPort(ip.String(), bindPort), Host: net.JoinHostPort(ip.String(), bindPortString),
} }
// if the controlplane endpoint is defined // if the controlplane endpoint is defined
if len(api.ControlPlaneEndpoint) > 0 { if len(cfg.ControlPlaneEndpoint) > 0 {
// parse the controlplane endpoint // parse the controlplane endpoint
var host, port string var host, port string
var err error var err error
if host, port, err = ParseHostPort(api.ControlPlaneEndpoint); err != nil { if host, port, err = ParseHostPort(cfg.ControlPlaneEndpoint); err != nil {
return "", fmt.Errorf("invalid value %q given for api.controlPlaneEndpoint: %s", api.ControlPlaneEndpoint, err) return "", fmt.Errorf("invalid value %q given for controlPlaneEndpoint: %s", cfg.ControlPlaneEndpoint, err)
} }
// if a port is provided within the controlPlaneAddress warn the users we are using it, else use the bindport // if a port is provided within the controlPlaneAddress warn the users we are using it, else use the bindport
if port != "" { if port != "" {
fmt.Println("[endpoint] WARNING: port specified in api.controlPlaneEndpoint overrides api.bindPort in the controlplane address") fmt.Println("[endpoint] WARNING: port specified in controlPlaneEndpoint overrides bindPort in the controlplane address")
} else { } else {
port = bindPort port = bindPortString
} }
// overrides the master url using the controlPlaneAddress (and eventually the bindport) // overrides the master url using the controlPlaneAddress (and eventually the bindport)
...@@ -90,7 +90,7 @@ func ParseHostPort(hostport string) (string, string, error) { ...@@ -90,7 +90,7 @@ func ParseHostPort(hostport string) (string, string, error) {
// if port is defined, parse and validate it // if port is defined, parse and validate it
if port != "" { if port != "" {
if _, err := parsePort(port); err != nil { if _, err := ParsePort(port); err != nil {
return "", "", fmt.Errorf("port must be a valid number between 1 and 65535, inclusive") return "", "", fmt.Errorf("port must be a valid number between 1 and 65535, inclusive")
} }
} }
...@@ -110,8 +110,9 @@ func ParseHostPort(hostport string) (string, string, error) { ...@@ -110,8 +110,9 @@ func ParseHostPort(hostport string) (string, string, error) {
// ParsePort parses a string representing a TCP port. // ParsePort parses a string representing a TCP port.
// If the string is not a valid representation of a TCP port, ParsePort returns an error. // If the string is not a valid representation of a TCP port, ParsePort returns an error.
func parsePort(port string) (int, error) { func ParsePort(port string) (int, error) {
if portInt, err := strconv.Atoi(port); err == nil && (1 <= portInt && portInt <= 65535) { portInt, err := strconv.Atoi(port)
if err == nil && (1 <= portInt && portInt <= 65535) {
return portInt, nil return portInt, nil
} }
......
...@@ -25,139 +25,199 @@ import ( ...@@ -25,139 +25,199 @@ import (
func TestGetMasterEndpoint(t *testing.T) { func TestGetMasterEndpoint(t *testing.T) {
var tests = []struct { var tests = []struct {
name string name string
api *kubeadmapi.API cfg *kubeadmapi.InitConfiguration
expectedEndpoint string expectedEndpoint string
expectedError bool expectedError bool
}{ }{
{ {
name: "use ControlPlaneEndpoint (dns) if fully defined", name: "use ControlPlaneEndpoint (dns) if fully defined",
api: &kubeadmapi.API{ cfg: &kubeadmapi.InitConfiguration{
ControlPlaneEndpoint: "cp.k8s.io:1234", ClusterConfiguration: kubeadmapi.ClusterConfiguration{
BindPort: 4567, API: kubeadmapi.API{
AdvertiseAddress: "4.5.6.7", BindPort: 4567,
AdvertiseAddress: "4.5.6.7",
},
ControlPlaneEndpoint: "cp.k8s.io:1234",
},
}, },
expectedEndpoint: "https://cp.k8s.io:1234", expectedEndpoint: "https://cp.k8s.io:1234",
}, },
{ {
name: "use ControlPlaneEndpoint (ipv4) if fully defined", name: "use ControlPlaneEndpoint (ipv4) if fully defined",
api: &kubeadmapi.API{ cfg: &kubeadmapi.InitConfiguration{
ControlPlaneEndpoint: "1.2.3.4:1234", ClusterConfiguration: kubeadmapi.ClusterConfiguration{
BindPort: 4567, API: kubeadmapi.API{
AdvertiseAddress: "4.5.6.7", BindPort: 4567,
AdvertiseAddress: "4.5.6.7",
},
ControlPlaneEndpoint: "1.2.3.4:1234",
},
}, },
expectedEndpoint: "https://1.2.3.4:1234", expectedEndpoint: "https://1.2.3.4:1234",
}, },
{ {
name: "use ControlPlaneEndpoint (ipv6) if fully defined", name: "use ControlPlaneEndpoint (ipv6) if fully defined",
api: &kubeadmapi.API{ cfg: &kubeadmapi.InitConfiguration{
ControlPlaneEndpoint: "[2001:db8::1]:1234", ClusterConfiguration: kubeadmapi.ClusterConfiguration{
BindPort: 4567, API: kubeadmapi.API{
AdvertiseAddress: "4.5.6.7", BindPort: 4567,
AdvertiseAddress: "4.5.6.7",
},
ControlPlaneEndpoint: "[2001:db8::1]:1234",
},
}, },
expectedEndpoint: "https://[2001:db8::1]:1234", expectedEndpoint: "https://[2001:db8::1]:1234",
}, },
{ {
name: "use ControlPlaneEndpoint (dns) + BindPort if ControlPlaneEndpoint defined without port", name: "use ControlPlaneEndpoint (dns) + BindPort if ControlPlaneEndpoint defined without port",
api: &kubeadmapi.API{ cfg: &kubeadmapi.InitConfiguration{
ControlPlaneEndpoint: "cp.k8s.io", ClusterConfiguration: kubeadmapi.ClusterConfiguration{
BindPort: 4567, API: kubeadmapi.API{
AdvertiseAddress: "4.5.6.7", BindPort: 4567,
AdvertiseAddress: "4.5.6.7",
},
ControlPlaneEndpoint: "cp.k8s.io",
},
}, },
expectedEndpoint: "https://cp.k8s.io:4567", expectedEndpoint: "https://cp.k8s.io:4567",
}, },
{ {
name: "use ControlPlaneEndpoint (ipv4) + BindPort if ControlPlaneEndpoint defined without port", name: "use ControlPlaneEndpoint (ipv4) + BindPort if ControlPlaneEndpoint defined without port",
api: &kubeadmapi.API{ cfg: &kubeadmapi.InitConfiguration{
ControlPlaneEndpoint: "1.2.3.4", ClusterConfiguration: kubeadmapi.ClusterConfiguration{
BindPort: 4567, API: kubeadmapi.API{
AdvertiseAddress: "4.5.6.7", BindPort: 4567,
AdvertiseAddress: "4.5.6.7",
},
ControlPlaneEndpoint: "1.2.3.4",
},
}, },
expectedEndpoint: "https://1.2.3.4:4567", expectedEndpoint: "https://1.2.3.4:4567",
}, },
{ {
name: "use ControlPlaneEndpoint (ipv6) + BindPort if ControlPlaneEndpoint defined without port", name: "use ControlPlaneEndpoint (ipv6) + BindPort if ControlPlaneEndpoint defined without port",
api: &kubeadmapi.API{ cfg: &kubeadmapi.InitConfiguration{
ControlPlaneEndpoint: "2001:db8::1", ClusterConfiguration: kubeadmapi.ClusterConfiguration{
BindPort: 4567, API: kubeadmapi.API{
AdvertiseAddress: "4.5.6.7", BindPort: 4567,
AdvertiseAddress: "4.5.6.7",
},
ControlPlaneEndpoint: "2001:db8::1",
},
}, },
expectedEndpoint: "https://[2001:db8::1]:4567", expectedEndpoint: "https://[2001:db8::1]:4567",
}, },
{ {
name: "use AdvertiseAddress (ipv4) + BindPort if ControlPlaneEndpoint is not defined", name: "use AdvertiseAddress (ipv4) + BindPort if ControlPlaneEndpoint is not defined",
api: &kubeadmapi.API{ cfg: &kubeadmapi.InitConfiguration{
BindPort: 4567, ClusterConfiguration: kubeadmapi.ClusterConfiguration{
AdvertiseAddress: "4.5.6.7", API: kubeadmapi.API{
BindPort: 4567,
AdvertiseAddress: "4.5.6.7",
},
},
}, },
expectedEndpoint: "https://4.5.6.7:4567", expectedEndpoint: "https://4.5.6.7:4567",
}, },
{ {
name: "use AdvertiseAddress (ipv6) + BindPort if ControlPlaneEndpoint is not defined", name: "use AdvertiseAddress (ipv6) + BindPort if ControlPlaneEndpoint is not defined",
api: &kubeadmapi.API{ cfg: &kubeadmapi.InitConfiguration{
BindPort: 4567, ClusterConfiguration: kubeadmapi.ClusterConfiguration{
AdvertiseAddress: "2001:db8::1", API: kubeadmapi.API{
BindPort: 4567,
AdvertiseAddress: "2001:db8::1",
},
},
}, },
expectedEndpoint: "https://[2001:db8::1]:4567", expectedEndpoint: "https://[2001:db8::1]:4567",
}, },
{ {
name: "fail if invalid BindPort", name: "fail if invalid BindPort",
api: &kubeadmapi.API{ cfg: &kubeadmapi.InitConfiguration{
BindPort: 0, ClusterConfiguration: kubeadmapi.ClusterConfiguration{
API: kubeadmapi.API{
BindPort: 0,
},
},
}, },
expectedError: true, expectedError: true,
}, },
{ {
name: "fail if invalid ControlPlaneEndpoint (dns)", name: "fail if invalid ControlPlaneEndpoint (dns)",
api: &kubeadmapi.API{ cfg: &kubeadmapi.InitConfiguration{
ControlPlaneEndpoint: "bad!!.cp.k8s.io", ClusterConfiguration: kubeadmapi.ClusterConfiguration{
BindPort: 4567, API: kubeadmapi.API{
BindPort: 4567,
},
ControlPlaneEndpoint: "bad!!.cp.k8s.io",
},
}, },
expectedError: true, expectedError: true,
}, },
{ {
name: "fail if invalid ControlPlaneEndpoint (ip4)", name: "fail if invalid ControlPlaneEndpoint (ip4)",
api: &kubeadmapi.API{ cfg: &kubeadmapi.InitConfiguration{
ControlPlaneEndpoint: "1..0", ClusterConfiguration: kubeadmapi.ClusterConfiguration{
BindPort: 4567, API: kubeadmapi.API{
BindPort: 4567,
},
ControlPlaneEndpoint: "1..0",
},
}, },
expectedError: true, expectedError: true,
}, },
{ {
name: "fail if invalid ControlPlaneEndpoint (ip6)", name: "fail if invalid ControlPlaneEndpoint (ip6)",
api: &kubeadmapi.API{ cfg: &kubeadmapi.InitConfiguration{
ControlPlaneEndpoint: "1200::AB00:1234::2552:7777:1313", ClusterConfiguration: kubeadmapi.ClusterConfiguration{
BindPort: 4567, API: kubeadmapi.API{
BindPort: 4567,
},
ControlPlaneEndpoint: "1200::AB00:1234::2552:7777:1313",
},
}, },
expectedError: true, expectedError: true,
}, },
{ {
name: "fail if invalid ControlPlaneEndpoint (port)", name: "fail if invalid ControlPlaneEndpoint (port)",
api: &kubeadmapi.API{ cfg: &kubeadmapi.InitConfiguration{
ControlPlaneEndpoint: "cp.k8s.io:0", ClusterConfiguration: kubeadmapi.ClusterConfiguration{
BindPort: 4567, API: kubeadmapi.API{
BindPort: 4567,
},
ControlPlaneEndpoint: "cp.k8s.io:0",
},
}, },
expectedError: true, expectedError: true,
}, },
{ {
name: "fail if invalid AdvertiseAddress (ip4)", name: "fail if invalid AdvertiseAddress (ip4)",
api: &kubeadmapi.API{ cfg: &kubeadmapi.InitConfiguration{
AdvertiseAddress: "1..0", ClusterConfiguration: kubeadmapi.ClusterConfiguration{
BindPort: 4567, API: kubeadmapi.API{
AdvertiseAddress: "1..0",
BindPort: 4567,
},
},
}, },
expectedError: true, expectedError: true,
}, },
{ {
name: "fail if invalid AdvertiseAddress (ip6)", name: "fail if invalid AdvertiseAddress (ip6)",
api: &kubeadmapi.API{ cfg: &kubeadmapi.InitConfiguration{
AdvertiseAddress: "1200::AB00:1234::2552:7777:1313", ClusterConfiguration: kubeadmapi.ClusterConfiguration{
BindPort: 4567, API: kubeadmapi.API{
AdvertiseAddress: "1200::AB00:1234::2552:7777:1313",
BindPort: 4567,
},
},
}, },
expectedError: true, expectedError: true,
}, },
} }
for _, rt := range tests { for _, rt := range tests {
actualEndpoint, actualError := GetMasterEndpoint(rt.api) actualEndpoint, actualError := GetMasterEndpoint(rt.cfg)
if (actualError != nil) && !rt.expectedError { if (actualError != nil) && !rt.expectedError {
t.Errorf("%s unexpected failure: %v", rt.name, actualError) t.Errorf("%s unexpected failure: %v", rt.name, actualError)
...@@ -328,7 +388,7 @@ func TestParsePort(t *testing.T) { ...@@ -328,7 +388,7 @@ func TestParsePort(t *testing.T) {
} }
for _, rt := range tests { for _, rt := range tests {
actualPort, actualError := parsePort(rt.port) actualPort, actualError := ParsePort(rt.port)
if (actualError != nil) && !rt.expectedError { if (actualError != nil) && !rt.expectedError {
t.Errorf("%s unexpected failure: %v", rt.name, actualError) t.Errorf("%s unexpected failure: %v", rt.name, actualError)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment