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

Add Support for OpenAPIEnum in OpenAPI v2 #105057

Conversation

jiahuif
Copy link
Member

@jiahuif jiahuif commented Sep 15, 2021

What type of PR is this?

/kind feature

What this PR does / why we need it:

Prune enum types from schema if OpenAPIEnum feature is not enabled. Additionally, this PR provides a pattern to revert schema changes from alpha features.

Special notes for your reviewer:

  • Change to vendor/ is actually from Enum type generator kube-openapi#242 as a result of dependency upgrade
  • In this PR, enum markers are added to known enum types of built-in v1 API to better demonstrate how this PR works.
  • Bump k8s.io/kube-openapi will also bump go.mod files of all staging packages. That's why this PR touches so many SIGs.
  • Runtime cost: effectively, the runtime cost of GetOpenAPIDefinitions is roughly doubled, because it is constructed then overwritten while being traversed.
  • This PR affects specs of ONLY built-in types. CRDs and extensions are out of scope of this single PR.
  • The descriptions in OpenAPI spec (and thus in swagger.json) are generated from comments in Go source files. This PR does not author or change any comments of such types.

Does this PR introduce a user-facing change?

NONE

Additional documentation e.g., KEPs (Kubernetes Enhancement Proposals), usage docs, etc.:

- [KEP]: https://github.com/kubernetes/enhancements/issues/2887

@k8s-ci-robot k8s-ci-robot added do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. release-note-none Denotes a PR that doesn't merit a release note. kind/feature Categorizes issue or PR as related to a new feature. size/M Denotes a PR that changes 30-99 lines, ignoring generated files. do-not-merge/needs-sig Indicates an issue or PR lacks a `sig/foo` label and requires one. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one. needs-priority Indicates a PR lacks a `priority/foo` label and requires one. labels Sep 15, 2021
@jiahuif
Copy link
Member Author

jiahuif commented Sep 15, 2021

/sig api-machinery
/area apiserver

@k8s-ci-robot k8s-ci-robot added sig/api-machinery Categorizes an issue or PR as relevant to SIG API Machinery. area/apiserver and removed do-not-merge/needs-sig Indicates an issue or PR lacks a `sig/foo` label and requires one. labels Sep 15, 2021
@k8s-ci-robot k8s-ci-robot added area/cloudprovider area/code-generation area/dependency Issues or PRs related to dependency changes area/kubectl sig/architecture Categorizes an issue or PR as relevant to SIG Architecture. sig/auth Categorizes an issue or PR as relevant to SIG Auth. sig/cli Categorizes an issue or PR as relevant to SIG CLI. sig/cloud-provider Categorizes an issue or PR as relevant to SIG Cloud Provider. sig/cluster-lifecycle Categorizes an issue or PR as relevant to SIG Cluster Lifecycle. sig/instrumentation Categorizes an issue or PR as relevant to SIG Instrumentation. sig/storage Categorizes an issue or PR as relevant to SIG Storage. labels Sep 15, 2021
@jiahuif jiahuif force-pushed the feature/enum-types-feature-enablement branch from dcaf7b4 to bb45339 Compare September 15, 2021 21:23
@caesarxuchao
Copy link
Member

/triage accepted

@k8s-ci-robot k8s-ci-robot added triage/accepted Indicates an issue or PR is ready to be actively worked on. and removed needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one. labels Sep 16, 2021
@k8s-ci-robot k8s-ci-robot removed sig/architecture Categorizes an issue or PR as relevant to SIG Architecture. sig/auth Categorizes an issue or PR as relevant to SIG Auth. sig/cli Categorizes an issue or PR as relevant to SIG CLI. sig/cluster-lifecycle Categorizes an issue or PR as relevant to SIG Cluster Lifecycle. sig/instrumentation Categorizes an issue or PR as relevant to SIG Instrumentation. sig/storage Categorizes an issue or PR as relevant to SIG Storage. labels Nov 16, 2021
@cheftako
Copy link
Member

/approve

@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: apelisse, cheftako, jiahuif, jpbetz, thockin

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 Nov 16, 2021
@Jefftree Jefftree mentioned this pull request Nov 16, 2021
@thockin
Copy link
Member

thockin commented Nov 16, 2021

/milestone v1.23

@k8s-ci-robot k8s-ci-robot added this to the v1.23 milestone Nov 16, 2021
@jiahuif
Copy link
Member Author

jiahuif commented Nov 16, 2021

/retest

@liggitt liggitt removed their assignment Nov 17, 2021
@k8s-ci-robot k8s-ci-robot merged commit e14bdba into kubernetes:master Nov 17, 2021
SIG Auth Old automation moved this from Needs Triage to Closed / Done Nov 17, 2021
@jiahuif jiahuif deleted the feature/enum-types-feature-enablement branch November 17, 2021 00:49
@yue9944882
Copy link
Member

this PR is breaking the openapi spec for the type corev1.PodReadinessGate. the type has a casttype marker which points the declared openapi type to corev1.PodConditionType, as a result, in the 1.23 openapi spec, the field conditionType was forced to an enum of which the candidate values are completely irrelevant to readiness gates:

 "io.k8s.api.core.v1.PodReadinessGate": {
      "description": "PodReadinessGate contains the reference to a pod condition",
      "properties": {
        "conditionType": {
          "description": "ConditionType refers to a condition in the pod's condition list with matching type.\n\nPossible enum values:\n - `\"ContainersReady\"` indicates whether all containers in the pod are ready.\n - `\"Initialized\"` means that all init containers in the pod have started successfully.\n - `\"PodScheduled\"` represents status of the scheduling process for this pod.\n - `\"Ready\"` means the pod is able to service requests and should be added to the load balancing pools of all matching services.",
          "enum": [
            "ContainersReady",
            "Initialized",
            "PodScheduled",
            "Ready"
          ],
          "type": "string"
        }
      },
      "required": [
        "conditionType"
      ],
      "type": "object"
    },

oddly, the latest openapi spec on the master branch doesnt have this issue tho..

@jiahuif
Copy link
Member Author

jiahuif commented Mar 30, 2022

@yue9944882 Thanks for checking. This is the fix #108639
Here is the backport to 1.23 that fix this issue #108740

@brendandburns
Copy link
Contributor

@yue9944882 since this is committed into the release branch for 1.23, I think we can regenerate to fix this, right?

@liggitt
Copy link
Member

liggitt commented Mar 30, 2022

As an aside, it seems like there's a mismatch in the generation/use of the committed openapi spec... it's being generated with all alpha features enabled, including alpha features impacting the content of the openapi spec (like this enum feature). That is then being used to generate decidedly non-alpha clients.

The fact that an alpha, off-by-default feature in kubernetes/kubernetes broke generated clients is pretty concerning.

I'm wondering if we should be generating the committed openapi only enabling alpha APIs, but not all alpha feature gates.

@apelisse
Copy link
Member

apelisse commented Mar 30, 2022

Should we generate both openapi-spec, one with alpha enabled and one without? First it'd give us an opportunity to compare the two, make sure that alpha features generate the right thing, but also provide a more stable openapi for offline clients generation?

@liggitt
Copy link
Member

liggitt commented Mar 30, 2022

maybe?

@yue9944882
Copy link
Member

@yue9944882 since this is committed into the release branch for 1.23, I think we can regenerate to fix this, right?

yes, after the fix is picked we also need to wait until the next patch release. i guess 1-2 weeks is expected.

i presume it's fine to preserve the alpha objects/fields in the openapi (as long as it doesnt break the backward-compatibility) b/c users may wanna opt-in to the alpha feature. but the enum change involved by this PR is a bit different. the crux is that there's no native enum type in golang, the so-called "enum" in golang doesnt have a hard requirement on the candidate values. e.g. for a type MyEnum string we can cast any string to the enum by MyEnum("..."). while in other languages, the enum type strictly forbids non-candidate values.

@liggitt
Copy link
Member

liggitt commented Mar 31, 2022

the enum change involved by this PR is a bit different. the crux is that there's no native enum type in golang, the so-called "enum" in golang doesnt have a hard requirement on the candidate values. e.g. for a type MyEnum string we can cast any string to the enum by MyEnum("..."). while in other languages, the enum type strictly forbids non-candidate values

Agreed. The more I think about this, the more concerning it is.

First, some of the currently marked +enum fields are documented to allow expansion in the future, which would break clients generated against earlier openapi schemas:

  • // CompletionMode specifies how Pod completions of a Job are tracked.
    // +enum
    type CompletionMode string
    
      // More completion modes can be added in the future.
    
  • These are only well-known types, other types are allowed:

    // RequestConditionType is the type of a CertificateSigningRequestCondition
    // +enum
    type RequestConditionType string
    
  • This makes explicit reference to a future value we want to add:

    // +enum
    type TaintEffect string
      // NOT YET IMPLEMENTED. TODO: Uncomment field once it is implemented.
      // Like TaintEffectNoSchedule, but additionally do not allow pods submitted to
      // Kubelet without going through the scheduler to start.
      // Enforced by Kubelet and the scheduler.
      // TaintEffectNoScheduleNoAdmit TaintEffect = "NoScheduleNoAdmit"
    
  • We've regularly added new resourcequota scopes:

    // A ResourceQuotaScope defines a filter that must match each object tracked by a quota
    // +enum
    type ResourceQuotaScope string
    
  • Docs reference "currently supported" values, which sounds like there could be expansion in the future:

    // AddressType represents the type of address referred to by an endpoint.
    // +enum
    type AddressType string
    
    The following address types are currently supported:
      // * IPv4: Represents an IPv4 Address.
      // * IPv6: Represents an IPv6 Address.
      // * FQDN: Represents a Fully Qualified Domain Name.
    

Second, and more concerning, this breaks deserialization of new values by old generated clients using enums. I didn't see this aspect discussed in the KEP at all, and it's fairly significant.

@liggitt
Copy link
Member

liggitt commented Mar 31, 2022

I opened #109177 to track this issue

For 1.24, I opened #109178 to fix the CSR condition and omit enums from the static openapi snapshot to avoid propagating enums into generated clients until we understand the implications better.

For 1.23, I picked that change back in #109179 as well, to fix the CSR condition and avoid exposing an alpha-level feature.

@liggitt
Copy link
Member

liggitt commented Mar 31, 2022

#109179 is now merged to the release-1.23 branch, so regenerating a client from the HEAD of release-1.23 would resolve enum-based issues

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. area/apiserver area/cloudprovider area/code-generation area/dependency Issues or PRs related to dependency changes area/kubectl area/test cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. kind/api-change Categorizes issue or PR as related to adding, removing, or otherwise changing an API kind/feature Categorizes issue or PR as related to a new feature. lgtm "Looks good to me", indicates that a PR is ready to be merged. priority/important-soon Must be staffed and worked on either currently, or very soon, ideally in time for the next release. release-note-none Denotes a PR that doesn't merit a release note. sig/api-machinery Categorizes an issue or PR as relevant to SIG API Machinery. sig/cloud-provider Categorizes an issue or PR as relevant to SIG Cloud Provider. sig/testing Categorizes an issue or PR as relevant to SIG Testing. size/L Denotes a PR that changes 100-499 lines, ignoring generated files. triage/accepted Indicates an issue or PR is ready to be actively worked on.
Projects
Archived in project
SIG Auth Old
Closed / Done
Development

Successfully merging this pull request may close these issues.

None yet