-
Notifications
You must be signed in to change notification settings - Fork 204
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support x-kubernetes-v2-spec extension #166
Support x-kubernetes-v2-spec extension #166
Conversation
Welcome @yue9944882! |
@sttts we dont need changes in the generator. e.g. for |
pkg/builder/openapi.go
Outdated
@@ -154,6 +155,11 @@ func (o *openAPI) buildDefinitionRecursively(name string) error { | |||
schema.Extensions[k] = v | |||
} | |||
} | |||
if v, ok := item.Schema.Extensions[extensionV2Spec]; ok { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
wondering, can we mirror OpenAPIV2Definition()
output there instead? so we will assume that OpenAPIDefinition()
will output v3 schemas (which we down-convert by default), with OpenAPIV2Definition
for cases where the down-conversion is not good enough.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
or we go the opposite direction: OpenAPIDefinition
will stay v2, and OpenAPIV3Definition
can give an advanced v3 schema. That sounds more compatible. We won't have many cases where we need this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the latter sounds better and i took a shot. updated the integration test a bit cover v2 spec generation.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the next step is to start v3 builder and verify the v2-extension by its integration test
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@yue9944882 I started working on builderv3, I should issue a WIP PR by the end of the week.
5f2986b
to
3d0b6eb
Compare
3d0b6eb
to
9bd9b46
Compare
if mn != "OpenAPIV3Definition" { | ||
continue | ||
} | ||
return methodReturnsValue(mt, openAPICommonPackagePath, "OpenAPIDefinition") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OpenAPIDefinition or OpenAPIV3Definition?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it's the name of struct OpenAPIDefinition
under "pkg/common" package, instead of the function name
Looks good. /assign @p0lyn0mial for some more eyes on this. |
@@ -172,3 +182,11 @@ func EscapeJsonPointer(p string) string { | |||
p = strings.Replace(p, "/", "~1", -1) | |||
return p | |||
} | |||
|
|||
func EmbedOpenAPIDefinitionIntoV2Extension(main OpenAPIDefinition, embedded OpenAPIDefinition) OpenAPIDefinition { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
for now, i cant think of a better name for this
) | ||
|
||
func GetOpenAPIDefinitions(ref common.ReferenceCallback) map[string]common.OpenAPIDefinition { | ||
return map[string]common.OpenAPIDefinition{ | ||
"k8s.io/kube-openapi/test/integration/testdata/custom.Bac": common.EmbedOpenAPIDefinitionIntoV2Extension(custom.Bac{}.OpenAPIV3Definition(), custom.Bac{}.OpenAPIDefinition()), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
so what does it mean?
it means that in the future (once we have oas v3
) we could have a way to keep and generate both v2
and v2
schemas (spec.Schema
) in just one place, is that correct?
do we expect them to be different (v2
vs v3
) ?
from what I have seen they are quite compatible:
https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.0.md#schemaObject
https://github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0.md#schemaObject
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it means that in the future (once we have oas v3) we could have a way to keep and generate both v2 and v2 schemas (spec.Schema) in just one place, is that correct?
exactly, but only for those types have differences in v2 and v3 schema. for now, it's basically meant for IntOrString
. in v2 spec, we made walkaround for those types we have to denote in v3 grammar e.g.anyOf
. the pull will keep those v2 walkarounds in the builder in the extension so that:
- for v2 publishing endpoint: we shadow the generated schema using the new extension.
- for v3 publishing endpoint: we filter out the extension from the generated schema.
// OpenAPISchemaType is used by the kube-openapi generator when constructing
// the OpenAPI spec of this type.
//
// See: https://github.com/kubernetes/kube-openapi/tree/master/pkg/generators
func (IntOrString) OpenAPISchemaType() []string { return []string{"string"} }
// OpenAPISchemaFormat is used by the kube-openapi generator when constructing
// the OpenAPI spec of this type.
func (IntOrString) OpenAPISchemaFormat() string { return "int-or-string" }
for IntOrString
, the v3 spec will be an anyOf
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/lgtm
// already invoked directly | ||
return nil | ||
} | ||
|
||
args := argsFromType(t) | ||
g.Do("func "+nameTmpl+"(ref $.ReferenceCallback|raw$) $.OpenAPIDefinition|raw$ {\n", args) | ||
if hasOpenAPIDefinitionMethods(t) { | ||
switch { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no separate case for hasV3Definition
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no separate case for hasV3Definition?
hasV3Definition case is short-circuited at L329, do you think about adding a case for hasV3Definition and commenting // this line will never reach
for readability? the branches is getting more complex tho
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no, we can leave it as it is now.
update test to reflect how custom types works
9bd9b46
to
6610e5a
Compare
@sttts @p0lyn0mial PTAL |
/lgtm leaving approval to @sttts |
/approve |
[APPROVALNOTIFIER] This PR is APPROVED This pull-request has been approved by: sttts The full list of commands accepted by this bot can be found here. The pull request process is described here
Needs approval from an approver in each of these files:
Approvers can indicate their approval by writing |
@yue9944882 now we need a follow-up which vendors this into kube and which does not lead to any change for the published spec, right? |
yep, this extension is opt-in and backward-compatible. will send the follow-up |
now in v2 builder, any schema marked the v2-spec extension will be shadowed to the extension value.
/cc @sttts