Skip to content

Commit

Permalink
Set get logs since duration (#77)
Browse files Browse the repository at this point in the history
Co-authored-by: Richard Hagen <richard.hagen@bouvet.no>
  • Loading branch information
Richard87 and Richard87 authored Nov 20, 2023
1 parent f691481 commit b18133a
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
5 changes: 4 additions & 1 deletion cmd/logsEnvironment.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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")
Expand All @@ -64,7 +66,7 @@ Example:
return err
}

return logForComponentReplicas(cmd, apiClient, *appName, environmentName, componentReplicas, previousLog)
return logForComponentReplicas(cmd, apiClient, *appName, environmentName, since, componentReplicas, previousLog)
},
}

Expand Down Expand Up @@ -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)
}
12 changes: 7 additions & 5 deletions cmd/logsEnvironmentComponent.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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
Expand All @@ -97,16 +98,16 @@ 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)
logParameters.WithDeploymentName("irrelevant")
logParameters.WithComponentName(componentName)
logParameters.WithPodName(replica)
if !previousLog {
logParameters.SetSinceTime(&since)
logParameters.SetSinceTime(&sinceDt)
}
logParameters.WithPrevious(&previous)
logData, err := apiClient.Component.Log(logParameters, nil)
Expand Down Expand Up @@ -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)
}

0 comments on commit b18133a

Please sign in to comment.