Skip to content

Commit

Permalink
Merge pull request #118204 from sttts/sttts-openapi-v2-parameter-refs
Browse files Browse the repository at this point in the history
openapi: reference shared parameters

Kubernetes-commit: f42ff8687026f8e12fb3d3b0da0760525d8d8ab2
  • Loading branch information
k8s-publishing-bot committed Jul 18, 2023
2 parents 1f56e52 + e41ba3e commit 448da40
Show file tree
Hide file tree
Showing 6 changed files with 70,238 additions and 29 deletions.
70,172 changes: 70,172 additions & 0 deletions artifacts/openapi/swagger-with-shared-parameters.json

Large diffs are not rendered by default.

14 changes: 7 additions & 7 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ require (
golang.org/x/sync v0.2.0
golang.org/x/text v0.11.0
gopkg.in/yaml.v2 v2.4.0
k8s.io/api v0.0.0-20230718014949-277ce0d086e7
k8s.io/apimachinery v0.0.0-20230718014249-03c97687112f
k8s.io/client-go v0.0.0-20230718015924-efe741a289f1
k8s.io/api v0.0.0-20230718054858-1b0ec3bb3296
k8s.io/apimachinery v0.0.0-20230718054246-5cb236977966
k8s.io/client-go v0.0.0-20230718055620-74c18d3a4044
k8s.io/klog/v2 v2.100.1
k8s.io/kube-openapi v0.0.0-20230601164746-7562a1006961
k8s.io/kube-openapi v0.0.0-20230717233707-2695361300d9
k8s.io/utils v0.0.0-20230406110748-d93618cff8a2
sigs.k8s.io/kustomize/api v0.13.5-0.20230601165947-6ce0bf390ce3
sigs.k8s.io/kustomize/kyaml v0.14.3-0.20230601165947-6ce0bf390ce3
Expand Down Expand Up @@ -69,7 +69,7 @@ require (
)

replace (
k8s.io/api => k8s.io/api v0.0.0-20230718014949-277ce0d086e7
k8s.io/apimachinery => k8s.io/apimachinery v0.0.0-20230718014249-03c97687112f
k8s.io/client-go => k8s.io/client-go v0.0.0-20230718015924-efe741a289f1
k8s.io/api => k8s.io/api v0.0.0-20230718054858-1b0ec3bb3296
k8s.io/apimachinery => k8s.io/apimachinery v0.0.0-20230718054246-5cb236977966
k8s.io/client-go => k8s.io/client-go v0.0.0-20230718055620-74c18d3a4044
)
16 changes: 8 additions & 8 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -225,16 +225,16 @@ gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
k8s.io/api v0.0.0-20230718014949-277ce0d086e7 h1:OWynHOnTzwj08ZEICnHlEbLI8xSs7Tmt5pguevblEck=
k8s.io/api v0.0.0-20230718014949-277ce0d086e7/go.mod h1:zxLOnGEFihnzaAvaWmy+DW4UbCIWFMfI3tRG5S9XEjE=
k8s.io/apimachinery v0.0.0-20230718014249-03c97687112f h1:asn3+fc9pfDX01Yfu83LeWkfdxFvwSQlHBVrsHnw6r4=
k8s.io/apimachinery v0.0.0-20230718014249-03c97687112f/go.mod h1:qGEW5CgfSiVy8AuAsSu5e5Se+9ZsnXBlbrIjeCn1/Dw=
k8s.io/client-go v0.0.0-20230718015924-efe741a289f1 h1:B3ah2Vuyt8ym0sso6up7voNMlOe9IT6cU9h7BIS8rsY=
k8s.io/client-go v0.0.0-20230718015924-efe741a289f1/go.mod h1:Gaq2IIlHLhO9wlYVCPAGW/G1DzwffXQiyEoLZmjDGXQ=
k8s.io/api v0.0.0-20230718054858-1b0ec3bb3296 h1:zrh3HjHGP8BPmB4K+QdV0VG7YOPxhj+6Pv1bSBAxzL4=
k8s.io/api v0.0.0-20230718054858-1b0ec3bb3296/go.mod h1:DlNtgYu2BX/uM/IkiPaFRqDBVazxK5ggJs19ERbMLZ4=
k8s.io/apimachinery v0.0.0-20230718054246-5cb236977966 h1:IYMQQBHbPi6jyorsuYWXB3+kJFH/MxVyLvKJH4nITrg=
k8s.io/apimachinery v0.0.0-20230718054246-5cb236977966/go.mod h1:xhQIsaL3hXneGluH+0pzF7kr+VYuLS/VcYJxF1xQf+g=
k8s.io/client-go v0.0.0-20230718055620-74c18d3a4044 h1:9K7ct/hT6SVFNiaZaZdA4kT72hLwxMGDf0H/C3ArqBw=
k8s.io/client-go v0.0.0-20230718055620-74c18d3a4044/go.mod h1:eiFpTys7rFV9G/ot71ArpI8jn31WihiYQFoq4IoxmWg=
k8s.io/klog/v2 v2.100.1 h1:7WCHKK6K8fNhTqfBhISHQ97KrnJNFZMcQvKp7gP/tmg=
k8s.io/klog/v2 v2.100.1/go.mod h1:y1WjHnz7Dj687irZUWR/WLkLc5N1YHtjLdmgWjndZn0=
k8s.io/kube-openapi v0.0.0-20230601164746-7562a1006961 h1:pqRVJGQJz6oeZby8qmPKXYIBjyrcv7EHCe/33UkZMYA=
k8s.io/kube-openapi v0.0.0-20230601164746-7562a1006961/go.mod h1:l8HTwL5fqnlns4jOveW1L75eo7R9KFHxiE0bsPGy428=
k8s.io/kube-openapi v0.0.0-20230717233707-2695361300d9 h1:LyMgNKD2P8Wn1iAwQU5OhxCKlKJy0sHc+PcDwFB24dQ=
k8s.io/kube-openapi v0.0.0-20230717233707-2695361300d9/go.mod h1:wZK2AVp1uHCp4VamDVgBP2COHZjqD1T68Rf0CM3YjSM=
k8s.io/utils v0.0.0-20230406110748-d93618cff8a2 h1:qY1Ad8PODbnymg2pRbkyMT/ylpTrCM8P2RJ0yroCyIk=
k8s.io/utils v0.0.0-20230406110748-d93618cff8a2/go.mod h1:OLgZIPagt7ERELqWJFomSt595RzquPNLL48iOWgYOg0=
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd h1:EDPBXCAspyGV4jQlpZSudPeMmr1bNJefnuqLsRAsHZo=
Expand Down
13 changes: 13 additions & 0 deletions pkg/resource/query_param_verifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (

openapi_v2 "github.com/google/gnostic-models/openapiv2"
yaml "gopkg.in/yaml.v2"

"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/discovery"
"k8s.io/client-go/dynamic"
Expand Down Expand Up @@ -146,6 +147,11 @@ func hasGVKExtension(extensions []*openapi_v2.NamedAny, gvk schema.GroupVersionK
// specific group-version-kind supports the specific query parameter for
// the PATCH end-point.
func supportsQueryParam(doc *openapi_v2.Document, gvk schema.GroupVersionKind, queryParam VerifiableQueryParam) (bool, error) {
globalParams := map[string]*openapi_v2.NamedParameter{}
for _, p := range doc.GetParameters().GetAdditionalProperties() {
globalParams["#/parameters/"+p.GetName()] = p
}

for _, path := range doc.GetPaths().GetPath() {
// Is this describing the gvk we're looking for?
if !hasGVKExtension(path.GetValue().GetPatch().GetVendorExtension(), gvk) {
Expand All @@ -155,6 +161,13 @@ func supportsQueryParam(doc *openapi_v2.Document, gvk schema.GroupVersionKind, q
if param.GetParameter().GetNonBodyParameter().GetQueryParameterSubSchema().GetName() == string(queryParam) {
return true, nil
}

// lookup global parameters
if ref := param.GetJsonReference().GetXRef(); ref != "" {
if globalParam, ok := globalParams[ref]; ok && globalParam != nil && globalParam.GetValue().GetNonBodyParameter().GetQueryParameterSubSchema().GetName() == string(queryParam) {
return true, nil
}
}
}
return false, nil
}
Expand Down
39 changes: 26 additions & 13 deletions pkg/resource/query_param_verifier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,20 @@ import (
"testing"

openapi_v2 "github.com/google/gnostic-models/openapiv2"

"k8s.io/apimachinery/pkg/runtime/schema"
openapitesting "k8s.io/kube-openapi/pkg/util/proto/testing"
)

var fakeSchema = openapitesting.Fake{Path: filepath.Join("..", "..", "artifacts", "openapi", "swagger.json")}
var fakeSchemaSharedParams = openapitesting.Fake{Path: filepath.Join("..", "..", "artifacts", "openapi", "swagger-with-shared-parameters.json")}

func TestSupportsQueryParam(t *testing.T) {
doc, err := fakeSchema.OpenAPISchema()
docInlineParams, err := fakeSchema.OpenAPISchema()
if err != nil {
t.Fatalf("Failed to get OpenAPI Schema: %v", err)
}
docSharedParams, err := fakeSchemaSharedParams.OpenAPISchema()
if err != nil {
t.Fatalf("Failed to get OpenAPI Schema: %v", err)
}
Expand Down Expand Up @@ -81,19 +87,26 @@ func TestSupportsQueryParam(t *testing.T) {
},
}

for _, test := range tests {
supports, err := supportsQueryParam(doc, test.gvk, test.queryParam)
if supports != test.supports || ((err == nil) != test.success) {
errStr := "nil"
if test.success == false {
errStr = "err"
for name, doc := range map[string]*openapi_v2.Document{
"inline parameters": docInlineParams,
"shared parameters": docSharedParams,
} {
t.Run(name, func(t *testing.T) {
for _, test := range tests {
supports, err := supportsQueryParam(doc, test.gvk, test.queryParam)
if supports != test.supports || ((err == nil) != test.success) {
errStr := "nil"
if test.success == false {
errStr = "err"
}
t.Errorf("SupportsQueryParam(doc, %v, %v) = (%v, %v), expected (%v, %v)",
test.gvk, test.queryParam,
supports, err,
test.supports, errStr,
)
}
}
t.Errorf("SupportsQueryParam(doc, %v, %v) = (%v, %v), expected (%v, %v)",
test.gvk, test.queryParam,
supports, err,
test.supports, errStr,
)
}
})
}
}

Expand Down
13 changes: 12 additions & 1 deletion pkg/resource/query_param_verifier_v3.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package resource

import (
"fmt"
"strings"

"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/dynamic"
Expand Down Expand Up @@ -124,9 +125,19 @@ func supportsQueryParamV3(doc *spec3.OpenAPI, gvk schema.GroupVersionKind, query
// Now look for the query parameter among the parameters
// for the PATCH operation.
for _, param := range op.OperationProps.Parameters {
if param.ParameterProps.Name == string(queryParam) {
if param.ParameterProps.Name == string(queryParam) && param.In == "query" {
return nil
}

// lookup global parameters
if ref := param.Refable.Ref.Ref.String(); strings.HasPrefix(ref, "#/parameters/") && doc.Components != nil {
k := strings.TrimPrefix(ref, "#/parameters/")
if globalParam, ok := doc.Components.Parameters[k]; ok && globalParam != nil {
if globalParam.In == "query" && globalParam.Name == string(queryParam) {
return nil
}
}
}
}
return NewParamUnsupportedError(gvk, queryParam)
}
Expand Down

0 comments on commit 448da40

Please sign in to comment.