Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit eab1cec

Browse files
committed
Merge pull request #8628 from jlowdermilk/delete-not-found
Delete fails on notfound errors.
2 parents 78621f9 + 3cf4d9b commit eab1cec

File tree

9 files changed

+89
-31
lines changed

9 files changed

+89
-31
lines changed

contrib/completions/bash/kubectl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,7 @@ _kubectl_delete()
364364
flags+=("--grace-period=")
365365
flags+=("--help")
366366
flags+=("-h")
367+
flags+=("--ignore-not-found")
367368
flags+=("--selector=")
368369
two_word_flags+=("-l")
369370

@@ -587,6 +588,7 @@ _kubectl_stop()
587588
flags+=("--grace-period=")
588589
flags+=("--help")
589590
flags+=("-h")
591+
flags+=("--ignore-not-found")
590592
flags+=("--selector=")
591593
two_word_flags+=("-l")
592594

docs/kubectl_delete.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,13 @@ $ kubectl delete pods --all
4242
### Options
4343

4444
```
45-
--all=false: [-all] to select all the specified resources
45+
--all=false: [-all] to select all the specified resources.
4646
--cascade=true: If true, cascade the delete resources managed by this resource (e.g. Pods created by a ReplicationController). Default true.
47-
-f, --filename=[]: Filename, directory, or URL to a file containing the resource to delete
47+
-f, --filename=[]: Filename, directory, or URL to a file containing the resource to delete.
4848
--grace-period=-1: Period of time in seconds given to the resource to terminate gracefully. Ignored if negative.
4949
-h, --help=false: help for delete
50-
-l, --selector="": Selector (label query) to filter on
50+
--ignore-not-found=false: Treat "resource not found" as a successful delete.
51+
-l, --selector="": Selector (label query) to filter on.
5152
```
5253

5354
### Options inherited from parent commands
@@ -82,6 +83,6 @@ $ kubectl delete pods --all
8283
### SEE ALSO
8384
* [kubectl](kubectl.md) - kubectl controls the Kubernetes cluster manager
8485

85-
###### Auto generated by spf13/cobra at 2015-05-21 10:33:11.18056941 +0000 UTC
86+
###### Auto generated by spf13/cobra at 2015-05-21 18:30:45.437003409 +0000 UTC
8687

8788
[![Analytics](https://kubernetes-site.appspot.com/UA-36037335-10/GitHub/docs/kubectl_delete.md?pixel)]()

docs/kubectl_stop.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,12 @@ $ kubectl stop -f path/to/resources
3333
### Options
3434

3535
```
36-
--all=false: [-all] to select all the specified resources
37-
-f, --filename=[]: Filename, directory, or URL to file of resource(s) to be stopped
36+
--all=false: [-all] to select all the specified resources.
37+
-f, --filename=[]: Filename, directory, or URL to file of resource(s) to be stopped.
3838
--grace-period=-1: Period of time in seconds given to the resource to terminate gracefully. Ignored if negative.
3939
-h, --help=false: help for stop
40-
-l, --selector="": Selector (label query) to filter on
40+
--ignore-not-found=false: Treat "resource not found" as a successful stop.
41+
-l, --selector="": Selector (label query) to filter on.
4142
```
4243

4344
### Options inherited from parent commands
@@ -72,6 +73,6 @@ $ kubectl stop -f path/to/resources
7273
### SEE ALSO
7374
* [kubectl](kubectl.md) - kubectl controls the Kubernetes cluster manager
7475

75-
###### Auto generated by spf13/cobra at 2015-05-21 10:33:11.190996891 +0000 UTC
76+
###### Auto generated by spf13/cobra at 2015-05-21 18:30:45.439945376 +0000 UTC
7677

7778
[![Analytics](https://kubernetes-site.appspot.com/UA-36037335-10/GitHub/docs/kubectl_stop.md?pixel)]()

docs/man/man1/kubectl-delete.1

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,15 @@ will be lost along with the rest of the resource.
3131
.SH OPTIONS
3232
.PP
3333
\fB\-\-all\fP=false
34-
[\-all] to select all the specified resources
34+
[\-all] to select all the specified resources.
3535

3636
.PP
3737
\fB\-\-cascade\fP=true
3838
If true, cascade the delete resources managed by this resource (e.g. Pods created by a ReplicationController). Default true.
3939

4040
.PP
4141
\fB\-f\fP, \fB\-\-filename\fP=[]
42-
Filename, directory, or URL to a file containing the resource to delete
42+
Filename, directory, or URL to a file containing the resource to delete.
4343

4444
.PP
4545
\fB\-\-grace\-period\fP=\-1
@@ -49,9 +49,13 @@ will be lost along with the rest of the resource.
4949
\fB\-h\fP, \fB\-\-help\fP=false
5050
help for delete
5151

52+
.PP
53+
\fB\-\-ignore\-not\-found\fP=false
54+
Treat "resource not found" as a successful delete.
55+
5256
.PP
5357
\fB\-l\fP, \fB\-\-selector\fP=""
54-
Selector (label query) to filter on
58+
Selector (label query) to filter on.
5559

5660

5761
.SH OPTIONS INHERITED FROM PARENT COMMANDS

docs/man/man1/kubectl-stop.1

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@ If the resource is scalable it will be scaled to 0 before deletion.
2323
.SH OPTIONS
2424
.PP
2525
\fB\-\-all\fP=false
26-
[\-all] to select all the specified resources
26+
[\-all] to select all the specified resources.
2727

2828
.PP
2929
\fB\-f\fP, \fB\-\-filename\fP=[]
30-
Filename, directory, or URL to file of resource(s) to be stopped
30+
Filename, directory, or URL to file of resource(s) to be stopped.
3131

3232
.PP
3333
\fB\-\-grace\-period\fP=\-1
@@ -37,9 +37,13 @@ If the resource is scalable it will be scaled to 0 before deletion.
3737
\fB\-h\fP, \fB\-\-help\fP=false
3838
help for stop
3939

40+
.PP
41+
\fB\-\-ignore\-not\-found\fP=false
42+
Treat "resource not found" as a successful stop.
43+
4044
.PP
4145
\fB\-l\fP, \fB\-\-selector\fP=""
42-
Selector (label query) to filter on
46+
Selector (label query) to filter on.
4347

4448

4549
.SH OPTIONS INHERITED FROM PARENT COMMANDS

hooks/pre-commit

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ if ! hack/verify-gendocs.sh > /dev/null; then
9292
echo "${red}ERROR!"
9393
echo "Some docs are out of sync between CLI and markdown."
9494
echo "To regenerate docs, run:"
95-
echo " hack/run-gendocs.sh > docs/kubectl.md"
95+
echo " hack/run-gendocs.sh"
9696
exit_code=1
9797
else
9898
echo "${green}OK"

pkg/kubectl/cmd/delete.go

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,11 @@ func NewCmdDelete(f *cmdutil.Factory, out io.Writer) *cobra.Command {
6969
cmdutil.CheckErr(err)
7070
},
7171
}
72-
usage := "Filename, directory, or URL to a file containing the resource to delete"
72+
usage := "Filename, directory, or URL to a file containing the resource to delete."
7373
kubectl.AddJsonFilenameFlag(cmd, &filenames, usage)
74-
cmd.Flags().StringP("selector", "l", "", "Selector (label query) to filter on")
75-
cmd.Flags().Bool("all", false, "[-all] to select all the specified resources")
74+
cmd.Flags().StringP("selector", "l", "", "Selector (label query) to filter on.")
75+
cmd.Flags().Bool("all", false, "[-all] to select all the specified resources.")
76+
cmd.Flags().Bool("ignore-not-found", false, "Treat \"resource not found\" as a successful delete.")
7677
cmd.Flags().Bool("cascade", true, "If true, cascade the delete resources managed by this resource (e.g. Pods created by a ReplicationController). Default true.")
7778
cmd.Flags().Int("grace-period", -1, "Period of time in seconds given to the resource to terminate gracefully. Ignored if negative.")
7879
return cmd
@@ -98,20 +99,24 @@ func RunDelete(f *cmdutil.Factory, out io.Writer, cmd *cobra.Command, args []str
9899
return err
99100
}
100101

102+
ignoreNotFound := cmdutil.GetFlagBool(cmd, "ignore-not-found")
101103
// By default use a reaper to delete all related resources.
102104
if cmdutil.GetFlagBool(cmd, "cascade") {
103-
return ReapResult(r, f, out, cmdutil.GetFlagBool(cmd, "cascade"), cmdutil.GetFlagInt(cmd, "grace-period"))
105+
return ReapResult(r, f, out, cmdutil.GetFlagBool(cmd, "cascade"), ignoreNotFound, cmdutil.GetFlagInt(cmd, "grace-period"))
104106
}
105-
return DeleteResult(r, out)
107+
return DeleteResult(r, out, ignoreNotFound)
106108
}
107109

108-
func ReapResult(r *resource.Result, f *cmdutil.Factory, out io.Writer, isDefaultDelete bool, gracePeriod int) error {
110+
func ReapResult(r *resource.Result, f *cmdutil.Factory, out io.Writer, isDefaultDelete, ignoreNotFound bool, gracePeriod int) error {
109111
found := 0
110-
err := r.IgnoreErrors(errors.IsNotFound).Visit(func(info *resource.Info) error {
112+
if ignoreNotFound {
113+
r = r.IgnoreErrors(errors.IsNotFound)
114+
}
115+
err := r.Visit(func(info *resource.Info) error {
111116
found++
112117
reaper, err := f.Reaper(info.Mapping)
113118
if err != nil {
114-
// If the error is "not found" and the user didn't explicitly ask for stop.
119+
// If there is no reaper for this resources and the user didn't explicitly ask for stop.
115120
if kubectl.IsNoSuchReaperError(err) && isDefaultDelete {
116121
return deleteResource(info, out)
117122
}
@@ -136,9 +141,12 @@ func ReapResult(r *resource.Result, f *cmdutil.Factory, out io.Writer, isDefault
136141
return nil
137142
}
138143

139-
func DeleteResult(r *resource.Result, out io.Writer) error {
144+
func DeleteResult(r *resource.Result, out io.Writer, ignoreNotFound bool) error {
140145
found := 0
141-
err := r.IgnoreErrors(errors.IsNotFound).Visit(func(info *resource.Info) error {
146+
if ignoreNotFound {
147+
r = r.IgnoreErrors(errors.IsNotFound)
148+
}
149+
err := r.Visit(func(info *resource.Info) error {
142150
found++
143151
return deleteResource(info, out)
144152
})

pkg/kubectl/cmd/delete_test.go

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,16 @@ package cmd
1818

1919
import (
2020
"bytes"
21+
"fmt"
2122
"net/http"
2223
"strings"
2324
"testing"
2425

2526
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
27+
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/errors"
2628
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/testapi"
2729
"github.com/GoogleCloudPlatform/kubernetes/pkg/client"
30+
"github.com/GoogleCloudPlatform/kubernetes/pkg/util"
2831
)
2932

3033
func TestDeleteObjectByTuple(t *testing.T) {
@@ -120,6 +123,34 @@ func TestDeleteObject(t *testing.T) {
120123
}
121124
}
122125

126+
func TestDeleteObjectNotFound(t *testing.T) {
127+
f, tf, codec := NewAPIFactory()
128+
tf.Printer = &testPrinter{}
129+
tf.Client = &client.FakeRESTClient{
130+
Codec: codec,
131+
Client: client.HTTPClientFunc(func(req *http.Request) (*http.Response, error) {
132+
switch p, m := req.URL.Path, req.Method; {
133+
case p == "/namespaces/test/replicationcontrollers/redis-master" && m == "DELETE":
134+
return &http.Response{StatusCode: 404, Body: stringBody("")}, nil
135+
default:
136+
t.Fatalf("unexpected request: %#v\n%#v", req.URL, req)
137+
return nil, nil
138+
}
139+
}),
140+
}
141+
tf.Namespace = "test"
142+
buf := bytes.NewBuffer([]byte{})
143+
144+
cmd := NewCmdDelete(f, buf)
145+
cmd.Flags().Set("filename", "../../../examples/guestbook/redis-master-controller.json")
146+
cmd.Flags().Set("cascade", "false")
147+
filenames := cmd.Flags().Lookup("filename").Value.(*util.StringList)
148+
err := RunDelete(f, buf, cmd, []string{}, *filenames)
149+
if err == nil || !errors.IsNotFound(err) {
150+
t.Errorf("unexpected error: expected NotFound, got %v", err)
151+
}
152+
}
153+
123154
func TestDeleteObjectIgnoreNotFound(t *testing.T) {
124155
f, tf, codec := NewAPIFactory()
125156
tf.Printer = &testPrinter{}
@@ -141,6 +172,7 @@ func TestDeleteObjectIgnoreNotFound(t *testing.T) {
141172
cmd := NewCmdDelete(f, buf)
142173
cmd.Flags().Set("filename", "../../../examples/guestbook/redis-master-controller.json")
143174
cmd.Flags().Set("cascade", "false")
175+
cmd.Flags().Set("ignore-not-found", "true")
144176
cmd.Run(cmd, []string{})
145177

146178
if buf.String() != "" {
@@ -181,7 +213,7 @@ func TestDeleteMultipleObject(t *testing.T) {
181213
}
182214
}
183215

184-
func TestDeleteMultipleObjectIgnoreMissing(t *testing.T) {
216+
func TestDeleteMultipleObjectContinueOnMissing(t *testing.T) {
185217
_, svc, _ := testData()
186218

187219
f, tf, codec := NewAPIFactory()
@@ -207,7 +239,12 @@ func TestDeleteMultipleObjectIgnoreMissing(t *testing.T) {
207239
cmd.Flags().Set("filename", "../../../examples/guestbook/redis-master-controller.json")
208240
cmd.Flags().Set("filename", "../../../examples/guestbook/frontend-service.json")
209241
cmd.Flags().Set("cascade", "false")
210-
cmd.Run(cmd, []string{})
242+
filenames := cmd.Flags().Lookup("filename").Value.(*util.StringList)
243+
fmt.Printf("filenames: %v\n", filenames)
244+
err := RunDelete(f, buf, cmd, []string{}, *filenames)
245+
if err == nil || !errors.IsNotFound(err) {
246+
t.Errorf("unexpected error: expected NotFound, got %v", err)
247+
}
211248

212249
if buf.String() != "services/frontend\n" {
213250
t.Errorf("unexpected output: %s", buf.String())

pkg/kubectl/cmd/stop.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,11 @@ func NewCmdStop(f *cmdutil.Factory, out io.Writer) *cobra.Command {
5757
cmdutil.CheckErr(RunStop(f, cmd, args, flags.Filenames, out))
5858
},
5959
}
60-
usage := "Filename, directory, or URL to file of resource(s) to be stopped"
60+
usage := "Filename, directory, or URL to file of resource(s) to be stopped."
6161
kubectl.AddJsonFilenameFlag(cmd, &flags.Filenames, usage)
62-
cmd.Flags().StringP("selector", "l", "", "Selector (label query) to filter on")
63-
cmd.Flags().Bool("all", false, "[-all] to select all the specified resources")
62+
cmd.Flags().StringP("selector", "l", "", "Selector (label query) to filter on.")
63+
cmd.Flags().Bool("all", false, "[-all] to select all the specified resources.")
64+
cmd.Flags().Bool("ignore-not-found", false, "Treat \"resource not found\" as a successful stop.")
6465
cmd.Flags().Int("grace-period", -1, "Period of time in seconds given to the resource to terminate gracefully. Ignored if negative.")
6566
return cmd
6667
}
@@ -83,5 +84,5 @@ func RunStop(f *cmdutil.Factory, cmd *cobra.Command, args []string, filenames ut
8384
if r.Err() != nil {
8485
return r.Err()
8586
}
86-
return ReapResult(r, f, out, false, cmdutil.GetFlagInt(cmd, "grace-period"))
87+
return ReapResult(r, f, out, false, cmdutil.GetFlagBool(cmd, "ignore-not-found"), cmdutil.GetFlagInt(cmd, "grace-period"))
8788
}

0 commit comments

Comments
 (0)