@@ -34,22 +34,10 @@ import (
34
34
// - if the controlPlaneEndpoint is defined but without a port number, use the controlPlaneEndpoint + localEndpoint.BindPort is used.
35
35
// - Otherwise, in case the controlPlaneEndpoint is not defined, use the localEndpoint.AdvertiseAddress + the localEndpoint.BindPort.
36
36
func GetControlPlaneEndpoint (controlPlaneEndpoint string , localEndpoint * kubeadmapi.APIEndpoint ) (string , error ) {
37
- // parse the bind port
38
- bindPortString := strconv .Itoa (int (localEndpoint .BindPort ))
39
- if _ , err := ParsePort (bindPortString ); err != nil {
40
- return "" , errors .Wrapf (err , "invalid value %q given for api.bindPort" , localEndpoint .BindPort )
41
- }
42
-
43
- // parse the AdvertiseAddress
44
- var ip = net .ParseIP (localEndpoint .AdvertiseAddress )
45
- if ip == nil {
46
- return "" , errors .Errorf ("invalid value `%s` given for api.advertiseAddress" , localEndpoint .AdvertiseAddress )
47
- }
48
-
49
- // set the control-plane url using localEndpoint.AdvertiseAddress + the localEndpoint.BindPort
50
- controlPlaneURL := & url.URL {
51
- Scheme : "https" ,
52
- Host : net .JoinHostPort (ip .String (), bindPortString ),
37
+ // get the URL of the local endpoint
38
+ localAPIEndpoint , err := GetLocalAPIEndpoint (localEndpoint )
39
+ if err != nil {
40
+ return "" , err
53
41
}
54
42
55
43
// if the controlplane endpoint is defined
@@ -62,22 +50,32 @@ func GetControlPlaneEndpoint(controlPlaneEndpoint string, localEndpoint *kubeadm
62
50
}
63
51
64
52
// if a port is provided within the controlPlaneAddress warn the users we are using it, else use the bindport
53
+ localEndpointPort := strconv .Itoa (int (localEndpoint .BindPort ))
65
54
if port != "" {
66
- if port != bindPortString {
55
+ if port != localEndpointPort {
67
56
fmt .Println ("[endpoint] WARNING: port specified in controlPlaneEndpoint overrides bindPort in the controlplane address" )
68
57
}
69
58
} else {
70
- port = bindPortString
59
+ port = localEndpointPort
71
60
}
72
61
73
62
// overrides the control-plane url using the controlPlaneAddress (and eventually the bindport)
74
- controlPlaneURL = & url.URL {
75
- Scheme : "https" ,
76
- Host : net .JoinHostPort (host , port ),
77
- }
63
+ return formatURL (host , port ).String (), nil
78
64
}
79
65
80
- return controlPlaneURL .String (), nil
66
+ return localAPIEndpoint , nil
67
+ }
68
+
69
+ // GetLocalAPIEndpoint parses an APIEndpoint and returns it as a string,
70
+ // or returns and error in case it cannot be parsed.
71
+ func GetLocalAPIEndpoint (localEndpoint * kubeadmapi.APIEndpoint ) (string , error ) {
72
+ // get the URL of the local endpoint
73
+ localEndpointIP , localEndpointPort , err := parseAPIEndpoint (localEndpoint )
74
+ if err != nil {
75
+ return "" , err
76
+ }
77
+ url := formatURL (localEndpointIP .String (), localEndpointPort )
78
+ return url .String (), nil
81
79
}
82
80
83
81
// ParseHostPort parses a network address of the form "host:port", "ipv4:port", "[ipv6]:port" into host and port;
@@ -123,3 +121,29 @@ func ParsePort(port string) (int, error) {
123
121
124
122
return 0 , errors .New ("port must be a valid number between 1 and 65535, inclusive" )
125
123
}
124
+
125
+ // parseAPIEndpoint parses an APIEndpoint and returns the AdvertiseAddress as net.IP and the BindPort as string.
126
+ // If the BindPort or AdvertiseAddress are invalid it returns an error.
127
+ func parseAPIEndpoint (localEndpoint * kubeadmapi.APIEndpoint ) (net.IP , string , error ) {
128
+ // parse the bind port
129
+ bindPortString := strconv .Itoa (int (localEndpoint .BindPort ))
130
+ if _ , err := ParsePort (bindPortString ); err != nil {
131
+ return nil , "" , errors .Wrapf (err , "invalid value %q given for api.bindPort" , localEndpoint .BindPort )
132
+ }
133
+
134
+ // parse the AdvertiseAddress
135
+ var ip = net .ParseIP (localEndpoint .AdvertiseAddress )
136
+ if ip == nil {
137
+ return nil , "" , errors .Errorf ("invalid value `%s` given for api.advertiseAddress" , localEndpoint .AdvertiseAddress )
138
+ }
139
+
140
+ return ip , bindPortString , nil
141
+ }
142
+
143
+ // formatURL takes a host and a port string and creates a net.URL using https scheme
144
+ func formatURL (host , port string ) * url.URL {
145
+ return & url.URL {
146
+ Scheme : "https" ,
147
+ Host : net .JoinHostPort (host , port ),
148
+ }
149
+ }
0 commit comments