mirror of https://github.com/linkerd/linkerd2.git
Build request urls in the most generic possible way (#2206)
Build request urls in the most generic possible way Fixes #2132 Signed-off-by: Alejandro Pedraza <alejandro@buoyant.io>
This commit is contained in:
parent
02f128ec5a
commit
0b42a02bb9
|
@ -126,7 +126,7 @@ func (kubeAPI *KubernetesAPI) URLFor(namespace string, extraPathStartingWithSlas
|
||||||
}
|
}
|
||||||
|
|
||||||
func (kubeAPI *KubernetesAPI) getRequest(ctx context.Context, client *http.Client, path string) (*http.Response, error) {
|
func (kubeAPI *KubernetesAPI) getRequest(ctx context.Context, client *http.Client, path string) (*http.Response, error) {
|
||||||
endpoint, err := url.Parse(kubeAPI.Host + path)
|
endpoint, err := BuildURL(kubeAPI.Host, path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,17 +10,27 @@ func TestKubernetesApiUrlFor(t *testing.T) {
|
||||||
const extraPath = "/some/extra/path"
|
const extraPath = "/some/extra/path"
|
||||||
|
|
||||||
t.Run("Returns base config containing k8s endpoint listed in config.test", func(t *testing.T) {
|
t.Run("Returns base config containing k8s endpoint listed in config.test", func(t *testing.T) {
|
||||||
expected := fmt.Sprintf("https://55.197.171.239/api/v1/namespaces/%s%s", namespace, extraPath)
|
tests := []struct {
|
||||||
api, err := NewAPI("testdata/config.test", "")
|
server string
|
||||||
if err != nil {
|
kubeContext string
|
||||||
t.Fatalf("Unexpected error creating Kubernetes API: %+v", err)
|
}{
|
||||||
|
{"https://55.197.171.239", ""},
|
||||||
|
{"https://162.128.50.11", "clusterTrailingSlash"},
|
||||||
}
|
}
|
||||||
actualURL, err := api.URLFor(namespace, extraPath)
|
|
||||||
if err != nil {
|
for _, test := range tests {
|
||||||
t.Fatalf("Unexpected error generating URL: %+v", err)
|
expected := fmt.Sprintf("%s/api/v1/namespaces/%s%s", test.server, namespace, extraPath)
|
||||||
}
|
api, err := NewAPI("testdata/config.test", test.kubeContext)
|
||||||
if actualURL.String() != expected {
|
if err != nil {
|
||||||
t.Fatalf("Expected generated URL to be [%s], but got [%s]", expected, actualURL.String())
|
t.Fatalf("Unexpected error creating Kubernetes API: %+v", err)
|
||||||
|
}
|
||||||
|
actualURL, err := api.URLFor(namespace, extraPath)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Unexpected error generating URL: %+v", err)
|
||||||
|
}
|
||||||
|
if actualURL.String() != expected {
|
||||||
|
t.Fatalf("Expected generated URL to be [%s], but got [%s]", expected, actualURL.String())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
|
@ -73,12 +73,7 @@ func generateKubernetesAPIBaseURLFor(schemeHostAndPort string, namespace string,
|
||||||
}
|
}
|
||||||
|
|
||||||
func generateBaseKubernetesAPIURL(schemeHostAndPort string) (*url.URL, error) {
|
func generateBaseKubernetesAPIURL(schemeHostAndPort string) (*url.URL, error) {
|
||||||
urlString := fmt.Sprintf("%s/api/v1/", schemeHostAndPort)
|
return BuildURL(schemeHostAndPort, "/api/v1/")
|
||||||
url, err := url.Parse(urlString)
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("error generating base URL for Kubernetes API from [%s]", urlString)
|
|
||||||
}
|
|
||||||
return url, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetConfig returns kubernetes config based on the current environment.
|
// GetConfig returns kubernetes config based on the current environment.
|
||||||
|
@ -171,3 +166,19 @@ func KindToL5DLabel(k8sKind string) string {
|
||||||
}
|
}
|
||||||
return k8sKind
|
return k8sKind
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// BuildURL returns an abosolute URL from a reference URL. It parses a base
|
||||||
|
// rawurl and reference rawurl. Then, it tries to resolve the reference from
|
||||||
|
// the absolute base.
|
||||||
|
func BuildURL(base string, ref string) (*url.URL, error) {
|
||||||
|
u, err := url.Parse(ref)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("error generating reference URL for endpoint from [%s]", base)
|
||||||
|
}
|
||||||
|
b, err := url.Parse(base)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("error generating base URL for endpoint from [%s]", base)
|
||||||
|
}
|
||||||
|
url := b.ResolveReference(u)
|
||||||
|
return url, nil
|
||||||
|
}
|
||||||
|
|
|
@ -16,6 +16,10 @@ clusters:
|
||||||
certificate-authority-data: cXVlIHBhcmFkYSBhdHJhc2FkYQ==
|
certificate-authority-data: cXVlIHBhcmFkYSBhdHJhc2FkYQ==
|
||||||
server: https://162.128.50.10
|
server: https://162.128.50.10
|
||||||
name: cluster4
|
name: cluster4
|
||||||
|
- cluster:
|
||||||
|
certificate-authority-data: cXVlIHBhcmFkYSBhdHJhc2FkYQ==
|
||||||
|
server: https://162.128.50.11/
|
||||||
|
name: clusterTrailingSlash
|
||||||
contexts:
|
contexts:
|
||||||
- context:
|
- context:
|
||||||
cluster: cluster3
|
cluster: cluster3
|
||||||
|
@ -38,6 +42,10 @@ contexts:
|
||||||
cluster: cluster4
|
cluster: cluster4
|
||||||
user: cluster4
|
user: cluster4
|
||||||
name: cluster4
|
name: cluster4
|
||||||
|
- context:
|
||||||
|
cluster: clusterTrailingSlash
|
||||||
|
user: clusterTrailingSlash
|
||||||
|
name: clusterTrailingSlash
|
||||||
current-context: cluster1
|
current-context: cluster1
|
||||||
kind: Config
|
kind: Config
|
||||||
preferences: {}
|
preferences: {}
|
||||||
|
@ -86,3 +94,14 @@ users:
|
||||||
expiry-key: '{.credential.token_expiry}'
|
expiry-key: '{.credential.token_expiry}'
|
||||||
token-key: '{.credential.access_token}'
|
token-key: '{.credential.access_token}'
|
||||||
name: gcp
|
name: gcp
|
||||||
|
- name: clusterTrailingSlash
|
||||||
|
user:
|
||||||
|
auth-provider:
|
||||||
|
config:
|
||||||
|
access-token: 4cc3sspassatempoq
|
||||||
|
cmd-args: config config-helper --format=json
|
||||||
|
cmd-path: /Users/bobojones/bin/google-cloud-sdk/bin/gcloud
|
||||||
|
expiry: 2017-11-22 22:13:05
|
||||||
|
expiry-key: '{.credential.token_expiry}'
|
||||||
|
token-key: '{.credential.access_token}'
|
||||||
|
name: gcp
|
||||||
|
|
Loading…
Reference in New Issue