Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
K
k3s
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Jacklull
k3s
Commits
a3ff55e4
Commit
a3ff55e4
authored
Jan 03, 2015
by
Clayton Coleman
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Allow delete to work from directories, files, multiple resources
Simple refactoring reusing the resource builder changes.
parent
d24c5b14
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
242 additions
and
7 deletions
+242
-7
cmd_test.go
pkg/kubectl/cmd/cmd_test.go
+4
-0
delete.go
pkg/kubectl/cmd/delete.go
+17
-7
delete_test.go
pkg/kubectl/cmd/delete_test.go
+221
-0
No files found.
pkg/kubectl/cmd/cmd_test.go
View file @
a3ff55e4
...
...
@@ -153,3 +153,7 @@ func NewAPIFactory() (*Factory, *testFactory, runtime.Codec) {
func
objBody
(
codec
runtime
.
Codec
,
obj
runtime
.
Object
)
io
.
ReadCloser
{
return
ioutil
.
NopCloser
(
bytes
.
NewReader
([]
byte
(
runtime
.
EncodeOrDie
(
codec
,
obj
))))
}
func
stringBody
(
body
string
)
io
.
ReadCloser
{
return
ioutil
.
NopCloser
(
bytes
.
NewReader
([]
byte
(
body
)))
}
pkg/kubectl/cmd/delete.go
View file @
a3ff55e4
...
...
@@ -23,10 +23,15 @@ import (
"github.com/golang/glog"
"github.com/spf13/cobra"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/errors"
"github.com/GoogleCloudPlatform/kubernetes/pkg/kubectl/resource"
"github.com/GoogleCloudPlatform/kubernetes/pkg/util"
)
func
(
f
*
Factory
)
NewCmdDelete
(
out
io
.
Writer
)
*
cobra
.
Command
{
flags
:=
&
struct
{
Filenames
util
.
StringList
}{}
cmd
:=
&
cobra
.
Command
{
Use
:
"delete ([-f filename] | (<resource> [(<id> | -l <label>)]"
,
Short
:
"Delete a resource by filename, stdin or resource and id"
,
...
...
@@ -54,13 +59,18 @@ Examples:
$ kubectl delete pod 1234-56-7890-234234-456456
<delete a pod with ID 1234-56-7890-234234-456456>`
,
Run
:
func
(
cmd
*
cobra
.
Command
,
args
[]
string
)
{
filename
:=
GetFlagString
(
cmd
,
"filename"
)
schema
,
err
:=
f
.
Validator
(
cmd
)
checkErr
(
err
)
selector
:=
GetFlagString
(
cmd
,
"selector"
)
found
:=
0
mapper
,
typer
:=
f
.
Object
(
cmd
)
ResourcesFromArgsOrFile
(
cmd
,
args
,
filename
,
selector
,
typer
,
mapper
,
f
.
RESTClient
,
schema
,
true
)
.
Visit
(
func
(
r
*
resource
.
Info
)
error
{
r
:=
resource
.
NewBuilder
(
mapper
,
typer
,
ClientMapperForCommand
(
cmd
,
f
))
.
ContinueOnError
()
.
NamespaceParam
(
GetKubeNamespace
(
cmd
))
.
DefaultNamespace
()
.
FilenameParam
(
flags
.
Filenames
...
)
.
SelectorParam
(
GetFlagString
(
cmd
,
"selector"
))
.
ResourceTypeOrNameArgs
(
args
...
)
.
Flatten
()
.
Do
()
found
:=
0
r
.
IgnoreErrors
(
errors
.
IsNotFound
)
.
Visit
(
func
(
r
*
resource
.
Info
)
error
{
found
++
if
err
:=
resource
.
NewHelper
(
r
.
Client
,
r
.
Mapping
)
.
Delete
(
r
.
Namespace
,
r
.
Name
);
err
!=
nil
{
return
err
...
...
@@ -73,7 +83,7 @@ Examples:
}
},
}
cmd
.
Flags
()
.
StringP
(
"filename"
,
"f"
,
""
,
"Filename or URL to file to use to delete the resourc
e"
)
cmd
.
Flags
()
.
VarP
(
&
flags
.
Filenames
,
"filename"
,
"f"
,
"Filename, directory, or URL to a file containing the resource to delet
e"
)
cmd
.
Flags
()
.
StringP
(
"selector"
,
"l"
,
""
,
"Selector (label query) to filter on"
)
return
cmd
}
pkg/kubectl/cmd/delete_test.go
0 → 100644
View file @
a3ff55e4
/*
Copyright 2014 Google Inc. All rights reserved.
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
cmd_test
import
(
"bytes"
"net/http"
"strings"
"testing"
"github.com/GoogleCloudPlatform/kubernetes/pkg/client"
)
func
TestDeleteObject
(
t
*
testing
.
T
)
{
pods
,
_
:=
testData
()
f
,
tf
,
codec
:=
NewAPIFactory
()
tf
.
Printer
=
&
testPrinter
{}
tf
.
Client
=
&
client
.
FakeRESTClient
{
Codec
:
codec
,
Client
:
client
.
HTTPClientFunc
(
func
(
req
*
http
.
Request
)
(
*
http
.
Response
,
error
)
{
switch
p
,
m
:=
req
.
URL
.
Path
,
req
.
Method
;
{
case
p
==
"/ns/test/pods/redis-master"
&&
m
==
"DELETE"
:
return
&
http
.
Response
{
StatusCode
:
200
,
Body
:
objBody
(
codec
,
&
pods
.
Items
[
0
])},
nil
default
:
t
.
Fatalf
(
"unexpected request: %#v
\n
%#v"
,
req
.
URL
,
req
)
return
nil
,
nil
}
}),
}
buf
:=
bytes
.
NewBuffer
([]
byte
{})
cmd
:=
f
.
NewCmdDelete
(
buf
)
cmd
.
Flags
()
.
String
(
"namespace"
,
"test"
,
""
)
cmd
.
Flags
()
.
Set
(
"filename"
,
"../../../examples/guestbook/redis-master.json"
)
cmd
.
Run
(
cmd
,
[]
string
{})
// uses the name from the file, not the response
if
buf
.
String
()
!=
"redis-master
\n
"
{
t
.
Errorf
(
"unexpected output: %s"
,
buf
.
String
())
}
}
func
TestDeleteObjectIgnoreNotFound
(
t
*
testing
.
T
)
{
f
,
tf
,
codec
:=
NewAPIFactory
()
tf
.
Printer
=
&
testPrinter
{}
tf
.
Client
=
&
client
.
FakeRESTClient
{
Codec
:
codec
,
Client
:
client
.
HTTPClientFunc
(
func
(
req
*
http
.
Request
)
(
*
http
.
Response
,
error
)
{
switch
p
,
m
:=
req
.
URL
.
Path
,
req
.
Method
;
{
case
p
==
"/ns/test/pods/redis-master"
&&
m
==
"DELETE"
:
return
&
http
.
Response
{
StatusCode
:
404
,
Body
:
stringBody
(
""
)},
nil
default
:
t
.
Fatalf
(
"unexpected request: %#v
\n
%#v"
,
req
.
URL
,
req
)
return
nil
,
nil
}
}),
}
buf
:=
bytes
.
NewBuffer
([]
byte
{})
cmd
:=
f
.
NewCmdDelete
(
buf
)
cmd
.
Flags
()
.
String
(
"namespace"
,
"test"
,
""
)
cmd
.
Flags
()
.
Set
(
"filename"
,
"../../../examples/guestbook/redis-master.json"
)
cmd
.
Run
(
cmd
,
[]
string
{})
if
buf
.
String
()
!=
""
{
t
.
Errorf
(
"unexpected output: %s"
,
buf
.
String
())
}
}
func
TestDeleteMultipleObject
(
t
*
testing
.
T
)
{
pods
,
svc
:=
testData
()
f
,
tf
,
codec
:=
NewAPIFactory
()
tf
.
Printer
=
&
testPrinter
{}
tf
.
Client
=
&
client
.
FakeRESTClient
{
Codec
:
codec
,
Client
:
client
.
HTTPClientFunc
(
func
(
req
*
http
.
Request
)
(
*
http
.
Response
,
error
)
{
switch
p
,
m
:=
req
.
URL
.
Path
,
req
.
Method
;
{
case
p
==
"/ns/test/pods/redis-master"
&&
m
==
"DELETE"
:
return
&
http
.
Response
{
StatusCode
:
200
,
Body
:
objBody
(
codec
,
&
pods
.
Items
[
0
])},
nil
case
p
==
"/ns/test/services/frontend"
&&
m
==
"DELETE"
:
return
&
http
.
Response
{
StatusCode
:
200
,
Body
:
objBody
(
codec
,
&
svc
.
Items
[
0
])},
nil
default
:
t
.
Fatalf
(
"unexpected request: %#v
\n
%#v"
,
req
.
URL
,
req
)
return
nil
,
nil
}
}),
}
buf
:=
bytes
.
NewBuffer
([]
byte
{})
cmd
:=
f
.
NewCmdDelete
(
buf
)
cmd
.
Flags
()
.
String
(
"namespace"
,
"test"
,
""
)
cmd
.
Flags
()
.
Set
(
"filename"
,
"../../../examples/guestbook/redis-master.json"
)
cmd
.
Flags
()
.
Set
(
"filename"
,
"../../../examples/guestbook/frontend-service.json"
)
cmd
.
Run
(
cmd
,
[]
string
{})
if
buf
.
String
()
!=
"redis-master
\n
frontend
\n
"
{
t
.
Errorf
(
"unexpected output: %s"
,
buf
.
String
())
}
}
func
TestDeleteMultipleObjectIgnoreMissing
(
t
*
testing
.
T
)
{
_
,
svc
:=
testData
()
f
,
tf
,
codec
:=
NewAPIFactory
()
tf
.
Printer
=
&
testPrinter
{}
tf
.
Client
=
&
client
.
FakeRESTClient
{
Codec
:
codec
,
Client
:
client
.
HTTPClientFunc
(
func
(
req
*
http
.
Request
)
(
*
http
.
Response
,
error
)
{
switch
p
,
m
:=
req
.
URL
.
Path
,
req
.
Method
;
{
case
p
==
"/ns/test/pods/redis-master"
&&
m
==
"DELETE"
:
return
&
http
.
Response
{
StatusCode
:
404
,
Body
:
stringBody
(
""
)},
nil
case
p
==
"/ns/test/services/frontend"
&&
m
==
"DELETE"
:
return
&
http
.
Response
{
StatusCode
:
200
,
Body
:
objBody
(
codec
,
&
svc
.
Items
[
0
])},
nil
default
:
t
.
Fatalf
(
"unexpected request: %#v
\n
%#v"
,
req
.
URL
,
req
)
return
nil
,
nil
}
}),
}
buf
:=
bytes
.
NewBuffer
([]
byte
{})
cmd
:=
f
.
NewCmdDelete
(
buf
)
cmd
.
Flags
()
.
String
(
"namespace"
,
"test"
,
""
)
cmd
.
Flags
()
.
Set
(
"filename"
,
"../../../examples/guestbook/redis-master.json"
)
cmd
.
Flags
()
.
Set
(
"filename"
,
"../../../examples/guestbook/frontend-service.json"
)
cmd
.
Run
(
cmd
,
[]
string
{})
if
buf
.
String
()
!=
"frontend
\n
"
{
t
.
Errorf
(
"unexpected output: %s"
,
buf
.
String
())
}
}
func
TestDeleteDirectory
(
t
*
testing
.
T
)
{
pods
,
svc
:=
testData
()
f
,
tf
,
codec
:=
NewAPIFactory
()
tf
.
Printer
=
&
testPrinter
{}
tf
.
Client
=
&
client
.
FakeRESTClient
{
Codec
:
codec
,
Client
:
client
.
HTTPClientFunc
(
func
(
req
*
http
.
Request
)
(
*
http
.
Response
,
error
)
{
switch
p
,
m
:=
req
.
URL
.
Path
,
req
.
Method
;
{
case
strings
.
HasPrefix
(
p
,
"/ns/test/pods/"
)
&&
m
==
"DELETE"
:
return
&
http
.
Response
{
StatusCode
:
200
,
Body
:
objBody
(
codec
,
&
pods
.
Items
[
0
])},
nil
case
strings
.
HasPrefix
(
p
,
"/ns/test/services/"
)
&&
m
==
"DELETE"
:
return
&
http
.
Response
{
StatusCode
:
200
,
Body
:
objBody
(
codec
,
&
svc
.
Items
[
0
])},
nil
case
strings
.
HasPrefix
(
p
,
"/ns/test/replicationcontrollers/"
)
&&
m
==
"DELETE"
:
return
&
http
.
Response
{
StatusCode
:
200
,
Body
:
objBody
(
codec
,
&
svc
.
Items
[
0
])},
nil
default
:
t
.
Fatalf
(
"unexpected request: %#v
\n
%#v"
,
req
.
URL
,
req
)
return
nil
,
nil
}
}),
}
buf
:=
bytes
.
NewBuffer
([]
byte
{})
cmd
:=
f
.
NewCmdDelete
(
buf
)
cmd
.
Flags
()
.
String
(
"namespace"
,
"test"
,
""
)
cmd
.
Flags
()
.
Set
(
"filename"
,
"../../../examples/guestbook"
)
cmd
.
Run
(
cmd
,
[]
string
{})
if
buf
.
String
()
!=
"frontendController
\n
frontend
\n
redis-master
\n
redis-master
\n
redisSlaveController
\n
redisslave
\n
"
{
t
.
Errorf
(
"unexpected output: %s"
,
buf
.
String
())
}
}
func
TestDeleteMultipleSelector
(
t
*
testing
.
T
)
{
pods
,
svc
:=
testData
()
f
,
tf
,
codec
:=
NewAPIFactory
()
tf
.
Printer
=
&
testPrinter
{}
tf
.
Client
=
&
client
.
FakeRESTClient
{
Codec
:
codec
,
Client
:
client
.
HTTPClientFunc
(
func
(
req
*
http
.
Request
)
(
*
http
.
Response
,
error
)
{
switch
p
,
m
:=
req
.
URL
.
Path
,
req
.
Method
;
{
case
p
==
"/ns/test/pods"
&&
m
==
"GET"
:
if
req
.
URL
.
Query
()
.
Get
(
"labels"
)
!=
"a=b"
{
t
.
Fatalf
(
"unexpected request: %#v
\n
%#v"
,
req
.
URL
,
req
)
}
return
&
http
.
Response
{
StatusCode
:
200
,
Body
:
objBody
(
codec
,
pods
)},
nil
case
p
==
"/ns/test/services"
&&
m
==
"GET"
:
if
req
.
URL
.
Query
()
.
Get
(
"labels"
)
!=
"a=b"
{
t
.
Fatalf
(
"unexpected request: %#v
\n
%#v"
,
req
.
URL
,
req
)
}
return
&
http
.
Response
{
StatusCode
:
200
,
Body
:
objBody
(
codec
,
svc
)},
nil
case
strings
.
HasPrefix
(
p
,
"/ns/test/pods/"
)
&&
m
==
"DELETE"
:
return
&
http
.
Response
{
StatusCode
:
200
,
Body
:
objBody
(
codec
,
&
pods
.
Items
[
0
])},
nil
case
strings
.
HasPrefix
(
p
,
"/ns/test/services/"
)
&&
m
==
"DELETE"
:
return
&
http
.
Response
{
StatusCode
:
200
,
Body
:
objBody
(
codec
,
&
svc
.
Items
[
0
])},
nil
default
:
t
.
Fatalf
(
"unexpected request: %#v
\n
%#v"
,
req
.
URL
,
req
)
return
nil
,
nil
}
}),
}
buf
:=
bytes
.
NewBuffer
([]
byte
{})
cmd
:=
f
.
NewCmdDelete
(
buf
)
cmd
.
Flags
()
.
String
(
"namespace"
,
"test"
,
""
)
cmd
.
Flags
()
.
Set
(
"selector"
,
"a=b"
)
cmd
.
Run
(
cmd
,
[]
string
{
"pods,services"
})
if
buf
.
String
()
!=
"foo
\n
bar
\n
baz
\n
"
{
t
.
Errorf
(
"unexpected output: %s"
,
buf
.
String
())
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment