Skip to content

Commit

Permalink
feat: get pod logs (argoproj#5311)
Browse files Browse the repository at this point in the history
* feat: get pod logs sequential

Signed-off-by: May Zhang <[email protected]>

* feat: check in the md file

Signed-off-by: May Zhang <[email protected]>

* feat: get pod logs

Signed-off-by: May Zhang <[email protected]>

* feat: fix conflicts

Signed-off-by: May Zhang <[email protected]>

* feat: fix lint error

Signed-off-by: May Zhang <[email protected]>

* feat: added timeout for test

Signed-off-by: May Zhang <[email protected]>

* feat: update doc

Signed-off-by: May Zhang <[email protected]>

* feat: update test

Signed-off-by: May Zhang <[email protected]>

* feat: add unit test

Signed-off-by: May Zhang <[email protected]>

* feat: add unit test

Signed-off-by: May Zhang <[email protected]>

* feat: fix merge conflict

Signed-off-by: May Zhang <[email protected]>

* feat: add e2e test

Signed-off-by: May Zhang <[email protected]>

* feat: clone query

Signed-off-by: May Zhang <[email protected]>

* feat: fix lint error

Signed-off-by: May Zhang <[email protected]>

* feat: rename tail-lines to tail

Signed-off-by: May Zhang <[email protected]>

* feat: fix when to send last message status

Signed-off-by: May Zhang <[email protected]>

* feat: fix lint error

Signed-off-by: May Zhang <[email protected]>

* feat: fix lint error

Signed-off-by: May Zhang <[email protected]>

* feat: retry on the client side

Signed-off-by: May Zhang <[email protected]>

* feat: fix lint error

Signed-off-by: May Zhang <[email protected]>

* feat: fix lint error

Signed-off-by: May Zhang <[email protected]>

* feat: fix lint error

Signed-off-by: May Zhang <[email protected]>

* feat: fix lint error

Signed-off-by: May Zhang <[email protected]>

* feat: fix lint error

Signed-off-by: May Zhang <[email protected]>

* feat: fix lint error

Signed-off-by: May Zhang <[email protected]>

* feat: if --follow, keep retry

Signed-off-by: May Zhang <[email protected]>

* feat: added two more flags for CLI

Signed-off-by: May Zhang <[email protected]>

* feat: added two more flags for CLI

Signed-off-by: May Zhang <[email protected]>

* feat: added two more flags for CLI

Signed-off-by: May Zhang <[email protected]>

* feat: added two more flags for CLI

Signed-off-by: May Zhang <[email protected]>

* feat: error return when there are more than 10 pods to render.

Signed-off-by: May Zhang <[email protected]>

* feat: if podname is present, use the same flow as if query by resource kind

Signed-off-by: May Zhang <[email protected]>
  • Loading branch information
mayzhang2000 authored Feb 8, 2021
1 parent e4165d0 commit ae3de24
Show file tree
Hide file tree
Showing 12 changed files with 755 additions and 170 deletions.
15 changes: 15 additions & 0 deletions assets/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -789,6 +789,21 @@
"type": "string",
"name": "filter",
"in": "query"
},
{
"type": "string",
"name": "kind",
"in": "query"
},
{
"type": "string",
"name": "group",
"in": "query"
},
{
"type": "string",
"name": "resourceName",
"in": "query"
}
],
"responses": {
Expand Down
87 changes: 87 additions & 0 deletions cmd/argocd/commands/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import (
"github.com/mattn/go-isatty"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/types"
Expand Down Expand Up @@ -93,6 +95,7 @@ func NewApplicationCommand(clientOpts *argocdclient.ClientOptions) *cobra.Comman
command.AddCommand(NewApplicationPatchResourceCommand(clientOpts))
command.AddCommand(NewApplicationResourceActionsCommand(clientOpts))
command.AddCommand(NewApplicationListResourcesCommand(clientOpts))
command.AddCommand(NewApplicationLogsCommand(clientOpts))
return command
}

Expand Down Expand Up @@ -261,6 +264,90 @@ func NewApplicationGetCommand(clientOpts *argocdclient.ClientOptions) *cobra.Com
return command
}

// NewApplicationLogsCommand returns logs of application pods
func NewApplicationLogsCommand(clientOpts *argocdclient.ClientOptions) *cobra.Command {
var (
group string
kind string
namespace string
resourceName string
follow bool
tail int64
sinceSeconds int64
untilTime string
filter string
)
var command = &cobra.Command{
Use: "logs APPNAME",
Short: "Get logs of application pods",
Run: func(c *cobra.Command, args []string) {
if len(args) == 0 {
c.HelpFunc()(c, args)
os.Exit(1)
}
acdClient := argocdclient.NewClientOrDie(clientOpts)
conn, appIf := acdClient.NewApplicationClientOrDie()
defer argoio.Close(conn)
appName := args[0]

retry := true
for retry {
retry = false
stream, err := appIf.PodLogs(context.Background(), &applicationpkg.ApplicationPodLogsQuery{
Name: &appName,
Group: &group,
Namespace: namespace,
Kind: &kind,
ResourceName: &resourceName,
Follow: follow,
TailLines: tail,
SinceSeconds: sinceSeconds,
UntilTime: &untilTime,
Filter: &filter,
})
if err != nil {
log.Fatalf("failed to get pod logs: %v", err)
}
for {
msg, err := stream.Recv()
if err == io.EOF {
return
}
if err != nil {
st, ok := status.FromError(err)
if !ok {
log.Fatalf("stream read failed: %v", err)
}
if st.Code() == codes.Unavailable && follow {
retry = true
sinceSeconds = 1
break
}
log.Fatalf("stream read failed: %v", err)
}
if !msg.Last {
fmt.Println(msg.Content)
} else {
return
}
} //Done with receive message
} //Done with retry
},
}

command.Flags().StringVar(&group, "group", "", "Resource group")
command.Flags().StringVar(&kind, "kind", "", "Resource kind")
command.Flags().StringVar(&namespace, "namespace", "", "Resource namespace")
command.Flags().StringVar(&resourceName, "name", "", "Resource name")
command.Flags().BoolVar(&follow, "follow", false, "Specify if the logs should be streamed")
command.Flags().Int64Var(&tail, "tail", 0, "The number of lines from the end of the logs to show")
command.Flags().Int64Var(&sinceSeconds, "since-seconds", 0, "A relative time in seconds before the current time from which to show logs")
command.Flags().StringVar(&untilTime, "until-time", "", "Show logs until this time")
command.Flags().StringVar(&filter, "filter", "", "Show logs contain this string")

return command
}

func printAppSummaryTable(app *argoappv1.Application, appURL string, windows *argoappv1.SyncWindows) {
fmt.Printf(printOpFmtStr, "Name:", app.Name)
fmt.Printf(printOpFmtStr, "Project:", app.Spec.GetProject())
Expand Down
1 change: 1 addition & 0 deletions docs/user-guide/commands/argocd_app.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ argocd app [flags]
* [argocd app get](argocd_app_get.md) - Get application details
* [argocd app history](argocd_app_history.md) - Show application deployment history
* [argocd app list](argocd_app_list.md) - List applications
* [argocd app logs](argocd_app_logs.md) - Get logs of application pods
* [argocd app manifests](argocd_app_manifests.md) - Print manifests of an application
* [argocd app patch](argocd_app_patch.md) - Patch application
* [argocd app patch-resource](argocd_app_patch-resource.md) - Patch resource in an application
Expand Down
47 changes: 47 additions & 0 deletions docs/user-guide/commands/argocd_app_logs.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
## argocd app logs

Get logs of application pods

```
argocd app logs APPNAME [flags]
```

### Options

```
--filter string Show logs contain this string
--follow Specify if the logs should be streamed
--group string Resource group
-h, --help help for logs
--kind string Resource kind
--name string Resource name
--namespace string Resource namespace
--since-seconds int A relative time in seconds before the current time from which to show logs
--tail int The number of lines from the end of the logs to show
--until-time string Show logs until this time
```

### Options inherited from parent commands

```
--auth-token string Authentication token
--client-crt string Client certificate file
--client-crt-key string Client certificate key file
--config string Path to Argo CD config (default "/home/user/.argocd/config")
--grpc-web Enables gRPC-web protocol. Useful if Argo CD server is behind proxy which does not support HTTP2.
--grpc-web-root-path string Enables gRPC-web protocol. Useful if Argo CD server is behind proxy which does not support HTTP2. Set web root.
-H, --header strings Sets additional header to all requests made by Argo CD CLI. (Can be repeated multiple times to add multiple headers, also supports comma separated headers)
--insecure Skip server certificate and domain verification
--logformat string Set the logging format. One of: text|json (default "text")
--loglevel string Set the logging level. One of: debug|info|warn|error (default "info")
--plaintext Disable TLS
--port-forward Connect to a random argocd-server port using port forwarding
--port-forward-namespace string Namespace name which should be used for port forwarding
--server string Argo CD server address
--server-crt string Server certificate file
```

### SEE ALSO

* [argocd app](argocd_app.md) - Manage applications

Loading

0 comments on commit ae3de24

Please sign in to comment.