Skip to content

Commit 6ce3e7e

Browse files
ardagucluk8s-publishing-bot
authored andcommitted
Use absolute path instead requestURI in openapiv3 discovery
Currently, openapiv3 discovery uses requestURI to discover resources. However, that does not work when the rest endpoint contains prefixes (e.g. `http://localhost/test-endpoint/`). Because requestURI overwrites prefixes also in rest endpoint (e.g. `http://localhost/openapiv3/apis/apps/v1`). Since `absPath` keeps the prefixes in the rest endpoint, this PR changes to absPath instead requestURI. Kubernetes-commit: ee1d7eb5d82f3b2a76afc57bc33bc7e08c34bf27
1 parent aab9b0a commit 6ce3e7e

File tree

3 files changed

+145
-10
lines changed

3 files changed

+145
-10
lines changed

openapi/client.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package openapi
1919
import (
2020
"context"
2121
"encoding/json"
22+
"strings"
2223

2324
"k8s.io/client-go/rest"
2425
"k8s.io/kube-openapi/pkg/handler3"
@@ -58,7 +59,11 @@ func (c *client) Paths() (map[string]GroupVersion, error) {
5859
// Create GroupVersions for each element of the result
5960
result := map[string]GroupVersion{}
6061
for k, v := range discoMap.Paths {
61-
result[k] = newGroupVersion(c, v)
62+
// If the server returned a URL rooted at /openapi/v3, preserve any additional client-side prefix.
63+
// If the server returned a URL not rooted at /openapi/v3, treat it as an actual server-relative URL.
64+
// See https://github.com/kubernetes/kubernetes/issues/117463 for details
65+
useClientPrefix := strings.HasPrefix(v.ServerRelativeURL, "/openapi/v3")
66+
result[k] = newGroupVersion(c, v, useClientPrefix)
6267
}
6368
return result, nil
6469
}

openapi/groupversion.go

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package openapi
1818

1919
import (
2020
"context"
21+
"net/url"
2122

2223
"k8s.io/kube-openapi/pkg/handler3"
2324
)
@@ -29,18 +30,41 @@ type GroupVersion interface {
2930
}
3031

3132
type groupversion struct {
32-
client *client
33-
item handler3.OpenAPIV3DiscoveryGroupVersion
33+
client *client
34+
item handler3.OpenAPIV3DiscoveryGroupVersion
35+
useClientPrefix bool
3436
}
3537

36-
func newGroupVersion(client *client, item handler3.OpenAPIV3DiscoveryGroupVersion) *groupversion {
37-
return &groupversion{client: client, item: item}
38+
func newGroupVersion(client *client, item handler3.OpenAPIV3DiscoveryGroupVersion, useClientPrefix bool) *groupversion {
39+
return &groupversion{client: client, item: item, useClientPrefix: useClientPrefix}
3840
}
3941

4042
func (g *groupversion) Schema(contentType string) ([]byte, error) {
41-
return g.client.restClient.Get().
42-
RequestURI(g.item.ServerRelativeURL).
43-
SetHeader("Accept", contentType).
44-
Do(context.TODO()).
45-
Raw()
43+
if !g.useClientPrefix {
44+
return g.client.restClient.Get().
45+
RequestURI(g.item.ServerRelativeURL).
46+
SetHeader("Accept", contentType).
47+
Do(context.TODO()).
48+
Raw()
49+
}
50+
51+
locator, err := url.Parse(g.item.ServerRelativeURL)
52+
if err != nil {
53+
return nil, err
54+
}
55+
56+
path := g.client.restClient.Get().
57+
AbsPath(locator.Path).
58+
SetHeader("Accept", contentType)
59+
60+
// Other than root endpoints(openapiv3/apis), resources have hash query parameter to support etags.
61+
// However, absPath does not support handling query parameters internally,
62+
// so that hash query parameter is added manually
63+
for k, value := range locator.Query() {
64+
for _, v := range value {
65+
path.Param(k, v)
66+
}
67+
}
68+
69+
return path.Do(context.TODO()).Raw()
4670
}

openapi/groupversion_test.go

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
/*
2+
Copyright 2023 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package openapi
18+
19+
import (
20+
"fmt"
21+
"net/http"
22+
"net/http/httptest"
23+
"testing"
24+
25+
appsv1 "k8s.io/api/apps/v1"
26+
"k8s.io/apimachinery/pkg/runtime"
27+
"k8s.io/client-go/kubernetes/scheme"
28+
"k8s.io/client-go/rest"
29+
)
30+
31+
func TestGroupVersion(t *testing.T) {
32+
tests := []struct {
33+
name string
34+
prefix string
35+
serverReturnsPrefix bool
36+
}{
37+
{
38+
name: "no prefix",
39+
prefix: "",
40+
serverReturnsPrefix: false,
41+
},
42+
{
43+
name: "prefix not in discovery",
44+
prefix: "/test-endpoint",
45+
serverReturnsPrefix: false,
46+
},
47+
{
48+
name: "prefix in discovery",
49+
prefix: "/test-endpoint",
50+
serverReturnsPrefix: true,
51+
},
52+
}
53+
54+
for _, test := range tests {
55+
t.Run(test.name, func(t *testing.T) {
56+
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
57+
switch {
58+
case r.URL.Path == test.prefix+"/openapi/v3/apis/apps/v1" && r.URL.RawQuery == "hash=014fbff9a07c":
59+
w.Header().Set("Content-Type", "application/json")
60+
w.WriteHeader(http.StatusOK)
61+
w.Write([]byte(`{"openapi":"3.0.0","info":{"title":"Kubernetes","version":"unversioned"}}`))
62+
case r.URL.Path == test.prefix+"/openapi/v3":
63+
// return root content
64+
w.Header().Set("Content-Type", "application/json")
65+
w.WriteHeader(http.StatusOK)
66+
if test.serverReturnsPrefix {
67+
w.Write([]byte(fmt.Sprintf(`{"paths":{"apis/apps/v1":{"serverRelativeURL":"%s/openapi/v3/apis/apps/v1?hash=014fbff9a07c"}}}`, test.prefix)))
68+
} else {
69+
w.Write([]byte(`{"paths":{"apis/apps/v1":{"serverRelativeURL":"/openapi/v3/apis/apps/v1?hash=014fbff9a07c"}}}`))
70+
}
71+
default:
72+
t.Errorf("unexpected request: %s", r.URL.String())
73+
w.WriteHeader(http.StatusNotFound)
74+
return
75+
}
76+
}))
77+
defer server.Close()
78+
79+
c, err := rest.RESTClientFor(&rest.Config{
80+
Host: server.URL + test.prefix,
81+
ContentConfig: rest.ContentConfig{
82+
NegotiatedSerializer: scheme.Codecs,
83+
GroupVersion: &appsv1.SchemeGroupVersion,
84+
},
85+
})
86+
87+
if err != nil {
88+
t.Fatalf("unexpected error occurred: %v", err)
89+
}
90+
91+
openapiClient := NewClient(c)
92+
paths, err := openapiClient.Paths()
93+
if err != nil {
94+
t.Fatalf("unexpected error occurred: %v", err)
95+
}
96+
schema, err := paths["apis/apps/v1"].Schema(runtime.ContentTypeJSON)
97+
if err != nil {
98+
t.Fatalf("unexpected error occurred: %v", err)
99+
}
100+
expectedResult := `{"openapi":"3.0.0","info":{"title":"Kubernetes","version":"unversioned"}}`
101+
if string(schema) != expectedResult {
102+
t.Fatalf("unexpected result actual: %s expected: %s", string(schema), expectedResult)
103+
}
104+
})
105+
}
106+
}

0 commit comments

Comments
 (0)