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

Remove SetDefaults from Service() function #6202

Merged
merged 3 commits into from
Dec 16, 2019

Conversation

taragu
Copy link
Contributor

@taragu taragu commented Dec 11, 2019

/lint

Proposed Changes

Remove SetDefaults from default service creation so that ServiceOption's can override the defaults. Also update unit tests to set defaults for services.

Release Note

NONE

/cc @nimakaviani

@googlebot googlebot added the cla: yes Indicates the PR's author has signed the CLA. label Dec 11, 2019
@knative-prow-robot knative-prow-robot added the size/S Denotes a PR that changes 10-29 lines, ignoring generated files. label Dec 11, 2019
Copy link
Contributor

@knative-prow-robot knative-prow-robot left a comment

Choose a reason for hiding this comment

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

@taragu: 0 warnings.

In response to this:

/lint

Proposed Changes

Moving SetDefaults before ServiceOption's are applied so that ServiceOption's can override the defaults

Release Note

NONE

/cc @nimakaviani

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.

@knative-prow-robot knative-prow-robot added the area/test-and-release It flags unit/e2e/conformance/perf test issues for product features label Dec 11, 2019
@vagababov
Copy link
Contributor

/approve
I'll leave lgtm to Dan
/assign @dgerd

@knative-prow-robot knative-prow-robot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Dec 11, 2019
@dgerd
Copy link

dgerd commented Dec 11, 2019

I'm a bit confused why this is needed. Defaults should only be set on unspecified fields (nil, "", or 0 values). If you are overriding a field with ServiceOptions then the defaulter should skip over this.

What is this trying to unblock?

@nimakaviani
Copy link
Contributor

this is particularly to allow for ServiceOptions to remove or modify defaults, if needed.

Note that in tests, this defaulting is done when creating the service object and before it is sent to the serving api, so this gives more flexibility to the tests to decide on the shape of the object that hits the api endpoints.

Also assuming that the code implements Dave Cheney's functional options pattern, semantically it makes more sense for the options to be the last to operate on the service object

@dgerd
Copy link

dgerd commented Dec 12, 2019

this is particularly to allow for ServiceOptions to remove or modify defaults, if needed.

I still am not following... If I write a service option that sets revisionTimeoutSeconds: 301 when the defaulter runs it does not touch that value since it is set. Shouldn't this be the same as defaulting revisionTimeoutSeconds: 300 and then overriding the value to 301.

I don't see the point in changing this unless I am missing something.

@dgerd
Copy link

dgerd commented Dec 12, 2019

And to clarify the reason why I don't think we should change this is that we use this function in our unit tests where our webhook is not called to set defaults. The pattern here of defaulting after the user provides the full Service they desire is what will happen when actually running Knative. Moving defaulting first could mask problems in our defaulter where we accidentally modify values set by the user.

@nimakaviani
Copy link
Contributor

what if in a test you want to remove a default value and test for the impact?

we use this function in our unit tests where our webhook is not called to set defaults. The pattern here of defaulting after the user provides the full Service they desire is what will happen when actually running Knative. Moving defaulting first could mask problems in our defaulter where we accidentally modify values set by the user.

In the above case, I would consider it a bug in setting up the unit tests and the issue is isolated to the test that uses the ServiceOption in an improper way.

The bigger problem is that we use the same function for creating a service object in integration tests too. This means every service object will have the defaults set before making it to the test cluster, which will mask problems with how defaulting would work in webhooks. I think that is more of an issue in fact.

@dgerd
Copy link

dgerd commented Dec 12, 2019

The bigger problem is that we use the same function for creating a service object in integration tests too. This means every service object will have the defaults set before making it to the test cluster, which will mask problems with how defaulting would work in webhooks. I think that is more of an issue in fact.

I agree that does sound problematic and we should fix that.

@knative-prow-robot knative-prow-robot added size/L Denotes a PR that changes 100-499 lines, ignoring generated files. and removed size/S Denotes a PR that changes 10-29 lines, ignoring generated files. labels Dec 12, 2019
@knative-prow-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: taragu, vagababov

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

@knative-prow-robot knative-prow-robot added the area/API API objects and controllers label Dec 12, 2019
Copy link
Contributor

@nimakaviani nimakaviani left a comment

Choose a reason for hiding this comment

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

@taragu you probably want the ServiceOption that sets the defaults to be the first in unit tests.

To @dgerd 's point, since the behavior is supposed to imitate the webhook, defaulting should apply to the service before the other opts.

@knative-test-reporter-robot

The following jobs failed:

Test name Triggers Retries
pull-knative-serving-unit-tests 0/3

Failed non-flaky tests preventing automatic retry of pull-knative-serving-unit-tests:

pkg/reconciler/nscert.TestDomainConfigDefaultDomain

@taragu
Copy link
Contributor Author

taragu commented Dec 13, 2019

/retest

1 similar comment
@taragu
Copy link
Contributor Author

taragu commented Dec 13, 2019

/retest

func config(name, namespace string, so ServiceOption, co ...ConfigOption) *v1alpha1.Configuration {
s := Service(name, namespace, so)
func config(name, namespace string, sos []ServiceOption, co ...ConfigOption) *v1alpha1.Configuration {
s := Service(name, namespace, sos...)
s.SetDefaults(v1.WithUpgradeViaDefaulting(context.Background()))
Copy link
Contributor

Choose a reason for hiding this comment

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

looks like we apply defaulting to the service in config and route. I wonder if it is necessary to pass the defaulting ServiceOption through. or if we do, we should probably call it with v1.WithUpgradeViaDefaulting(context.Background() and then remove the line above?

func route(name, namespace string, so ServiceOption, ro ...RouteOption) *v1alpha1.Route {
s := Service(name, namespace, so)
func route(name, namespace string, sos []ServiceOption, ro ...RouteOption) *v1alpha1.Route {
s := Service(name, namespace, sos...)
Copy link
Contributor

Choose a reason for hiding this comment

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

same as for config

@taragu taragu force-pushed the e2e-set-defaults branch 2 times, most recently from 3ea4b86 to 6011e70 Compare December 13, 2019 19:43
@dgerd
Copy link

dgerd commented Dec 16, 2019

Thanks for the updates. Can you change the commit and PR description? Otherwise this looks good to me.

Given its ubiquity in unit tests I could see DefaultedService() as an appealing helper function so I don't forget to add WithServiceDefaults at the end of my argument list every time. But I will leave that up to your discretion.

@taragu
Copy link
Contributor Author

taragu commented Dec 16, 2019

Thanks @dgerd ! I've updated the description and added the default service helper method. Please review.

Comment on lines 70 to 72
s := &v1alpha1.Service{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: namespace,
},
}
for _, opt := range so {
opt(s)
}
s.SetDefaults(context.Background())
return s
Copy link

Choose a reason for hiding this comment

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

Why not reuse the existing Service function by calling it?

Service(name, namespace, so, WithServiceDefaults)

@dgerd
Copy link

dgerd commented Dec 16, 2019

For commit names you will want to keep the description shorter and then add details in a paragraph below. This helps it render correctly on Github and keeps it readable on command line tools.

For example,

Remove `SetDefaults` from Service() function

This change removes `SetDefaults` so that `ServiceOption`'s can override the defaults. 
Also update unit tests to set defaults for services.

This change removes `SetDefaults` so that `ServiceOption`'s can override the defaults.
Also update unit tests to set defaults for services.
@taragu taragu changed the title Apply ServiceOptions after setting defaults Remove SetDefaults from Service() function Dec 16, 2019
@taragu
Copy link
Contributor Author

taragu commented Dec 16, 2019

@dgerd thanks for the suggestions! I've updated the PR. Would you please review again?

Copy link

@dgerd dgerd left a comment

Choose a reason for hiding this comment

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

One last comment. I thought I had already sent this.


// DefaultService creates a service with ServiceOptions and with default values set
func DefaultService(name, namespace string, so ...ServiceOption) *v1alpha1.Service {
s := Service(name, namespace, so...)
Copy link

Choose a reason for hiding this comment

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

You already have a function for defaults. You can simplify this further :)

Suggested change
s := Service(name, namespace, so...)
s := Service(name, namespace, append(so,WithServiceDefaults)...)

return s
}

// DefaultService creates a service with ServiceOptions and with default values set
func DefaultService(name, namespace string, so ...ServiceOption) *v1alpha1.Service {
s := Service(name, namespace, append(so,WithServiceDefaults)...)
Copy link

Choose a reason for hiding this comment

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

Now that you have combined it all into one line you don't need a variable or separate return statement.

Suggested change
s := Service(name, namespace, append(so,WithServiceDefaults)...)
return Service(name, namespace, append(so,WithServiceDefaults)...)

Copy link
Contributor

Choose a reason for hiding this comment

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

That is actually logically wrong

@dgerd
Copy link

dgerd commented Dec 16, 2019

/lgtm
/hold
for victor as I am not sure the logic is wrong.

@knative-prow-robot knative-prow-robot added do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. lgtm Indicates that a PR is ready to be merged. labels Dec 16, 2019
@vagababov
Copy link
Contributor

/hold cancel
As discussed, I am presuming worst case, which should not happen [case being that Defaults is applied after all so and I've seen this fail before, but probably here we're safe].

@knative-prow-robot knative-prow-robot removed the do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. label Dec 16, 2019
@knative-prow-robot knative-prow-robot merged commit 310886a into knative:master Dec 16, 2019
@taragu taragu deleted the e2e-set-defaults branch January 2, 2020 12:28
@taragu taragu restored the e2e-set-defaults branch February 18, 2020 15:07
@taragu taragu deleted the e2e-set-defaults branch February 18, 2020 15:08
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/API API objects and controllers area/test-and-release It flags unit/e2e/conformance/perf test issues for product features cla: yes Indicates the PR's author has signed the CLA. lgtm Indicates that a PR is ready to be merged. size/L Denotes a PR that changes 100-499 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants