From 61b85bce7c24f6b7010a989d191f850362a4c8c7 Mon Sep 17 00:00:00 2001 From: Sai Harsha Kottapalli Date: Sun, 11 Oct 2020 02:21:32 +0530 Subject: [PATCH] warn user about resource being deleted Kubernetes-commit: 7b0ef888f3205f06c871cff901ef0f5b8fdb250c --- pkg/cmd/apply/apply.go | 15 +++++++++++++++ pkg/cmd/diff/diff.go | 3 +++ 2 files changed, 18 insertions(+) diff --git a/pkg/cmd/apply/apply.go b/pkg/cmd/apply/apply.go index f85247e39..2722af720 100644 --- a/pkg/cmd/apply/apply.go +++ b/pkg/cmd/apply/apply.go @@ -18,6 +18,7 @@ package apply import ( "fmt" + "io" "net/http" "github.com/spf13/cobra" @@ -133,6 +134,7 @@ 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 = "Warning: Detected changes to resource %[1]s which is currently being deleted.\n" ) // NewApplyOptions creates new ApplyOptions for the `apply` command @@ -469,6 +471,8 @@ See http://k8s.io/docs/reference/using-api/api-concepts/#conflicts`, err) info.Refresh(obj, true) + WarnIfDeleting(info.Object, o.ErrOut) + if err := o.MarkObjectVisited(info); err != nil { return err } @@ -556,6 +560,8 @@ See http://k8s.io/docs/reference/using-api/api-concepts/#conflicts`, err) info.Refresh(patchedObject, true) + WarnIfDeleting(info.Object, o.ErrOut) + if string(patchBytes) == "{}" && !o.shouldPrintObject() { printer, err := o.ToPrinter("unchanged") if err != nil { @@ -708,3 +714,12 @@ func GetApplyFieldManagerFlag(cmd *cobra.Command, serverSide bool) string { return FieldManagerClientSideApply } + +// WarnIfDeleting prints a warning if a resource is being deleted +func WarnIfDeleting(obj runtime.Object, stderr io.Writer) { + metadata, _ := meta.Accessor(obj) + if metadata != nil && metadata.GetDeletionTimestamp() != nil { + // just warn the user about the conflict + fmt.Fprintf(stderr, warningChangesOnDeletingResource, metadata.GetName()) + } +} diff --git a/pkg/cmd/diff/diff.go b/pkg/cmd/diff/diff.go index 75ee84b7d..749843506 100644 --- a/pkg/cmd/diff/diff.go +++ b/pkg/cmd/diff/diff.go @@ -564,6 +564,9 @@ func (o *DiffOptions) Run() error { break } } + + apply.WarnIfDeleting(info.Object, o.Diff.ErrOut) + return err }) if err != nil {