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
d46ae5d8
Commit
d46ae5d8
authored
Feb 04, 2015
by
Jeff Lowdermilk
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Refactor kubectl/cmd helpers into pkg/kubectl/cmd/util
Allows helpers to be used by config commands.
parent
afeaf8fc
Show whitespace changes
Inline
Side-by-side
Showing
20 changed files
with
179 additions
and
185 deletions
+179
-185
cmd.go
pkg/kubectl/cmd/cmd.go
+58
-8
cmd_test.go
pkg/kubectl/cmd/cmd_test.go
+41
-0
create.go
pkg/kubectl/cmd/create.go
+1
-1
delete.go
pkg/kubectl/cmd/delete.go
+3
-2
describe.go
pkg/kubectl/cmd/describe.go
+2
-1
expose.go
pkg/kubectl/cmd/expose.go
+12
-10
get.go
pkg/kubectl/cmd/get.go
+10
-9
log.go
pkg/kubectl/cmd/log.go
+2
-1
printing_test.go
pkg/kubectl/cmd/printing_test.go
+0
-67
proxy.go
pkg/kubectl/cmd/proxy.go
+4
-3
resize.go
pkg/kubectl/cmd/resize.go
+5
-4
rollingupdate.go
pkg/kubectl/cmd/rollingupdate.go
+7
-6
run.go
pkg/kubectl/cmd/run.go
+7
-6
stop.go
pkg/kubectl/cmd/stop.go
+2
-1
update.go
pkg/kubectl/cmd/update.go
+5
-4
helpers.go
pkg/kubectl/cmd/util/helpers.go
+13
-1
helpers_test.go
pkg/kubectl/cmd/util/helpers_test.go
+1
-1
printing.go
pkg/kubectl/cmd/util/printing.go
+3
-58
resource.go
pkg/kubectl/cmd/util/resource.go
+1
-1
version.go
pkg/kubectl/cmd/version.go
+2
-1
No files found.
pkg/kubectl/cmd/cmd.go
View file @
d46ae5d8
...
@@ -29,6 +29,7 @@ import (
...
@@ -29,6 +29,7 @@ import (
"github.com/GoogleCloudPlatform/kubernetes/pkg/client/clientcmd"
"github.com/GoogleCloudPlatform/kubernetes/pkg/client/clientcmd"
"github.com/GoogleCloudPlatform/kubernetes/pkg/kubectl"
"github.com/GoogleCloudPlatform/kubernetes/pkg/kubectl"
cmdconfig
"github.com/GoogleCloudPlatform/kubernetes/pkg/kubectl/cmd/config"
cmdconfig
"github.com/GoogleCloudPlatform/kubernetes/pkg/kubectl/cmd/config"
cmdutil
"github.com/GoogleCloudPlatform/kubernetes/pkg/kubectl/cmd/util"
"github.com/GoogleCloudPlatform/kubernetes/pkg/kubectl/resource"
"github.com/GoogleCloudPlatform/kubernetes/pkg/kubectl/resource"
"github.com/GoogleCloudPlatform/kubernetes/pkg/runtime"
"github.com/GoogleCloudPlatform/kubernetes/pkg/runtime"
"github.com/GoogleCloudPlatform/kubernetes/pkg/util"
"github.com/GoogleCloudPlatform/kubernetes/pkg/util"
...
@@ -143,7 +144,7 @@ func NewFactory(optionalClientConfig clientcmd.ClientConfig) *Factory {
...
@@ -143,7 +144,7 @@ func NewFactory(optionalClientConfig clientcmd.ClientConfig) *Factory {
return
kubectl
.
ReaperFor
(
mapping
.
Kind
,
client
)
return
kubectl
.
ReaperFor
(
mapping
.
Kind
,
client
)
},
},
Validator
:
func
(
cmd
*
cobra
.
Command
)
(
validation
.
Schema
,
error
)
{
Validator
:
func
(
cmd
*
cobra
.
Command
)
(
validation
.
Schema
,
error
)
{
if
GetFlagBool
(
cmd
,
"validate"
)
{
if
cmdutil
.
GetFlagBool
(
cmd
,
"validate"
)
{
client
,
err
:=
clients
.
ClientForVersion
(
""
)
client
,
err
:=
clients
.
ClientForVersion
(
""
)
if
err
!=
nil
{
if
err
!=
nil
{
return
nil
,
err
return
nil
,
err
...
@@ -217,6 +218,62 @@ Find more information at https://github.com/GoogleCloudPlatform/kubernetes.`,
...
@@ -217,6 +218,62 @@ Find more information at https://github.com/GoogleCloudPlatform/kubernetes.`,
return
cmds
return
cmds
}
}
// PrintObject prints an api object given command line flags to modify the output format
func
(
f
*
Factory
)
PrintObject
(
cmd
*
cobra
.
Command
,
obj
runtime
.
Object
,
out
io
.
Writer
)
error
{
mapper
,
_
:=
f
.
Object
(
cmd
)
_
,
kind
,
err
:=
api
.
Scheme
.
ObjectVersionAndKind
(
obj
)
if
err
!=
nil
{
return
err
}
mapping
,
err
:=
mapper
.
RESTMapping
(
kind
)
if
err
!=
nil
{
return
err
}
printer
,
err
:=
f
.
PrinterForMapping
(
cmd
,
mapping
)
if
err
!=
nil
{
return
err
}
return
printer
.
PrintObj
(
obj
,
out
)
}
// PrinterForMapping returns a printer suitable for displaying the provided resource type.
// Requires that printer flags have been added to cmd (see AddPrinterFlags).
func
(
f
*
Factory
)
PrinterForMapping
(
cmd
*
cobra
.
Command
,
mapping
*
meta
.
RESTMapping
)
(
kubectl
.
ResourcePrinter
,
error
)
{
printer
,
ok
,
err
:=
cmdutil
.
PrinterForCommand
(
cmd
)
if
err
!=
nil
{
return
nil
,
err
}
if
ok
{
clientConfig
,
err
:=
f
.
ClientConfig
(
cmd
)
checkErr
(
err
)
defaultVersion
:=
clientConfig
.
Version
version
:=
cmdutil
.
OutputVersion
(
cmd
,
defaultVersion
)
if
len
(
version
)
==
0
{
version
=
mapping
.
APIVersion
}
if
len
(
version
)
==
0
{
return
nil
,
fmt
.
Errorf
(
"you must specify an output-version when using this output format"
)
}
printer
=
kubectl
.
NewVersionedPrinter
(
printer
,
mapping
.
ObjectConvertor
,
version
)
}
else
{
printer
,
err
=
f
.
Printer
(
cmd
,
mapping
,
cmdutil
.
GetFlagBool
(
cmd
,
"no-headers"
))
if
err
!=
nil
{
return
nil
,
err
}
}
return
printer
,
nil
}
// ClientMapperForCommand returns a ClientMapper for the given command and factory.
func
(
f
*
Factory
)
ClientMapperForCommand
(
cmd
*
cobra
.
Command
)
resource
.
ClientMapper
{
return
resource
.
ClientMapperFunc
(
func
(
mapping
*
meta
.
RESTMapping
)
(
resource
.
RESTClient
,
error
)
{
return
f
.
RESTClient
(
cmd
,
mapping
)
})
}
// DefaultClientConfig creates a clientcmd.ClientConfig with the following hierarchy:
// DefaultClientConfig creates a clientcmd.ClientConfig with the following hierarchy:
// 1. Use the kubeconfig builder. The number of merges and overrides here gets a little crazy. Stay with me.
// 1. Use the kubeconfig builder. The number of merges and overrides here gets a little crazy. Stay with me.
// 1. Merge together the kubeconfig itself. This is done with the following hierarchy and merge rules:
// 1. Merge together the kubeconfig itself. This is done with the following hierarchy and merge rules:
...
@@ -266,13 +323,6 @@ func DefaultClientConfig(flags *pflag.FlagSet) clientcmd.ClientConfig {
...
@@ -266,13 +323,6 @@ func DefaultClientConfig(flags *pflag.FlagSet) clientcmd.ClientConfig {
return
clientConfig
return
clientConfig
}
}
// ClientMapperForCommand returns a ClientMapper for the given command and factory.
func
ClientMapperForCommand
(
cmd
*
cobra
.
Command
,
f
*
Factory
)
resource
.
ClientMapper
{
return
resource
.
ClientMapperFunc
(
func
(
mapping
*
meta
.
RESTMapping
)
(
resource
.
RESTClient
,
error
)
{
return
f
.
RESTClient
(
cmd
,
mapping
)
})
}
func
checkErr
(
err
error
)
{
func
checkErr
(
err
error
)
{
if
err
!=
nil
{
if
err
!=
nil
{
glog
.
FatalDepth
(
1
,
err
)
glog
.
FatalDepth
(
1
,
err
)
...
...
pkg/kubectl/cmd/cmd_test.go
View file @
d46ae5d8
...
@@ -21,6 +21,7 @@ import (
...
@@ -21,6 +21,7 @@ import (
"fmt"
"fmt"
"io"
"io"
"io/ioutil"
"io/ioutil"
"os"
"testing"
"testing"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
...
@@ -203,3 +204,43 @@ func TestClientVersions(t *testing.T) {
...
@@ -203,3 +204,43 @@ func TestClientVersions(t *testing.T) {
}
}
}
}
}
}
func
ExamplePrintReplicationController
()
{
f
,
tf
,
codec
:=
NewAPIFactory
()
tf
.
Printer
=
kubectl
.
NewHumanReadablePrinter
(
false
)
tf
.
Client
=
&
client
.
FakeRESTClient
{
Codec
:
codec
,
Client
:
nil
,
}
cmd
:=
f
.
NewCmdRunContainer
(
os
.
Stdout
)
ctrl
:=
&
api
.
ReplicationController
{
ObjectMeta
:
api
.
ObjectMeta
{
Name
:
"foo"
,
Labels
:
map
[
string
]
string
{
"foo"
:
"bar"
},
},
Spec
:
api
.
ReplicationControllerSpec
{
Replicas
:
1
,
Selector
:
map
[
string
]
string
{
"foo"
:
"bar"
},
Template
:
&
api
.
PodTemplateSpec
{
ObjectMeta
:
api
.
ObjectMeta
{
Labels
:
map
[
string
]
string
{
"foo"
:
"bar"
},
},
Spec
:
api
.
PodSpec
{
Containers
:
[]
api
.
Container
{
{
Name
:
"foo"
,
Image
:
"someimage"
,
},
},
},
},
},
}
err
:=
f
.
PrintObject
(
cmd
,
ctrl
,
os
.
Stdout
)
if
err
!=
nil
{
fmt
.
Printf
(
"Unexpected error: %v"
,
err
)
}
// Output:
// CONTROLLER CONTAINER(S) IMAGE(S) SELECTOR REPLICAS
// foo foo someimage foo=bar 1
}
pkg/kubectl/cmd/create.go
View file @
d46ae5d8
...
@@ -51,7 +51,7 @@ Examples:
...
@@ -51,7 +51,7 @@ Examples:
checkErr
(
err
)
checkErr
(
err
)
mapper
,
typer
:=
f
.
Object
(
cmd
)
mapper
,
typer
:=
f
.
Object
(
cmd
)
r
:=
resource
.
NewBuilder
(
mapper
,
typer
,
ClientMapperForCommand
(
cmd
,
f
))
.
r
:=
resource
.
NewBuilder
(
mapper
,
typer
,
f
.
ClientMapperForCommand
(
cmd
))
.
ContinueOnError
()
.
ContinueOnError
()
.
NamespaceParam
(
cmdNamespace
)
.
RequireNamespace
()
.
NamespaceParam
(
cmdNamespace
)
.
RequireNamespace
()
.
FilenameParam
(
flags
.
Filenames
...
)
.
FilenameParam
(
flags
.
Filenames
...
)
.
...
...
pkg/kubectl/cmd/delete.go
View file @
d46ae5d8
...
@@ -23,6 +23,7 @@ import (
...
@@ -23,6 +23,7 @@ import (
"github.com/spf13/cobra"
"github.com/spf13/cobra"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/errors"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/errors"
cmdutil
"github.com/GoogleCloudPlatform/kubernetes/pkg/kubectl/cmd/util"
"github.com/GoogleCloudPlatform/kubernetes/pkg/kubectl/resource"
"github.com/GoogleCloudPlatform/kubernetes/pkg/kubectl/resource"
"github.com/GoogleCloudPlatform/kubernetes/pkg/util"
"github.com/GoogleCloudPlatform/kubernetes/pkg/util"
)
)
...
@@ -62,11 +63,11 @@ Examples:
...
@@ -62,11 +63,11 @@ Examples:
checkErr
(
err
)
checkErr
(
err
)
mapper
,
typer
:=
f
.
Object
(
cmd
)
mapper
,
typer
:=
f
.
Object
(
cmd
)
r
:=
resource
.
NewBuilder
(
mapper
,
typer
,
ClientMapperForCommand
(
cmd
,
f
))
.
r
:=
resource
.
NewBuilder
(
mapper
,
typer
,
f
.
ClientMapperForCommand
(
cmd
))
.
ContinueOnError
()
.
ContinueOnError
()
.
NamespaceParam
(
cmdNamespace
)
.
DefaultNamespace
()
.
NamespaceParam
(
cmdNamespace
)
.
DefaultNamespace
()
.
FilenameParam
(
flags
.
Filenames
...
)
.
FilenameParam
(
flags
.
Filenames
...
)
.
SelectorParam
(
GetFlagString
(
cmd
,
"selector"
))
.
SelectorParam
(
cmdutil
.
GetFlagString
(
cmd
,
"selector"
))
.
ResourceTypeOrNameArgs
(
args
...
)
.
ResourceTypeOrNameArgs
(
args
...
)
.
Flatten
()
.
Flatten
()
.
Do
()
Do
()
...
...
pkg/kubectl/cmd/describe.go
View file @
d46ae5d8
...
@@ -20,6 +20,7 @@ import (
...
@@ -20,6 +20,7 @@ import (
"fmt"
"fmt"
"io"
"io"
"github.com/GoogleCloudPlatform/kubernetes/pkg/kubectl/cmd/util"
"github.com/spf13/cobra"
"github.com/spf13/cobra"
)
)
...
@@ -36,7 +37,7 @@ given resource.`,
...
@@ -36,7 +37,7 @@ given resource.`,
checkErr
(
err
)
checkErr
(
err
)
mapper
,
_
:=
f
.
Object
(
cmd
)
mapper
,
_
:=
f
.
Object
(
cmd
)
mapping
,
namespace
,
name
:=
ResourceFromArgs
(
cmd
,
args
,
mapper
,
cmdNamespace
)
mapping
,
namespace
,
name
:=
util
.
ResourceFromArgs
(
cmd
,
args
,
mapper
,
cmdNamespace
)
describer
,
err
:=
f
.
Describer
(
cmd
,
mapping
)
describer
,
err
:=
f
.
Describer
(
cmd
,
mapping
)
checkErr
(
err
)
checkErr
(
err
)
...
...
pkg/kubectl/cmd/expose.go
View file @
d46ae5d8
...
@@ -22,6 +22,7 @@ import (
...
@@ -22,6 +22,7 @@ import (
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
"github.com/GoogleCloudPlatform/kubernetes/pkg/kubectl"
"github.com/GoogleCloudPlatform/kubernetes/pkg/kubectl"
"github.com/GoogleCloudPlatform/kubernetes/pkg/kubectl/cmd/util"
"github.com/spf13/cobra"
"github.com/spf13/cobra"
)
)
...
@@ -51,27 +52,28 @@ $ kubectl expose streamer --port=4100 --protocol=udp --service-name=video-stream
...
@@ -51,27 +52,28 @@ $ kubectl expose streamer --port=4100 --protocol=udp --service-name=video-stream
client
,
err
:=
f
.
Client
(
cmd
)
client
,
err
:=
f
.
Client
(
cmd
)
checkErr
(
err
)
checkErr
(
err
)
generatorName
:=
GetFlagString
(
cmd
,
"generator"
)
generatorName
:=
util
.
GetFlagString
(
cmd
,
"generator"
)
generator
,
found
:=
kubectl
.
Generators
[
generatorName
]
generator
,
found
:=
kubectl
.
Generators
[
generatorName
]
if
!
found
{
if
!
found
{
usageError
(
cmd
,
fmt
.
Sprintf
(
"Generator: %s not found."
,
generator
))
usageError
(
cmd
,
fmt
.
Sprintf
(
"Generator: %s not found."
,
generator
))
}
}
if
GetFlagInt
(
cmd
,
"port"
)
<
1
{
if
util
.
GetFlagInt
(
cmd
,
"port"
)
<
1
{
usageError
(
cmd
,
"--port is required and must be a positive integer."
)
usageError
(
cmd
,
"--port is required and must be a positive integer."
)
}
}
names
:=
generator
.
ParamNames
()
names
:=
generator
.
ParamNames
()
params
:=
kubectl
.
MakeParams
(
cmd
,
names
)
params
:=
kubectl
.
MakeParams
(
cmd
,
names
)
if
len
(
GetFlagString
(
cmd
,
"service-name"
))
==
0
{
if
len
(
util
.
GetFlagString
(
cmd
,
"service-name"
))
==
0
{
params
[
"name"
]
=
args
[
0
]
params
[
"name"
]
=
args
[
0
]
}
else
{
}
else
{
params
[
"name"
]
=
GetFlagString
(
cmd
,
"service-name"
)
params
[
"name"
]
=
util
.
GetFlagString
(
cmd
,
"service-name"
)
}
}
if
_
,
found
:=
params
[
"selector"
];
!
found
{
if
_
,
found
:=
params
[
"selector"
];
!
found
{
rc
,
err
:=
client
.
ReplicationControllers
(
namespace
)
.
Get
(
args
[
0
])
rc
,
err
:=
client
.
ReplicationControllers
(
namespace
)
.
Get
(
args
[
0
])
checkErr
(
err
)
checkErr
(
err
)
params
[
"selector"
]
=
kubectl
.
MakeLabels
(
rc
.
Spec
.
Selector
)
params
[
"selector"
]
=
kubectl
.
MakeLabels
(
rc
.
Spec
.
Selector
)
}
}
if
GetFlagBool
(
cmd
,
"create-external-load-balancer"
)
{
if
util
.
GetFlagBool
(
cmd
,
"create-external-load-balancer"
)
{
params
[
"create-external-load-balancer"
]
=
"true"
params
[
"create-external-load-balancer"
]
=
"true"
}
}
...
@@ -81,22 +83,22 @@ $ kubectl expose streamer --port=4100 --protocol=udp --service-name=video-stream
...
@@ -81,22 +83,22 @@ $ kubectl expose streamer --port=4100 --protocol=udp --service-name=video-stream
service
,
err
:=
generator
.
Generate
(
params
)
service
,
err
:=
generator
.
Generate
(
params
)
checkErr
(
err
)
checkErr
(
err
)
inline
:=
GetFlagString
(
cmd
,
"overrides"
)
inline
:=
util
.
GetFlagString
(
cmd
,
"overrides"
)
if
len
(
inline
)
>
0
{
if
len
(
inline
)
>
0
{
Merge
(
service
,
inline
,
"Service"
)
util
.
Merge
(
service
,
inline
,
"Service"
)
}
}
// TODO: extract this flag to a central location, when such a location exists.
// TODO: extract this flag to a central location, when such a location exists.
if
!
GetFlagBool
(
cmd
,
"dry-run"
)
{
if
!
util
.
GetFlagBool
(
cmd
,
"dry-run"
)
{
service
,
err
=
client
.
Services
(
namespace
)
.
Create
(
service
.
(
*
api
.
Service
))
service
,
err
=
client
.
Services
(
namespace
)
.
Create
(
service
.
(
*
api
.
Service
))
checkErr
(
err
)
checkErr
(
err
)
}
}
err
=
PrintObject
(
cmd
,
service
,
f
,
out
)
err
=
f
.
PrintObject
(
cmd
,
service
,
out
)
checkErr
(
err
)
checkErr
(
err
)
},
},
}
}
AddPrinterFlags
(
cmd
)
util
.
AddPrinterFlags
(
cmd
)
cmd
.
Flags
()
.
String
(
"generator"
,
"service/v1"
,
"The name of the api generator that you want to use. Default 'service/v1'"
)
cmd
.
Flags
()
.
String
(
"generator"
,
"service/v1"
,
"The name of the api generator that you want to use. Default 'service/v1'"
)
cmd
.
Flags
()
.
String
(
"protocol"
,
"TCP"
,
"The network protocol for the service you want to be created. Default 'tcp'"
)
cmd
.
Flags
()
.
String
(
"protocol"
,
"TCP"
,
"The network protocol for the service you want to be created. Default 'tcp'"
)
cmd
.
Flags
()
.
Int
(
"port"
,
-
1
,
"The port that the service should serve on. Required."
)
cmd
.
Flags
()
.
Int
(
"port"
,
-
1
,
"The port that the service should serve on. Required."
)
...
...
pkg/kubectl/cmd/get.go
View file @
d46ae5d8
...
@@ -23,6 +23,7 @@ import (
...
@@ -23,6 +23,7 @@ import (
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/latest"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/latest"
"github.com/GoogleCloudPlatform/kubernetes/pkg/kubectl"
"github.com/GoogleCloudPlatform/kubernetes/pkg/kubectl"
"github.com/GoogleCloudPlatform/kubernetes/pkg/kubectl/cmd/util"
"github.com/GoogleCloudPlatform/kubernetes/pkg/kubectl/resource"
"github.com/GoogleCloudPlatform/kubernetes/pkg/kubectl/resource"
"github.com/GoogleCloudPlatform/kubernetes/pkg/watch"
"github.com/GoogleCloudPlatform/kubernetes/pkg/watch"
...
@@ -59,7 +60,7 @@ Examples:
...
@@ -59,7 +60,7 @@ Examples:
RunGet
(
f
,
out
,
cmd
,
args
)
RunGet
(
f
,
out
,
cmd
,
args
)
},
},
}
}
AddPrinterFlags
(
cmd
)
util
.
AddPrinterFlags
(
cmd
)
cmd
.
Flags
()
.
StringP
(
"selector"
,
"l"
,
""
,
"Selector (label query) to filter on"
)
cmd
.
Flags
()
.
StringP
(
"selector"
,
"l"
,
""
,
"Selector (label query) to filter on"
)
cmd
.
Flags
()
.
BoolP
(
"watch"
,
"w"
,
false
,
"After listing/getting the requested object, watch for changes."
)
cmd
.
Flags
()
.
BoolP
(
"watch"
,
"w"
,
false
,
"After listing/getting the requested object, watch for changes."
)
cmd
.
Flags
()
.
Bool
(
"watch-only"
,
false
,
"Watch for changes to the requested object(s), without listing/getting first."
)
cmd
.
Flags
()
.
Bool
(
"watch-only"
,
false
,
"Watch for changes to the requested object(s), without listing/getting first."
)
...
@@ -70,16 +71,16 @@ Examples:
...
@@ -70,16 +71,16 @@ Examples:
// TODO: convert all direct flag accessors to a struct and pass that instead of cmd
// TODO: convert all direct flag accessors to a struct and pass that instead of cmd
// TODO: return an error instead of using glog.Fatal and checkErr
// TODO: return an error instead of using glog.Fatal and checkErr
func
RunGet
(
f
*
Factory
,
out
io
.
Writer
,
cmd
*
cobra
.
Command
,
args
[]
string
)
{
func
RunGet
(
f
*
Factory
,
out
io
.
Writer
,
cmd
*
cobra
.
Command
,
args
[]
string
)
{
selector
:=
GetFlagString
(
cmd
,
"selector"
)
selector
:=
util
.
GetFlagString
(
cmd
,
"selector"
)
mapper
,
typer
:=
f
.
Object
(
cmd
)
mapper
,
typer
:=
f
.
Object
(
cmd
)
cmdNamespace
,
err
:=
f
.
DefaultNamespace
(
cmd
)
cmdNamespace
,
err
:=
f
.
DefaultNamespace
(
cmd
)
checkErr
(
err
)
checkErr
(
err
)
// handle watch separately since we cannot watch multiple resource types
// handle watch separately since we cannot watch multiple resource types
isWatch
,
isWatchOnly
:=
GetFlagBool
(
cmd
,
"watch"
),
GetFlagBool
(
cmd
,
"watch-only"
)
isWatch
,
isWatchOnly
:=
util
.
GetFlagBool
(
cmd
,
"watch"
),
util
.
GetFlagBool
(
cmd
,
"watch-only"
)
if
isWatch
||
isWatchOnly
{
if
isWatch
||
isWatchOnly
{
r
:=
resource
.
NewBuilder
(
mapper
,
typer
,
ClientMapperForCommand
(
cmd
,
f
))
.
r
:=
resource
.
NewBuilder
(
mapper
,
typer
,
f
.
ClientMapperForCommand
(
cmd
))
.
NamespaceParam
(
cmdNamespace
)
.
DefaultNamespace
()
.
NamespaceParam
(
cmdNamespace
)
.
DefaultNamespace
()
.
SelectorParam
(
selector
)
.
SelectorParam
(
selector
)
.
ResourceTypeOrNameArgs
(
args
...
)
.
ResourceTypeOrNameArgs
(
args
...
)
.
...
@@ -89,7 +90,7 @@ func RunGet(f *Factory, out io.Writer, cmd *cobra.Command, args []string) {
...
@@ -89,7 +90,7 @@ func RunGet(f *Factory, out io.Writer, cmd *cobra.Command, args []string) {
mapping
,
err
:=
r
.
ResourceMapping
()
mapping
,
err
:=
r
.
ResourceMapping
()
checkErr
(
err
)
checkErr
(
err
)
printer
,
err
:=
PrinterForMapping
(
f
,
cmd
,
mapping
)
printer
,
err
:=
f
.
PrinterForMapping
(
cmd
,
mapping
)
checkErr
(
err
)
checkErr
(
err
)
obj
,
err
:=
r
.
Object
()
obj
,
err
:=
r
.
Object
()
...
@@ -115,12 +116,12 @@ func RunGet(f *Factory, out io.Writer, cmd *cobra.Command, args []string) {
...
@@ -115,12 +116,12 @@ func RunGet(f *Factory, out io.Writer, cmd *cobra.Command, args []string) {
return
return
}
}
b
:=
resource
.
NewBuilder
(
mapper
,
typer
,
ClientMapperForCommand
(
cmd
,
f
))
.
b
:=
resource
.
NewBuilder
(
mapper
,
typer
,
f
.
ClientMapperForCommand
(
cmd
))
.
NamespaceParam
(
cmdNamespace
)
.
DefaultNamespace
()
.
NamespaceParam
(
cmdNamespace
)
.
DefaultNamespace
()
.
SelectorParam
(
selector
)
.
SelectorParam
(
selector
)
.
ResourceTypeOrNameArgs
(
args
...
)
.
ResourceTypeOrNameArgs
(
args
...
)
.
Latest
()
Latest
()
printer
,
generic
,
err
:=
PrinterForCommand
(
cmd
)
printer
,
generic
,
err
:=
util
.
PrinterForCommand
(
cmd
)
checkErr
(
err
)
checkErr
(
err
)
if
generic
{
if
generic
{
...
@@ -129,7 +130,7 @@ func RunGet(f *Factory, out io.Writer, cmd *cobra.Command, args []string) {
...
@@ -129,7 +130,7 @@ func RunGet(f *Factory, out io.Writer, cmd *cobra.Command, args []string) {
defaultVersion
:=
clientConfig
.
Version
defaultVersion
:=
clientConfig
.
Version
// the outermost object will be converted to the output-version
// the outermost object will be converted to the output-version
version
:=
o
utputVersion
(
cmd
,
defaultVersion
)
version
:=
util
.
O
utputVersion
(
cmd
,
defaultVersion
)
if
len
(
version
)
==
0
{
if
len
(
version
)
==
0
{
// TODO: add a new ResourceBuilder mode for Object() that attempts to ensure the objects
// TODO: add a new ResourceBuilder mode for Object() that attempts to ensure the objects
// are in the appropriate version if one exists (and if not, use the best effort).
// are in the appropriate version if one exists (and if not, use the best effort).
...
@@ -149,7 +150,7 @@ func RunGet(f *Factory, out io.Writer, cmd *cobra.Command, args []string) {
...
@@ -149,7 +150,7 @@ func RunGet(f *Factory, out io.Writer, cmd *cobra.Command, args []string) {
// use the default printer for each object
// use the default printer for each object
err
=
b
.
Do
()
.
Visit
(
func
(
r
*
resource
.
Info
)
error
{
err
=
b
.
Do
()
.
Visit
(
func
(
r
*
resource
.
Info
)
error
{
printer
,
err
:=
PrinterForMapping
(
f
,
cmd
,
r
.
Mapping
)
printer
,
err
:=
f
.
PrinterForMapping
(
cmd
,
r
.
Mapping
)
if
err
!=
nil
{
if
err
!=
nil
{
return
err
return
err
}
}
...
...
pkg/kubectl/cmd/log.go
View file @
d46ae5d8
...
@@ -20,6 +20,7 @@ import (
...
@@ -20,6 +20,7 @@ import (
"io"
"io"
"strconv"
"strconv"
"github.com/GoogleCloudPlatform/kubernetes/pkg/kubectl/cmd/util"
"github.com/spf13/cobra"
"github.com/spf13/cobra"
)
)
...
@@ -66,7 +67,7 @@ Examples:
...
@@ -66,7 +67,7 @@ Examples:
}
}
follow
:=
false
follow
:=
false
if
GetFlagBool
(
cmd
,
"follow"
)
{
if
util
.
GetFlagBool
(
cmd
,
"follow"
)
{
follow
=
true
follow
=
true
}
}
...
...
pkg/kubectl/cmd/printing_test.go
deleted
100644 → 0
View file @
afeaf8fc
/*
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
(
"fmt"
"os"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
"github.com/GoogleCloudPlatform/kubernetes/pkg/client"
"github.com/GoogleCloudPlatform/kubernetes/pkg/kubectl"
.
"github.com/GoogleCloudPlatform/kubernetes/pkg/kubectl/cmd"
)
func
ExamplePrintReplicationController
()
{
f
,
tf
,
codec
:=
NewAPIFactory
()
tf
.
Printer
=
kubectl
.
NewHumanReadablePrinter
(
false
)
tf
.
Client
=
&
client
.
FakeRESTClient
{
Codec
:
codec
,
Client
:
nil
,
}
cmd
:=
f
.
NewCmdRunContainer
(
os
.
Stdout
)
ctrl
:=
&
api
.
ReplicationController
{
ObjectMeta
:
api
.
ObjectMeta
{
Name
:
"foo"
,
Labels
:
map
[
string
]
string
{
"foo"
:
"bar"
},
},
Spec
:
api
.
ReplicationControllerSpec
{
Replicas
:
1
,
Selector
:
map
[
string
]
string
{
"foo"
:
"bar"
},
Template
:
&
api
.
PodTemplateSpec
{
ObjectMeta
:
api
.
ObjectMeta
{
Labels
:
map
[
string
]
string
{
"foo"
:
"bar"
},
},
Spec
:
api
.
PodSpec
{
Containers
:
[]
api
.
Container
{
{
Name
:
"foo"
,
Image
:
"someimage"
,
},
},
},
},
},
}
err
:=
PrintObject
(
cmd
,
ctrl
,
f
,
os
.
Stdout
)
if
err
!=
nil
{
fmt
.
Printf
(
"Unexpected error: %v"
,
err
)
}
// Output:
// CONTROLLER CONTAINER(S) IMAGE(S) SELECTOR REPLICAS
// foo foo someimage foo=bar 1
}
pkg/kubectl/cmd/proxy.go
View file @
d46ae5d8
...
@@ -21,6 +21,7 @@ import (
...
@@ -21,6 +21,7 @@ import (
"strings"
"strings"
"github.com/GoogleCloudPlatform/kubernetes/pkg/kubectl"
"github.com/GoogleCloudPlatform/kubernetes/pkg/kubectl"
"github.com/GoogleCloudPlatform/kubernetes/pkg/kubectl/cmd/util"
"github.com/golang/glog"
"github.com/golang/glog"
"github.com/spf13/cobra"
"github.com/spf13/cobra"
)
)
...
@@ -31,17 +32,17 @@ func (f *Factory) NewCmdProxy(out io.Writer) *cobra.Command {
...
@@ -31,17 +32,17 @@ func (f *Factory) NewCmdProxy(out io.Writer) *cobra.Command {
Short
:
"Run a proxy to the Kubernetes API server"
,
Short
:
"Run a proxy to the Kubernetes API server"
,
Long
:
`Run a proxy to the Kubernetes API server.`
,
Long
:
`Run a proxy to the Kubernetes API server.`
,
Run
:
func
(
cmd
*
cobra
.
Command
,
args
[]
string
)
{
Run
:
func
(
cmd
*
cobra
.
Command
,
args
[]
string
)
{
port
:=
GetFlagInt
(
cmd
,
"port"
)
port
:=
util
.
GetFlagInt
(
cmd
,
"port"
)
glog
.
Infof
(
"Starting to serve on localhost:%d"
,
port
)
glog
.
Infof
(
"Starting to serve on localhost:%d"
,
port
)
clientConfig
,
err
:=
f
.
ClientConfig
(
cmd
)
clientConfig
,
err
:=
f
.
ClientConfig
(
cmd
)
checkErr
(
err
)
checkErr
(
err
)
staticPrefix
:=
GetFlagString
(
cmd
,
"www-prefix"
)
staticPrefix
:=
util
.
GetFlagString
(
cmd
,
"www-prefix"
)
if
!
strings
.
HasSuffix
(
staticPrefix
,
"/"
)
{
if
!
strings
.
HasSuffix
(
staticPrefix
,
"/"
)
{
staticPrefix
+=
"/"
staticPrefix
+=
"/"
}
}
server
,
err
:=
kubectl
.
NewProxyServer
(
GetFlagString
(
cmd
,
"www"
),
staticPrefix
,
clientConfig
)
server
,
err
:=
kubectl
.
NewProxyServer
(
util
.
GetFlagString
(
cmd
,
"www"
),
staticPrefix
,
clientConfig
)
checkErr
(
err
)
checkErr
(
err
)
glog
.
Fatal
(
server
.
Serve
(
port
))
glog
.
Fatal
(
server
.
Serve
(
port
))
},
},
...
...
pkg/kubectl/cmd/resize.go
View file @
d46ae5d8
...
@@ -21,6 +21,7 @@ import (
...
@@ -21,6 +21,7 @@ import (
"io"
"io"
"github.com/GoogleCloudPlatform/kubernetes/pkg/kubectl"
"github.com/GoogleCloudPlatform/kubernetes/pkg/kubectl"
"github.com/GoogleCloudPlatform/kubernetes/pkg/kubectl/cmd/util"
"github.com/spf13/cobra"
"github.com/spf13/cobra"
)
)
...
@@ -44,7 +45,7 @@ Examples:
...
@@ -44,7 +45,7 @@ Examples:
$ kubectl resize --current-replicas=2 --replicas=3 replicationcontrollers foo
$ kubectl resize --current-replicas=2 --replicas=3 replicationcontrollers foo
`
,
`
,
Run
:
func
(
cmd
*
cobra
.
Command
,
args
[]
string
)
{
Run
:
func
(
cmd
*
cobra
.
Command
,
args
[]
string
)
{
count
:=
GetFlagInt
(
cmd
,
"replicas"
)
count
:=
util
.
GetFlagInt
(
cmd
,
"replicas"
)
if
len
(
args
)
!=
2
||
count
<
0
{
if
len
(
args
)
!=
2
||
count
<
0
{
usageError
(
cmd
,
"--replicas=<count> <resource> <id>"
)
usageError
(
cmd
,
"--replicas=<count> <resource> <id>"
)
}
}
...
@@ -53,13 +54,13 @@ Examples:
...
@@ -53,13 +54,13 @@ Examples:
checkErr
(
err
)
checkErr
(
err
)
mapper
,
_
:=
f
.
Object
(
cmd
)
mapper
,
_
:=
f
.
Object
(
cmd
)
mapping
,
namespace
,
name
:=
ResourceFromArgs
(
cmd
,
args
,
mapper
,
cmdNamespace
)
mapping
,
namespace
,
name
:=
util
.
ResourceFromArgs
(
cmd
,
args
,
mapper
,
cmdNamespace
)
resizer
,
err
:=
f
.
Resizer
(
cmd
,
mapping
)
resizer
,
err
:=
f
.
Resizer
(
cmd
,
mapping
)
checkErr
(
err
)
checkErr
(
err
)
resourceVersion
:=
GetFlagString
(
cmd
,
"resource-version"
)
resourceVersion
:=
util
.
GetFlagString
(
cmd
,
"resource-version"
)
currentSize
:=
GetFlagInt
(
cmd
,
"current-replicas"
)
currentSize
:=
util
.
GetFlagInt
(
cmd
,
"current-replicas"
)
s
,
err
:=
resizer
.
Resize
(
namespace
,
name
,
&
kubectl
.
ResizePrecondition
{
currentSize
,
resourceVersion
},
uint
(
count
))
s
,
err
:=
resizer
.
Resize
(
namespace
,
name
,
&
kubectl
.
ResizePrecondition
{
currentSize
,
resourceVersion
},
uint
(
count
))
checkErr
(
err
)
checkErr
(
err
)
fmt
.
Fprintf
(
out
,
"%s
\n
"
,
s
)
fmt
.
Fprintf
(
out
,
"%s
\n
"
,
s
)
...
...
pkg/kubectl/cmd/rollingupdate.go
View file @
d46ae5d8
...
@@ -22,6 +22,7 @@ import (
...
@@ -22,6 +22,7 @@ import (
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
"github.com/GoogleCloudPlatform/kubernetes/pkg/kubectl"
"github.com/GoogleCloudPlatform/kubernetes/pkg/kubectl"
"github.com/GoogleCloudPlatform/kubernetes/pkg/kubectl/cmd/util"
"github.com/spf13/cobra"
"github.com/spf13/cobra"
)
)
...
@@ -48,13 +49,13 @@ $ kubectl rollingupdate frontend-v1 -f frontend-v2.json
...
@@ -48,13 +49,13 @@ $ kubectl rollingupdate frontend-v1 -f frontend-v2.json
$ cat frontend-v2.json | kubectl rollingupdate frontend-v1 -f -
$ cat frontend-v2.json | kubectl rollingupdate frontend-v1 -f -
<update pods of frontend-v1 using json data passed into stdin>`
,
<update pods of frontend-v1 using json data passed into stdin>`
,
Run
:
func
(
cmd
*
cobra
.
Command
,
args
[]
string
)
{
Run
:
func
(
cmd
*
cobra
.
Command
,
args
[]
string
)
{
filename
:=
GetFlagString
(
cmd
,
"filename"
)
filename
:=
util
.
GetFlagString
(
cmd
,
"filename"
)
if
len
(
filename
)
==
0
{
if
len
(
filename
)
==
0
{
usageError
(
cmd
,
"Must specify filename for new controller"
)
usageError
(
cmd
,
"Must specify filename for new controller"
)
}
}
period
:=
GetFlagDuration
(
cmd
,
"update-period"
)
period
:=
util
.
GetFlagDuration
(
cmd
,
"update-period"
)
interval
:=
GetFlagDuration
(
cmd
,
"poll-interval"
)
interval
:=
util
.
GetFlagDuration
(
cmd
,
"poll-interval"
)
timeout
:=
GetFlagDuration
(
cmd
,
"timeout"
)
timeout
:=
util
.
GetFlagDuration
(
cmd
,
"timeout"
)
if
len
(
args
)
!=
1
{
if
len
(
args
)
!=
1
{
usageError
(
cmd
,
"Must specify the controller to update"
)
usageError
(
cmd
,
"Must specify the controller to update"
)
}
}
...
@@ -67,7 +68,7 @@ $ cat frontend-v2.json | kubectl rollingupdate frontend-v1 -f -
...
@@ -67,7 +68,7 @@ $ cat frontend-v2.json | kubectl rollingupdate frontend-v1 -f -
cmdApiVersion
:=
clientConfig
.
Version
cmdApiVersion
:=
clientConfig
.
Version
mapper
,
typer
:=
f
.
Object
(
cmd
)
mapper
,
typer
:=
f
.
Object
(
cmd
)
mapping
,
namespace
,
newName
,
data
:=
ResourceFromFile
(
filename
,
typer
,
mapper
,
schema
,
cmdApiVersion
)
mapping
,
namespace
,
newName
,
data
:=
util
.
ResourceFromFile
(
filename
,
typer
,
mapper
,
schema
,
cmdApiVersion
)
if
mapping
.
Kind
!=
"ReplicationController"
{
if
mapping
.
Kind
!=
"ReplicationController"
{
usageError
(
cmd
,
"%s does not specify a valid ReplicationController"
,
filename
)
usageError
(
cmd
,
"%s does not specify a valid ReplicationController"
,
filename
)
}
}
...
@@ -78,7 +79,7 @@ $ cat frontend-v2.json | kubectl rollingupdate frontend-v1 -f -
...
@@ -78,7 +79,7 @@ $ cat frontend-v2.json | kubectl rollingupdate frontend-v1 -f -
cmdNamespace
,
err
:=
f
.
DefaultNamespace
(
cmd
)
cmdNamespace
,
err
:=
f
.
DefaultNamespace
(
cmd
)
checkErr
(
err
)
checkErr
(
err
)
err
=
CompareNamespace
(
cmdNamespace
,
namespace
)
err
=
util
.
CompareNamespace
(
cmdNamespace
,
namespace
)
checkErr
(
err
)
checkErr
(
err
)
client
,
err
:=
f
.
Client
(
cmd
)
client
,
err
:=
f
.
Client
(
cmd
)
...
...
pkg/kubectl/cmd/run.go
View file @
d46ae5d8
...
@@ -22,6 +22,7 @@ import (
...
@@ -22,6 +22,7 @@ import (
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
"github.com/GoogleCloudPlatform/kubernetes/pkg/kubectl"
"github.com/GoogleCloudPlatform/kubernetes/pkg/kubectl"
"github.com/GoogleCloudPlatform/kubernetes/pkg/kubectl/cmd/util"
"github.com/spf13/cobra"
"github.com/spf13/cobra"
)
)
...
@@ -55,7 +56,7 @@ Examples:
...
@@ -55,7 +56,7 @@ Examples:
client
,
err
:=
f
.
Client
(
cmd
)
client
,
err
:=
f
.
Client
(
cmd
)
checkErr
(
err
)
checkErr
(
err
)
generatorName
:=
GetFlagString
(
cmd
,
"generator"
)
generatorName
:=
util
.
GetFlagString
(
cmd
,
"generator"
)
generator
,
found
:=
kubectl
.
Generators
[
generatorName
]
generator
,
found
:=
kubectl
.
Generators
[
generatorName
]
if
!
found
{
if
!
found
{
usageError
(
cmd
,
fmt
.
Sprintf
(
"Generator: %s not found."
,
generator
))
usageError
(
cmd
,
fmt
.
Sprintf
(
"Generator: %s not found."
,
generator
))
...
@@ -70,22 +71,22 @@ Examples:
...
@@ -70,22 +71,22 @@ Examples:
controller
,
err
:=
generator
.
Generate
(
params
)
controller
,
err
:=
generator
.
Generate
(
params
)
checkErr
(
err
)
checkErr
(
err
)
inline
:=
GetFlagString
(
cmd
,
"overrides"
)
inline
:=
util
.
GetFlagString
(
cmd
,
"overrides"
)
if
len
(
inline
)
>
0
{
if
len
(
inline
)
>
0
{
Merge
(
controller
,
inline
,
"ReplicationController"
)
util
.
Merge
(
controller
,
inline
,
"ReplicationController"
)
}
}
// TODO: extract this flag to a central location, when such a location exists.
// TODO: extract this flag to a central location, when such a location exists.
if
!
GetFlagBool
(
cmd
,
"dry-run"
)
{
if
!
util
.
GetFlagBool
(
cmd
,
"dry-run"
)
{
controller
,
err
=
client
.
ReplicationControllers
(
namespace
)
.
Create
(
controller
.
(
*
api
.
ReplicationController
))
controller
,
err
=
client
.
ReplicationControllers
(
namespace
)
.
Create
(
controller
.
(
*
api
.
ReplicationController
))
checkErr
(
err
)
checkErr
(
err
)
}
}
err
=
PrintObject
(
cmd
,
controller
,
f
,
out
)
err
=
f
.
PrintObject
(
cmd
,
controller
,
out
)
checkErr
(
err
)
checkErr
(
err
)
},
},
}
}
AddPrinterFlags
(
cmd
)
util
.
AddPrinterFlags
(
cmd
)
cmd
.
Flags
()
.
String
(
"generator"
,
"run-container/v1"
,
"The name of the api generator that you want to use. Default 'run-container-controller/v1'"
)
cmd
.
Flags
()
.
String
(
"generator"
,
"run-container/v1"
,
"The name of the api generator that you want to use. Default 'run-container-controller/v1'"
)
cmd
.
Flags
()
.
String
(
"image"
,
""
,
"The image for the container you wish to run."
)
cmd
.
Flags
()
.
String
(
"image"
,
""
,
"The image for the container you wish to run."
)
cmd
.
Flags
()
.
IntP
(
"replicas"
,
"r"
,
1
,
"Number of replicas to create for this container. Default 1"
)
cmd
.
Flags
()
.
IntP
(
"replicas"
,
"r"
,
1
,
"Number of replicas to create for this container. Default 1"
)
...
...
pkg/kubectl/cmd/stop.go
View file @
d46ae5d8
...
@@ -20,6 +20,7 @@ import (
...
@@ -20,6 +20,7 @@ import (
"fmt"
"fmt"
"io"
"io"
"github.com/GoogleCloudPlatform/kubernetes/pkg/kubectl/cmd/util"
"github.com/spf13/cobra"
"github.com/spf13/cobra"
)
)
...
@@ -42,7 +43,7 @@ Examples:
...
@@ -42,7 +43,7 @@ Examples:
}
}
cmdNamespace
,
err
:=
f
.
DefaultNamespace
(
cmd
)
cmdNamespace
,
err
:=
f
.
DefaultNamespace
(
cmd
)
mapper
,
_
:=
f
.
Object
(
cmd
)
mapper
,
_
:=
f
.
Object
(
cmd
)
mapping
,
namespace
,
name
:=
ResourceFromArgs
(
cmd
,
args
,
mapper
,
cmdNamespace
)
mapping
,
namespace
,
name
:=
util
.
ResourceFromArgs
(
cmd
,
args
,
mapper
,
cmdNamespace
)
reaper
,
err
:=
f
.
Reaper
(
cmd
,
mapping
)
reaper
,
err
:=
f
.
Reaper
(
cmd
,
mapping
)
checkErr
(
err
)
checkErr
(
err
)
...
...
pkg/kubectl/cmd/update.go
View file @
d46ae5d8
...
@@ -20,6 +20,7 @@ import (
...
@@ -20,6 +20,7 @@ import (
"fmt"
"fmt"
"io"
"io"
cmdutil
"github.com/GoogleCloudPlatform/kubernetes/pkg/kubectl/cmd/util"
"github.com/GoogleCloudPlatform/kubernetes/pkg/kubectl/resource"
"github.com/GoogleCloudPlatform/kubernetes/pkg/kubectl/resource"
"github.com/GoogleCloudPlatform/kubernetes/pkg/util"
"github.com/GoogleCloudPlatform/kubernetes/pkg/util"
"github.com/spf13/cobra"
"github.com/spf13/cobra"
...
@@ -53,14 +54,14 @@ Examples:
...
@@ -53,14 +54,14 @@ Examples:
checkErr
(
err
)
checkErr
(
err
)
mapper
,
typer
:=
f
.
Object
(
cmd
)
mapper
,
typer
:=
f
.
Object
(
cmd
)
r
:=
resource
.
NewBuilder
(
mapper
,
typer
,
ClientMapperForCommand
(
cmd
,
f
))
.
r
:=
resource
.
NewBuilder
(
mapper
,
typer
,
f
.
ClientMapperForCommand
(
cmd
))
.
ContinueOnError
()
.
ContinueOnError
()
.
NamespaceParam
(
cmdNamespace
)
.
RequireNamespace
()
.
NamespaceParam
(
cmdNamespace
)
.
RequireNamespace
()
.
FilenameParam
(
flags
.
Filenames
...
)
.
FilenameParam
(
flags
.
Filenames
...
)
.
Flatten
()
.
Flatten
()
.
Do
()
Do
()
patch
:=
GetFlagString
(
cmd
,
"patch"
)
patch
:=
cmdutil
.
GetFlagString
(
cmd
,
"patch"
)
if
len
(
flags
.
Filenames
)
==
0
&&
len
(
patch
)
==
0
{
if
len
(
flags
.
Filenames
)
==
0
&&
len
(
patch
)
==
0
{
usageError
(
cmd
,
"Must specify --filename or --patch to update"
)
usageError
(
cmd
,
"Must specify --filename or --patch to update"
)
}
}
...
@@ -102,7 +103,7 @@ func updateWithPatch(cmd *cobra.Command, args []string, f *Factory, patch string
...
@@ -102,7 +103,7 @@ func updateWithPatch(cmd *cobra.Command, args []string, f *Factory, patch string
checkErr
(
err
)
checkErr
(
err
)
mapper
,
_
:=
f
.
Object
(
cmd
)
mapper
,
_
:=
f
.
Object
(
cmd
)
mapping
,
namespace
,
name
:=
ResourceFromArgs
(
cmd
,
args
,
mapper
,
cmdNamespace
)
mapping
,
namespace
,
name
:=
cmdutil
.
ResourceFromArgs
(
cmd
,
args
,
mapper
,
cmdNamespace
)
client
,
err
:=
f
.
RESTClient
(
cmd
,
mapping
)
client
,
err
:=
f
.
RESTClient
(
cmd
,
mapping
)
checkErr
(
err
)
checkErr
(
err
)
...
@@ -110,7 +111,7 @@ func updateWithPatch(cmd *cobra.Command, args []string, f *Factory, patch string
...
@@ -110,7 +111,7 @@ func updateWithPatch(cmd *cobra.Command, args []string, f *Factory, patch string
obj
,
err
:=
helper
.
Get
(
namespace
,
name
)
obj
,
err
:=
helper
.
Get
(
namespace
,
name
)
checkErr
(
err
)
checkErr
(
err
)
Merge
(
obj
,
patch
,
mapping
.
Kind
)
cmdutil
.
Merge
(
obj
,
patch
,
mapping
.
Kind
)
data
,
err
:=
helper
.
Codec
.
Encode
(
obj
)
data
,
err
:=
helper
.
Codec
.
Encode
(
obj
)
checkErr
(
err
)
checkErr
(
err
)
...
...
pkg/kubectl/cmd/helpers.go
→
pkg/kubectl/cmd/
util/
helpers.go
View file @
d46ae5d8
...
@@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
...
@@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
limitations under the License.
*/
*/
package
cmd
package
util
import
(
import
(
"encoding/json"
"encoding/json"
...
@@ -34,6 +34,18 @@ import (
...
@@ -34,6 +34,18 @@ import (
"github.com/spf13/cobra"
"github.com/spf13/cobra"
)
)
func
checkErr
(
err
error
)
{
if
err
!=
nil
{
glog
.
FatalDepth
(
1
,
err
)
}
}
func
usageError
(
cmd
*
cobra
.
Command
,
format
string
,
args
...
interface
{})
{
glog
.
Errorf
(
format
,
args
...
)
glog
.
Errorf
(
"See '%s -h' for help."
,
cmd
.
CommandPath
())
os
.
Exit
(
1
)
}
func
GetFlagString
(
cmd
*
cobra
.
Command
,
flag
string
)
string
{
func
GetFlagString
(
cmd
*
cobra
.
Command
,
flag
string
)
string
{
f
:=
cmd
.
Flags
()
.
Lookup
(
flag
)
f
:=
cmd
.
Flags
()
.
Lookup
(
flag
)
if
f
==
nil
{
if
f
==
nil
{
...
...
pkg/kubectl/cmd/helpers_test.go
→
pkg/kubectl/cmd/
util/
helpers_test.go
View file @
d46ae5d8
...
@@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
...
@@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
limitations under the License.
*/
*/
package
cmd
package
util
import
(
import
(
"reflect"
"reflect"
...
...
pkg/kubectl/cmd/printing.go
→
pkg/kubectl/cmd/
util/
printing.go
View file @
d46ae5d8
...
@@ -14,16 +14,10 @@ See the License for the specific language governing permissions and
...
@@ -14,16 +14,10 @@ See the License for the specific language governing permissions and
limitations under the License.
limitations under the License.
*/
*/
package
cmd
package
util
import
(
import
(
"fmt"
"io"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/meta"
"github.com/GoogleCloudPlatform/kubernetes/pkg/kubectl"
"github.com/GoogleCloudPlatform/kubernetes/pkg/kubectl"
"github.com/GoogleCloudPlatform/kubernetes/pkg/runtime"
"github.com/spf13/cobra"
"github.com/spf13/cobra"
)
)
...
@@ -35,28 +29,8 @@ func AddPrinterFlags(cmd *cobra.Command) {
...
@@ -35,28 +29,8 @@ func AddPrinterFlags(cmd *cobra.Command) {
cmd
.
Flags
()
.
StringP
(
"template"
,
"t"
,
""
,
"Template string or path to template file to use when -o=template or -o=templatefile."
)
cmd
.
Flags
()
.
StringP
(
"template"
,
"t"
,
""
,
"Template string or path to template file to use when -o=template or -o=templatefile."
)
}
}
// PrintObject prints an api object given command line flags to modify the output format
// OutputVersion returns the preferred output version for generic content (JSON, YAML, or templates)
func
PrintObject
(
cmd
*
cobra
.
Command
,
obj
runtime
.
Object
,
f
*
Factory
,
out
io
.
Writer
)
error
{
func
OutputVersion
(
cmd
*
cobra
.
Command
,
defaultVersion
string
)
string
{
mapper
,
_
:=
f
.
Object
(
cmd
)
_
,
kind
,
err
:=
api
.
Scheme
.
ObjectVersionAndKind
(
obj
)
if
err
!=
nil
{
return
err
}
mapping
,
err
:=
mapper
.
RESTMapping
(
kind
)
if
err
!=
nil
{
return
err
}
printer
,
err
:=
PrinterForMapping
(
f
,
cmd
,
mapping
)
if
err
!=
nil
{
return
err
}
return
printer
.
PrintObj
(
obj
,
out
)
}
// outputVersion returns the preferred output version for generic content (JSON, YAML, or templates)
func
outputVersion
(
cmd
*
cobra
.
Command
,
defaultVersion
string
)
string
{
outputVersion
:=
GetFlagString
(
cmd
,
"output-version"
)
outputVersion
:=
GetFlagString
(
cmd
,
"output-version"
)
if
len
(
outputVersion
)
==
0
{
if
len
(
outputVersion
)
==
0
{
outputVersion
=
defaultVersion
outputVersion
=
defaultVersion
...
@@ -75,32 +49,3 @@ func PrinterForCommand(cmd *cobra.Command) (kubectl.ResourcePrinter, bool, error
...
@@ -75,32 +49,3 @@ func PrinterForCommand(cmd *cobra.Command) (kubectl.ResourcePrinter, bool, error
return
kubectl
.
GetPrinter
(
outputFormat
,
templateFile
)
return
kubectl
.
GetPrinter
(
outputFormat
,
templateFile
)
}
}
// PrinterForMapping returns a printer suitable for displaying the provided resource type.
// Requires that printer flags have been added to cmd (see AddPrinterFlags).
func
PrinterForMapping
(
f
*
Factory
,
cmd
*
cobra
.
Command
,
mapping
*
meta
.
RESTMapping
)
(
kubectl
.
ResourcePrinter
,
error
)
{
printer
,
ok
,
err
:=
PrinterForCommand
(
cmd
)
if
err
!=
nil
{
return
nil
,
err
}
if
ok
{
clientConfig
,
err
:=
f
.
ClientConfig
(
cmd
)
checkErr
(
err
)
defaultVersion
:=
clientConfig
.
Version
version
:=
outputVersion
(
cmd
,
defaultVersion
)
if
len
(
version
)
==
0
{
version
=
mapping
.
APIVersion
}
if
len
(
version
)
==
0
{
return
nil
,
fmt
.
Errorf
(
"you must specify an output-version when using this output format"
)
}
printer
=
kubectl
.
NewVersionedPrinter
(
printer
,
mapping
.
ObjectConvertor
,
version
)
}
else
{
printer
,
err
=
f
.
Printer
(
cmd
,
mapping
,
GetFlagBool
(
cmd
,
"no-headers"
))
if
err
!=
nil
{
return
nil
,
err
}
}
return
printer
,
nil
}
pkg/kubectl/cmd/resource.go
→
pkg/kubectl/cmd/
util/
resource.go
View file @
d46ae5d8
...
@@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
...
@@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
limitations under the License.
*/
*/
package
cmd
package
util
import
(
import
(
"fmt"
"fmt"
...
...
pkg/kubectl/cmd/version.go
View file @
d46ae5d8
...
@@ -22,6 +22,7 @@ import (
...
@@ -22,6 +22,7 @@ import (
"github.com/spf13/cobra"
"github.com/spf13/cobra"
"github.com/GoogleCloudPlatform/kubernetes/pkg/kubectl"
"github.com/GoogleCloudPlatform/kubernetes/pkg/kubectl"
"github.com/GoogleCloudPlatform/kubernetes/pkg/kubectl/cmd/util"
)
)
func
(
f
*
Factory
)
NewCmdVersion
(
out
io
.
Writer
)
*
cobra
.
Command
{
func
(
f
*
Factory
)
NewCmdVersion
(
out
io
.
Writer
)
*
cobra
.
Command
{
...
@@ -29,7 +30,7 @@ func (f *Factory) NewCmdVersion(out io.Writer) *cobra.Command {
...
@@ -29,7 +30,7 @@ func (f *Factory) NewCmdVersion(out io.Writer) *cobra.Command {
Use
:
"version"
,
Use
:
"version"
,
Short
:
"Print version of client and server"
,
Short
:
"Print version of client and server"
,
Run
:
func
(
cmd
*
cobra
.
Command
,
args
[]
string
)
{
Run
:
func
(
cmd
*
cobra
.
Command
,
args
[]
string
)
{
if
GetFlagBool
(
cmd
,
"client"
)
{
if
util
.
GetFlagBool
(
cmd
,
"client"
)
{
kubectl
.
GetClientVersion
(
out
)
kubectl
.
GetClientVersion
(
out
)
return
return
}
}
...
...
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