url_utils.go 3.5 KB

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