...
1
16
17 package rest
18
19 import (
20 "fmt"
21 "net/url"
22 "path"
23
24 "k8s.io/apimachinery/pkg/runtime/schema"
25 )
26
27
28
29
30 func 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
51
52
53
54
55
56
57
58
59 versionedAPIPath := DefaultVersionedAPIPath(apiPath, groupVersion)
60
61 return hostURL, versionedAPIPath, nil
62 }
63
64
65
66 func DefaultVersionedAPIPath(apiPath string, groupVersion schema.GroupVersion) string {
67 versionedAPIPath := path.Join("/", apiPath)
68
69
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
81
82 func DefaultServerUrlFor(config *Config) (*url.URL, string, error) {
83
84
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 }
98
View as plain text