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

✨ Set default value for corev1.Protocol type if not already specified #480

Merged

Conversation

munnerz
Copy link
Member

@munnerz munnerz commented Aug 26, 2020

Mitigation to help resolve #444 whilst waiting for kubernetes/enhancements#1928 to merge.

This patch will cause resources that embed e.g. ServiceSpec or PodSpec to have the default field set for the protocol field:

                          ports:
                            items:
                              properties:
                                appProtocol:
                                  type: string
                                name:
                                  type: string
                                nodePort:
                                  format: int32
                                  type: integer
                                port:
                                  format: int32
                                  type: integer
                                protocol:
                                  default: TCP
                                  type: string
                                targetPort:
                                  anyOf:
                                  - type: integer
                                  - type: string
                                  x-kubernetes-int-or-string: true
                              required:
                              - port
                              type: object
                            type: array
                            x-kubernetes-list-map-keys:
                            - port
                            - protocol
                            x-kubernetes-list-type: map

This will workaround the issue described in #444, and can be easily extended to new fields/types as they are discovered.

We should probably also consider fixing #445 as well, as this may cause some users that are not specifying // +kubebuilder:default annotations in their resources to begin specifying a default field (if they utilise this Protocol type), which will cause kubectl client side validation errors (which will require users to add --validate=false to their kubectl create usages)

/cc @pwittrock @tamalsaha

@k8s-ci-robot
Copy link
Contributor

@munnerz: GitHub didn't allow me to request PR reviews from the following users: tamalsaha.

Note that only kubernetes-sigs members and repo collaborators can review this PR, and authors cannot review their own PRs.

In response to this:

Mitigation to help resolve #444 whilst waiting for kubernetes/enhancements#1928 to merge.

This patch will cause resources that embed e.g. ServiceSpec or PodSpec to have the default field set for the protocol field:

                         ports:
                           items:
                             properties:
                               appProtocol:
                                 type: string
                               name:
                                 type: string
                               nodePort:
                                 format: int32
                                 type: integer
                               port:
                                 format: int32
                                 type: integer
                               protocol:
                                 default: TCP
                                 type: string
                               targetPort:
                                 anyOf:
                                 - type: integer
                                 - type: string
                                 x-kubernetes-int-or-string: true
                             required:
                             - port
                             type: object
                           type: array
                           x-kubernetes-list-map-keys:
                           - port
                           - protocol
                           x-kubernetes-list-type: map

This will workaround the issue described in #444, and can be easily extended to new fields/types as they are discovered.

We should probably also consider fixing #445 as well, as this may cause some users that are not specifying // +kubebuilder:default annotations in their resources to begin specifying a default field (if they utilise this Protocol type), which will cause kubectl client side validation errors (which will require users to add --validate=false to their kubectl create usages)

/cc @pwittrock @tamalsaha

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

@k8s-ci-robot k8s-ci-robot added cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. size/S Denotes a PR that changes 10-29 lines, ignoring generated files. labels Aug 26, 2020
@pwittrock
Copy link
Contributor

This is great. Is this possible to unit test for?

@pwittrock
Copy link
Contributor

Any idea why the tests are failing?

@munnerz
Copy link
Member Author

munnerz commented Aug 26, 2020

I've updated the testdata (the CronJob resource used embeds a PodSpec, so this change causes the default field to be correctly generated).

In terms of testing specifically this change, we don't currently have any unit test for gen.go or known_types.go. All testing functionality is encompassed through the existing testdata directory with the single CronJob CRD (and is being exercised due to the embedded PodSpec)

The only behaviour that is not tested is ensuring that if // +kubebuilder:default is explicitly specified on this field (or any other marker that gets added once the KEP merges), that it takes precedence over this built-in. The only way we can test that is if the annotation/marker actually does exist on the upstream type, as k8s.io/api/core/v1 is hardcoded into the known_types file.

The only way I can think of to work around this is to have the integration test framework inject a 'copy' of this package/entry into KnownTypes during tests only, which could contain a type named Protocol and thus make the existing logic in that function usable against our fake test package. If you don't think that sounds too brittle/awful, I can put that together 😄

@munnerz munnerz force-pushed the corev1-protocol-default-value branch from 6fb84ad to 2f191a5 Compare August 27, 2020 09:43
@munnerz
Copy link
Member Author

munnerz commented Aug 27, 2020

Okay, I did some more messing around with this and concluded that adding a test to ensure that the default value, if explicit specified with a marker, overrides this built-in default is actually not particularly useful (and not trivial, as during the evaluation of the functions defined in known_types.go, the markers have not yet been processed so we are unable to determine whether an explicit default has been provided here).

Given that this default value is not going to change within k8s.io/api/core/v1, I think it is safe for us to always set this default value to TCP. #481 will help users running older Kubernetes versions (pre 1.18) that do not support default values either.

@pwittrock this is ready for review now and passing 😄

@pwittrock
Copy link
Contributor

@munnerz Can you look into the failed tests?

@munnerz munnerz force-pushed the corev1-protocol-default-value branch from 2f191a5 to 0c2ff29 Compare September 1, 2020 16:53
@munnerz
Copy link
Member Author

munnerz commented Sep 1, 2020

This PR seems to do the same thing: #440

@pwittrock
Copy link
Contributor

This PR seems to do the same thing: #440

The comments on that PR don't seem like blockers. Do you have a preference for which PR we take?

@pwittrock
Copy link
Contributor

/lgtm
/approve

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Sep 2, 2020
@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: munnerz, pwittrock

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 Sep 2, 2020
@k8s-ci-robot k8s-ci-robot merged commit 2f203e4 into kubernetes-sigs:master Sep 2, 2020
@pugangxa
Copy link

pugangxa commented Sep 4, 2020

It still does not work for me. if "crdVersions=v1" is specified default value is not generated, see #476

If I use v1beta1, then although default value is generated, but during apply it report error like:

* spec.validation.openAPIV3Schema.properties[spec].properties[predictor].properties[triton].properties[ports].items.properties[protocol].default: Forbidden: must not be set
* spec.validation.openAPIV3Schema.properties[spec].properties[predictor].properties[tensorflow].properties[ports].items.properties[protocol].default: Forbidden: must not be set
* spec.validation.openAPIV3Schema.properties[spec].properties[predictor].properties[onnx].properties[ports].items.properties[protocol].default: Forbidden: must not be set
* spec.validation.openAPIV3Schema.properties[spec].properties[predictor].properties[pytorch].properties[ports].items.properties[protocol].default: Forbidden: must not be set
* spec.validation.openAPIV3Schema.properties[spec].properties[predictor].properties[sklearn].properties[ports].items.properties[protocol].default: Forbidden: must not be set
* spec.validation.openAPIV3Schema.properties[spec].properties[predictor].properties[xgboost].properties[ports].items.properties[protocol].default: Forbidden: must not be set

Any ideas? Thanks~

cqbqdd11519 added a commit to tmax-cloud/cicd-operator that referenced this pull request Nov 24, 2020
cqbqdd11519 added a commit to tmax-cloud/cicd-operator that referenced this pull request Nov 24, 2020
@estroz
Copy link
Contributor

estroz commented Dec 1, 2020

I'm hoping this can be cherry-picked to fix controller-gen in v0.3 for users still wanting v1beta1 CRDs and webhooks by default (ex. in kubebuilder's go/v2 plugin).

/cherry-pick release-0.3

benwh added a commit to gocardless/theatre that referenced this pull request Mar 5, 2021
This crucially includes [controller-tools#480][0] which allows us to
apply these manifests to a Kubernetes 1.18 cluster without issues.

A key change here is that we'll now generate CRD manifests using the
`apiextensions.k8s.io/v1` API version rather than `v1beta1`.

[0]: kubernetes-sigs/controller-tools#480 which
benwh added a commit to gocardless/theatre that referenced this pull request Mar 5, 2021
This crucially includes [controller-tools#480][0] which allows us to
apply these manifests to a Kubernetes 1.18 cluster without issues.

A key change here is that we'll now generate CRD manifests using the
`apiextensions.k8s.io/v1` API version rather than `v1beta1`.

[0]: kubernetes-sigs/controller-tools#480
STRRL added a commit to STRRL/chaos-mesh that referenced this pull request May 24, 2021
 compliable issue with kubernetes 1.18, detail:
kubernetes-sigs/controller-tools#480

Signed-off-by: STRRL <str_ruiling@outlook.com>
STRRL added a commit to STRRL/chaos-mesh that referenced this pull request May 24, 2021
 compliable issue with kubernetes 1.18, detail:
kubernetes-sigs/controller-tools#480

Signed-off-by: STRRL <str_ruiling@outlook.com>
ti-chi-bot added a commit to chaos-mesh/chaos-mesh that referenced this pull request Jun 16, 2021
* feat: conditional branches in workflownode

Signed-off-by: STRRL <str_ruiling@outlook.com>

* feat: reconciler of task node

Signed-off-by: STRRL <str_ruiling@outlook.com>

* feat: setup one container for task node

Signed-off-by: STRRL <str_ruiling@outlook.com>

* fix: address the comments by muse-dev

Signed-off-by: STRRL <str_ruiling@outlook.com>

* chore: cleanup duplicated codes

Signed-off-by: STRRL <str_ruiling@outlook.com>

* fix: bump controller-gen version

 compliable issue with kubernetes 1.18, detail:
kubernetes-sigs/controller-tools#480

Signed-off-by: STRRL <str_ruiling@outlook.com>

* chore: bump the version of controller-gen to v0.4.1

Signed-off-by: STRRL <str_ruiling@outlook.com>

* fix: restrict kubernetes version with replace

Signed-off-by: STRRL <str_ruiling@outlook.com>

* fix: also restrict version for e2e-test

Signed-off-by: STRRL <str_ruiling@outlook.com>

* fix: also bump the verison of controller-runtime

Signed-off-by: STRRL <str_ruiling@outlook.com>

* fix: fallback to v1beta1

Signed-off-by: STRRL <str_ruiling@outlook.com>

* Revert "fix: also bump the verison of controller-runtime"

This reverts commit 8e05196.

Signed-off-by: STRRL <str_ruiling@outlook.com>

* fix: fill the conditional branches if nil

Signed-off-by: STRRL <str_ruiling@outlook.com>

* fix: permission of custom task about creating pod

Signed-off-by: STRRL <str_ruiling@outlook.com>

* feat: use v1 instead of v1beta1 with CRD

Signed-off-by: STRRL <str_ruiling@outlook.com>

* fix: NOOP when pods exactly same

Signed-off-by: STRRL <str_ruiling@outlook.com>

* refactor: refactor the collectors

Signed-off-by: STRRL <str_ruiling@outlook.com>

* feat: finish the rest of task nodes

Signed-off-by: STRRL <str_ruiling@outlook.com>

* fix: check before sync nodes in task

Signed-off-by: STRRL <str_ruiling@outlook.com>

* chore: make linters happier

Signed-off-by: STRRL <str_ruiling@outlook.com>

* chore: make linters happier

Signed-off-by: STRRL <str_ruiling@outlook.com>

* Update pkg/workflow/controllers/task_reconciler.go

Signed-off-by: STRRL <str_ruiling@outlook.com>

Co-authored-by: AsterNighT <klxjt99@outlook.com>

* fix: use create instead of apply

apply will create a huge annotaion which exceed the limit of size of
annotation

Signed-off-by: STRRL <str_ruiling@outlook.com>

* chore: update the comments for workflow template

Signed-off-by: STRRL <str_ruiling@outlook.com>

* fix: crd installation in e2e test

Signed-off-by: STRRL <str_ruiling@outlook.com>

Co-authored-by: AsterNighT <klxjt99@outlook.com>
Co-authored-by: Ti Chi Robot <ti-community-prow-bot@tidb.io>
aido123 pushed a commit to aido123/chaos-mesh that referenced this pull request Jun 16, 2021
* feat: conditional branches in workflownode

Signed-off-by: STRRL <str_ruiling@outlook.com>

* feat: reconciler of task node

Signed-off-by: STRRL <str_ruiling@outlook.com>

* feat: setup one container for task node

Signed-off-by: STRRL <str_ruiling@outlook.com>

* fix: address the comments by muse-dev

Signed-off-by: STRRL <str_ruiling@outlook.com>

* chore: cleanup duplicated codes

Signed-off-by: STRRL <str_ruiling@outlook.com>

* fix: bump controller-gen version

 compliable issue with kubernetes 1.18, detail:
kubernetes-sigs/controller-tools#480

Signed-off-by: STRRL <str_ruiling@outlook.com>

* chore: bump the version of controller-gen to v0.4.1

Signed-off-by: STRRL <str_ruiling@outlook.com>

* fix: restrict kubernetes version with replace

Signed-off-by: STRRL <str_ruiling@outlook.com>

* fix: also restrict version for e2e-test

Signed-off-by: STRRL <str_ruiling@outlook.com>

* fix: also bump the verison of controller-runtime

Signed-off-by: STRRL <str_ruiling@outlook.com>

* fix: fallback to v1beta1

Signed-off-by: STRRL <str_ruiling@outlook.com>

* Revert "fix: also bump the verison of controller-runtime"

This reverts commit 8e05196.

Signed-off-by: STRRL <str_ruiling@outlook.com>

* fix: fill the conditional branches if nil

Signed-off-by: STRRL <str_ruiling@outlook.com>

* fix: permission of custom task about creating pod

Signed-off-by: STRRL <str_ruiling@outlook.com>

* feat: use v1 instead of v1beta1 with CRD

Signed-off-by: STRRL <str_ruiling@outlook.com>

* fix: NOOP when pods exactly same

Signed-off-by: STRRL <str_ruiling@outlook.com>

* refactor: refactor the collectors

Signed-off-by: STRRL <str_ruiling@outlook.com>

* feat: finish the rest of task nodes

Signed-off-by: STRRL <str_ruiling@outlook.com>

* fix: check before sync nodes in task

Signed-off-by: STRRL <str_ruiling@outlook.com>

* chore: make linters happier

Signed-off-by: STRRL <str_ruiling@outlook.com>

* chore: make linters happier

Signed-off-by: STRRL <str_ruiling@outlook.com>

* Update pkg/workflow/controllers/task_reconciler.go

Signed-off-by: STRRL <str_ruiling@outlook.com>

Co-authored-by: AsterNighT <klxjt99@outlook.com>

* fix: use create instead of apply

apply will create a huge annotaion which exceed the limit of size of
annotation

Signed-off-by: STRRL <str_ruiling@outlook.com>

* chore: update the comments for workflow template

Signed-off-by: STRRL <str_ruiling@outlook.com>

* fix: crd installation in e2e test

Signed-off-by: STRRL <str_ruiling@outlook.com>

Co-authored-by: AsterNighT <klxjt99@outlook.com>
Co-authored-by: Ti Chi Robot <ti-community-prow-bot@tidb.io>
Signed-off-by: root <root@vm3.jisiqhvhuzaupp2x5y5vff1eff.fx.internal.cloudapp.net>
aido123 pushed a commit to aido123/chaos-mesh that referenced this pull request Jun 16, 2021
* feat: conditional branches in workflownode

Signed-off-by: STRRL <str_ruiling@outlook.com>

* feat: reconciler of task node

Signed-off-by: STRRL <str_ruiling@outlook.com>

* feat: setup one container for task node

Signed-off-by: STRRL <str_ruiling@outlook.com>

* fix: address the comments by muse-dev

Signed-off-by: STRRL <str_ruiling@outlook.com>

* chore: cleanup duplicated codes

Signed-off-by: STRRL <str_ruiling@outlook.com>

* fix: bump controller-gen version

 compliable issue with kubernetes 1.18, detail:
kubernetes-sigs/controller-tools#480

Signed-off-by: STRRL <str_ruiling@outlook.com>

* chore: bump the version of controller-gen to v0.4.1

Signed-off-by: STRRL <str_ruiling@outlook.com>

* fix: restrict kubernetes version with replace

Signed-off-by: STRRL <str_ruiling@outlook.com>

* fix: also restrict version for e2e-test

Signed-off-by: STRRL <str_ruiling@outlook.com>

* fix: also bump the verison of controller-runtime

Signed-off-by: STRRL <str_ruiling@outlook.com>

* fix: fallback to v1beta1

Signed-off-by: STRRL <str_ruiling@outlook.com>

* Revert "fix: also bump the verison of controller-runtime"

This reverts commit 8e05196.

Signed-off-by: STRRL <str_ruiling@outlook.com>

* fix: fill the conditional branches if nil

Signed-off-by: STRRL <str_ruiling@outlook.com>

* fix: permission of custom task about creating pod

Signed-off-by: STRRL <str_ruiling@outlook.com>

* feat: use v1 instead of v1beta1 with CRD

Signed-off-by: STRRL <str_ruiling@outlook.com>

* fix: NOOP when pods exactly same

Signed-off-by: STRRL <str_ruiling@outlook.com>

* refactor: refactor the collectors

Signed-off-by: STRRL <str_ruiling@outlook.com>

* feat: finish the rest of task nodes

Signed-off-by: STRRL <str_ruiling@outlook.com>

* fix: check before sync nodes in task

Signed-off-by: STRRL <str_ruiling@outlook.com>

* chore: make linters happier

Signed-off-by: STRRL <str_ruiling@outlook.com>

* chore: make linters happier

Signed-off-by: STRRL <str_ruiling@outlook.com>

* Update pkg/workflow/controllers/task_reconciler.go

Signed-off-by: STRRL <str_ruiling@outlook.com>

Co-authored-by: AsterNighT <klxjt99@outlook.com>

* fix: use create instead of apply

apply will create a huge annotaion which exceed the limit of size of
annotation

Signed-off-by: STRRL <str_ruiling@outlook.com>

* chore: update the comments for workflow template

Signed-off-by: STRRL <str_ruiling@outlook.com>

* fix: crd installation in e2e test

Signed-off-by: STRRL <str_ruiling@outlook.com>

Co-authored-by: AsterNighT <klxjt99@outlook.com>
Co-authored-by: Ti Chi Robot <ti-community-prow-bot@tidb.io>
Signed-off-by: root <root@vm3.jisiqhvhuzaupp2x5y5vff1eff.fx.internal.cloudapp.net>
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/S Denotes a PR that changes 10-29 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Default value not allowed for in x-kubernetes-list-map-keys
5 participants