Skip to content
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

Add since-second and since-time feature so that pod #322

Merged
merged 1 commit into from Jun 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
32 changes: 27 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
}
}

crombus marked this conversation as resolved.
Show resolved Hide resolved
printer, err := o.printFlags.ToPrinter()
if err != nil {
return err
Expand All @@ -158,6 +176,9 @@ func (o *InspectOptions) Validate() error {
if len(o.destDir) == 0 {
return fmt.Errorf("--dest-dir must not be empty")
}
if len(o.sinceTime) > 0 && o.since != 0 {
return fmt.Errorf("at most one of `sinceTime` or `since` may be specified")
}
return nil
}

Expand All @@ -177,6 +198,7 @@ func (o *InspectOptions) Run() error {
// ensure we're able to proceed writing data to specified destination
if err := ensureDirectoryViable(o.destDir, o.overwrite); err != nil {
return err

}

// finally, gather polymorphic resources specified by the user
Expand Down
6 changes: 6 additions & 0 deletions pkg/cli/admin/inspect/pod.go
Expand Up @@ -290,6 +290,12 @@ func (o *InspectOptions) gatherContainerLogs(destDir string, pod *corev1.Pod, co
Previous: false,
Timestamps: true,
}
if len(o.sinceTime) > 0 {
logOptions.SinceTime = &o.sinceTimestamp
}
if o.since != 0 {
logOptions.SinceSeconds = &o.sinceInt
}
filename := "current.log"
logsReq := o.kubeClient.CoreV1().Pods(pod.Namespace).GetLogs(pod.Name, logOptions)
if err := o.fileWriter.WriteFromSource(path.Join(destDir, "/"+filename), logsReq); err != nil {
Expand Down