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

Set get logs since duration #77

Merged
merged 1 commit into from
Nov 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}