@@ -61,27 +61,33 @@ func selectContainer(pod *api.Pod, in io.Reader, out io.Writer) string {
6161 }
6262}
6363
64+ type logParams struct {
65+ containerName string
66+ }
67+
6468// NewCmdLog creates a new pod log command
6569func NewCmdLog (f * cmdutil.Factory , out io.Writer ) * cobra.Command {
70+ params := & logParams {}
6671 cmd := & cobra.Command {
67- Use : "logs [-f] [-p] POD [CONTAINER]" ,
72+ Use : "logs [-f] [-p] POD [-c CONTAINER]" ,
6873 Short : "Print the logs for a container in a pod." ,
6974 Long : "Print the logs for a container in a pod. If the pod has only one container, the container name is optional." ,
7075 Example : log_example ,
7176 Run : func (cmd * cobra.Command , args []string ) {
72- err := RunLog (f , out , cmd , args )
77+ err := RunLog (f , out , cmd , args , params )
7378 cmdutil .CheckErr (err )
7479 },
7580 Aliases : []string {"log" },
7681 }
7782 cmd .Flags ().BoolP ("follow" , "f" , false , "Specify if the logs should be streamed." )
7883 cmd .Flags ().Bool ("interactive" , true , "If true, prompt the user for input when required. Default true." )
7984 cmd .Flags ().BoolP ("previous" , "p" , false , "If true, print the logs for the previous instance of the container in a pod if it exists." )
85+ cmd .Flags ().StringVarP (& params .containerName , "container" , "c" , "" , "Container name" )
8086 return cmd
8187}
8288
8389// RunLog retrieves a pod log
84- func RunLog (f * cmdutil.Factory , out io.Writer , cmd * cobra.Command , args []string ) error {
90+ func RunLog (f * cmdutil.Factory , out io.Writer , cmd * cobra.Command , args []string , p * logParams ) error {
8591 if len (os .Args ) > 1 && os .Args [1 ] == "log" {
8692 printDeprecationWarning ("logs" , "log" )
8793 }
@@ -111,13 +117,19 @@ func RunLog(f *cmdutil.Factory, out io.Writer, cmd *cobra.Command, args []string
111117 }
112118
113119 var container string
114- if len (args ) == 1 {
115- if len (pod .Spec .Containers ) != 1 {
116- return fmt .Errorf ("POD %s has more than one container; please specify the container to print logs for" , pod .ObjectMeta .Name )
117- }
118- container = pod .Spec .Containers [0 ].Name
120+ if cmdutil .GetFlagString (cmd , "container" ) != "" {
121+ // [-c CONTAINER]
122+ container = p .containerName
119123 } else {
120- container = args [1 ]
124+ // [CONTAINER] (container as arg not flag) is supported as legacy behavior. See PR #10519 for more details.
125+ if len (args ) == 1 {
126+ if len (pod .Spec .Containers ) != 1 {
127+ return fmt .Errorf ("POD %s has more than one container; please specify the container to print logs for" , pod .ObjectMeta .Name )
128+ }
129+ container = pod .Spec .Containers [0 ].Name
130+ } else {
131+ container = args [1 ]
132+ }
121133 }
122134
123135 follow := false
0 commit comments