From b18133a1d7b127b3d1d7a5270db6c8adff69dadc Mon Sep 17 00:00:00 2001 From: Richard Hagen Date: Mon, 20 Nov 2023 16:00:04 +0100 Subject: [PATCH] Set get logs since duration (#77) Co-authored-by: Richard Hagen --- cmd/logsEnvironment.go | 5 ++++- cmd/logsEnvironmentComponent.go | 12 +++++++----- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/cmd/logsEnvironment.go b/cmd/logsEnvironment.go index 96e6f60..9ea5dc2 100644 --- a/cmd/logsEnvironment.go +++ b/cmd/logsEnvironment.go @@ -20,6 +20,7 @@ import ( apiclient "github.com/equinor/radix-cli/generated-client/client" "github.com/equinor/radix-cli/generated-client/client/environment" "github.com/equinor/radix-cli/pkg/client" + "github.com/equinor/radix-cli/pkg/settings" "github.com/spf13/cobra" ) @@ -47,6 +48,7 @@ Example: environmentName, _ := cmd.Flags().GetString("environment") previousLog, _ := cmd.Flags().GetBool("previous") + since, _ := cmd.Flags().GetDuration("since") if environmentName == "" { return errors.New("both `environment` and `component` are required") @@ -64,7 +66,7 @@ Example: return err } - return logForComponentReplicas(cmd, apiClient, *appName, environmentName, componentReplicas, previousLog) + return logForComponentReplicas(cmd, apiClient, *appName, environmentName, since, componentReplicas, previousLog) }, } @@ -98,5 +100,6 @@ func init() { logsEnvironmentCmd.Flags().StringP("application", "a", "", "Name of the application owning the component") logsEnvironmentCmd.Flags().StringP("environment", "e", "", "Environment the component runs in") logsEnvironmentCmd.Flags().BoolP("previous", "p", false, "If set, print the logs for the previous instances of containers in environment component pods, if they exist") + logsEnvironmentCmd.Flags().DurationP("since", "s", settings.DeltaRefreshApplication, "If set, start get logs from the specified time, eg. 5m or 12h") setContextSpecificPersistentFlags(logsEnvironmentCmd) } diff --git a/cmd/logsEnvironmentComponent.go b/cmd/logsEnvironmentComponent.go index 4a3ca53..9dd94f9 100644 --- a/cmd/logsEnvironmentComponent.go +++ b/cmd/logsEnvironmentComponent.go @@ -61,6 +61,7 @@ Examples: environmentName, _ := cmd.Flags().GetString("environment") componentName, _ := cmd.Flags().GetString("component") previousLog, _ := cmd.Flags().GetBool("previous") + since, _ := cmd.Flags().GetDuration("since") if environmentName == "" || componentName == "" { return errors.New("both `environment` and `component` are required") @@ -81,11 +82,11 @@ Examples: componentReplicas := make(map[string][]string) componentReplicas[componentName] = replicas - return logForComponentReplicas(cmd, apiClient, *appName, environmentName, componentReplicas, previousLog) + return logForComponentReplicas(cmd, apiClient, *appName, environmentName, since, componentReplicas, previousLog) }, } -func logForComponentReplicas(cmd *cobra.Command, apiClient *apiclient.Radixapi, appName, environmentName string, componentReplicas map[string][]string, previousLog bool) error { +func logForComponentReplicas(cmd *cobra.Command, apiClient *apiclient.Radixapi, appName, environmentName string, since time.Duration, componentReplicas map[string][]string, previousLog bool) error { refreshLog := time.Tick(settings.DeltaRefreshApplication) // Sometimes, even though we get delta, the log is the same as previous @@ -97,8 +98,8 @@ func logForComponentReplicas(cmd *cobra.Command, apiClient *apiclient.Radixapi, for componentName, replicas := range componentReplicas { for _, replica := range replicas { now := time.Now() - sinceTime := now.Add(-settings.DeltaRefreshApplication) - since := strfmt.DateTime(sinceTime) + sinceTime := now.Add(-since) + sinceDt := strfmt.DateTime(sinceTime) logParameters := component.NewLogParams() logParameters.WithAppName(appName) @@ -106,7 +107,7 @@ func logForComponentReplicas(cmd *cobra.Command, apiClient *apiclient.Radixapi, logParameters.WithComponentName(componentName) logParameters.WithPodName(replica) if !previousLog { - logParameters.SetSinceTime(&since) + logParameters.SetSinceTime(&sinceDt) } logParameters.WithPrevious(&previous) logData, err := apiClient.Component.Log(logParameters, nil) @@ -173,5 +174,6 @@ func init() { logsEnvironmentComponentCmd.Flags().StringP("environment", "e", "", "Environment the component runs in") logsEnvironmentComponentCmd.Flags().String("component", "", "The component to follow") logsEnvironmentComponentCmd.Flags().BoolP("previous", "p", false, "If set, print the logs for the previous instance of the container in a component pod, if it exists") + logsEnvironmentComponentCmd.Flags().DurationP("since", "s", settings.DeltaRefreshApplication, "If set, start get logs from the specified time, eg. 5m or 12h") setContextSpecificPersistentFlags(logsEnvironmentComponentCmd) }