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

[jobs][registry]: Warn when propogationpolicy is not set #104080

Merged

Conversation

ravisantoshgudimetla
Copy link
Contributor

@ravisantoshgudimetla ravisantoshgudimetla commented Aug 2, 2021

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?

Surface warning when users don't set propagationPolicy for jobs while deleting

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


xref: #103449 (comment)

cc @smarterclayton @liggitt @soltysh

@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. do-not-merge/release-note-label-needed Indicates that a PR should not merge because it's missing one of the release note labels. size/S Denotes a PR that changes 10-29 lines, ignoring generated files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. do-not-merge/needs-kind Indicates a PR lacks a `kind/foo` label and requires one. do-not-merge/needs-sig Indicates an issue or PR lacks a `sig/foo` label and requires one. labels Aug 2, 2021
@k8s-ci-robot
Copy link
Contributor

@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 triage/accepted label and provide further guidance.

The triage/accepted label can be added by org members by writing /triage accepted in a comment.

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 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 Aug 2, 2021
@@ -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 {
Copy link
Contributor Author

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)

}

Copy link
Member

@liggitt liggitt Aug 2, 2021

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

Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
if options.PropagationPolicy == nil {
if options.PropagationPolicy == nil && options.OrphanDependents == nil {

Copy link
Contributor Author

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.

Copy link
Contributor Author

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?

Copy link
Member

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

@k8s-ci-robot k8s-ci-robot added size/L Denotes a PR that changes 100-499 lines, ignoring generated files. release-note Denotes a PR that will be considered when it comes time to generate release notes. and removed size/S Denotes a PR that changes 10-29 lines, ignoring generated files. do-not-merge/release-note-label-needed Indicates that a PR should not merge because it's missing one of the release note labels. labels Aug 4, 2021
Copy link
Contributor Author

@ravisantoshgudimetla ravisantoshgudimetla left a 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

@ravisantoshgudimetla ravisantoshgudimetla changed the title [WIP][jobs][registry]: Warn when propogationpolicy is not set [jobs][registry]: Warn when propogationpolicy is not set Aug 4, 2021
@k8s-ci-robot k8s-ci-robot removed the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. label Aug 4, 2021
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
Copy link
Member

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 {
Copy link
Member

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

Copy link
Contributor Author

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 {
Copy link
Member

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
Copy link
Member

Choose a reason for hiding this comment

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

don't mutate r

Copy link
Contributor Author

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 {
Copy link
Member

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 {
Copy link
Member

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) {
Copy link
Member

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()
Copy link
Member

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

@ravisantoshgudimetla ravisantoshgudimetla force-pushed the warning-job-del branch 2 times, most recently from 78f35bf to afb95d1 Compare August 4, 2021 17:17
Comment on lines 55 to 57
var deleteOptionWarnings = "Set propagationPolicy=Orphan, if you want to intentionally orphan pods. If you want to " +
"clean up children, you should set propagationPolicy=Background."

Copy link
Member

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

Copy link
Contributor Author

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

Copy link
Member

@liggitt liggitt left a 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",
Copy link
Member

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)
Copy link
Member

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

Copy link
Contributor Author

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)
@ravisantoshgudimetla
Copy link
Contributor Author

/retest

@liggitt
Copy link
Member

liggitt commented Aug 5, 2021

/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 Aug 5, 2021
@k8s-ci-robot
Copy link
Contributor

[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 /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 Aug 5, 2021
@liggitt
Copy link
Member

liggitt commented Aug 5, 2021

/sig apps
/kind cleanup

@k8s-ci-robot k8s-ci-robot added sig/apps Categorizes an issue or PR as relevant to SIG Apps. kind/cleanup Categorizes issue or PR as related to cleaning up code, process, or technical debt. and removed do-not-merge/needs-sig Indicates an issue or PR lacks a `sig/foo` label and requires one. do-not-merge/needs-kind Indicates a PR lacks a `kind/foo` label and requires one. labels Aug 5, 2021
@k8s-ci-robot k8s-ci-robot merged commit 8d7a797 into kubernetes:master Aug 5, 2021
@k8s-ci-robot k8s-ci-robot added this to the v1.23 milestone Aug 5, 2021
@ravisantoshgudimetla ravisantoshgudimetla deleted the warning-job-del branch August 6, 2021 00:13
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. kind/cleanup Categorizes issue or PR as related to cleaning up code, process, or technical debt. lgtm "Looks good to me", indicates that a PR is ready to be merged. needs-priority Indicates a PR lacks a `priority/foo` label and requires one. needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one. release-note Denotes a PR that will be considered when it comes time to generate release notes. sig/apps Categorizes an issue or PR as relevant to SIG Apps. 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.

None yet

3 participants