blob: a56d1838d8fdbd9189904139b6f4bf94bc28ae92 [file] [log] [blame]
Matthias Andreas Benkard832a54e2019-01-29 09:27:38 +01001/*
2Copyright 2016 The Kubernetes Authors.
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16
17package rest
18
19import (
20 "fmt"
21 "net/url"
22 "path"
23
24 "k8s.io/apimachinery/pkg/runtime/schema"
25)
26
27// DefaultServerURL converts a host, host:port, or URL string to the default base server API path
28// to use with a Client at a given API version following the standard conventions for a
29// Kubernetes API.
30func DefaultServerURL(host, apiPath string, groupVersion schema.GroupVersion, defaultTLS bool) (*url.URL, string, error) {
31 if host == "" {
32 return nil, "", fmt.Errorf("host must be a URL or a host:port pair")
33 }
34 base := host
35 hostURL, err := url.Parse(base)
36 if err != nil || hostURL.Scheme == "" || hostURL.Host == "" {
37 scheme := "http://"
38 if defaultTLS {
39 scheme = "https://"
40 }
41 hostURL, err = url.Parse(scheme + base)
42 if err != nil {
43 return nil, "", err
44 }
45 if hostURL.Path != "" && hostURL.Path != "/" {
46 return nil, "", fmt.Errorf("host must be a URL or a host:port pair: %q", base)
47 }
48 }
49
50 // hostURL.Path is optional; a non-empty Path is treated as a prefix that is to be applied to
51 // all URIs used to access the host. this is useful when there's a proxy in front of the
52 // apiserver that has relocated the apiserver endpoints, forwarding all requests from, for
53 // example, /a/b/c to the apiserver. in this case the Path should be /a/b/c.
54 //
55 // if running without a frontend proxy (that changes the location of the apiserver), then
56 // hostURL.Path should be blank.
57 //
58 // versionedAPIPath, a path relative to baseURL.Path, points to a versioned API base
59 versionedAPIPath := DefaultVersionedAPIPath(apiPath, groupVersion)
60
61 return hostURL, versionedAPIPath, nil
62}
63
64// DefaultVersionedAPIPathFor constructs the default path for the given group version, assuming the given
65// API path, following the standard conventions of the Kubernetes API.
66func DefaultVersionedAPIPath(apiPath string, groupVersion schema.GroupVersion) string {
67 versionedAPIPath := path.Join("/", apiPath)
68
69 // Add the version to the end of the path
70 if len(groupVersion.Group) > 0 {
71 versionedAPIPath = path.Join(versionedAPIPath, groupVersion.Group, groupVersion.Version)
72
73 } else {
74 versionedAPIPath = path.Join(versionedAPIPath, groupVersion.Version)
75 }
76
77 return versionedAPIPath
78}
79
80// defaultServerUrlFor is shared between IsConfigTransportTLS and RESTClientFor. It
81// requires Host and Version to be set prior to being called.
82func defaultServerUrlFor(config *Config) (*url.URL, string, error) {
83 // TODO: move the default to secure when the apiserver supports TLS by default
84 // config.Insecure is taken to mean "I want HTTPS but don't bother checking the certs against a CA."
85 hasCA := len(config.CAFile) != 0 || len(config.CAData) != 0
86 hasCert := len(config.CertFile) != 0 || len(config.CertData) != 0
87 defaultTLS := hasCA || hasCert || config.Insecure
88 host := config.Host
89 if host == "" {
90 host = "localhost"
91 }
92
93 if config.GroupVersion != nil {
94 return DefaultServerURL(host, config.APIPath, *config.GroupVersion, defaultTLS)
95 }
96 return DefaultServerURL(host, config.APIPath, schema.GroupVersion{}, defaultTLS)
97}