Skip to content

Commit

Permalink
Add since-second and since-time feature so that pod
Browse files Browse the repository at this point in the history
logs can be gathered at a particular time.
  • Loading branch information
crombus committed Mar 31, 2020
1 parent b3a52aa commit c52f391
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 4 deletions.
3 changes: 3 additions & 0 deletions contrib/completions/bash/oc
Expand Up @@ -2726,6 +2726,9 @@ _oc_adm_inspect()
flags+=("--dest-dir=")
two_word_flags+=("--dest-dir")
local_nonpersistent_flags+=("--dest-dir=")
flags+=("--sinceseconds=")
two_word_flags+=("--sinceseconds")
local_nonpersistent_flags+=("--sinceseconds=")
flags+=("--as=")
two_word_flags+=("--as")
flags+=("--as-group=")
Expand Down
3 changes: 3 additions & 0 deletions contrib/completions/zsh/oc
Expand Up @@ -2868,6 +2868,9 @@ _oc_adm_inspect()
flags+=("--dest-dir=")
two_word_flags+=("--dest-dir")
local_nonpersistent_flags+=("--dest-dir=")
flags+=("--sinceseconds=")
two_word_flags+=("--sinceseconds")
local_nonpersistent_flags+=("--sinceseconds=")
flags+=("--as=")
two_word_flags+=("--as")
flags+=("--as-group=")
Expand Down
5 changes: 5 additions & 0 deletions pkg/cli/admin/inspect/inspect.go
Expand Up @@ -7,6 +7,7 @@ import (
"os"
"path"
"strings"
"time"

"github.com/spf13/cobra"

Expand Down Expand Up @@ -61,8 +62,10 @@ type InspectOptions struct {

fileWriter *MultiSourceFileWriter
builder *resource.Builder
sincetime time.Duration
args []string
namespace string
since int64
allNamespaces bool

// directory where all gathered data will be stored
Expand Down Expand Up @@ -100,6 +103,8 @@ func NewCmdInspect(streams genericclioptions.IOStreams, parentCommandPath string

cmd.Flags().StringVar(&o.destDir, "dest-dir", o.destDir, "Root directory used for storing all gathered cluster operator data. Defaults to $(PWD)/inspect.local.<rand>")
cmd.Flags().BoolVarP(&o.allNamespaces, "all-namespaces", "A", o.allNamespaces, "If present, list the requested object(s) across all namespaces. Namespace in current context is ignored even if specified with --namespace.")
cmd.Flags().Int64Var(&o.since, "sinceseconds", o.since, "An RFC3339 timestamp from which to show logs. If this value precedes the time a pod was started, only logs since the pod start will be returned.")
cmd.Flags().DurationVar(&o.sincetime, "sincetime", o.sincetime, "Only return logs after a specific date (RFC3339). Defaults to all logs. Only one of sinceTime or sinceseconds may be used.")

o.configFlags.AddFlags(cmd.Flags())
return cmd
Expand Down
11 changes: 7 additions & 4 deletions pkg/cli/admin/inspect/pod.go
Expand Up @@ -9,6 +9,7 @@ import (
"strings"
"sync"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
utilerrors "k8s.io/apimachinery/pkg/util/errors"
Expand Down Expand Up @@ -285,10 +286,12 @@ func (o *InspectOptions) gatherContainerLogs(destDir string, pod *corev1.Pod, co

innerErrs := []error{}
logOptions := &corev1.PodLogOptions{
Container: container.Name,
Follow: false,
Previous: false,
Timestamps: true,
Container: container.Name,
Follow: false,
Previous: false,
Timestamps: true,
SinceSeconds: &o.since,
SinceTime: &metav1.Time{metav1.Now().Add(o.sincetime)},
}
filename := "current.log"
logsReq := o.kubeClient.CoreV1().Pods(pod.Namespace).GetLogs(pod.Name, logOptions)
Expand Down

0 comments on commit c52f391

Please sign in to comment.