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

warn user about resource being deleted #95544

Merged
merged 1 commit into from
Nov 5, 2020

Conversation

SaiHarshaK
Copy link
Contributor

@SaiHarshaK SaiHarshaK commented Oct 14, 2020

What type of PR is this?
/kind feature

What this PR does / why we need it:
Detect "kubectl apply" to resources currently being deleted and warn user.

Which issue(s) this PR fixes:
Ref #93363

Special notes for your reviewer:
/cc @apelisse
Not sure if i should use Fixes keyword so used Ref instead.

Does this PR introduce a user-facing change?:

Warns user when executing kubectl apply/diff to resource currently being deleted.

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


@k8s-ci-robot k8s-ci-robot added the release-note Denotes a PR that will be considered when it comes time to generate release notes. label Oct 14, 2020
@k8s-ci-robot k8s-ci-robot requested a review from apelisse October 14, 2020 05:38
@k8s-ci-robot k8s-ci-robot added kind/feature Categorizes issue or PR as related to a new feature. 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. needs-sig Indicates an issue or PR lacks a `sig/foo` label and requires one. needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one. needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. needs-priority Indicates a PR lacks a `priority/foo` label and requires one. labels Oct 14, 2020
@k8s-ci-robot
Copy link
Contributor

Hi @SaiHarshaK. Thanks for your PR.

I'm waiting for a kubernetes member to verify that this patch is reasonable to test. If it is, they should reply with /ok-to-test on its own line. Until that is done, I will not automatically test new commits in this PR, but the usual testing commands by org members will still work. Regular contributors should join the org to skip this step.

Once the patch is verified, the new status will be reflected by the ok-to-test label.

I understand the commands that are listed here.

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 area/kubectl sig/cli Categorizes an issue or PR as relevant to SIG CLI. and removed needs-sig Indicates an issue or PR lacks a `sig/foo` label and requires one. labels Oct 14, 2020
@dims
Copy link
Member

dims commented Oct 14, 2020

/ok-to-test

@k8s-ci-robot k8s-ci-robot added ok-to-test Indicates a non-member PR verified by an org member that is safe to test. and removed needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. labels Oct 14, 2020
Copy link
Member

@apelisse apelisse left a comment

Choose a reason for hiding this comment

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

Note that this is not going to work for server-side apply, but I don't think we can do it client-side since we don't see the object before. Alternately, especially since it's merely a warning, we could check that on the final/merged/returned object.

@@ -538,8 +540,13 @@ See http://k8s.io/docs/reference/using-api/api-concepts/#conflicts`, err)
return err
}

metadata, _ = meta.Accessor(info.Object)
Copy link
Member

Choose a reason for hiding this comment

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

I'm assuming you moved the "Accessor" above (L411) so that you would be able to access it here? so I don't think that is needed, is it?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Actually both were required because it didnt receive the delete timestamp yet on 411. But change is still unncessary so reverting change to that Line

@@ -74,6 +74,8 @@ var (

# Diff file read from stdin
cat service.yaml | kubectl diff -f -`))

warningChangesOnDeletingResource = "Warning: Detected changes to resource %[1]s which is currently being deleted.\n"
Copy link
Member

Choose a reason for hiding this comment

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

It makes me a little sad that we're duplicating this string, is there a good place to share it?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

is apply fine? I could just export this by capitalizing
Diff anyway imports apply too

@@ -548,6 +550,13 @@ func (o *DiffOptions) Run() error {
break
}
}

metadata, _ := meta.Accessor(info.Object)
if metadata != nil && metadata.GetDeletionTimestamp() != nil {
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 rather check for the error than for empty metadata.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

When you diff a resource which hasnt yet applied i think then this throws error.

@SaiHarshaK
Copy link
Contributor Author

/retest

1 similar comment
@SaiHarshaK
Copy link
Contributor Author

/retest

@SaiHarshaK SaiHarshaK requested a review from apelisse October 15, 2020 08:32
@apelisse
Copy link
Member

What do you think about doing it on the post-merged resource?

@SaiHarshaK
Copy link
Contributor Author

SaiHarshaK commented Oct 15, 2020

What do you think about doing it on the post-merged resource?

as in warning when there are multiple apply running parallely?

@apelisse
Copy link
Member

No, my problem is that your current solution doesn't work for server-side apply because we don't get the resource before sending the patch. Also, you have a race-condition if the deletionTimestamp is added between the get and the actual patch is applied. Alternately, you could look at the resource that gets back from the patch, and see if it contains the deletion timestamp.

@SaiHarshaK
Copy link
Contributor Author

For the 1st part does this get the object from server?
As for the 2nd part, yes i think i should do it after the patch is done, ill update that

@apelisse
Copy link
Member

For the 1st part does this get the object from server?

SSA happens right before and isn't included: https://github.com/kubernetes/kubernetes/blob/52630540538c7c0949f50abe4fe960c7b17398c0/staging/src/k8s.io/kubectl/pkg/cmd/apply/apply.go#L434-L491

@SaiHarshaK
Copy link
Contributor Author

SaiHarshaK commented Oct 15, 2020

For the 1st part does this get the object from server?

SSA happens right before and isn't included:

https://github.com/kubernetes/kubernetes/blob/52630540538c7c0949f50abe4fe960c7b17398c0/staging/src/k8s.io/kubectl/pkg/cmd/apply/apply.go#L434-L491

Oh right, should i insert the warning block here too or some function is preferred?

@apelisse
Copy link
Member

Oh right, should i insert the warning block here too or some function is preferred?

yeah, that sounds good. And for client-side apply, I would apply it here:
https://github.com/kubernetes/kubernetes/blob/52630540538c7c0949f50abe4fe960c7b17398c0/staging/src/k8s.io/kubectl/pkg/cmd/apply/apply.go#L564

Copy link
Member

@apelisse apelisse left a comment

Choose a reason for hiding this comment

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

That looks good, I think it's fine now. I'd maybe make a function for the error:

func WarnIfDeleting(obj runtime.Object, stderr io.Write) {
   ...
}

Then you wouldn't have to export the warning string, and you could re-use the same logic throughout.

@@ -133,6 +133,8 @@ var (
kubectl apply --prune -f manifest.yaml --all --prune-whitelist=core/v1/ConfigMap`))

warningNoLastAppliedConfigAnnotation = "Warning: resource %[1]s is missing the %[2]s annotation which is required by %[3]s apply. %[3]s apply should only be used on resources created declaratively by either %[3]s create --save-config or %[3]s apply. The missing annotation will be patched automatically.\n"
// WarningChangesOnDeletingResource is for warning about the resource currently being deleted on which a kubectl operation is run.
Copy link
Member

Choose a reason for hiding this comment

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

That comment is not useful. If it was, then it would probably mean that the message needs to be improved ;-)

@apelisse
Copy link
Member

/assign @seans3

@apelisse
Copy link
Member

Looks great to me, thanks!
/approve

I'll let @seans3 lgtm.

@k8s-ci-robot k8s-ci-robot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Oct 16, 2020
@SaiHarshaK
Copy link
Contributor Author

/retest

Copy link
Contributor

@soltysh soltysh left a comment

Choose a reason for hiding this comment

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

/lgtm

@soltysh
Copy link
Contributor

soltysh commented Nov 5, 2020

/priority backlog
/triage accepted

@k8s-ci-robot k8s-ci-robot added lgtm "Looks good to me", indicates that a PR is ready to be merged. priority/backlog Higher priority than priority/awaiting-more-evidence. triage/accepted Indicates an issue or PR is ready to be actively worked on. and removed 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. labels Nov 5, 2020
@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: apelisse, SaiHarshaK, soltysh

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

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/kubectl cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. kind/feature Categorizes issue or PR as related to a new feature. lgtm "Looks good to me", indicates that a PR is ready to be merged. ok-to-test Indicates a non-member PR verified by an org member that is safe to test. priority/backlog Higher priority than priority/awaiting-more-evidence. release-note Denotes a PR that will be considered when it comes time to generate release notes. sig/cli Categorizes an issue or PR as relevant to SIG CLI. size/S Denotes a PR that changes 10-29 lines, ignoring generated files. triage/accepted Indicates an issue or PR is ready to be actively worked on.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants