mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-08 11:38:15 +00:00
Merge pull request #2871 from zulily/kubectl-log-default-container
Modifies kubectl log cmd to not require container arg for single-contain...
This commit is contained in:
commit
ef76981e78
@ -24,27 +24,46 @@ import (
|
|||||||
|
|
||||||
func (f *Factory) NewCmdLog(out io.Writer) *cobra.Command {
|
func (f *Factory) NewCmdLog(out io.Writer) *cobra.Command {
|
||||||
cmd := &cobra.Command{
|
cmd := &cobra.Command{
|
||||||
Use: "log <pod> <container>",
|
Use: "log <pod> [<container>]",
|
||||||
Short: "Print the logs for a container in a pod",
|
Short: "Print the logs for a container in a pod.",
|
||||||
|
Long: "Print the logs for a container in a pod. If the pod has only one container, the container name is optional.",
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
if len(args) != 2 {
|
if len(args) == 0 {
|
||||||
usageError(cmd, "<pod> and <container> are required for log")
|
usageError(cmd, "<pod> is required for log")
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(args) > 2 {
|
||||||
|
usageError(cmd, "log <pod> [<container>]")
|
||||||
}
|
}
|
||||||
|
|
||||||
namespace := GetKubeNamespace(cmd)
|
namespace := GetKubeNamespace(cmd)
|
||||||
|
|
||||||
client, err := f.ClientBuilder.Client()
|
client, err := f.ClientBuilder.Client()
|
||||||
checkErr(err)
|
checkErr(err)
|
||||||
pod, err := client.Pods(namespace).Get(args[0])
|
|
||||||
|
podID := args[0]
|
||||||
|
|
||||||
|
pod, err := client.Pods(namespace).Get(podID)
|
||||||
checkErr(err)
|
checkErr(err)
|
||||||
|
|
||||||
|
var container string
|
||||||
|
if len(args) == 1 {
|
||||||
|
if len(pod.Spec.Containers) != 1 {
|
||||||
|
usageError(cmd, "<container> is required for pods with multiple containers")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get logs for the only container in the pod
|
||||||
|
container = pod.Spec.Containers[0].Name
|
||||||
|
} else {
|
||||||
|
container = args[1]
|
||||||
|
}
|
||||||
|
|
||||||
data, err := client.RESTClient.Get().
|
data, err := client.RESTClient.Get().
|
||||||
Path("proxy/minions").
|
Path("proxy/minions").
|
||||||
Path(pod.Status.Host).
|
Path(pod.Status.Host).
|
||||||
Path("containerLogs").
|
Path("containerLogs").
|
||||||
Path(namespace).
|
Path(namespace).
|
||||||
Path(args[0]).
|
Path(podID).
|
||||||
Path(args[1]).
|
Path(container).
|
||||||
Do().
|
Do().
|
||||||
Raw()
|
Raw()
|
||||||
checkErr(err)
|
checkErr(err)
|
||||||
|
Loading…
Reference in New Issue
Block a user