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
[jobs][registry]: Warn when propogationpolicy is not set #104080
[jobs][registry]: Warn when propogationpolicy is not set #104080
Conversation
@ravisantoshgudimetla: This issue is currently awaiting triage. If a SIG or subproject determines this is a relevant issue, they will accept it by applying the The 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. |
@@ -117,3 +118,13 @@ func (r *StatusREST) Update(ctx context.Context, name string, objInfo rest.Updat | |||
func (r *StatusREST) GetResetFields() map[fieldpath.APIVersion]*fieldpath.Set { | |||
return r.store.GetResetFields() | |||
} | |||
|
|||
func (r *StatusREST) Delete(ctx context.Context, name string, deleteValidation rest.ValidateObjectFunc, options *metav1.DeleteOptions) (runtime.Object, bool, error) { | |||
if options.PropagationPolicy == nil { |
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.
@liggitt . Especially this line. The other option is to have a method similar to WarningOnCreate:
// WarningsOnDelete returns warnings for the deletion of job object. It can be moved to RESTDeleteStrategy interface but
// not sure, if there are any objects that can benefit from this movement. So, keeping this method local to
// jobStrategy for now.
func (jobStrategy) WarningsOnDelete(ctx context.Context, obj runtime.Object) []string {
newJob := obj.(*batch.Job)
}
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.
I have a hard time seeing more than this use of a warning on delete, so I probably wouldn't add a general method like WarningsOnDelete
yet
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.
if options.PropagationPolicy == nil { | |
if options.PropagationPolicy == nil && options.OrphanDependents == nil { |
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.
So, the approach is fine? Having set the warnings in storage? Because I see no other example of warnings being used in storage.
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.
And isn't OrphanDependents a deprecated one?
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.
So, the approach is fine? Having set the warnings in storage? Because I see no other example of warnings being used in storage.
we tend to add warnings at the level that knows the relevant info... endpoint-level warnings for deprecated APIs are added in k8s.io/apiserver/pkg/endpoints/*
, object-level warnings for create/update are added in strategy create and update methods, this is related to delete options the storage/strategy has an opinion about, so adding in delete implementations doesn't seem bad to me
isn't OrphanDependents a deprecated one?
yes, but if the requester has explicitly set that, we don't have to wonder if they are getting implicit behavior they didn't intend
e56adce
to
224468e
Compare
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.
@liggitt - This is up for review. Can you PTAL
if options != nil && options.PropagationPolicy == nil && options.OrphanDependents == nil { | ||
// Throw a warning if delete options are not explicitly set as Job deletion strategy by default is orphaning | ||
// pods in v1. | ||
r.warnings = deleteOptionWarnings |
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.
don't mutate r
here
// Throw a warning if delete options are not explicitly set as Job deletion strategy by default is orphaning | ||
// pods in v1. | ||
r.warnings = deleteOptionWarnings | ||
for _, warningMsg := range r.warnings { |
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.
no need to range, just add the single warning you want
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.
I started with a string and realized, it'd be easier to extend if we go with a map. Changed it back to string.
@@ -91,6 +102,30 @@ func (r *REST) Categories() []string { | |||
return []string{"all"} | |||
} | |||
|
|||
func (r *REST) Delete(ctx context.Context, name string, deleteValidation rest.ValidateObjectFunc, options *metav1.DeleteOptions) (runtime.Object, bool, error) { | |||
if options != nil && options.PropagationPolicy == nil && options.OrphanDependents == nil { |
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.
limit the warning to requests which default to OrphanDependents via batch/v1 by adding this:
&& job.Strategy.DefaultGarbageCollectionPolicy(ctx) == rest.OrphanDependents
if deleteOptions.PropagationPolicy == nil && deleteOptions.OrphanDependents == nil { | ||
// Throw a warning if delete options are not explicitly set as Job deletion strategy by default is orphaning | ||
// pods in v1. | ||
r.warnings = deleteOptionWarnings |
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.
don't mutate r
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.
This part of codebase is new to me. I was a bit hesistant to mutate r but I did not know how to surface the warning. Now, I think I am clear, you want me to mock out recorder and capture warnings from the recorder. Will do that. Thank you :)
// Throw a warning if delete options are not explicitly set as Job deletion strategy by default is orphaning | ||
// pods in v1. | ||
r.warnings = deleteOptionWarnings | ||
for _, warningMsg := range r.warnings { |
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.
don't range, just add the warning you want
} | ||
|
||
func (r *REST) DeleteCollection(ctx context.Context, deleteValidation rest.ValidateObjectFunc, deleteOptions *metav1.DeleteOptions, listOptions *internalversion.ListOptions) (runtime.Object, error) { | ||
if deleteOptions.PropagationPolicy == nil && deleteOptions.OrphanDependents == nil { |
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.
&& job.Strategy.DefaultGarbageCollectionPolicy(ctx) == rest.OrphanDependents
@@ -132,12 +135,117 @@ func TestUpdate(t *testing.T) { | |||
) | |||
} | |||
|
|||
func TestDelete(t *testing.T) { |
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.
leave the existing test intact and add a new one to exercise the warning
test := genericregistrytest.New(t, storage.Job.Store) | ||
test.TestDelete(validNewJob()) | ||
func TestJobDeletion(t *testing.T) { | ||
ctx := genericapirequest.NewDefaultContext() |
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.
add a dummy warning recorder to the context (WithWarningRecorder
) to intercept whether warnings were added
78f35bf
to
afb95d1
Compare
var deleteOptionWarnings = "Set propagationPolicy=Orphan, if you want to intentionally orphan pods. If you want to " + | ||
"clean up children, you should set propagationPolicy=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.
I'd suggest something more succinct like child pods are preserved by default when jobs are deleted; set propagationPolicy=Background to remove them or set propagationPolicy=Orphan to suppress this warning
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.
Done. Thank you for the quick review
afb95d1
to
bac38da
Compare
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.
a couple nits, then lgtm
requestInfo *genericapirequest.RequestInfo | ||
}{ | ||
{ | ||
description: "DeleteOptions without any policy: warnings expected for batch/v1", |
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.
nit: these are really verbose for subtest names (they tend to blow out testgrid views of runs)
suggest something like no policy, v1, warning
, no policy, v2, no warning
, etc
if err := storage.Job.Storage.Create(ctxWithRecorder, key, job, nil, 0, false); err != nil { | ||
t.Fatalf("unexpected error: %v", err) | ||
} | ||
_, _, err := storage.Job.Delete(ctxWithRecorder, job.Name, rest.ValidateAllObjectFunc, test.deleteOptions) |
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 can simplify the tests to exercise both Delete and DeleteCollection in the same testcase, since they should behave identically
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.
Done. I was thinking of one test per function but I am fine with it.
If no propagation policy has been set, the pods associated with the jobs are going to linger because of OrphanDependents policy set currently. This patch ensures that a warning will be thrown when the user explicitly doesn't set deletionPolicy. More context: kubernetes#103449 (comment)
bac38da
to
3e44139
Compare
/retest |
/lgtm |
[APPROVALNOTIFIER] This PR is APPROVED This pull-request has been approved by: liggitt, ravisantoshgudimetla 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 |
/sig apps |
What type of PR is this?
What this PR does / why we need it:
Warn if no propagationpolicy set
If no propagation policy has been set, the pods associated
with the jobs are going to linger because of OrphanDependents
policy set currently. This patch ensures that a warning
will be thrown when the user explicitly doesn't set deletionPolicy.
Which issue(s) this PR fixes:
Fixes #
Special notes for your reviewer:
Does this PR introduce a user-facing change?
Additional documentation e.g., KEPs (Kubernetes Enhancement Proposals), usage docs, etc.:
xref: #103449 (comment)
cc @smarterclayton @liggitt @soltysh