Support kubectl delete foreground
Kubernetes-commit: 383b5f676670d99dc1fba4254ba7f6a81a052ba3
This commit is contained in:
parent
18af7c6ad0
commit
8411619cbf
|
|
@ -244,7 +244,11 @@ func (o *ApplyOptions) Complete(f cmdutil.Factory, cmd *cobra.Command) error {
|
|||
return err
|
||||
}
|
||||
|
||||
o.DeleteOptions = o.DeleteFlags.ToOptions(o.DynamicClient, o.IOStreams)
|
||||
o.DeleteOptions, err = o.DeleteFlags.ToOptions(o.DynamicClient, o.IOStreams)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = o.DeleteOptions.FilenameOptions.RequireFilenameOrKustomize()
|
||||
if err != nil {
|
||||
return err
|
||||
|
|
|
|||
|
|
@ -25,6 +25,7 @@ import (
|
|||
"github.com/jonboulle/clockwork"
|
||||
"k8s.io/apimachinery/pkg/api/errors"
|
||||
"k8s.io/apimachinery/pkg/api/meta"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
|
||||
"k8s.io/apimachinery/pkg/runtime"
|
||||
"k8s.io/apimachinery/pkg/types"
|
||||
|
|
@ -58,7 +59,7 @@ type Patcher struct {
|
|||
BackOff clockwork.Clock
|
||||
|
||||
Force bool
|
||||
Cascade bool
|
||||
CascadingStrategy metav1.DeletionPropagation
|
||||
Timeout time.Duration
|
||||
GracePeriod int
|
||||
|
||||
|
|
@ -83,7 +84,7 @@ func newPatcher(o *ApplyOptions, info *resource.Info, helper *resource.Helper) (
|
|||
Overwrite: o.Overwrite,
|
||||
BackOff: clockwork.NewRealClock(),
|
||||
Force: o.DeleteOptions.ForceDeletion,
|
||||
Cascade: o.DeleteOptions.Cascade,
|
||||
CascadingStrategy: o.DeleteOptions.CascadingStrategy,
|
||||
Timeout: o.DeleteOptions.Timeout,
|
||||
GracePeriod: o.DeleteOptions.GracePeriod,
|
||||
OpenapiSchema: openapiSchema,
|
||||
|
|
@ -92,7 +93,7 @@ func newPatcher(o *ApplyOptions, info *resource.Info, helper *resource.Helper) (
|
|||
}
|
||||
|
||||
func (p *Patcher) delete(namespace, name string) error {
|
||||
options := asDeleteOptions(p.Cascade, p.GracePeriod)
|
||||
options := asDeleteOptions(p.CascadingStrategy, p.GracePeriod)
|
||||
_, err := p.Helper.DeleteWithOptions(namespace, name, &options)
|
||||
return err
|
||||
}
|
||||
|
|
|
|||
|
|
@ -41,7 +41,7 @@ type pruner struct {
|
|||
labelSelector string
|
||||
fieldSelector string
|
||||
|
||||
cascade bool
|
||||
cascadingStrategy metav1.DeletionPropagation
|
||||
dryRunStrategy cmdutil.DryRunStrategy
|
||||
gracePeriod int
|
||||
|
||||
|
|
@ -59,7 +59,7 @@ func newPruner(o *ApplyOptions) pruner {
|
|||
visitedUids: o.VisitedUids,
|
||||
visitedNamespaces: o.VisitedNamespaces,
|
||||
|
||||
cascade: o.DeleteOptions.Cascade,
|
||||
cascadingStrategy: o.DeleteOptions.CascadingStrategy,
|
||||
dryRunStrategy: o.DryRunStrategy,
|
||||
gracePeriod: o.DeleteOptions.GracePeriod,
|
||||
|
||||
|
|
@ -139,27 +139,23 @@ func (p *pruner) prune(namespace string, mapping *meta.RESTMapping) error {
|
|||
}
|
||||
|
||||
func (p *pruner) delete(namespace, name string, mapping *meta.RESTMapping) error {
|
||||
return runDelete(namespace, name, mapping, p.dynamicClient, p.cascade, p.gracePeriod, p.dryRunStrategy == cmdutil.DryRunServer)
|
||||
return runDelete(namespace, name, mapping, p.dynamicClient, p.cascadingStrategy, p.gracePeriod, p.dryRunStrategy == cmdutil.DryRunServer)
|
||||
}
|
||||
|
||||
func runDelete(namespace, name string, mapping *meta.RESTMapping, c dynamic.Interface, cascade bool, gracePeriod int, serverDryRun bool) error {
|
||||
options := asDeleteOptions(cascade, gracePeriod)
|
||||
func runDelete(namespace, name string, mapping *meta.RESTMapping, c dynamic.Interface, cascadingStrategy metav1.DeletionPropagation, gracePeriod int, serverDryRun bool) error {
|
||||
options := asDeleteOptions(cascadingStrategy, gracePeriod)
|
||||
if serverDryRun {
|
||||
options.DryRun = []string{metav1.DryRunAll}
|
||||
}
|
||||
return c.Resource(mapping.Resource).Namespace(namespace).Delete(context.TODO(), name, options)
|
||||
}
|
||||
|
||||
func asDeleteOptions(cascade bool, gracePeriod int) metav1.DeleteOptions {
|
||||
func asDeleteOptions(cascadingStrategy metav1.DeletionPropagation, gracePeriod int) metav1.DeleteOptions {
|
||||
options := metav1.DeleteOptions{}
|
||||
if gracePeriod >= 0 {
|
||||
options = *metav1.NewDeleteOptions(int64(gracePeriod))
|
||||
}
|
||||
policy := metav1.DeletePropagationForeground
|
||||
if !cascade {
|
||||
policy = metav1.DeletePropagationOrphan
|
||||
}
|
||||
options.PropagationPolicy = &policy
|
||||
options.PropagationPolicy = &cascadingStrategy
|
||||
return options
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -102,8 +102,8 @@ type DeleteOptions struct {
|
|||
FieldSelector string
|
||||
DeleteAll bool
|
||||
DeleteAllNamespaces bool
|
||||
CascadingStrategy metav1.DeletionPropagation
|
||||
IgnoreNotFound bool
|
||||
Cascade bool
|
||||
DeleteNow bool
|
||||
ForceDeletion bool
|
||||
WaitForDeletion bool
|
||||
|
|
@ -136,7 +136,8 @@ func NewCmdDelete(f cmdutil.Factory, streams genericclioptions.IOStreams) *cobra
|
|||
Long: deleteLong,
|
||||
Example: deleteExample,
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
o := deleteFlags.ToOptions(nil, streams)
|
||||
o, err := deleteFlags.ToOptions(nil, streams)
|
||||
cmdutil.CheckErr(err)
|
||||
cmdutil.CheckErr(o.Complete(f, args, cmd))
|
||||
cmdutil.CheckErr(o.Validate())
|
||||
cmdutil.CheckErr(o.RunDelete(f))
|
||||
|
|
@ -301,11 +302,7 @@ func (o *DeleteOptions) DeleteResult(r *resource.Result) error {
|
|||
if o.GracePeriod >= 0 {
|
||||
options = metav1.NewDeleteOptions(int64(o.GracePeriod))
|
||||
}
|
||||
policy := metav1.DeletePropagationBackground
|
||||
if !o.Cascade {
|
||||
policy = metav1.DeletePropagationOrphan
|
||||
}
|
||||
options.PropagationPolicy = &policy
|
||||
options.PropagationPolicy = &o.CascadingStrategy
|
||||
|
||||
if warnClusterScope && info.Mapping.Scope.Name() == meta.RESTScopeNameRoot {
|
||||
fmt.Fprintf(o.ErrOut, "warning: deleting cluster-scoped resources, not scoped to the provided namespace\n")
|
||||
|
|
|
|||
|
|
@ -17,12 +17,16 @@ limitations under the License.
|
|||
package delete
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/cli-runtime/pkg/genericclioptions"
|
||||
"k8s.io/client-go/dynamic"
|
||||
"k8s.io/klog/v2"
|
||||
)
|
||||
|
||||
// DeleteFlags composes common printer flag structs
|
||||
|
|
@ -34,7 +38,7 @@ type DeleteFlags struct {
|
|||
|
||||
All *bool
|
||||
AllNamespaces *bool
|
||||
Cascade *bool
|
||||
CascadingStrategy *string
|
||||
Force *bool
|
||||
GracePeriod *int
|
||||
IgnoreNotFound *bool
|
||||
|
|
@ -45,7 +49,7 @@ type DeleteFlags struct {
|
|||
Raw *string
|
||||
}
|
||||
|
||||
func (f *DeleteFlags) ToOptions(dynamicClient dynamic.Interface, streams genericclioptions.IOStreams) *DeleteOptions {
|
||||
func (f *DeleteFlags) ToOptions(dynamicClient dynamic.Interface, streams genericclioptions.IOStreams) (*DeleteOptions, error) {
|
||||
options := &DeleteOptions{
|
||||
DynamicClient: dynamicClient,
|
||||
IOStreams: streams,
|
||||
|
|
@ -73,8 +77,12 @@ func (f *DeleteFlags) ToOptions(dynamicClient dynamic.Interface, streams generic
|
|||
if f.AllNamespaces != nil {
|
||||
options.DeleteAllNamespaces = *f.AllNamespaces
|
||||
}
|
||||
if f.Cascade != nil {
|
||||
options.Cascade = *f.Cascade
|
||||
if f.CascadingStrategy != nil {
|
||||
var err error
|
||||
options.CascadingStrategy, err = getCascadingStrategy(*f.CascadingStrategy)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
if f.Force != nil {
|
||||
options.ForceDeletion = *f.Force
|
||||
|
|
@ -98,7 +106,7 @@ func (f *DeleteFlags) ToOptions(dynamicClient dynamic.Interface, streams generic
|
|||
options.Raw = *f.Raw
|
||||
}
|
||||
|
||||
return options
|
||||
return options, nil
|
||||
}
|
||||
|
||||
func (f *DeleteFlags) AddFlags(cmd *cobra.Command) {
|
||||
|
|
@ -118,8 +126,13 @@ func (f *DeleteFlags) AddFlags(cmd *cobra.Command) {
|
|||
if f.Force != nil {
|
||||
cmd.Flags().BoolVar(f.Force, "force", *f.Force, "If true, immediately remove resources from API and bypass graceful deletion. Note that immediate deletion of some resources may result in inconsistency or data loss and requires confirmation.")
|
||||
}
|
||||
if f.Cascade != nil {
|
||||
cmd.Flags().BoolVar(f.Cascade, "cascade", *f.Cascade, "If true, run background cascade deletion of the resources managed by this resource (e.g. Pods created by a ReplicationController). Default true.")
|
||||
if f.CascadingStrategy != nil {
|
||||
cmd.Flags().StringVar(
|
||||
f.CascadingStrategy,
|
||||
"cascade",
|
||||
*f.CascadingStrategy,
|
||||
`Must be "background", "orphan", or "foreground". Selects the deletion cascading strategy for the dependents (e.g. Pods created by a ReplicationController). Defaults to background.`)
|
||||
cmd.Flags().Lookup("cascade").NoOptDefVal = "background"
|
||||
}
|
||||
if f.Now != nil {
|
||||
cmd.Flags().BoolVar(f.Now, "now", *f.Now, "If true, resources are signaled for immediate shutdown (same as --grace-period=1).")
|
||||
|
|
@ -146,7 +159,7 @@ func (f *DeleteFlags) AddFlags(cmd *cobra.Command) {
|
|||
|
||||
// NewDeleteCommandFlags provides default flags and values for use with the "delete" command
|
||||
func NewDeleteCommandFlags(usage string) *DeleteFlags {
|
||||
cascade := true
|
||||
cascadingStrategy := "background"
|
||||
gracePeriod := -1
|
||||
|
||||
// setup command defaults
|
||||
|
|
@ -172,7 +185,7 @@ func NewDeleteCommandFlags(usage string) *DeleteFlags {
|
|||
LabelSelector: &labelSelector,
|
||||
FieldSelector: &fieldSelector,
|
||||
|
||||
Cascade: &cascade,
|
||||
CascadingStrategy: &cascadingStrategy,
|
||||
GracePeriod: &gracePeriod,
|
||||
|
||||
All: &all,
|
||||
|
|
@ -189,7 +202,7 @@ func NewDeleteCommandFlags(usage string) *DeleteFlags {
|
|||
|
||||
// NewDeleteFlags provides default flags and values for use in commands outside of "delete"
|
||||
func NewDeleteFlags(usage string) *DeleteFlags {
|
||||
cascade := true
|
||||
cascadingStrategy := "background"
|
||||
gracePeriod := -1
|
||||
|
||||
force := false
|
||||
|
|
@ -203,7 +216,7 @@ func NewDeleteFlags(usage string) *DeleteFlags {
|
|||
return &DeleteFlags{
|
||||
FileNameFlags: &genericclioptions.FileNameFlags{Usage: usage, Filenames: &filenames, Kustomize: &kustomize, Recursive: &recursive},
|
||||
|
||||
Cascade: &cascade,
|
||||
CascadingStrategy: &cascadingStrategy,
|
||||
GracePeriod: &gracePeriod,
|
||||
|
||||
// add non-defaults
|
||||
|
|
@ -212,3 +225,27 @@ func NewDeleteFlags(usage string) *DeleteFlags {
|
|||
Wait: &wait,
|
||||
}
|
||||
}
|
||||
|
||||
func getCascadingStrategy(cascadingFlag string) (metav1.DeletionPropagation, error) {
|
||||
b, err := strconv.ParseBool(cascadingFlag)
|
||||
// The flag is not a boolean
|
||||
if err != nil {
|
||||
switch cascadingFlag {
|
||||
case "orphan":
|
||||
return metav1.DeletePropagationOrphan, nil
|
||||
case "foreground":
|
||||
return metav1.DeletePropagationForeground, nil
|
||||
case "background":
|
||||
return metav1.DeletePropagationBackground, nil
|
||||
default:
|
||||
return metav1.DeletePropagationBackground, fmt.Errorf(`Invalid cascade value (%v). Must be "background", "foreground", or "orphan".`, cascadingFlag)
|
||||
}
|
||||
}
|
||||
// The flag was a boolean
|
||||
if b {
|
||||
klog.Warningf(`--cascade=%v is deprecated (boolean value) and can be replaced with --cascade=%s.`, cascadingFlag, "background")
|
||||
return metav1.DeletePropagationBackground, nil
|
||||
}
|
||||
klog.Warningf(`--cascade=%v is deprecated (boolean value) and can be replaced with --cascade=%s.`, cascadingFlag, "orphan")
|
||||
return metav1.DeletePropagationOrphan, nil
|
||||
}
|
||||
|
|
|
|||
|
|
@ -112,8 +112,8 @@ func hasExpectedPropagationPolicy(body io.ReadCloser, policy *metav1.DeletionPro
|
|||
return *policy == *parsedBody.PropagationPolicy
|
||||
}
|
||||
|
||||
// Tests that DeleteOptions.OrphanDependents is appropriately set while deleting objects.
|
||||
func TestOrphanDependentsInDeleteObject(t *testing.T) {
|
||||
// TestCascadingStrategy tests that DeleteOptions.DeletionPropagation is appropriately set while deleting objects.
|
||||
func TestCascadingStrategy(t *testing.T) {
|
||||
cmdtesting.InitTestErrorHandler(t)
|
||||
_, _, rc := cmdtesting.TestData()
|
||||
|
||||
|
|
@ -137,7 +137,7 @@ func TestOrphanDependentsInDeleteObject(t *testing.T) {
|
|||
}),
|
||||
}
|
||||
|
||||
// DeleteOptions.PropagationPolicy should be Background, when cascade is true (default).
|
||||
// DeleteOptions.PropagationPolicy should be Background, when cascading strategy is empty (default).
|
||||
backgroundPolicy := metav1.DeletePropagationBackground
|
||||
policy = &backgroundPolicy
|
||||
streams, _, buf, _ := genericclioptions.NewTestIOStreams()
|
||||
|
|
@ -149,13 +149,26 @@ func TestOrphanDependentsInDeleteObject(t *testing.T) {
|
|||
t.Errorf("unexpected output: %s", buf.String())
|
||||
}
|
||||
|
||||
// Test that delete options should be set to orphan when cascade is false.
|
||||
// DeleteOptions.PropagationPolicy should be Foreground, when cascading strategy is foreground.
|
||||
foregroundPolicy := metav1.DeletePropagationForeground
|
||||
policy = &foregroundPolicy
|
||||
streams, _, buf, _ = genericclioptions.NewTestIOStreams()
|
||||
cmd = NewCmdDelete(tf, streams)
|
||||
cmd.Flags().Set("namespace", "test")
|
||||
cmd.Flags().Set("cascade", "foreground")
|
||||
cmd.Flags().Set("output", "name")
|
||||
cmd.Run(cmd, []string{"secrets/mysecret"})
|
||||
if buf.String() != "secret/mysecret\n" {
|
||||
t.Errorf("unexpected output: %s", buf.String())
|
||||
}
|
||||
|
||||
// Test that delete options should be set to orphan when cascading strategy is orphan.
|
||||
orphanPolicy := metav1.DeletePropagationOrphan
|
||||
policy = &orphanPolicy
|
||||
streams, _, buf, _ = genericclioptions.NewTestIOStreams()
|
||||
cmd = NewCmdDelete(tf, streams)
|
||||
cmd.Flags().Set("namespace", "test")
|
||||
cmd.Flags().Set("cascade", "false")
|
||||
cmd.Flags().Set("cascade", "orphan")
|
||||
cmd.Flags().Set("output", "name")
|
||||
cmd.Run(cmd, []string{"secrets/mysecret"})
|
||||
if buf.String() != "secret/mysecret\n" {
|
||||
|
|
@ -428,7 +441,7 @@ func TestDeleteObjectNotFound(t *testing.T) {
|
|||
Filenames: []string{"../../../testdata/redis-master-controller.yaml"},
|
||||
},
|
||||
GracePeriod: -1,
|
||||
Cascade: false,
|
||||
CascadingStrategy: metav1.DeletePropagationOrphan,
|
||||
Output: "name",
|
||||
IOStreams: genericclioptions.NewTestIOStreamsDiscard(),
|
||||
}
|
||||
|
|
@ -506,7 +519,7 @@ func TestDeleteAllNotFound(t *testing.T) {
|
|||
options := &DeleteOptions{
|
||||
FilenameOptions: resource.FilenameOptions{},
|
||||
GracePeriod: -1,
|
||||
Cascade: false,
|
||||
CascadingStrategy: metav1.DeletePropagationOrphan,
|
||||
DeleteAll: true,
|
||||
IgnoreNotFound: false,
|
||||
Output: "name",
|
||||
|
|
@ -631,7 +644,7 @@ func TestDeleteMultipleObjectContinueOnMissing(t *testing.T) {
|
|||
Filenames: []string{"../../../testdata/redis-master-controller.yaml", "../../../testdata/frontend-service.yaml"},
|
||||
},
|
||||
GracePeriod: -1,
|
||||
Cascade: false,
|
||||
CascadingStrategy: metav1.DeletePropagationOrphan,
|
||||
Output: "name",
|
||||
IOStreams: streams,
|
||||
}
|
||||
|
|
@ -803,7 +816,7 @@ func TestResourceErrors(t *testing.T) {
|
|||
options := &DeleteOptions{
|
||||
FilenameOptions: resource.FilenameOptions{},
|
||||
GracePeriod: -1,
|
||||
Cascade: false,
|
||||
CascadingStrategy: metav1.DeletePropagationOrphan,
|
||||
Output: "name",
|
||||
IOStreams: streams,
|
||||
}
|
||||
|
|
|
|||
|
|
@ -171,7 +171,10 @@ func (o *ReplaceOptions) Complete(f cmdutil.Factory, cmd *cobra.Command, args []
|
|||
return printer.PrintObj(obj, o.Out)
|
||||
}
|
||||
|
||||
deleteOpts := o.DeleteFlags.ToOptions(dynamicClient, o.IOStreams)
|
||||
deleteOpts, err := o.DeleteFlags.ToOptions(dynamicClient, o.IOStreams)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
//Replace will create a resource if it doesn't exist already, so ignore not found error
|
||||
deleteOpts.IgnoreNotFound = true
|
||||
|
|
|
|||
|
|
@ -246,7 +246,11 @@ func (o *RunOptions) Complete(f cmdutil.Factory, cmd *cobra.Command) error {
|
|||
return printer.PrintObj(obj, o.Out)
|
||||
}
|
||||
|
||||
deleteOpts := o.DeleteFlags.ToOptions(dynamicClient, o.IOStreams)
|
||||
deleteOpts, err := o.DeleteFlags.ToOptions(dynamicClient, o.IOStreams)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
deleteOpts.IgnoreNotFound = true
|
||||
deleteOpts.WaitForDeletion = false
|
||||
deleteOpts.GracePeriod = -1
|
||||
|
|
|
|||
|
|
@ -199,9 +199,14 @@ func TestRunArgsFollowDashRules(t *testing.T) {
|
|||
}
|
||||
|
||||
deleteFlags := delete.NewDeleteFlags("to use to replace the resource.")
|
||||
deleteOptions, err := deleteFlags.ToOptions(nil, genericclioptions.NewTestIOStreamsDiscard())
|
||||
if err != nil {
|
||||
t.Errorf("unexpected error: %v", err)
|
||||
return
|
||||
}
|
||||
opts := &RunOptions{
|
||||
PrintFlags: printFlags,
|
||||
DeleteOptions: deleteFlags.ToOptions(nil, genericclioptions.NewTestIOStreamsDiscard()),
|
||||
DeleteOptions: deleteOptions,
|
||||
|
||||
IOStreams: genericclioptions.NewTestIOStreamsDiscard(),
|
||||
|
||||
|
|
@ -371,9 +376,14 @@ func TestGenerateService(t *testing.T) {
|
|||
|
||||
ioStreams, _, buff, _ := genericclioptions.NewTestIOStreams()
|
||||
deleteFlags := delete.NewDeleteFlags("to use to replace the resource.")
|
||||
deleteOptions, err := deleteFlags.ToOptions(nil, genericclioptions.NewTestIOStreamsDiscard())
|
||||
if err != nil {
|
||||
t.Errorf("unexpected error: %v", err)
|
||||
return
|
||||
}
|
||||
opts := &RunOptions{
|
||||
PrintFlags: printFlags,
|
||||
DeleteOptions: deleteFlags.ToOptions(nil, genericclioptions.NewTestIOStreamsDiscard()),
|
||||
DeleteOptions: deleteOptions,
|
||||
|
||||
IOStreams: ioStreams,
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue