Skip to content

Commit

Permalink
Use absolute path instead requestURI in openapiv3 discovery
Browse files Browse the repository at this point in the history
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
  • Loading branch information
ardaguclu authored and k8s-publishing-bot committed Apr 20, 2023
1 parent aab9b0a commit 6ce3e7e
Show file tree
Hide file tree
Showing 3 changed files with 145 additions and 10 deletions.
7 changes: 6 additions & 1 deletion openapi/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package openapi
import (
"context"
"encoding/json"
"strings"

"k8s.io/client-go/rest"
"k8s.io/kube-openapi/pkg/handler3"
Expand Down Expand Up @@ -58,7 +59,11 @@ func (c *client) Paths() (map[string]GroupVersion, error) {
// Create GroupVersions for each element of the result
result := map[string]GroupVersion{}
for k, v := range discoMap.Paths {
result[k] = newGroupVersion(c, v)
// If the server returned a URL rooted at /openapi/v3, preserve any additional client-side prefix.
// If the server returned a URL not rooted at /openapi/v3, treat it as an actual server-relative URL.
// See https://github.com/kubernetes/kubernetes/issues/117463 for details
useClientPrefix := strings.HasPrefix(v.ServerRelativeURL, "/openapi/v3")
result[k] = newGroupVersion(c, v, useClientPrefix)
}
return result, nil
}
42 changes: 33 additions & 9 deletions openapi/groupversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package openapi

import (
"context"
"net/url"

"k8s.io/kube-openapi/pkg/handler3"
)
Expand All @@ -29,18 +30,41 @@ type GroupVersion interface {
}

type groupversion struct {
client *client
item handler3.OpenAPIV3DiscoveryGroupVersion
client *client
item handler3.OpenAPIV3DiscoveryGroupVersion
useClientPrefix bool
}

func newGroupVersion(client *client, item handler3.OpenAPIV3DiscoveryGroupVersion) *groupversion {
return &groupversion{client: client, item: item}
func newGroupVersion(client *client, item handler3.OpenAPIV3DiscoveryGroupVersion, useClientPrefix bool) *groupversion {
return &groupversion{client: client, item: item, useClientPrefix: useClientPrefix}
}

func (g *groupversion) Schema(contentType string) ([]byte, error) {
return g.client.restClient.Get().
RequestURI(g.item.ServerRelativeURL).
SetHeader("Accept", contentType).
Do(context.TODO()).
Raw()
if !g.useClientPrefix {
return g.client.restClient.Get().
RequestURI(g.item.ServerRelativeURL).
SetHeader("Accept", contentType).
Do(context.TODO()).
Raw()
}

locator, err := url.Parse(g.item.ServerRelativeURL)
if err != nil {
return nil, err
}

path := g.client.restClient.Get().
AbsPath(locator.Path).
SetHeader("Accept", contentType)

// Other than root endpoints(openapiv3/apis), resources have hash query parameter to support etags.
// However, absPath does not support handling query parameters internally,
// so that hash query parameter is added manually
for k, value := range locator.Query() {
for _, v := range value {
path.Param(k, v)
}
}

return path.Do(context.TODO()).Raw()
}
106 changes: 106 additions & 0 deletions openapi/groupversion_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/*
Copyright 2023 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package openapi

import (
"fmt"
"net/http"
"net/http/httptest"
"testing"

appsv1 "k8s.io/api/apps/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/rest"
)

func TestGroupVersion(t *testing.T) {
tests := []struct {
name string
prefix string
serverReturnsPrefix bool
}{
{
name: "no prefix",
prefix: "",
serverReturnsPrefix: false,
},
{
name: "prefix not in discovery",
prefix: "/test-endpoint",
serverReturnsPrefix: false,
},
{
name: "prefix in discovery",
prefix: "/test-endpoint",
serverReturnsPrefix: true,
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch {
case r.URL.Path == test.prefix+"/openapi/v3/apis/apps/v1" && r.URL.RawQuery == "hash=014fbff9a07c":
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
w.Write([]byte(`{"openapi":"3.0.0","info":{"title":"Kubernetes","version":"unversioned"}}`))
case r.URL.Path == test.prefix+"/openapi/v3":
// return root content
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
if test.serverReturnsPrefix {
w.Write([]byte(fmt.Sprintf(`{"paths":{"apis/apps/v1":{"serverRelativeURL":"%s/openapi/v3/apis/apps/v1?hash=014fbff9a07c"}}}`, test.prefix)))
} else {
w.Write([]byte(`{"paths":{"apis/apps/v1":{"serverRelativeURL":"/openapi/v3/apis/apps/v1?hash=014fbff9a07c"}}}`))
}
default:
t.Errorf("unexpected request: %s", r.URL.String())
w.WriteHeader(http.StatusNotFound)
return
}
}))
defer server.Close()

c, err := rest.RESTClientFor(&rest.Config{
Host: server.URL + test.prefix,
ContentConfig: rest.ContentConfig{
NegotiatedSerializer: scheme.Codecs,
GroupVersion: &appsv1.SchemeGroupVersion,
},
})

if err != nil {
t.Fatalf("unexpected error occurred: %v", err)
}

openapiClient := NewClient(c)
paths, err := openapiClient.Paths()
if err != nil {
t.Fatalf("unexpected error occurred: %v", err)
}
schema, err := paths["apis/apps/v1"].Schema(runtime.ContentTypeJSON)
if err != nil {
t.Fatalf("unexpected error occurred: %v", err)
}
expectedResult := `{"openapi":"3.0.0","info":{"title":"Kubernetes","version":"unversioned"}}`
if string(schema) != expectedResult {
t.Fatalf("unexpected result actual: %s expected: %s", string(schema), expectedResult)
}
})
}
}

0 comments on commit 6ce3e7e

Please sign in to comment.