add fetching into discovery client for OpenAPI v3

reflect latest struct changes

use correct discovery openapi test data layout

make the OpenAPIv3 interface less blue

field grouping

add copyrights

implement cached discovery client

add cached discovery tests

address review feedback

Kubernetes-commit: 075866b3e3ea029c243d82d8d6eb99e96d9c49d3
This commit is contained in:
Alexander Zielenski
2022-03-22 10:40:56 -07:00
committed by Kubernetes Publisher
parent 92adc4de69
commit 018cf8ace6
18 changed files with 680 additions and 11 deletions

View File

@@ -20,19 +20,24 @@ import (
"io/ioutil"
"os"
"path/filepath"
"strings"
"testing"
"time"
openapi_v2 "github.com/google/gnostic/openapiv2"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/version"
"k8s.io/client-go/discovery"
"k8s.io/client-go/openapi"
"k8s.io/client-go/rest"
restclient "k8s.io/client-go/rest"
"k8s.io/client-go/rest/fake"
testutil "k8s.io/client-go/util/testing"
)
func TestCachedDiscoveryClient_Fresh(t *testing.T) {
@@ -123,6 +128,83 @@ func TestNewCachedDiscoveryClient_PathPerm(t *testing.T) {
assert.NoError(err)
}
// Tests that schema instances returned by openapi cached and returned after
// successive calls
func TestOpenAPIDiskCache(t *testing.T) {
// Create discovery cache dir (unused)
discoCache, err := ioutil.TempDir("", "")
require.NoError(t, err)
os.RemoveAll(discoCache)
defer os.RemoveAll(discoCache)
// Create http cache dir
httpCache, err := ioutil.TempDir("", "")
require.NoError(t, err)
os.RemoveAll(httpCache)
defer os.RemoveAll(httpCache)
// Start test OpenAPI server
fakeServer, err := testutil.NewFakeOpenAPIV3Server("../../testdata")
require.NoError(t, err)
defer fakeServer.HttpServer.Close()
require.Greater(t, len(fakeServer.ServedDocuments), 0)
client, err := NewCachedDiscoveryClientForConfig(
&rest.Config{Host: fakeServer.HttpServer.URL},
discoCache,
httpCache,
1*time.Nanosecond,
)
require.NoError(t, err)
openapiClient := client.OpenAPIV3()
// Ensure initial Paths call hits server
_, err = openapiClient.Paths()
require.NoError(t, err)
assert.Equal(t, 1, fakeServer.RequestCounters["/openapi/v3"])
// Ensure Paths call does hits server again
// This is expected since openapiClient is the same instance, so Paths()
// should be cached in memory.
paths, err := openapiClient.Paths()
require.NoError(t, err)
assert.Equal(t, 1, fakeServer.RequestCounters["/openapi/v3"])
require.Greater(t, len(paths), 0)
i := 0
for k, v := range paths {
i++
_, err = v.Schema()
assert.NoError(t, err)
path := "/openapi/v3/" + strings.TrimPrefix(k, "/")
assert.Equal(t, 1, fakeServer.RequestCounters[path])
// Ensure schema call is served from memory
_, err = v.Schema()
assert.NoError(t, err)
assert.Equal(t, 1, fakeServer.RequestCounters[path])
client.Invalidate()
// Refetch the schema from a new openapi client to try to force a new
// http request
newPaths, err := client.OpenAPIV3().Paths()
if !assert.NoError(t, err) {
continue
}
// Ensure schema call is still served from disk
_, err = newPaths[k].Schema()
assert.NoError(t, err)
assert.Equal(t, 1+i, fakeServer.RequestCounters["/openapi/v3"])
assert.Equal(t, 1, fakeServer.RequestCounters[path])
}
}
type fakeDiscoveryClient struct {
groupCalls int
resourceCalls int
@@ -207,3 +289,7 @@ func (c *fakeDiscoveryClient) OpenAPISchema() (*openapi_v2.Document, error) {
c.openAPICalls = c.openAPICalls + 1
return &openapi_v2.Document{}, nil
}
func (d *fakeDiscoveryClient) OpenAPIV3() openapi.Client {
panic("unimplemented")
}