Skip to content

Commit

Permalink
--since flag added
Browse files Browse the repository at this point in the history
  • Loading branch information
manavellamnimble committed Oct 21, 2020
1 parent e16eabd commit 7186b75
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 1 deletion.
1 change: 1 addition & 0 deletions cmd/preflight/cli/root.go
Expand Up @@ -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("-", "_"))

Expand Down
9 changes: 9 additions & 0 deletions cmd/preflight/cli/run.go
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions cmd/troubleshoot/cli/root.go
Expand Up @@ -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")
Expand Down
9 changes: 9 additions & 0 deletions cmd/troubleshoot/cli/run.go
Expand Up @@ -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")
Expand Down
1 change: 1 addition & 0 deletions pkg/apis/troubleshoot/v1beta2/collector_shared.go
Expand Up @@ -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 {
Expand Down
4 changes: 3 additions & 1 deletion pkg/collect/logs.go
Expand Up @@ -120,14 +120,16 @@ 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)
}

sinceTime := metav1.NewTime(t)
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)
Expand Down

0 comments on commit 7186b75

Please sign in to comment.