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

added flag --since-time for Logs Collectors #287

Merged
merged 17 commits into from
Oct 29, 2020
Merged
Show file tree
Hide file tree
Changes from 13 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
2 changes: 2 additions & 0 deletions cmd/preflight/cli/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ that a cluster meets the requirements to run an application.`,
cmd.Flags().String("collector-image", "", "the full name of the collector image to use")
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", "", "force pod logs collectors to return logs after a specific date (RFC3339)")
cmd.Flags().String("since", "", "force pod logs collectors to return logs newer than a relative duration like 5s, 2m, or 3h.")

viper.SetEnvKeyReplacer(strings.NewReplacer("-", "_"))

Expand Down
40 changes: 40 additions & 0 deletions cmd/preflight/cli/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"github.com/replicatedhq/troubleshoot/pkg/specs"
"github.com/spf13/viper"
spin "github.com/tj/go-spin"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes/scheme"
)

Expand Down Expand Up @@ -129,6 +130,13 @@ func runPreflights(v *viper.Viper, arg string) error {
KubernetesRestConfig: restConfig,
}

if v.GetString("since") != "" || v.GetString("since-time") != "" {
err := parseTimeFlags(v, progressChan, preflightSpec.Spec.Collectors)
if err != nil {
return err
}
}

collectResults, err := preflight.Collect(collectOpts, preflightSpec)
if err != nil {
if !collectResults.IsRBACAllowed {
Expand Down Expand Up @@ -161,3 +169,35 @@ func runPreflights(v *viper.Viper, arg string) error {

return showStdoutResults(v.GetString("format"), preflightSpec.Name, analyzeResults)
}

func parseTimeFlags(v *viper.Viper, progressChan chan interface{}, collectors []*troubleshootv1beta2.Collect) error {
var (
sinceTime time.Time
err 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.")
manavellamnimble marked this conversation as resolved.
Show resolved Hide resolved
}
sinceTime, err = time.Parse(time.RFC3339, v.GetString("since-time"))
if err != nil {
return errors.Errorf("unable to parse date and time %s as YYYY-MM-DDTHH:MM:SSZHH:MM, e.g.:\"2006-01-02T15:04:05Z07:00\"", v.GetString("since-time"))
}
} else {
parsedDuration, err := time.ParseDuration(v.GetString("since"))
if err != nil {
return errors.Errorf("unable to parse time duration %s", v.GetString("since"))
}
now := time.Now()
sinceTime = now.Add(0 - parsedDuration)
}
for _, collector := range collectors {
if collector.Logs != nil {
if collector.Logs.Limits == nil {
collector.Logs.Limits = new(troubleshootv1beta2.LogLimits)
}
collector.Logs.Limits.SinceTime = metav1.NewTime(sinceTime)
}
}
return nil
}
2 changes: 2 additions & 0 deletions cmd/troubleshoot/cli/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ from a server that can be used to assist when troubleshooting a Kubernetes clust
cmd.Flags().StringSlice("redactors", []string{}, "names of the additional redactors to use")
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", "", "force pod logs collectors to return logs after a specific date (RFC3339)")
cmd.Flags().String("since", "", "force pod 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
38 changes: 38 additions & 0 deletions cmd/troubleshoot/cli/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,12 @@ func runCollectors(v *viper.Viper, collectors []*troubleshootv1beta2.Collect, ad
if additionalRedactors != nil {
globalRedactors = additionalRedactors.Spec.Redactors
}
if v.GetString("since-time") != "" || v.GetString("since") != "" {
err := parseTimeFlags(v, progressChan, &cleanedCollectors)
if err != nil {
return "", err
}
}

// Run preflights collectors synchronously
for _, collector := range cleanedCollectors {
Expand Down Expand Up @@ -680,3 +686,35 @@ type CollectorFailure struct {
Collector *troubleshootv1beta2.Collect
Failure string
}

func parseTimeFlags(v *viper.Viper, progressChan chan interface{}, collectors *collect.Collectors) error {
var (
sinceTime time.Time
err 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.")
}
sinceTime, err = time.Parse(time.RFC3339, v.GetString("since-time"))
if err != nil {
return errors.Errorf("unable to parse date and time %s as YYYY-MM-DDTHH:MM:SSZHH:MM, e.g.:\"2006-01-02T15:04:05Z07:00\"", v.GetString("since-time"))
}
} else {
parsedDuration, err := time.ParseDuration(v.GetString("since"))
if err != nil {
return errors.Errorf("unable to parse time duration %s", v.GetString("since"))
}
now := time.Now()
sinceTime = now.Add(0 - parsedDuration)
}
for _, collector := range *collectors {
if collector.Collect.Logs != nil {
if collector.Collect.Logs.Limits == nil {
collector.Collect.Logs.Limits = new(troubleshootv1beta2.LogLimits)
}
collector.Collect.Logs.Limits.SinceTime = metav1.NewTime(sinceTime)
}
}
return nil
}
6 changes: 4 additions & 2 deletions pkg/apis/troubleshoot/v1beta2/collector_shared.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/replicatedhq/troubleshoot/pkg/multitype"
authorizationv1 "k8s.io/api/authorization/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

type CollectorMeta struct {
Expand All @@ -31,8 +32,9 @@ type Secret struct {
}

type LogLimits struct {
MaxAge string `json:"maxAge,omitempty" yaml:"maxAge,omitempty"`
MaxLines int64 `json:"maxLines,omitempty" yaml:"maxLines,omitempty"`
MaxAge string `json:"maxAge,omitempty" yaml:"maxAge,omitempty"`
MaxLines int64 `json:"maxLines,omitempty" yaml:"maxLines,omitempty"`
SinceTime metav1.Time
}

type Logs struct {
Expand Down
4 changes: 3 additions & 1 deletion pkg/collect/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,10 @@ func getPodLogs(ctx context.Context, client *kubernetes.Clientset, pod corev1.Po
} else {
podLogOpts.TailLines = &limits.MaxLines
}
if limits != nil && !limits.SinceTime.IsZero() {
podLogOpts.SinceTime = &limits.SinceTime

if limits != nil && limits.MaxAge != "" {
} else if limits != nil && limits.MaxAge != "" {
parsedDuration, err := time.ParseDuration(limits.MaxAge)
if err != nil {
logger.Printf("unable to parse time duration %s\n", limits.MaxAge)
Expand Down