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 7 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", "", "forces pod's logs collectors to return logs after a specific date (RFC3339)")
manavellamnimble marked this conversation as resolved.
Show resolved Hide resolved
cmd.Flags().String("since", "", "forces pod's logs collectors to return logs newer than a relative duration like 5s, 2m, or 3h.")
manavellamnimble marked this conversation as resolved.
Show resolved Hide resolved

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

Expand Down
23 changes: 23 additions & 0 deletions cmd/preflight/cli/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,29 @@ func runPreflights(v *viper.Viper, arg string) error {
KubernetesRestConfig: restConfig,
}

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.")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is that how Kubectl handles it? Or does it abort because of invalid args?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes I had my doubts about this too. K8s aborts with the message error: parsing time. I followed the error handling for the limits in the logs collector (communicating it through the progress channel), but it would make sense to abort because if someone used the flag and it can't be parsed maybe there would be no point in running through all the collectors and analyzers

}
for _, collector := range preflightSpec.Spec.Collectors {
if collector.Logs != nil {
if collector.Logs.Limits == nil {
collector.Logs.Limits = new(troubleshootv1beta2.LogLimits)
}
collector.Logs.Limits.SinceTime = v.GetString("since-time")
}
}
} else if v.GetString("since") != "" {
for _, collector := range preflightSpec.Spec.Collectors {
if collector.Logs != nil {
if collector.Logs.Limits == nil {
collector.Logs.Limits = new(troubleshootv1beta2.LogLimits)
}
collector.Logs.Limits.Since = v.GetString("since")
}
}
}

collectResults, err := preflight.Collect(collectOpts, preflightSpec)
if err != nil {
if !collectResults.IsRBACAllowed {
Expand Down
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", "", "forces pods logs collectors to return logs after a specific date (RFC3339)")
manavellamnimble marked this conversation as resolved.
Show resolved Hide resolved
cmd.Flags().String("since", "", "forces pod's logs collectors to return logs newer than a relative duration like 5s, 2m, or 3h.")
manavellamnimble marked this conversation as resolved.
Show resolved Hide resolved

// 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
23 changes: 23 additions & 0 deletions cmd/troubleshoot/cli/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,29 @@ func runCollectors(v *viper.Viper, collectors []*troubleshootv1beta2.Collect, ad
globalRedactors = additionalRedactors.Spec.Redactors
}

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 _, collector := range cleanedCollectors {
if collector.Collect.Logs != nil {
if collector.Collect.Logs.Limits == nil {
collector.Collect.Logs.Limits = new(troubleshootv1beta2.LogLimits)
}
collector.Collect.Logs.Limits.SinceTime = v.GetString("since-time")
}
}
} else if v.GetString("since") != "" {
for _, collector := range cleanedCollectors {
if collector.Collect.Logs != nil {
if collector.Collect.Logs.Limits == nil {
collector.Collect.Logs.Limits = new(troubleshootv1beta2.LogLimits)
}
collector.Collect.Logs.Limits.Since = v.GetString("since")
}
}
}

// Run preflights collectors synchronously
for _, collector := range cleanedCollectors {
if len(collector.RBACErrors) > 0 {
Expand Down
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 @@ -31,8 +31,10 @@ 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 string
sgalsaleh marked this conversation as resolved.
Show resolved Hide resolved
Since string
}

type Logs struct {
Expand Down
15 changes: 14 additions & 1 deletion pkg/collect/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,10 @@ func getPodLogs(ctx context.Context, client *kubernetes.Clientset, pod corev1.Po
podLogOpts.TailLines = &limits.MaxLines
}

if limits != nil && limits.MaxAge != "" {
if limits != nil && (limits.MaxAge != "" || limits.Since != "") {
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 All @@ -130,6 +133,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)
fmt.Println("time", t)
if err != nil {
logger.Printf("unable to parse --since-time=%s\n", limits.SinceTime)
} else {
sinceTime := metav1.NewTime(t)
podLogOpts.SinceTime = &sinceTime
}
}
fileKey := fmt.Sprintf("%s/%s", name, pod.Name)
if container != "" {
fileKey = fmt.Sprintf("%s/%s/%s", name, pod.Name, container)
Expand Down