url_utils.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /*
  2. Copyright 2016 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 restclient
  14. import (
  15. "fmt"
  16. "net/url"
  17. "path"
  18. "k8s.io/kubernetes/pkg/api/unversioned"
  19. )
  20. // DefaultServerURL converts a host, host:port, or URL string to the default base server API path
  21. // to use with a Client at a given API version following the standard conventions for a
  22. // Kubernetes API.
  23. func DefaultServerURL(host, apiPath string, groupVersion unversioned.GroupVersion, defaultTLS bool) (*url.URL, string, error) {
  24. if host == "" {
  25. return nil, "", fmt.Errorf("host must be a URL or a host:port pair")
  26. }
  27. base := host
  28. hostURL, err := url.Parse(base)
  29. if err != nil {
  30. return nil, "", err
  31. }
  32. if hostURL.Scheme == "" || hostURL.Host == "" {
  33. scheme := "http://"
  34. if defaultTLS {
  35. scheme = "https://"
  36. }
  37. hostURL, err = url.Parse(scheme + base)
  38. if err != nil {
  39. return nil, "", err
  40. }
  41. if hostURL.Path != "" && hostURL.Path != "/" {
  42. return nil, "", fmt.Errorf("host must be a URL or a host:port pair: %q", base)
  43. }
  44. }
  45. // hostURL.Path is optional; a non-empty Path is treated as a prefix that is to be applied to
  46. // all URIs used to access the host. this is useful when there's a proxy in front of the
  47. // apiserver that has relocated the apiserver endpoints, forwarding all requests from, for
  48. // example, /a/b/c to the apiserver. in this case the Path should be /a/b/c.
  49. //
  50. // if running without a frontend proxy (that changes the location of the apiserver), then
  51. // hostURL.Path should be blank.
  52. //
  53. // versionedAPIPath, a path relative to baseURL.Path, points to a versioned API base
  54. versionedAPIPath := path.Join("/", apiPath)
  55. // Add the version to the end of the path
  56. if len(groupVersion.Group) > 0 {
  57. versionedAPIPath = path.Join(versionedAPIPath, groupVersion.Group, groupVersion.Version)
  58. } else {
  59. versionedAPIPath = path.Join(versionedAPIPath, groupVersion.Version)
  60. }
  61. return hostURL, versionedAPIPath, nil
  62. }
  63. // defaultServerUrlFor is shared between IsConfigTransportTLS and RESTClientFor. It
  64. // requires Host and Version to be set prior to being called.
  65. func defaultServerUrlFor(config *Config) (*url.URL, string, error) {
  66. // TODO: move the default to secure when the apiserver supports TLS by default
  67. // config.Insecure is taken to mean "I want HTTPS but don't bother checking the certs against a CA."
  68. hasCA := len(config.CAFile) != 0 || len(config.CAData) != 0
  69. hasCert := len(config.CertFile) != 0 || len(config.CertData) != 0
  70. defaultTLS := hasCA || hasCert || config.Insecure
  71. host := config.Host
  72. if host == "" {
  73. host = "localhost"
  74. }
  75. if config.GroupVersion != nil {
  76. return DefaultServerURL(host, config.APIPath, *config.GroupVersion, defaultTLS)
  77. }
  78. return DefaultServerURL(host, config.APIPath, unversioned.GroupVersion{}, defaultTLS)
  79. }