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 May 5, 2020
1 parent 8dda2e7 commit 1b6e840
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 9 deletions.
6 changes: 6 additions & 0 deletions contrib/completions/bash/oc
Expand Up @@ -2811,6 +2811,12 @@ _oc_adm_inspect()
flags+=("--dest-dir=")
two_word_flags+=("--dest-dir")
local_nonpersistent_flags+=("--dest-dir=")
flags+=("--since=")
two_word_flags+=("--since")
local_nonpersistent_flags+=("--since=")
flags+=("--since-time=")
two_word_flags+=("--since-time")
local_nonpersistent_flags+=("--since-time=")
flags+=("--as=")
two_word_flags+=("--as")
flags+=("--as-group=")
Expand Down
6 changes: 6 additions & 0 deletions contrib/completions/zsh/oc
Expand Up @@ -2953,6 +2953,12 @@ _oc_adm_inspect()
flags+=("--dest-dir=")
two_word_flags+=("--dest-dir")
local_nonpersistent_flags+=("--dest-dir=")
flags+=("--since=")
two_word_flags+=("--since")
local_nonpersistent_flags+=("--since=")
flags+=("--since-time=")
two_word_flags+=("--since-time")
local_nonpersistent_flags+=("--since-time=")
flags+=("--as=")
two_word_flags+=("--as")
flags+=("--as-group=")
Expand Down
28 changes: 23 additions & 5 deletions pkg/cli/admin/inspect/inspect.go
Expand Up @@ -8,6 +8,7 @@ import (
"os"
"path"
"strings"
"time"

"github.com/spf13/cobra"

Expand All @@ -25,6 +26,7 @@ import (
"k8s.io/client-go/rest"
"k8s.io/klog"
kcmdutil "k8s.io/kubectl/pkg/cmd/util"
"k8s.io/kubectl/pkg/util"
"k8s.io/kubectl/pkg/util/templates"

configv1 "github.com/openshift/api/config/v1"
Expand Down Expand Up @@ -60,11 +62,15 @@ type InspectOptions struct {

podUrlGetter *PortForwardURLGetter

fileWriter *MultiSourceFileWriter
builder *resource.Builder
args []string
namespace string
allNamespaces bool
fileWriter *MultiSourceFileWriter
builder *resource.Builder
since time.Duration
args []string
namespace string
sinceTime string
allNamespaces bool
sinceInt int64
sinceTimestamp metav1.Time

// directory where all gathered data will be stored
destDir string
Expand Down Expand Up @@ -101,6 +107,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().StringVar(&o.sinceTime, "since-time", o.sinceTime, "Only return logs after a specific date (RFC3339). Defaults to all logs. Only one of since-time / since may be used.")
cmd.Flags().DurationVar(&o.since, "since", o.since, "Only return logs newer than a relative duration like 5s, 2m, or 3h. Defaults to all logs. Only one of since-time / since may be used.")

o.configFlags.AddFlags(cmd.Flags())
return cmd
Expand Down Expand Up @@ -135,6 +143,16 @@ func (o *InspectOptions) Complete(cmd *cobra.Command, args []string) error {
return err
}

if o.since != 0 {
o.sinceInt = (int64(o.since.Round(time.Second).Seconds()))
}
if len(o.sinceTime) > 0 {
o.sinceTimestamp, err = util.ParseRFC3339(o.sinceTime, metav1.Now)
if err != nil {
return err
}
}

printer, err := o.printFlags.ToPrinter()
if err != nil {
return err
Expand Down
10 changes: 6 additions & 4 deletions pkg/cli/admin/inspect/pod.go
Expand Up @@ -285,10 +285,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.sinceInt,
SinceTime: &o.sinceTimestamp,
}
filename := "current.log"
logsReq := o.kubeClient.CoreV1().Pods(pod.Namespace).GetLogs(pod.Name, logOptions)
Expand Down

0 comments on commit 1b6e840

Please sign in to comment.