endpoint.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*
  2. Copyright 2017 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package util
  14. import (
  15. "fmt"
  16. "net"
  17. "net/url"
  18. "strconv"
  19. "github.com/pkg/errors"
  20. "k8s.io/apimachinery/pkg/util/validation"
  21. kubeadmapi "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm"
  22. )
  23. // GetControlPlaneEndpoint returns a properly formatted endpoint for the control plane built according following rules:
  24. // - If the controlPlaneEndpoint is defined, use it.
  25. // - if the controlPlaneEndpoint is defined but without a port number, use the controlPlaneEndpoint + localEndpoint.BindPort is used.
  26. // - Otherwise, in case the controlPlaneEndpoint is not defined, use the localEndpoint.AdvertiseAddress + the localEndpoint.BindPort.
  27. func GetControlPlaneEndpoint(controlPlaneEndpoint string, localEndpoint *kubeadmapi.APIEndpoint) (string, error) {
  28. // parse the bind port
  29. bindPortString := strconv.Itoa(int(localEndpoint.BindPort))
  30. if _, err := ParsePort(bindPortString); err != nil {
  31. return "", errors.Wrapf(err, "invalid value %q given for api.bindPort", localEndpoint.BindPort)
  32. }
  33. // parse the AdvertiseAddress
  34. var ip = net.ParseIP(localEndpoint.AdvertiseAddress)
  35. if ip == nil {
  36. return "", errors.Errorf("invalid value `%s` given for api.advertiseAddress", localEndpoint.AdvertiseAddress)
  37. }
  38. // set the control-plane url using localEndpoint.AdvertiseAddress + the localEndpoint.BindPort
  39. controlPlaneURL := &url.URL{
  40. Scheme: "https",
  41. Host: net.JoinHostPort(ip.String(), bindPortString),
  42. }
  43. // if the controlplane endpoint is defined
  44. if len(controlPlaneEndpoint) > 0 {
  45. // parse the controlplane endpoint
  46. var host, port string
  47. var err error
  48. if host, port, err = ParseHostPort(controlPlaneEndpoint); err != nil {
  49. return "", errors.Wrapf(err, "invalid value %q given for controlPlaneEndpoint", controlPlaneEndpoint)
  50. }
  51. // if a port is provided within the controlPlaneAddress warn the users we are using it, else use the bindport
  52. if port != "" {
  53. if port != bindPortString {
  54. fmt.Println("[endpoint] WARNING: port specified in controlPlaneEndpoint overrides bindPort in the controlplane address")
  55. }
  56. } else {
  57. port = bindPortString
  58. }
  59. // overrides the control-plane url using the controlPlaneAddress (and eventually the bindport)
  60. controlPlaneURL = &url.URL{
  61. Scheme: "https",
  62. Host: net.JoinHostPort(host, port),
  63. }
  64. }
  65. return controlPlaneURL.String(), nil
  66. }
  67. // ParseHostPort parses a network address of the form "host:port", "ipv4:port", "[ipv6]:port" into host and port;
  68. // ":port" can be eventually omitted.
  69. // If the string is not a valid representation of network address, ParseHostPort returns an error.
  70. func ParseHostPort(hostport string) (string, string, error) {
  71. var host, port string
  72. var err error
  73. // try to split host and port
  74. if host, port, err = net.SplitHostPort(hostport); err != nil {
  75. // if SplitHostPort returns an error, the entire hostport is considered as host
  76. host = hostport
  77. }
  78. // if port is defined, parse and validate it
  79. if port != "" {
  80. if _, err := ParsePort(port); err != nil {
  81. return "", "", errors.Errorf("hostport %s: port %s must be a valid number between 1 and 65535, inclusive", hostport, port)
  82. }
  83. }
  84. // if host is a valid IP, returns it
  85. if ip := net.ParseIP(host); ip != nil {
  86. return host, port, nil
  87. }
  88. // if host is a validate RFC-1123 subdomain, returns it
  89. if errs := validation.IsDNS1123Subdomain(host); len(errs) == 0 {
  90. return host, port, nil
  91. }
  92. return "", "", errors.Errorf("hostport %s: host '%s' must be a valid IP address or a valid RFC-1123 DNS subdomain", hostport, host)
  93. }
  94. // ParsePort parses a string representing a TCP port.
  95. // If the string is not a valid representation of a TCP port, ParsePort returns an error.
  96. func ParsePort(port string) (int, error) {
  97. portInt, err := strconv.Atoi(port)
  98. if err == nil && (1 <= portInt && portInt <= 65535) {
  99. return portInt, nil
  100. }
  101. return 0, errors.New("port must be a valid number between 1 and 65535, inclusive")
  102. }