Skip to content
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

Merged
merged 1 commit into from
Jul 22, 2019

Conversation

yue9944882
Copy link
Member

now in v2 builder, any schema marked the v2-spec extension will be shadowed to the extension value.

/cc @sttts

@k8s-ci-robot k8s-ci-robot requested a review from sttts July 11, 2019 09:22
@k8s-ci-robot k8s-ci-robot added the size/M Denotes a PR that changes 30-99 lines, ignoring generated files. label Jul 11, 2019
@k8s-ci-robot
Copy link
Contributor

Welcome @yue9944882!

It looks like this is your first PR to kubernetes/kube-openapi 🎉. Please refer to our pull request process documentation to help your PR have a smooth ride to approval.

You will be prompted by a bot to use commands during the review process. Do not be afraid to follow the prompts! It is okay to experiment. Here is the bot commands documentation.

You can also check if kubernetes/kube-openapi has its own contribution guidelines.

You may want to refer to our testing guide if you run into trouble with your tests not passing.

If you are having difficulty getting your pull request seen, please follow the recommended escalation practices. Also, for tips and tricks in the contribution process you may want to read the Kubernetes contributor cheat sheet. We want to make sure your contribution gets all the attention it needs!

Thank you, and welcome to Kubernetes. 😃

@k8s-ci-robot k8s-ci-robot added the cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. label Jul 11, 2019
@yue9944882
Copy link
Member Author

@sttts we dont need changes in the generator. e.g. for IntOrString, we will add this extension into it's OpenAPIDefinition method. the v3 builder will filter out the extension, the v2 builder will shadow the schema w/ extension value.

@@ -154,6 +155,11 @@ func (o *openAPI) buildDefinitionRecursively(name string) error {
schema.Extensions[k] = v
}
}
if v, ok := item.Schema.Extensions[extensionV2Spec]; ok {
Copy link
Contributor

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.

Copy link
Contributor

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.

Copy link
Member Author

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.

Copy link
Member Author

@yue9944882 yue9944882 Jul 11, 2019

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

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.

@k8s-ci-robot k8s-ci-robot added size/L Denotes a PR that changes 100-499 lines, ignoring generated files. and removed size/M Denotes a PR that changes 30-99 lines, ignoring generated files. labels Jul 11, 2019
pkg/common/common.go Outdated Show resolved Hide resolved
if mn != "OpenAPIV3Definition" {
continue
}
return methodReturnsValue(mt, openAPICommonPackagePath, "OpenAPIDefinition")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OpenAPIDefinition or OpenAPIV3Definition?

Copy link
Member Author

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

@sttts
Copy link
Contributor

sttts commented Jul 12, 2019

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 {
Copy link
Member Author

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()),

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

Copy link
Member Author

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

Copy link

@p0lyn0mial p0lyn0mial left a 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 {

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?

Copy link
Member Author

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

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.

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Jul 17, 2019
update test to reflect how custom types works
@k8s-ci-robot k8s-ci-robot added size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. and removed lgtm "Looks good to me", indicates that a PR is ready to be merged. size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels Jul 18, 2019
@yue9944882
Copy link
Member Author

@sttts @p0lyn0mial PTAL

@p0lyn0mial
Copy link

/lgtm

leaving approval to @sttts

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Jul 22, 2019
@sttts
Copy link
Contributor

sttts commented Jul 22, 2019

/approve

@k8s-ci-robot
Copy link
Contributor

[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 /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@k8s-ci-robot k8s-ci-robot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Jul 22, 2019
@k8s-ci-robot k8s-ci-robot merged commit 5e22f3d into kubernetes:master Jul 22, 2019
@sttts
Copy link
Contributor

sttts commented Jul 22, 2019

@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?

@yue9944882
Copy link
Member Author

@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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. lgtm "Looks good to me", indicates that a PR is ready to be merged. size/XL Denotes a PR that changes 500-999 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants