Skip to content

Commit 874ab09

Browse files
committed
Cache busting WIP
1 parent 56c5413 commit 874ab09

File tree

9 files changed

+141
-10
lines changed

9 files changed

+141
-10
lines changed

staging/src/k8s.io/client-go/discovery/cached/disk/cached_discovery.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
"time"
2727

2828
openapi_v2 "github.com/googleapis/gnostic/openapiv2"
29+
openapi_v3 "github.com/googleapis/gnostic/openapiv3"
2930
"k8s.io/klog/v2"
3031

3132
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -240,6 +241,15 @@ func (d *CachedDiscoveryClient) OpenAPISchema() (*openapi_v2.Document, error) {
240241
return d.delegate.OpenAPISchema()
241242
}
242243

244+
// OpenAPISchema retrieves and parses the swagger API schema the server supports.
245+
func (d *CachedDiscoveryClient) OpenAPIV3Schema(path, hash string) (*openapi_v3.Document, error) {
246+
return d.delegate.OpenAPIV3Schema(path, hash)
247+
}
248+
249+
func (d *CachedDiscoveryClient) OpenAPIV3Discovery() (*discovery.OpenAPIV3Discovery, error) {
250+
return d.delegate.OpenAPIV3Discovery()
251+
}
252+
243253
// Fresh is supposed to tell the caller whether or not to retry if the cache
244254
// fails to find something (false = retry, true = no need to retry).
245255
func (d *CachedDiscoveryClient) Fresh() bool {

staging/src/k8s.io/client-go/discovery/cached/memory/memcache.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"syscall"
2424

2525
openapi_v2 "github.com/googleapis/gnostic/openapiv2"
26+
openapi_v3 "github.com/googleapis/gnostic/openapiv3"
2627

2728
errorsutil "k8s.io/apimachinery/pkg/api/errors"
2829
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -149,6 +150,14 @@ func (d *memCacheClient) OpenAPISchema() (*openapi_v2.Document, error) {
149150
return d.delegate.OpenAPISchema()
150151
}
151152

153+
func (d *memCacheClient) OpenAPIV3Schema(path, hash string) (*openapi_v3.Document, error) {
154+
return d.delegate.OpenAPIV3Schema(path, hash)
155+
}
156+
157+
func (d *memCacheClient) OpenAPIV3Discovery() (*discovery.OpenAPIV3Discovery, error) {
158+
return d.delegate.OpenAPIV3Discovery()
159+
}
160+
152161
func (d *memCacheClient) Fresh() bool {
153162
d.lock.RLock()
154163
defer d.lock.RUnlock()

staging/src/k8s.io/client-go/discovery/discovery_client.go

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import (
3030
//nolint:staticcheck // SA1019 Keep using module since it's still being maintained and the api of google.golang.org/protobuf/proto differs
3131
"github.com/golang/protobuf/proto"
3232
openapi_v2 "github.com/googleapis/gnostic/openapiv2"
33+
openapi_v3 "github.com/googleapis/gnostic/openapiv3"
3334

3435
"k8s.io/apimachinery/pkg/api/errors"
3536
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -46,7 +47,9 @@ const (
4647
// defaultRetries is the number of times a resource discovery is repeated if an api group disappears on the fly (e.g. CustomResourceDefinitions).
4748
defaultRetries = 2
4849
// protobuf mime type
49-
mimePb = "application/[email protected]+protobuf"
50+
openAPIV2mimePb = "application/[email protected]+protobuf"
51+
52+
openAPIV3mimePb = "application/[email protected]+protobuf"
5053
// defaultTimeout is the maximum amount of time per request when no timeout has been set on a RESTClient.
5154
// Defaults to 32s in order to have a distinguishable length of time, relative to other timeouts that exist.
5255
defaultTimeout = 32 * time.Second
@@ -60,6 +63,7 @@ type DiscoveryInterface interface {
6063
ServerResourcesInterface
6164
ServerVersionInterface
6265
OpenAPISchemaInterface
66+
OpenAPIV3SchemaInterface
6367
}
6468

6569
// CachedDiscoveryInterface is a DiscoveryInterface with cache invalidation and freshness.
@@ -128,6 +132,11 @@ type OpenAPISchemaInterface interface {
128132
OpenAPISchema() (*openapi_v2.Document, error)
129133
}
130134

135+
type OpenAPIV3SchemaInterface interface {
136+
OpenAPIV3Discovery() (*OpenAPIV3Discovery, error)
137+
OpenAPIV3Schema(string, string) (*openapi_v3.Document, error)
138+
}
139+
131140
// DiscoveryClient implements the functions that discover server-supported API groups,
132141
// versions and resources.
133142
type DiscoveryClient struct {
@@ -420,9 +429,9 @@ func (d *DiscoveryClient) ServerVersion() (*version.Info, error) {
420429
return &info, nil
421430
}
422431

423-
// OpenAPISchema fetches the open api schema using a rest client and parses the proto.
432+
// OpenAPISchema fetches the open api v2 schema using a rest client and parses the proto.
424433
func (d *DiscoveryClient) OpenAPISchema() (*openapi_v2.Document, error) {
425-
data, err := d.restClient.Get().AbsPath("/openapi/v2").SetHeader("Accept", mimePb).Do(context.TODO()).Raw()
434+
data, err := d.restClient.Get().AbsPath("/openapi/v2").SetHeader("Accept", openAPIV2mimePb).Do(context.TODO()).Raw()
426435
if err != nil {
427436
if errors.IsForbidden(err) || errors.IsNotFound(err) || errors.IsNotAcceptable(err) {
428437
// single endpoint not found/registered in old server, try to fetch old endpoint
@@ -443,6 +452,42 @@ func (d *DiscoveryClient) OpenAPISchema() (*openapi_v2.Document, error) {
443452
return document, nil
444453
}
445454

455+
456+
type OpenAPIV3Discovery struct {
457+
Paths map[string]string
458+
}
459+
460+
461+
func (d *DiscoveryClient) OpenAPIV3Discovery() (*OpenAPIV3Discovery, error) {
462+
data, err := d.restClient.Get().AbsPath("/openapi/v3").Do(context.TODO()).Raw()
463+
if err != nil {
464+
return nil, err
465+
}
466+
467+
foo := &OpenAPIV3Discovery{}
468+
err = json.Unmarshal(data, foo)
469+
if err != nil {
470+
return nil, err
471+
}
472+
473+
return foo, nil
474+
}
475+
476+
func (d *DiscoveryClient) OpenAPIV3Schema(path, hash string) (*openapi_v3.Document, error) {
477+
data, err := d.restClient.Get().AbsPath("/openapi/v3", path).Param("hash", hash).SetHeader("Accept", openAPIV3mimePb).Do(context.TODO()).Raw()
478+
if err != nil {
479+
return nil, err
480+
}
481+
document := &openapi_v3.Document{}
482+
err = proto.Unmarshal(data, document)
483+
if err != nil {
484+
return nil, err
485+
}
486+
487+
return document, nil
488+
}
489+
490+
446491
// withRetries retries the given recovery function in case the groups supported by the server change after ServerGroup() returns.
447492
func withRetries(maxRetries int, f func() ([]*metav1.APIGroup, []*metav1.APIResourceList, error)) ([]*metav1.APIGroup, []*metav1.APIResourceList, error) {
448493
var result []*metav1.APIResourceList

staging/src/k8s.io/client-go/discovery/fake/discovery.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"net/http"
2222

2323
openapi_v2 "github.com/googleapis/gnostic/openapiv2"
24+
openapi_v3 "github.com/googleapis/gnostic/openapiv3"
2425

2526
"k8s.io/apimachinery/pkg/api/errors"
2627
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -29,6 +30,8 @@ import (
2930
kubeversion "k8s.io/client-go/pkg/version"
3031
restclient "k8s.io/client-go/rest"
3132
"k8s.io/client-go/testing"
33+
34+
"k8s.io/client-go/discovery"
3235
)
3336

3437
// FakeDiscovery implements discovery.DiscoveryInterface and sometimes calls testing.Fake.Invoke with an action,
@@ -161,6 +164,14 @@ func (c *FakeDiscovery) OpenAPISchema() (*openapi_v2.Document, error) {
161164
return &openapi_v2.Document{}, nil
162165
}
163166

167+
func (d *FakeDiscovery) OpenAPIV3Schema(path, hash string) (*openapi_v3.Document, error) {
168+
return &openapi_v3.Document{}, nil
169+
}
170+
171+
func (d *FakeDiscovery) OpenAPIV3Discovery() (*discovery.OpenAPIV3Discovery, error) {
172+
return nil, nil
173+
}
174+
164175
// RESTClient returns a RESTClient that is used to communicate with API server
165176
// by this client implementation.
166177
func (c *FakeDiscovery) RESTClient() restclient.Interface {

staging/src/k8s.io/kube-aggregator/pkg/controllers/openapiv3/aggregator/downloader.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ func (s *Downloader) handlerWithUser(handler http.Handler, info user.Info) http.
4545

4646
// gvList is a struct for the response of the /openapi/v3 endpoint to unmarshal into
4747
type gvList struct {
48-
Paths []string `json:"Paths"`
48+
Paths map[string]string `json:"Paths"`
4949
}
5050

5151
// SpecETag is a OpenAPI v3 spec and etag pair for the endpoint of each OpenAPI group/version
@@ -81,7 +81,7 @@ func (s *Downloader) Download(handler http.Handler, etagList map[string]string)
8181
if err := json.Unmarshal(writer.data, &groups); err != nil {
8282
return nil, err
8383
}
84-
for _, path := range groups.Paths {
84+
for path, _ := range groups.Paths {
8585
reqPath := fmt.Sprintf("/openapi/v3/%s", path)
8686
req, err := http.NewRequest("GET", reqPath, nil)
8787
if err != nil {

staging/src/k8s.io/kubectl/pkg/cmd/explain/explain.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ func (o *ExplainOptions) Complete(f cmdutil.Factory, cmd *cobra.Command) error {
101101
if err != nil {
102102
return err
103103
}
104+
104105
return nil
105106
}
106107

staging/src/k8s.io/kubectl/pkg/cmd/util/factory.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
restclient "k8s.io/client-go/rest"
2727
"k8s.io/kubectl/pkg/util/openapi"
2828
"k8s.io/kubectl/pkg/validation"
29+
openapi_v3 "github.com/googleapis/gnostic/openapiv3"
2930
)
3031

3132
// Factory provides abstractions that allow the Kubectl command to be extended across multiple types
@@ -66,4 +67,10 @@ type Factory interface {
6667
OpenAPISchema() (openapi.Resources, error)
6768
// OpenAPIGetter returns a getter for the openapi schema document
6869
OpenAPIGetter() discovery.OpenAPISchemaInterface
70+
71+
// OpenAPIV3Discovery returns the list of paths for OpenAPI V3 documents
72+
OpenAPIV3Discovery() (*discovery.OpenAPIV3Discovery, error)
73+
74+
// OpenAPIV3GroupVersionSchema returns the OpenAPI V3 schema for the corresponding group version
75+
OpenAPIV3GroupVersionSchema(path, hash string) (*openapi_v3.Document, error)
6976
}

staging/src/k8s.io/kubectl/pkg/cmd/util/factory_client_access.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import (
3434
"k8s.io/kubectl/pkg/util/openapi"
3535
openapivalidation "k8s.io/kubectl/pkg/util/openapi/validation"
3636
"k8s.io/kubectl/pkg/validation"
37+
openapi_v3 "github.com/googleapis/gnostic/openapiv3"
3738
)
3839

3940
type factoryImpl struct {
@@ -186,3 +187,23 @@ func (f *factoryImpl) OpenAPIGetter() discovery.OpenAPISchemaInterface {
186187

187188
return f.openAPIGetter
188189
}
190+
191+
func (f *factoryImpl) OpenAPIV3Discovery() (*discovery.OpenAPIV3Discovery, error) {
192+
discovery, err := f.clientGetter.ToDiscoveryClient()
193+
if err != nil {
194+
return nil, err
195+
}
196+
197+
foo, err := discovery.OpenAPIV3Discovery()
198+
return foo, err
199+
}
200+
201+
func (f *factoryImpl) OpenAPIV3GroupVersionSchema(path, hash string) (*openapi_v3.Document, error) {
202+
discovery, err := f.clientGetter.ToDiscoveryClient()
203+
if err != nil {
204+
return nil, err
205+
}
206+
207+
schema, err := discovery.OpenAPIV3Schema(path, hash)
208+
return schema, err
209+
}

vendor/k8s.io/kube-openapi/pkg/handler3/handler.go

Lines changed: 32 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)