2019-01-12 04:58:27 +00:00
/ *
Copyright 2018 The Kubernetes Authors .
Licensed under the Apache License , Version 2.0 ( the "License" ) ;
you may not use this file except in compliance with the License .
You may obtain a copy of the License at
http : //www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing , software
distributed under the License is distributed on an "AS IS" BASIS ,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND , either express or implied .
See the License for the specific language governing permissions and
limitations under the License .
* /
package delete
import (
2020-12-01 01:06:26 +00:00
"fmt"
"strconv"
2019-01-12 04:58:27 +00:00
"time"
"github.com/spf13/cobra"
2020-12-01 01:06:26 +00:00
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2019-01-12 04:58:27 +00:00
"k8s.io/cli-runtime/pkg/genericclioptions"
"k8s.io/client-go/dynamic"
)
2019-04-07 17:07:55 +00:00
// DeleteFlags composes common printer flag structs
2019-01-12 04:58:27 +00:00
// used for commands requiring deletion logic.
type DeleteFlags struct {
FileNameFlags * genericclioptions . FileNameFlags
LabelSelector * string
FieldSelector * string
2020-12-01 01:06:26 +00:00
All * bool
AllNamespaces * bool
CascadingStrategy * string
Force * bool
GracePeriod * int
IgnoreNotFound * bool
Now * bool
Timeout * time . Duration
Wait * bool
Output * string
Raw * string
2019-01-12 04:58:27 +00:00
}
2020-12-01 01:06:26 +00:00
func ( f * DeleteFlags ) ToOptions ( dynamicClient dynamic . Interface , streams genericclioptions . IOStreams ) ( * DeleteOptions , error ) {
2019-01-12 04:58:27 +00:00
options := & DeleteOptions {
DynamicClient : dynamicClient ,
IOStreams : streams ,
}
// add filename options
if f . FileNameFlags != nil {
options . FilenameOptions = f . FileNameFlags . ToOptions ( )
}
if f . LabelSelector != nil {
options . LabelSelector = * f . LabelSelector
}
if f . FieldSelector != nil {
options . FieldSelector = * f . FieldSelector
}
// add output format
if f . Output != nil {
options . Output = * f . Output
}
if f . All != nil {
options . DeleteAll = * f . All
}
2019-04-07 17:07:55 +00:00
if f . AllNamespaces != nil {
options . DeleteAllNamespaces = * f . AllNamespaces
}
2020-12-01 01:06:26 +00:00
if f . CascadingStrategy != nil {
var err error
options . CascadingStrategy , err = parseCascadingFlag ( streams , * f . CascadingStrategy )
if err != nil {
return nil , err
}
2019-01-12 04:58:27 +00:00
}
if f . Force != nil {
options . ForceDeletion = * f . Force
}
if f . GracePeriod != nil {
options . GracePeriod = * f . GracePeriod
}
if f . IgnoreNotFound != nil {
options . IgnoreNotFound = * f . IgnoreNotFound
}
if f . Now != nil {
options . DeleteNow = * f . Now
}
if f . Timeout != nil {
options . Timeout = * f . Timeout
}
if f . Wait != nil {
options . WaitForDeletion = * f . Wait
}
2019-09-27 21:51:53 +00:00
if f . Raw != nil {
options . Raw = * f . Raw
}
2019-01-12 04:58:27 +00:00
2020-12-01 01:06:26 +00:00
return options , nil
2019-01-12 04:58:27 +00:00
}
func ( f * DeleteFlags ) AddFlags ( cmd * cobra . Command ) {
f . FileNameFlags . AddFlags ( cmd . Flags ( ) )
if f . LabelSelector != nil {
cmd . Flags ( ) . StringVarP ( f . LabelSelector , "selector" , "l" , * f . LabelSelector , "Selector (label query) to filter on, not including uninitialized ones." )
}
if f . FieldSelector != nil {
cmd . Flags ( ) . StringVarP ( f . FieldSelector , "field-selector" , "" , * f . FieldSelector , "Selector (field query) to filter on, supports '=', '==', and '!='.(e.g. --field-selector key1=value1,key2=value2). The server only supports a limited number of field queries per type." )
}
if f . All != nil {
cmd . Flags ( ) . BoolVar ( f . All , "all" , * f . All , "Delete all resources, including uninitialized ones, in the namespace of the specified resource types." )
}
2019-04-07 17:07:55 +00:00
if f . AllNamespaces != nil {
cmd . Flags ( ) . BoolVarP ( f . AllNamespaces , "all-namespaces" , "A" , * f . AllNamespaces , "If present, list the requested object(s) across all namespaces. Namespace in current context is ignored even if specified with --namespace." )
}
2019-01-12 04:58:27 +00:00
if f . Force != nil {
2020-03-26 21:07:15 +00:00
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." )
2019-01-12 04:58:27 +00:00
}
2020-12-01 01:06:26 +00:00
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"
2019-01-12 04:58:27 +00:00
}
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)." )
}
if f . GracePeriod != nil {
cmd . Flags ( ) . IntVar ( f . GracePeriod , "grace-period" , * f . GracePeriod , "Period of time in seconds given to the resource to terminate gracefully. Ignored if negative. Set to 1 for immediate shutdown. Can only be set to 0 when --force is true (force deletion)." )
}
if f . Timeout != nil {
cmd . Flags ( ) . DurationVar ( f . Timeout , "timeout" , * f . Timeout , "The length of time to wait before giving up on a delete, zero means determine a timeout from the size of the object" )
}
if f . IgnoreNotFound != nil {
cmd . Flags ( ) . BoolVar ( f . IgnoreNotFound , "ignore-not-found" , * f . IgnoreNotFound , "Treat \"resource not found\" as a successful delete. Defaults to \"true\" when --all is specified." )
}
if f . Wait != nil {
cmd . Flags ( ) . BoolVar ( f . Wait , "wait" , * f . Wait , "If true, wait for resources to be gone before returning. This waits for finalizers." )
}
if f . Output != nil {
cmd . Flags ( ) . StringVarP ( f . Output , "output" , "o" , * f . Output , "Output mode. Use \"-o name\" for shorter output (resource/name)." )
}
2019-09-27 21:51:53 +00:00
if f . Raw != nil {
cmd . Flags ( ) . StringVar ( f . Raw , "raw" , * f . Raw , "Raw URI to DELETE to the server. Uses the transport specified by the kubeconfig file." )
}
2019-01-12 04:58:27 +00:00
}
// NewDeleteCommandFlags provides default flags and values for use with the "delete" command
func NewDeleteCommandFlags ( usage string ) * DeleteFlags {
2020-12-01 01:06:26 +00:00
cascadingStrategy := "background"
2019-01-12 04:58:27 +00:00
gracePeriod := - 1
// setup command defaults
all := false
2019-04-07 17:07:55 +00:00
allNamespaces := false
2019-01-12 04:58:27 +00:00
force := false
ignoreNotFound := false
now := false
output := ""
labelSelector := ""
fieldSelector := ""
timeout := time . Duration ( 0 )
wait := true
2019-09-27 21:51:53 +00:00
raw := ""
2019-01-12 04:58:27 +00:00
filenames := [ ] string { }
recursive := false
2019-08-30 18:33:25 +00:00
kustomize := ""
2019-01-12 04:58:27 +00:00
return & DeleteFlags {
2019-04-07 17:07:55 +00:00
// Not using helpers.go since it provides function to add '-k' for FileNameOptions, but not FileNameFlags
2019-08-30 18:33:25 +00:00
FileNameFlags : & genericclioptions . FileNameFlags { Usage : usage , Filenames : & filenames , Kustomize : & kustomize , Recursive : & recursive } ,
2019-01-12 04:58:27 +00:00
LabelSelector : & labelSelector ,
FieldSelector : & fieldSelector ,
2020-12-01 01:06:26 +00:00
CascadingStrategy : & cascadingStrategy ,
GracePeriod : & gracePeriod ,
2019-01-12 04:58:27 +00:00
All : & all ,
2019-04-07 17:07:55 +00:00
AllNamespaces : & allNamespaces ,
2019-01-12 04:58:27 +00:00
Force : & force ,
IgnoreNotFound : & ignoreNotFound ,
Now : & now ,
Timeout : & timeout ,
Wait : & wait ,
Output : & output ,
2019-09-27 21:51:53 +00:00
Raw : & raw ,
2019-01-12 04:58:27 +00:00
}
}
// NewDeleteFlags provides default flags and values for use in commands outside of "delete"
func NewDeleteFlags ( usage string ) * DeleteFlags {
2020-12-01 01:06:26 +00:00
cascadingStrategy := "background"
2019-01-12 04:58:27 +00:00
gracePeriod := - 1
force := false
timeout := time . Duration ( 0 )
wait := false
filenames := [ ] string { }
2019-08-30 18:33:25 +00:00
kustomize := ""
2019-01-12 04:58:27 +00:00
recursive := false
return & DeleteFlags {
2019-08-30 18:33:25 +00:00
FileNameFlags : & genericclioptions . FileNameFlags { Usage : usage , Filenames : & filenames , Kustomize : & kustomize , Recursive : & recursive } ,
2019-01-12 04:58:27 +00:00
2020-12-01 01:06:26 +00:00
CascadingStrategy : & cascadingStrategy ,
GracePeriod : & gracePeriod ,
2019-01-12 04:58:27 +00:00
// add non-defaults
Force : & force ,
Timeout : & timeout ,
Wait : & wait ,
}
}
2020-12-01 01:06:26 +00:00
func parseCascadingFlag ( streams genericclioptions . IOStreams , cascadingFlag string ) ( metav1 . DeletionPropagation , error ) {
boolValue , 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 boolValue {
fmt . Fprintf ( streams . ErrOut , "warning: --cascade=%v is deprecated (boolean value) and can be replaced with --cascade=%s.\n" , cascadingFlag , "background" )
return metav1 . DeletePropagationBackground , nil
}
fmt . Fprintf ( streams . ErrOut , "warning: --cascade=%v is deprecated (boolean value) and can be replaced with --cascade=%s.\n" , cascadingFlag , "orphan" )
return metav1 . DeletePropagationOrphan , nil
}