-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Conversation
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.
@taragu: 0 warnings.
In response to this:
/lint
Proposed Changes
Moving
SetDefaults
beforeServiceOption
's are applied so thatServiceOption
's can override the defaultsRelease 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.
/approve |
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? |
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 |
I still am not following... If I write a service option that sets I don't see the point in changing this unless I am missing something. |
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. |
what if in a test you want to remove a default value and test for the impact?
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. |
I agree that does sound problematic and we should fix that. |
[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 |
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.
57c0acc
to
2c4bb0b
Compare
The following jobs failed:
Failed non-flaky tests preventing automatic retry of pull-knative-serving-unit-tests:
|
/retest |
1 similar comment
/retest |
2c4bb0b
to
b45d0c3
Compare
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())) |
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.
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...) |
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.
same as for config
3ea4b86
to
6011e70
Compare
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 |
6011e70
to
f8b7be4
Compare
Thanks @dgerd ! I've updated the description and added the default service helper method. Please review. |
pkg/testing/v1alpha1/service.go
Outdated
s := &v1alpha1.Service{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: name, | ||
Namespace: namespace, | ||
}, | ||
} | ||
for _, opt := range so { | ||
opt(s) | ||
} | ||
s.SetDefaults(context.Background()) | ||
return s |
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.
Why not reuse the existing Service function by calling it?
Service(name, namespace, so, WithServiceDefaults)
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,
|
This change removes `SetDefaults` so that `ServiceOption`'s can override the defaults. Also update unit tests to set defaults for services.
e26fa25
to
cf5efbd
Compare
SetDefaults
from Service() function
@dgerd thanks for the suggestions! I've updated the PR. Would you please review again? |
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.
One last comment. I thought I had already sent this.
pkg/testing/v1alpha1/service.go
Outdated
|
||
// 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...) |
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.
You already have a function for defaults. You can simplify this further :)
s := Service(name, namespace, so...) | |
s := Service(name, namespace, append(so,WithServiceDefaults)...) |
pkg/testing/v1alpha1/service.go
Outdated
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)...) |
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.
Now that you have combined it all into one line you don't need a variable or separate return statement.
s := Service(name, namespace, append(so,WithServiceDefaults)...) | |
return Service(name, namespace, append(so,WithServiceDefaults)...) |
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.
That is actually logically wrong
/lgtm |
/hold cancel |
/lint
Proposed Changes
Remove
SetDefaults
from default service creation so thatServiceOption
's can override the defaults. Also update unit tests to set defaults for services.Release Note
/cc @nimakaviani