2017-06-28 17:20:13 +00:00
|
|
|
/*
|
|
|
|
Copyright 2017 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 util
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net"
|
2018-04-22 22:16:13 +00:00
|
|
|
"net/url"
|
2017-06-28 17:20:13 +00:00
|
|
|
"strconv"
|
|
|
|
|
2018-10-26 03:43:06 +00:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
|
2018-01-31 20:19:46 +00:00
|
|
|
"k8s.io/apimachinery/pkg/util/validation"
|
2017-06-28 17:20:13 +00:00
|
|
|
kubeadmapi "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm"
|
|
|
|
)
|
|
|
|
|
2019-02-22 04:19:35 +00:00
|
|
|
// GetControlPlaneEndpoint returns a properly formatted endpoint for the control plane built according following rules:
|
2018-11-15 12:12:33 +00:00
|
|
|
// - If the controlPlaneEndpoint is defined, use it.
|
|
|
|
// - if the controlPlaneEndpoint is defined but without a port number, use the controlPlaneEndpoint + localEndpoint.BindPort is used.
|
|
|
|
// - Otherwise, in case the controlPlaneEndpoint is not defined, use the localEndpoint.AdvertiseAddress + the localEndpoint.BindPort.
|
2019-02-22 04:19:35 +00:00
|
|
|
func GetControlPlaneEndpoint(controlPlaneEndpoint string, localEndpoint *kubeadmapi.APIEndpoint) (string, error) {
|
2018-04-22 22:16:13 +00:00
|
|
|
// parse the bind port
|
2018-11-15 12:12:33 +00:00
|
|
|
bindPortString := strconv.Itoa(int(localEndpoint.BindPort))
|
2018-08-24 07:42:23 +00:00
|
|
|
if _, err := ParsePort(bindPortString); err != nil {
|
2018-11-15 12:12:33 +00:00
|
|
|
return "", errors.Wrapf(err, "invalid value %q given for api.bindPort", localEndpoint.BindPort)
|
2018-04-22 22:16:13 +00:00
|
|
|
}
|
2018-01-31 20:19:46 +00:00
|
|
|
|
2018-04-22 22:16:13 +00:00
|
|
|
// parse the AdvertiseAddress
|
2018-11-15 12:12:33 +00:00
|
|
|
var ip = net.ParseIP(localEndpoint.AdvertiseAddress)
|
2018-04-22 22:16:13 +00:00
|
|
|
if ip == nil {
|
2018-11-15 12:12:33 +00:00
|
|
|
return "", errors.Errorf("invalid value `%s` given for api.advertiseAddress", localEndpoint.AdvertiseAddress)
|
2017-06-28 17:20:13 +00:00
|
|
|
}
|
|
|
|
|
2019-02-22 04:19:35 +00:00
|
|
|
// set the control-plane url using localEndpoint.AdvertiseAddress + the localEndpoint.BindPort
|
|
|
|
controlPlaneURL := &url.URL{
|
2018-04-22 22:16:13 +00:00
|
|
|
Scheme: "https",
|
2018-08-24 07:42:23 +00:00
|
|
|
Host: net.JoinHostPort(ip.String(), bindPortString),
|
2018-04-22 22:16:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// if the controlplane endpoint is defined
|
2018-11-15 12:12:33 +00:00
|
|
|
if len(controlPlaneEndpoint) > 0 {
|
2018-04-22 22:16:13 +00:00
|
|
|
// parse the controlplane endpoint
|
|
|
|
var host, port string
|
|
|
|
var err error
|
2018-11-15 12:12:33 +00:00
|
|
|
if host, port, err = ParseHostPort(controlPlaneEndpoint); err != nil {
|
|
|
|
return "", errors.Wrapf(err, "invalid value %q given for controlPlaneEndpoint", controlPlaneEndpoint)
|
2018-04-10 05:26:20 +00:00
|
|
|
}
|
2018-04-22 22:16:13 +00:00
|
|
|
|
|
|
|
// if a port is provided within the controlPlaneAddress warn the users we are using it, else use the bindport
|
|
|
|
if port != "" {
|
2018-09-04 07:09:55 +00:00
|
|
|
if port != bindPortString {
|
|
|
|
fmt.Println("[endpoint] WARNING: port specified in controlPlaneEndpoint overrides bindPort in the controlplane address")
|
|
|
|
}
|
2018-04-22 22:16:13 +00:00
|
|
|
} else {
|
2018-08-24 07:42:23 +00:00
|
|
|
port = bindPortString
|
2018-01-31 20:19:46 +00:00
|
|
|
}
|
2018-04-22 22:16:13 +00:00
|
|
|
|
2019-02-22 04:19:35 +00:00
|
|
|
// overrides the control-plane url using the controlPlaneAddress (and eventually the bindport)
|
|
|
|
controlPlaneURL = &url.URL{
|
2018-04-22 22:16:13 +00:00
|
|
|
Scheme: "https",
|
|
|
|
Host: net.JoinHostPort(host, port),
|
2018-01-31 20:19:46 +00:00
|
|
|
}
|
2017-06-28 17:20:13 +00:00
|
|
|
}
|
|
|
|
|
2019-02-22 04:19:35 +00:00
|
|
|
return controlPlaneURL.String(), nil
|
2018-04-22 22:16:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ParseHostPort parses a network address of the form "host:port", "ipv4:port", "[ipv6]:port" into host and port;
|
|
|
|
// ":port" can be eventually omitted.
|
|
|
|
// If the string is not a valid representation of network address, ParseHostPort returns an error.
|
|
|
|
func ParseHostPort(hostport string) (string, string, error) {
|
|
|
|
var host, port string
|
|
|
|
var err error
|
|
|
|
|
|
|
|
// try to split host and port
|
|
|
|
if host, port, err = net.SplitHostPort(hostport); err != nil {
|
|
|
|
// if SplitHostPort returns an error, the entire hostport is considered as host
|
|
|
|
host = hostport
|
|
|
|
}
|
|
|
|
|
|
|
|
// if port is defined, parse and validate it
|
|
|
|
if port != "" {
|
2018-08-24 07:42:23 +00:00
|
|
|
if _, err := ParsePort(port); err != nil {
|
2018-11-20 11:10:59 +00:00
|
|
|
return "", "", errors.Errorf("hostport %s: port %s must be a valid number between 1 and 65535, inclusive", hostport, port)
|
2018-04-10 05:26:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-22 22:16:13 +00:00
|
|
|
// if host is a valid IP, returns it
|
|
|
|
if ip := net.ParseIP(host); ip != nil {
|
|
|
|
return host, port, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// if host is a validate RFC-1123 subdomain, returns it
|
|
|
|
if errs := validation.IsDNS1123Subdomain(host); len(errs) == 0 {
|
|
|
|
return host, port, nil
|
|
|
|
}
|
|
|
|
|
2018-11-20 11:10:59 +00:00
|
|
|
return "", "", errors.Errorf("hostport %s: host '%s' must be a valid IP address or a valid RFC-1123 DNS subdomain", hostport, host)
|
2018-04-22 22:16:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ParsePort parses a string representing a TCP port.
|
|
|
|
// If the string is not a valid representation of a TCP port, ParsePort returns an error.
|
2018-08-24 07:42:23 +00:00
|
|
|
func ParsePort(port string) (int, error) {
|
|
|
|
portInt, err := strconv.Atoi(port)
|
|
|
|
if err == nil && (1 <= portInt && portInt <= 65535) {
|
2018-04-22 22:16:13 +00:00
|
|
|
return portInt, nil
|
2017-06-28 17:20:13 +00:00
|
|
|
}
|
|
|
|
|
2018-10-26 03:43:06 +00:00
|
|
|
return 0, errors.New("port must be a valid number between 1 and 65535, inclusive")
|
2017-06-28 17:20:13 +00:00
|
|
|
}
|