From 7186b75f7e333777927aef08b462dbe6bd928f68 Mon Sep 17 00:00:00 2001 From: Matias Manavella Date: Wed, 21 Oct 2020 09:51:52 -0300 Subject: [PATCH] --since flag added --- cmd/preflight/cli/root.go | 1 + cmd/preflight/cli/run.go | 9 +++++++++ cmd/troubleshoot/cli/root.go | 1 + cmd/troubleshoot/cli/run.go | 9 +++++++++ pkg/apis/troubleshoot/v1beta2/collector_shared.go | 1 + pkg/collect/logs.go | 4 +++- 6 files changed, 24 insertions(+), 1 deletion(-) diff --git a/cmd/preflight/cli/root.go b/cmd/preflight/cli/root.go index d3ea9d33b..27548c3ad 100644 --- a/cmd/preflight/cli/root.go +++ b/cmd/preflight/cli/root.go @@ -36,6 +36,7 @@ that a cluster meets the requirements to run an application.`, cmd.Flags().String("collector-pullpolicy", "", "the pull policy of the collector image") cmd.Flags().Bool("collect-without-permissions", false, "always run preflight checks even if some require permissions that preflight does not have") cmd.Flags().String("since-time", "", "forces pod's logs collectors to return logs after a specific date (RFC3339)") + cmd.Flags().String("since", "", "forces pod's logs collectors to return logs newer than a relative duration like 5s, 2m, or 3h.") viper.SetEnvKeyReplacer(strings.NewReplacer("-", "_")) diff --git a/cmd/preflight/cli/run.go b/cmd/preflight/cli/run.go index e1e34a8e3..f34f78522 100644 --- a/cmd/preflight/cli/run.go +++ b/cmd/preflight/cli/run.go @@ -130,11 +130,20 @@ func runPreflights(v *viper.Viper, arg string) error { } if v.GetString("since-time") != "" { + if v.GetString("since") != "" { + progressChan <- errors.Errorf("Only one of since-time / since may be used. The flag since-time will be used.") + } for _, collectors := range preflightSpec.Spec.Collectors { if collectors.Logs != nil { collectors.Logs.Limits.SinceTime = v.GetString("since-time") } } + } else if v.GetString("since") != "" { + for _, collectors := range preflightSpec.Spec.Collectors { + if collectors.Logs != nil { + collectors.Logs.Limits.Since = v.GetString("since") + } + } } collectResults, err := preflight.Collect(collectOpts, preflightSpec) diff --git a/cmd/troubleshoot/cli/root.go b/cmd/troubleshoot/cli/root.go index 2d8f3938f..744ecabfb 100644 --- a/cmd/troubleshoot/cli/root.go +++ b/cmd/troubleshoot/cli/root.go @@ -40,6 +40,7 @@ from a server that can be used to assist when troubleshooting a Kubernetes clust cmd.Flags().Bool("redact", true, "enable/disable default redactions") cmd.Flags().Bool("collect-without-permissions", false, "always generate a support bundle, even if it some require additional permissions") cmd.Flags().String("since-time", "", "forces pods logs collectors to return logs after a specific date (RFC3339)") + cmd.Flags().String("since", "", "forces pod's logs collectors to return logs newer than a relative duration like 5s, 2m, or 3h.") // hidden in favor of the `insecure-skip-tls-verify` flag cmd.Flags().Bool("allow-insecure-connections", false, "when set, do not verify TLS certs when retrieving spec and reporting results") diff --git a/cmd/troubleshoot/cli/run.go b/cmd/troubleshoot/cli/run.go index 4c4f0b428..82ed8fd2e 100644 --- a/cmd/troubleshoot/cli/run.go +++ b/cmd/troubleshoot/cli/run.go @@ -440,6 +440,15 @@ func runCollectors(v *viper.Viper, collectors []*troubleshootv1beta2.Collect, ad } if v.GetString("since-time") != "" { + if v.GetString("since") != "" { + progressChan <- errors.Errorf("Only one of since-time / since may be used. The flag since-time will be used.") + } + for _, collectors := range cleanedCollectors { + if collectors.Collect.Logs != nil { + collectors.Collect.Logs.Limits.SinceTime = v.GetString("since-time") + } + } + } else if v.GetString("since") != "" { for _, collectors := range cleanedCollectors { if collectors.Collect.Logs != nil { collectors.Collect.Logs.Limits.SinceTime = v.GetString("since-time") diff --git a/pkg/apis/troubleshoot/v1beta2/collector_shared.go b/pkg/apis/troubleshoot/v1beta2/collector_shared.go index 82b691cfa..07f0d8d46 100644 --- a/pkg/apis/troubleshoot/v1beta2/collector_shared.go +++ b/pkg/apis/troubleshoot/v1beta2/collector_shared.go @@ -34,6 +34,7 @@ type LogLimits struct { MaxAge string `json:"maxAge,omitempty" yaml:"maxAge,omitempty"` MaxLines int64 `json:"maxLines,omitempty" yaml:"maxLines,omitempty"` SinceTime string + Since string } type Logs struct { diff --git a/pkg/collect/logs.go b/pkg/collect/logs.go index 1dd52bdd5..fa45f171c 100644 --- a/pkg/collect/logs.go +++ b/pkg/collect/logs.go @@ -120,7 +120,6 @@ func getPodLogs(ctx context.Context, client *kubernetes.Clientset, pod corev1.Po if limits != nil && limits.SinceTime != "" { t, err := time.Parse(time.RFC3339, limits.SinceTime) if err != nil { - //should this return an error? logger.Printf("unable to parse --since-time=%s\n", limits.SinceTime) } @@ -128,6 +127,9 @@ func getPodLogs(ctx context.Context, client *kubernetes.Clientset, pod corev1.Po podLogOpts.SinceTime = &sinceTime } else if limits != nil && limits.MaxAge != "" { + if limits.Since != "" { + limits.MaxAge = limits.Since + } parsedDuration, err := time.ParseDuration(limits.MaxAge) if err != nil { logger.Printf("unable to parse time duration %s\n", limits.MaxAge)