From 700e3b566428bca46d2d37514c8fd7af60c7ff34 Mon Sep 17 00:00:00 2001 From: Joe Betz Date: Fri, 25 Oct 2024 13:51:03 -0400 Subject: [PATCH] Update OpenAPI and fix openAPI tests to handle unexported jsonreferences Co-authored-by: Alexander Zielensk --- go.mod | 2 +- pkg/generated/openapi/openapi_test.go | 16 +++++++++++++--- .../client-go/openapi/cached/groupversion.go | 4 ++++ .../src/k8s.io/client-go/openapi/groupversion.go | 12 ++++++++++++ .../client-go/openapi/openapitest/fakeclient.go | 5 +++++ .../client-go/openapi/openapitest/fileclient.go | 5 +++++ 6 files changed, 40 insertions(+), 4 deletions(-) diff --git a/go.mod b/go.mod index ae60b971438..f85602f3b5c 100644 --- a/go.mod +++ b/go.mod @@ -30,6 +30,7 @@ require ( github.com/emicklei/go-restful/v3 v3.11.0 github.com/fsnotify/fsnotify v1.7.0 github.com/go-logr/logr v1.4.2 + github.com/go-openapi/jsonreference v0.20.2 github.com/godbus/dbus/v5 v5.1.0 github.com/gogo/protobuf v1.3.2 github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da @@ -153,7 +154,6 @@ require ( github.com/go-logr/stdr v1.2.2 // indirect github.com/go-logr/zapr v1.3.0 // indirect github.com/go-openapi/jsonpointer v0.21.0 // indirect - github.com/go-openapi/jsonreference v0.20.2 // indirect github.com/go-openapi/swag v0.23.0 // indirect github.com/go-task/slim-sprig/v3 v3.0.0 // indirect github.com/golang-jwt/jwt/v4 v4.5.0 // indirect diff --git a/pkg/generated/openapi/openapi_test.go b/pkg/generated/openapi/openapi_test.go index 1c9776ea857..1b1d1c302f9 100644 --- a/pkg/generated/openapi/openapi_test.go +++ b/pkg/generated/openapi/openapi_test.go @@ -18,10 +18,12 @@ package openapi import ( "encoding/json" - "reflect" "testing" + "github.com/go-openapi/jsonreference" "github.com/google/go-cmp/cmp" + "github.com/google/go-cmp/cmp/cmpopts" + "k8s.io/kube-openapi/pkg/common" "k8s.io/kube-openapi/pkg/handler" "k8s.io/kube-openapi/pkg/validation/spec" @@ -51,8 +53,16 @@ func TestOpenAPIRoundtrip(t *testing.T) { delete(roundTripped.Extensions, common.ExtensionV2Schema) delete(value.Schema.Extensions, common.ExtensionV2Schema) - if !reflect.DeepEqual(value.Schema, roundTripped) { - t.Errorf("unexpected diff (a=expected,b=roundtripped):\n%s", cmp.Diff(value.Schema, roundTripped)) + opts := []cmp.Option{ + cmpopts.EquateEmpty(), + // jsonreference.Ref contains unexported fields. Compare + // by string representation provides a consistent + cmp.Comparer(func(x, y jsonreference.Ref) bool { + return x.String() == y.String() + }), + } + if !cmp.Equal(value.Schema, roundTripped, opts...) { + t.Errorf("unexpected diff (a=expected,b=roundtripped):\n%s", cmp.Diff(value.Schema, roundTripped, opts...)) return } }) diff --git a/staging/src/k8s.io/client-go/openapi/cached/groupversion.go b/staging/src/k8s.io/client-go/openapi/cached/groupversion.go index 65a4189f7a8..73730c51be1 100644 --- a/staging/src/k8s.io/client-go/openapi/cached/groupversion.go +++ b/staging/src/k8s.io/client-go/openapi/cached/groupversion.go @@ -56,3 +56,7 @@ func (g *groupversion) Schema(contentType string) ([]byte, error) { return cachedInfo.data, cachedInfo.err } + +func (c *groupversion) ServerRelativeURL() string { + return c.delegate.ServerRelativeURL() +} diff --git a/staging/src/k8s.io/client-go/openapi/groupversion.go b/staging/src/k8s.io/client-go/openapi/groupversion.go index 601dcbe3ccb..40d91b9a533 100644 --- a/staging/src/k8s.io/client-go/openapi/groupversion.go +++ b/staging/src/k8s.io/client-go/openapi/groupversion.go @@ -27,6 +27,12 @@ const ContentTypeOpenAPIV3PB = "application/com.github.proto-openapi.spec.v3@v1. type GroupVersion interface { Schema(contentType string) ([]byte, error) + + // ServerRelativeURL. Returns the path and parameters used to fetch the schema. + // You should use the Schema method to fetch it, but this value can be used + // to key the current version of the schema in a cache since it contains a + // hash string which changes upon schema update. + ServerRelativeURL() string } type groupversion struct { @@ -68,3 +74,9 @@ func (g *groupversion) Schema(contentType string) ([]byte, error) { return path.Do(context.TODO()).Raw() } + +// URL used for fetching the schema. The URL includes a hash and can be used +// to key the current version of the schema in a cache. +func (g *groupversion) ServerRelativeURL() string { + return g.item.ServerRelativeURL +} diff --git a/staging/src/k8s.io/client-go/openapi/openapitest/fakeclient.go b/staging/src/k8s.io/client-go/openapi/openapitest/fakeclient.go index ec3d84322ec..43b5f679e86 100644 --- a/staging/src/k8s.io/client-go/openapi/openapitest/fakeclient.go +++ b/staging/src/k8s.io/client-go/openapi/openapitest/fakeclient.go @@ -77,3 +77,8 @@ func (f FakeGroupVersion) Schema(contentType string) ([]byte, error) { } return f.GVSpec, nil } + +// ServerRelativeURL returns an empty string. +func (f FakeGroupVersion) ServerRelativeURL() string { + panic("unimplemented") +} diff --git a/staging/src/k8s.io/client-go/openapi/openapitest/fileclient.go b/staging/src/k8s.io/client-go/openapi/openapitest/fileclient.go index d53f63a16bc..96b882de91b 100644 --- a/staging/src/k8s.io/client-go/openapi/openapitest/fileclient.go +++ b/staging/src/k8s.io/client-go/openapi/openapitest/fileclient.go @@ -94,3 +94,8 @@ func (f *fileGroupVersion) Schema(contentType string) ([]byte, error) { } return fs.ReadFile(f.f, f.filename) } + +// ServerRelativeURL returns an empty string. +func (f *fileGroupVersion) ServerRelativeURL() string { + return f.filename +}