-
Notifications
You must be signed in to change notification settings - Fork 2.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
kube: Add support for podman pod logs
.
#11427
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
package pods | ||
|
||
import ( | ||
"os" | ||
|
||
"github.com/containers/common/pkg/completion" | ||
"github.com/containers/podman/v3/cmd/podman/common" | ||
"github.com/containers/podman/v3/cmd/podman/registry" | ||
"github.com/containers/podman/v3/cmd/podman/validate" | ||
"github.com/containers/podman/v3/libpod/define" | ||
"github.com/containers/podman/v3/pkg/domain/entities" | ||
"github.com/containers/podman/v3/pkg/util" | ||
"github.com/pkg/errors" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
// logsOptionsWrapper wraps entities.LogsOptions and prevents leaking | ||
// CLI-only fields into the API types. | ||
type logsOptionsWrapper struct { | ||
entities.PodLogsOptions | ||
|
||
SinceRaw string | ||
|
||
UntilRaw string | ||
} | ||
|
||
var ( | ||
logsPodOptions logsOptionsWrapper | ||
logsPodDescription = `Displays logs for pod with one or more containers.` | ||
logsPodCommand = &cobra.Command{ | ||
Use: "logs [options] POD", | ||
Short: "Fetch logs for pod with one or more containers", | ||
Long: logsPodDescription, | ||
// We dont want users to invoke latest and pod togather | ||
Args: func(cmd *cobra.Command, args []string) error { | ||
switch { | ||
case registry.IsRemote() && logsPodOptions.Latest: | ||
return errors.New(cmd.Name() + " does not support 'latest' when run remotely") | ||
case len(args) > 1: | ||
return errors.New("requires exactly 1 arg") | ||
case logsPodOptions.Latest && len(args) > 0: | ||
return errors.New("--latest and pods cannot be used together") | ||
case !logsPodOptions.Latest && len(args) < 1: | ||
return errors.New("specify at least one pod name or ID to log") | ||
} | ||
return nil | ||
}, | ||
RunE: logs, | ||
ValidArgsFunction: common.AutocompletePods, | ||
Example: `podman pod logs podID | ||
podman pod logs -c ctrname podName | ||
podman pod logs --tail 2 mywebserver | ||
podman pod logs --follow=true --since 10m podID | ||
podman pod logs mywebserver`, | ||
} | ||
|
||
containerLogsCommand = &cobra.Command{ | ||
Use: logsPodCommand.Use, | ||
Short: logsPodCommand.Short, | ||
Long: logsPodCommand.Long, | ||
Args: logsPodCommand.Args, | ||
RunE: logsPodCommand.RunE, | ||
ValidArgsFunction: logsPodCommand.ValidArgsFunction, | ||
Example: `podman pod logs podId | ||
podman pod logs -c ctrname podName | ||
podman pod logs --tail 2 mywebserver | ||
podman pod logs --follow=true --since 10m podID`, | ||
} | ||
) | ||
|
||
func init() { | ||
registry.Commands = append(registry.Commands, registry.CliCommand{ | ||
Command: logsPodCommand, | ||
}) | ||
logsFlags(logsPodCommand) | ||
validate.AddLatestFlag(logsPodCommand, &logsPodOptions.Latest) | ||
|
||
// container logs | ||
registry.Commands = append(registry.Commands, registry.CliCommand{ | ||
Command: containerLogsCommand, | ||
Parent: podCmd, | ||
}) | ||
logsFlags(containerLogsCommand) | ||
validate.AddLatestFlag(containerLogsCommand, &logsPodOptions.Latest) | ||
} | ||
|
||
func logsFlags(cmd *cobra.Command) { | ||
flags := cmd.Flags() | ||
|
||
flags.BoolVar(&logsPodOptions.Details, "details", false, "Show extra details provided to the logs") | ||
flags.BoolVarP(&logsPodOptions.Follow, "follow", "f", false, "Follow log output.") | ||
|
||
containerNameFlag := "container" | ||
flags.StringVarP(&logsPodOptions.ContainerName, containerNameFlag, "c", "", "Filter logs by container name or id which belongs to pod") | ||
_ = cmd.RegisterFlagCompletionFunc(containerNameFlag, common.AutocompleteContainers) | ||
|
||
sinceFlagName := "since" | ||
flags.StringVar(&logsPodOptions.SinceRaw, sinceFlagName, "", "Show logs since TIMESTAMP") | ||
_ = cmd.RegisterFlagCompletionFunc(sinceFlagName, completion.AutocompleteNone) | ||
|
||
untilFlagName := "until" | ||
flags.StringVar(&logsPodOptions.UntilRaw, untilFlagName, "", "Show logs until TIMESTAMP") | ||
_ = cmd.RegisterFlagCompletionFunc(untilFlagName, completion.AutocompleteNone) | ||
|
||
tailFlagName := "tail" | ||
flags.Int64Var(&logsPodOptions.Tail, tailFlagName, -1, "Output the specified number of LINES at the end of the logs.") | ||
_ = cmd.RegisterFlagCompletionFunc(tailFlagName, completion.AutocompleteNone) | ||
|
||
flags.BoolVarP(&logsPodOptions.Timestamps, "timestamps", "t", false, "Output the timestamps in the log") | ||
flags.SetInterspersed(false) | ||
_ = flags.MarkHidden("details") | ||
} | ||
|
||
func logs(_ *cobra.Command, args []string) error { | ||
if logsPodOptions.SinceRaw != "" { | ||
// parse time, error out if something is wrong | ||
since, err := util.ParseInputTime(logsPodOptions.SinceRaw, true) | ||
if err != nil { | ||
return errors.Wrapf(err, "error parsing --since %q", logsPodOptions.SinceRaw) | ||
} | ||
logsPodOptions.Since = since | ||
} | ||
if logsPodOptions.UntilRaw != "" { | ||
// parse time, error out if something is wrong | ||
until, err := util.ParseInputTime(logsPodOptions.UntilRaw, false) | ||
if err != nil { | ||
return errors.Wrapf(err, "error parsing --until %q", logsPodOptions.UntilRaw) | ||
} | ||
logsPodOptions.Until = until | ||
} | ||
|
||
// Remote can only process one container at a time | ||
if registry.IsRemote() && logsPodOptions.ContainerName == "" { | ||
return errors.Wrapf(define.ErrInvalidArg, "-c or --container cannot be empty") | ||
} | ||
|
||
logsPodOptions.StdoutWriter = os.Stdout | ||
logsPodOptions.StderrWriter = os.Stderr | ||
return registry.ContainerEngine().PodLogs(registry.GetContext(), args[0], logsPodOptions.PodLogsOptions) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
% podman-pod-logs(1) | ||
|
||
## NAME | ||
podman\-pod\-logs - Displays logs for pod with one or more containers | ||
|
||
## SYNOPSIS | ||
**podman pod logs** [*options*] *pod* | ||
|
||
## DESCRIPTION | ||
The podman pod logs command batch-retrieves whatever logs are present with all the containers of a pod. Pod logs can be filtered by container name or id using flag **-c** or **--container** if needed. | ||
|
||
Note: Long running command of `podman pod log` with a `-f` or `--follow` needs to be reinvoked if new container is added to the pod dynamically otherwise logs of newly added containers would not be visible in log stream. | ||
|
||
## OPTIONS | ||
|
||
#### **--container**, **-c** | ||
|
||
By default `podman pod logs` retrives logs for all the containers available within the pod differentiate by field `container`. However there are use-cases where user would want to limit the log stream only to a particular container of a pod for such cases `-c` can be used like `podman pod logs -c ctrNameorID podname`. | ||
|
||
#### **--follow**, **-f** | ||
|
||
Follow log output. Default is false. | ||
|
||
Note: If you are following a pod which is removed `podman pod rm`, then there is a | ||
chance the the log file will be removed before `podman pod logs` reads the final content. | ||
|
||
#### **--latest**, **-l** | ||
flouthoc marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
Instead of providing the pod name or id, get logs of the last created pod. (This option is not available with the remote Podman client) | ||
|
||
#### **--since**=*TIMESTAMP* | ||
|
||
Show logs since TIMESTAMP. The --since option can be Unix timestamps, date formatted timestamps, or Go duration | ||
strings (e.g. 10m, 1h30m) computed relative to the client machine's time. Supported formats for date formatted | ||
time stamps include RFC3339Nano, RFC3339, 2006-01-02T15:04:05, 2006-01-02T15:04:05.999999999, 2006-01-02Z07:00, | ||
and 2006-01-02. | ||
|
||
#### **--until**=*TIMESTAMP* | ||
|
||
Show logs until TIMESTAMP. The --until option can be Unix timestamps, date formatted timestamps, or Go duration | ||
strings (e.g. 10m, 1h30m) computed relative to the client machine's time. Supported formats for date formatted | ||
time stamps include RFC3339Nano, RFC3339, 2006-01-02T15:04:05, 2006-01-02T15:04:05.999999999, 2006-01-02Z07:00, | ||
and 2006-01-02. | ||
|
||
|
||
#### **--tail**=*LINES* | ||
|
||
Output the specified number of LINES at the end of the logs. LINES must be an integer. Defaults to -1, | ||
which prints all lines | ||
|
||
#### **--timestamps**, **-t** | ||
|
||
Show timestamps in the log outputs. The default is false | ||
|
||
## EXAMPLE | ||
|
||
To view a pod's logs: | ||
``` | ||
podman pod logs -t podIdorName | ||
``` | ||
|
||
To view logs of a specific container on the pod | ||
``` | ||
podman pod logs -c ctrIdOrName podIdOrName | ||
``` | ||
|
||
To view all pod logs: | ||
``` | ||
podman pod logs -t --since 0 myserver-pod-1 | ||
``` | ||
|
||
To view a pod's logs since a certain time: | ||
``` | ||
podman pod logs -t --since 2017-08-07T10:10:09.055837383-04:00 myserver-pod-1 | ||
``` | ||
|
||
To view a pod's logs generated in the last 10 minutes: | ||
``` | ||
podman pod logs --since 10m myserver-pod-1 | ||
``` | ||
|
||
To view a pod's logs until 30 minutes ago: | ||
``` | ||
podman pod logs --until 30m myserver-pod-1 | ||
``` | ||
|
||
## SEE ALSO | ||
podman(1), podman-pod-start(1), podman-pod-rm(1), podman-logs(1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why wouldn't I just use
podman logs CID?
, wouldpodman pod logs
give me different data?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It majorly for mainlining interface parity between
kubectl pod logs
andpodman pod logs
, since kubectl would not print logs if there are multiple containers in a pod it rather asks users to providecontainer
using a-c
flag.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok