mirror of
https://github.com/kubernetes/client-go.git
synced 2025-06-27 15:39:39 +00:00
Merge pull request #113062 from alexzielenski/client-go-json-output
client-go: raw data options for fetching openapiv3 Kubernetes-commit: bfb78f3e74188b19e627c1126cafaa1be36901d4
This commit is contained in:
commit
42a0e1ca70
@ -29,6 +29,7 @@ import (
|
|||||||
|
|
||||||
"k8s.io/apimachinery/pkg/api/errors"
|
"k8s.io/apimachinery/pkg/api/errors"
|
||||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||||
|
"k8s.io/apimachinery/pkg/runtime"
|
||||||
"k8s.io/apimachinery/pkg/runtime/schema"
|
"k8s.io/apimachinery/pkg/runtime/schema"
|
||||||
"k8s.io/apimachinery/pkg/version"
|
"k8s.io/apimachinery/pkg/version"
|
||||||
"k8s.io/client-go/discovery"
|
"k8s.io/client-go/discovery"
|
||||||
@ -169,20 +170,31 @@ func TestOpenAPIDiskCache(t *testing.T) {
|
|||||||
paths, err := openapiClient.Paths()
|
paths, err := openapiClient.Paths()
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
assert.Equal(t, 1, fakeServer.RequestCounters["/openapi/v3"])
|
assert.Equal(t, 1, fakeServer.RequestCounters["/openapi/v3"])
|
||||||
|
|
||||||
require.Greater(t, len(paths), 0)
|
require.Greater(t, len(paths), 0)
|
||||||
|
|
||||||
|
contentTypes := []string{
|
||||||
|
runtime.ContentTypeJSON, openapi.ContentTypeOpenAPIV3PB,
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, contentType := range contentTypes {
|
||||||
|
t.Run(contentType, func(t *testing.T) {
|
||||||
|
// Reset all counters (cant just reset to nil since reference is shared)
|
||||||
|
for k := range fakeServer.RequestCounters {
|
||||||
|
delete(fakeServer.RequestCounters, k)
|
||||||
|
}
|
||||||
|
|
||||||
i := 0
|
i := 0
|
||||||
for k, v := range paths {
|
for k, v := range paths {
|
||||||
i++
|
i++
|
||||||
|
|
||||||
_, err = v.Schema()
|
_, err = v.Schema(contentType)
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
|
|
||||||
path := "/openapi/v3/" + strings.TrimPrefix(k, "/")
|
path := "/openapi/v3/" + strings.TrimPrefix(k, "/")
|
||||||
assert.Equal(t, 1, fakeServer.RequestCounters[path])
|
assert.Equal(t, 1, fakeServer.RequestCounters[path])
|
||||||
|
|
||||||
// Ensure schema call is served from memory
|
// Ensure schema call is served from memory
|
||||||
_, err = v.Schema()
|
_, err = v.Schema(contentType)
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
assert.Equal(t, 1, fakeServer.RequestCounters[path])
|
assert.Equal(t, 1, fakeServer.RequestCounters[path])
|
||||||
|
|
||||||
@ -196,11 +208,14 @@ func TestOpenAPIDiskCache(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Ensure schema call is still served from disk
|
// Ensure schema call is still served from disk
|
||||||
_, err = newPaths[k].Schema()
|
_, err = newPaths[k].Schema(contentType)
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
assert.Equal(t, 1+i, fakeServer.RequestCounters["/openapi/v3"])
|
assert.Equal(t, i, fakeServer.RequestCounters["/openapi/v3"])
|
||||||
assert.Equal(t, 1, fakeServer.RequestCounters[path])
|
assert.Equal(t, 1, fakeServer.RequestCounters[path])
|
||||||
}
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type fakeDiscoveryClient struct {
|
type fakeDiscoveryClient struct {
|
||||||
|
@ -27,8 +27,10 @@ import (
|
|||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
errorsutil "k8s.io/apimachinery/pkg/api/errors"
|
errorsutil "k8s.io/apimachinery/pkg/api/errors"
|
||||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||||
|
"k8s.io/apimachinery/pkg/runtime"
|
||||||
"k8s.io/client-go/discovery"
|
"k8s.io/client-go/discovery"
|
||||||
"k8s.io/client-go/discovery/fake"
|
"k8s.io/client-go/discovery/fake"
|
||||||
|
"k8s.io/client-go/openapi"
|
||||||
"k8s.io/client-go/rest"
|
"k8s.io/client-go/rest"
|
||||||
testutil "k8s.io/client-go/util/testing"
|
testutil "k8s.io/client-go/util/testing"
|
||||||
)
|
)
|
||||||
@ -415,8 +417,14 @@ func TestOpenAPIMemCache(t *testing.T) {
|
|||||||
paths, err := openapiClient.Paths()
|
paths, err := openapiClient.Paths()
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
contentTypes := []string{
|
||||||
|
runtime.ContentTypeJSON, openapi.ContentTypeOpenAPIV3PB,
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, contentType := range contentTypes {
|
||||||
|
t.Run(contentType, func(t *testing.T) {
|
||||||
for k, v := range paths {
|
for k, v := range paths {
|
||||||
original, err := v.Schema()
|
original, err := v.Schema(contentType)
|
||||||
if !assert.NoError(t, err) {
|
if !assert.NoError(t, err) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
@ -426,7 +434,7 @@ func TestOpenAPIMemCache(t *testing.T) {
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
schemaAgain, err := pathsAgain[k].Schema()
|
schemaAgain, err := pathsAgain[k].Schema(contentType)
|
||||||
if !assert.NoError(t, err) {
|
if !assert.NoError(t, err) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
@ -442,7 +450,7 @@ func TestOpenAPIMemCache(t *testing.T) {
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
schemaAgain, err = pathsAgain[k].Schema()
|
schemaAgain, err = pathsAgain[k].Schema(contentType)
|
||||||
if !assert.NoError(t, err) {
|
if !assert.NoError(t, err) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
@ -451,4 +459,6 @@ func TestOpenAPIMemCache(t *testing.T) {
|
|||||||
assert.True(t, reflect.ValueOf(original).Pointer() != reflect.ValueOf(schemaAgain).Pointer())
|
assert.True(t, reflect.ValueOf(original).Pointer() != reflect.ValueOf(schemaAgain).Pointer())
|
||||||
assert.Equal(t, original, schemaAgain)
|
assert.Equal(t, original, schemaAgain)
|
||||||
}
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -29,15 +29,20 @@ import (
|
|||||||
"github.com/gogo/protobuf/proto"
|
"github.com/gogo/protobuf/proto"
|
||||||
openapi_v2 "github.com/google/gnostic/openapiv2"
|
openapi_v2 "github.com/google/gnostic/openapiv2"
|
||||||
openapi_v3 "github.com/google/gnostic/openapiv3"
|
openapi_v3 "github.com/google/gnostic/openapiv3"
|
||||||
|
"github.com/google/go-cmp/cmp"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
golangproto "google.golang.org/protobuf/proto"
|
golangproto "google.golang.org/protobuf/proto"
|
||||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||||
|
"k8s.io/apimachinery/pkg/runtime"
|
||||||
"k8s.io/apimachinery/pkg/runtime/schema"
|
"k8s.io/apimachinery/pkg/runtime/schema"
|
||||||
"k8s.io/apimachinery/pkg/util/diff"
|
"k8s.io/apimachinery/pkg/util/diff"
|
||||||
"k8s.io/apimachinery/pkg/util/sets"
|
"k8s.io/apimachinery/pkg/util/sets"
|
||||||
"k8s.io/apimachinery/pkg/version"
|
"k8s.io/apimachinery/pkg/version"
|
||||||
|
"k8s.io/client-go/openapi"
|
||||||
restclient "k8s.io/client-go/rest"
|
restclient "k8s.io/client-go/rest"
|
||||||
testutil "k8s.io/client-go/util/testing"
|
testutil "k8s.io/client-go/util/testing"
|
||||||
|
"k8s.io/kube-openapi/pkg/spec3"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestGetServerVersion(t *testing.T) {
|
func TestGetServerVersion(t *testing.T) {
|
||||||
@ -537,7 +542,7 @@ func openapiSchemaFakeServer(t *testing.T) (*httptest.Server, error) {
|
|||||||
return server, nil
|
return server, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func openapiV3SchemaFakeServer(t *testing.T) (*httptest.Server, map[string]*openapi_v3.Document, error) {
|
func openapiV3SchemaFakeServer(t *testing.T) (*httptest.Server, map[string]*spec3.OpenAPI, error) {
|
||||||
res, err := testutil.NewFakeOpenAPIV3Server("testdata")
|
res, err := testutil.NewFakeOpenAPIV3Server("testdata")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, err
|
return nil, nil, err
|
||||||
@ -576,29 +581,69 @@ func TestGetOpenAPISchemaV3(t *testing.T) {
|
|||||||
t.Fatalf("unexpected error getting openapi: %v", err)
|
t.Fatalf("unexpected error getting openapi: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
contentTypes := []string{
|
||||||
|
runtime.ContentTypeJSON, openapi.ContentTypeOpenAPIV3PB,
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, contentType := range contentTypes {
|
||||||
|
t.Run(contentType, func(t *testing.T) {
|
||||||
for k, v := range paths {
|
for k, v := range paths {
|
||||||
actual, err := v.Schema()
|
actual, err := v.Schema(contentType)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
expected := testV3Specs[k]
|
expected := testV3Specs[k]
|
||||||
if !golangproto.Equal(expected, actual) {
|
switch contentType {
|
||||||
t.Fatalf("expected \n%v\n\ngot:\n%v", expected, actual)
|
|
||||||
|
case runtime.ContentTypeJSON:
|
||||||
|
var actualSpec spec3.OpenAPI
|
||||||
|
|
||||||
|
if err := json.Unmarshal(actual, &actualSpec); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Cannot use DeepEqual directly due to differences in how
|
||||||
|
// default key is being handled in gnostic vs kube-openapi
|
||||||
|
// Our test server parses the files in directly as gnostic
|
||||||
|
// which retains empty maps/lists, etc.
|
||||||
|
require.EqualValues(t, expected, &actualSpec)
|
||||||
|
case openapi.ContentTypeOpenAPIV3PB:
|
||||||
|
// Convert to JSON then to gnostic then to PB for comparison
|
||||||
|
expectedJSON, err := json.Marshal(expected)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
expectedGnostic, err := openapi_v3.ParseDocument(expectedJSON)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
expectedPB, err := golangproto.Marshal(expectedGnostic)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if !reflect.DeepEqual(expectedPB, actual) {
|
||||||
|
t.Fatalf("expected equal values: %v", cmp.Diff(expectedPB, actual))
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
panic(fmt.Errorf("unrecognized content type: %v", contentType))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Ensure that fetching schema once again does not return same instance
|
// Ensure that fetching schema once again does not return same instance
|
||||||
actualAgain, err := v.Schema()
|
actualAgain, err := v.Schema(contentType)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if reflect.ValueOf(actual).Pointer() == reflect.ValueOf(actualAgain).Pointer() {
|
if reflect.ValueOf(actual).Pointer() == reflect.ValueOf(actualAgain).Pointer() {
|
||||||
t.Fatal("expected schema not to be cached")
|
t.Fatal("expected schema not to be cached")
|
||||||
} else if !golangproto.Equal(actual, actualAgain) {
|
|
||||||
t.Fatal("expected schema values to be equal")
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestGetOpenAPISchemaForbiddenFallback(t *testing.T) {
|
func TestGetOpenAPISchemaForbiddenFallback(t *testing.T) {
|
||||||
|
@ -19,14 +19,18 @@ package cached
|
|||||||
import (
|
import (
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
openapi_v3 "github.com/google/gnostic/openapiv3"
|
|
||||||
"k8s.io/client-go/openapi"
|
"k8s.io/client-go/openapi"
|
||||||
)
|
)
|
||||||
|
|
||||||
type groupversion struct {
|
type groupversion struct {
|
||||||
delegate openapi.GroupVersion
|
delegate openapi.GroupVersion
|
||||||
once sync.Once
|
|
||||||
doc *openapi_v3.Document
|
lock sync.Mutex
|
||||||
|
docs map[string]docInfo
|
||||||
|
}
|
||||||
|
|
||||||
|
type docInfo struct {
|
||||||
|
data []byte
|
||||||
err error
|
err error
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -36,10 +40,19 @@ func newGroupVersion(delegate openapi.GroupVersion) *groupversion {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *groupversion) Schema() (*openapi_v3.Document, error) {
|
func (g *groupversion) Schema(contentType string) ([]byte, error) {
|
||||||
g.once.Do(func() {
|
g.lock.Lock()
|
||||||
g.doc, g.err = g.delegate.Schema()
|
defer g.lock.Unlock()
|
||||||
})
|
|
||||||
|
|
||||||
return g.doc, g.err
|
cachedInfo, ok := g.docs[contentType]
|
||||||
|
if !ok {
|
||||||
|
if g.docs == nil {
|
||||||
|
g.docs = make(map[string]docInfo)
|
||||||
|
}
|
||||||
|
|
||||||
|
cachedInfo.data, cachedInfo.err = g.delegate.Schema(contentType)
|
||||||
|
g.docs[contentType] = cachedInfo
|
||||||
|
}
|
||||||
|
|
||||||
|
return cachedInfo.data, cachedInfo.err
|
||||||
}
|
}
|
||||||
|
@ -19,15 +19,13 @@ package openapi
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
|
||||||
openapi_v3 "github.com/google/gnostic/openapiv3"
|
|
||||||
"google.golang.org/protobuf/proto"
|
|
||||||
"k8s.io/kube-openapi/pkg/handler3"
|
"k8s.io/kube-openapi/pkg/handler3"
|
||||||
)
|
)
|
||||||
|
|
||||||
const openAPIV3mimePb = "application/com.github.proto-openapi.spec.v3@v1.0+protobuf"
|
const ContentTypeOpenAPIV3PB = "application/com.github.proto-openapi.spec.v3@v1.0+protobuf"
|
||||||
|
|
||||||
type GroupVersion interface {
|
type GroupVersion interface {
|
||||||
Schema() (*openapi_v3.Document, error)
|
Schema(contentType string) ([]byte, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
type groupversion struct {
|
type groupversion struct {
|
||||||
@ -39,21 +37,10 @@ func newGroupVersion(client *client, item handler3.OpenAPIV3DiscoveryGroupVersio
|
|||||||
return &groupversion{client: client, item: item}
|
return &groupversion{client: client, item: item}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *groupversion) Schema() (*openapi_v3.Document, error) {
|
func (g *groupversion) Schema(contentType string) ([]byte, error) {
|
||||||
data, err := g.client.restClient.Get().
|
return g.client.restClient.Get().
|
||||||
RequestURI(g.item.ServerRelativeURL).
|
RequestURI(g.item.ServerRelativeURL).
|
||||||
SetHeader("Accept", openAPIV3mimePb).
|
SetHeader("Accept", contentType).
|
||||||
Do(context.TODO()).
|
Do(context.TODO()).
|
||||||
Raw()
|
Raw()
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
document := &openapi_v3.Document{}
|
|
||||||
if err := proto.Unmarshal(data, document); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return document, nil
|
|
||||||
}
|
}
|
||||||
|
@ -27,14 +27,13 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
openapi_v3 "github.com/google/gnostic/openapiv3"
|
|
||||||
"k8s.io/kube-openapi/pkg/handler3"
|
"k8s.io/kube-openapi/pkg/handler3"
|
||||||
"k8s.io/kube-openapi/pkg/spec3"
|
"k8s.io/kube-openapi/pkg/spec3"
|
||||||
)
|
)
|
||||||
|
|
||||||
type FakeOpenAPIServer struct {
|
type FakeOpenAPIServer struct {
|
||||||
HttpServer *httptest.Server
|
HttpServer *httptest.Server
|
||||||
ServedDocuments map[string]*openapi_v3.Document
|
ServedDocuments map[string]*spec3.OpenAPI
|
||||||
RequestCounters map[string]int
|
RequestCounters map[string]int
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -62,7 +61,7 @@ func NewFakeOpenAPIV3Server(specsPath string) (*FakeOpenAPIServer, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
grouped := make(map[string][]byte)
|
grouped := make(map[string][]byte)
|
||||||
var testV3Specs = make(map[string]*openapi_v3.Document)
|
var testV3Specs = make(map[string]*spec3.OpenAPI)
|
||||||
|
|
||||||
addSpec := func(path string) {
|
addSpec := func(path string) {
|
||||||
file, err := os.Open(path)
|
file, err := os.Open(path)
|
||||||
@ -97,11 +96,8 @@ func NewFakeOpenAPIV3Server(specsPath string) (*FakeOpenAPIServer, error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
gnosticSpec, err := openapi_v3.ParseDocument(jsonSpec)
|
|
||||||
if err != nil {
|
testV3Specs[gv] = spec
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
testV3Specs[gv] = gnosticSpec
|
|
||||||
openAPIVersionedService.UpdateGroupVersion(gv, spec)
|
openAPIVersionedService.UpdateGroupVersion(gv, spec)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user