@@ -32,17 +32,20 @@ import (
3232)
3333
3434const (
35- exec_example = `// get output from running 'date' in ruby-container from pod 123456-7890
36- $ kubectl exec -p 123456-7890 -c ruby-container date
35+ exec_example = `// get output from running 'date' from pod 123456-7890, using the first container by default
36+ $ kubectl exec 123456-7890 date
37+
38+ // get output from running 'date' in ruby-container from pod 123456-7890
39+ $ kubectl exec 123456-7890 -c ruby-container date
3740
3841//switch to raw terminal mode, sends stdin to 'bash' in ruby-container from pod 123456-780 and sends stdout/stderr from 'bash' back to the client
39- $ kubectl exec -p 123456-7890 -c ruby-container -i -t -- bash -il`
42+ $ kubectl exec 123456-7890 -c ruby-container -i -t -- bash -il`
4043)
4144
4245func NewCmdExec (f * cmdutil.Factory , cmdIn io.Reader , cmdOut , cmdErr io.Writer ) * cobra.Command {
4346 params := & execParams {}
4447 cmd := & cobra.Command {
45- Use : "exec -p POD -c CONTAINER -- COMMAND [args...]" ,
48+ Use : "exec POD -c CONTAINER -- COMMAND [args...]" ,
4649 Short : "Execute a command in a container." ,
4750 Long : "Execute a command in a container." ,
4851 Example : exec_example ,
@@ -52,10 +55,8 @@ func NewCmdExec(f *cmdutil.Factory, cmdIn io.Reader, cmdOut, cmdErr io.Writer) *
5255 },
5356 }
5457 cmd .Flags ().StringVarP (& params .podName , "pod" , "p" , "" , "Pod name" )
55- cmd .MarkFlagRequired ("pod" )
5658 // TODO support UID
5759 cmd .Flags ().StringVarP (& params .containerName , "container" , "c" , "" , "Container name" )
58- cmd .MarkFlagRequired ("container" )
5960 cmd .Flags ().BoolVarP (& params .stdin , "stdin" , "i" , false , "Pass stdin to the container" )
6061 cmd .Flags ().BoolVarP (& params .tty , "tty" , "t" , false , "Stdin is a TTY" )
6162 return cmd
@@ -79,14 +80,27 @@ type execParams struct {
7980 tty bool
8081}
8182
82- func RunExec ( f * cmdutil. Factory , cmd * cobra.Command , cmdIn io. Reader , cmdOut , cmdErr io. Writer , p * execParams , args [] string , re remoteExecutor ) error {
83- if len (p .podName ) == 0 {
84- return cmdutil .UsageError (cmd , "POD is required for exec" )
83+ func extractPodAndContainer ( cmd * cobra.Command , args [] string , p * execParams ) ( podName string , containerName string , err error ) {
84+ if len (p .podName ) == 0 && len ( args ) == 0 {
85+ return "" , "" , cmdutil .UsageError (cmd , "POD is required for exec" )
8586 }
86-
87- if len (args ) < 1 {
88- return cmdutil .UsageError (cmd , "COMMAND is required for exec" )
87+ if len (p .podName ) != 0 {
88+ printDeprecationWarning ("exec POD" , "-p POD" )
89+ podName = p .podName
90+ if len (args ) < 1 {
91+ return "" , "" , cmdutil .UsageError (cmd , "COMMAND is required for exec" )
92+ }
93+ } else {
94+ podName = args [0 ]
95+ if len (args ) < 2 {
96+ return "" , "" , cmdutil .UsageError (cmd , "COMMAND is required for exec" )
97+ }
8998 }
99+ return podName , p .containerName , nil
100+ }
101+
102+ func RunExec (f * cmdutil.Factory , cmd * cobra.Command , cmdIn io.Reader , cmdOut , cmdErr io.Writer , p * execParams , args []string , re remoteExecutor ) error {
103+ podName , containerName , err := extractPodAndContainer (cmd , args , p )
90104 namespace , err := f .DefaultNamespace ()
91105 if err != nil {
92106 return err
@@ -97,17 +111,17 @@ func RunExec(f *cmdutil.Factory, cmd *cobra.Command, cmdIn io.Reader, cmdOut, cm
97111 return err
98112 }
99113
100- pod , err := client .Pods (namespace ).Get (p . podName )
114+ pod , err := client .Pods (namespace ).Get (podName )
101115 if err != nil {
102116 return err
103117 }
104118
105119 if pod .Status .Phase != api .PodRunning {
106- glog .Fatalf ("Unable to execute command because pod is not running. Current status=%v" , pod .Status .Phase )
120+ glog .Fatalf ("Unable to execute command because pod %s is not running. Current status=%v" , podName , pod .Status .Phase )
107121 }
108122
109- containerName := p .containerName
110123 if len (containerName ) == 0 {
124+ glog .V (4 ).Infof ("defaulting container name to %s" , pod .Spec .Containers [0 ].Name )
111125 containerName = pod .Spec .Containers [0 ].Name
112126 }
113127
0 commit comments