Get logs from Pods using Kubernetes API for function log command (#2623)
* add controller enablement flag * throw an error if service not found * add logs from Kubernetes in function log command * pass context in function param * add pod-namespace in function log command * pass context in function param * search for the pod in fn ns in the test
This commit is contained in:
@@ -133,7 +133,7 @@ func Commands() *cobra.Command {
|
||||
Required: []flag.Flag{flag.FnName},
|
||||
Optional: []flag.Flag{
|
||||
flag.FnLogFollow, flag.FnLogReverseQuery, flag.FnLogCount,
|
||||
flag.FnLogDetail, flag.FnLogPod, flag.NamespaceFunction, flag.FnLogDBType},
|
||||
flag.FnLogDetail, flag.FnLogPod, flag.NamespaceFunction, flag.FnLogDBType, flag.NamespacePod},
|
||||
})
|
||||
|
||||
testCmd := &cobra.Command{
|
||||
|
||||
@@ -19,6 +19,8 @@ package function
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
@@ -28,7 +30,6 @@ import (
|
||||
"github.com/fission/fission/pkg/fission-cli/cmd"
|
||||
flagkey "github.com/fission/fission/pkg/fission-cli/flag/key"
|
||||
"github.com/fission/fission/pkg/fission-cli/logdb"
|
||||
"github.com/fission/fission/pkg/fission-cli/util"
|
||||
)
|
||||
|
||||
type LogSubCommand struct {
|
||||
@@ -60,13 +61,12 @@ func (opts *LogSubCommand) do(input cli.Input) error {
|
||||
return errors.Wrap(err, "error getting function")
|
||||
}
|
||||
|
||||
server, err := util.GetApplicationUrl(input.Context(), opts.Client(), "application=fission-api")
|
||||
if err != nil {
|
||||
return err
|
||||
logDBOptions := logdb.LogDBOptions{
|
||||
Client: opts.Client(),
|
||||
}
|
||||
|
||||
// request the controller to establish a proxy server to the database.
|
||||
logDB, err := logdb.GetLogDB(dbType, server)
|
||||
logDB, err := logdb.GetLogDB(dbType, input.Context(), logDBOptions)
|
||||
if err != nil {
|
||||
return errors.Wrapf(err, "failed to get log database")
|
||||
}
|
||||
@@ -77,31 +77,36 @@ func (opts *LogSubCommand) do(input cli.Input) error {
|
||||
|
||||
go func(ctx context.Context, requestChan, responseChan chan struct{}) {
|
||||
t := time.Unix(0, 0*int64(time.Millisecond))
|
||||
detail := input.Bool(flagkey.FnLogDetail)
|
||||
for {
|
||||
select {
|
||||
case <-requestChan:
|
||||
logFilter := logdb.LogFilter{
|
||||
Pod: fnPod,
|
||||
Function: f.ObjectMeta.Name,
|
||||
FuncUid: string(f.ObjectMeta.UID),
|
||||
Since: t,
|
||||
Reverse: logReverseQuery,
|
||||
RecordLimit: recordLimit,
|
||||
Pod: fnPod,
|
||||
PodNamespace: input.String(flagkey.NamespacePod),
|
||||
Function: f.ObjectMeta.Name,
|
||||
FuncUid: string(f.ObjectMeta.UID),
|
||||
Since: t,
|
||||
Reverse: logReverseQuery,
|
||||
RecordLimit: recordLimit,
|
||||
FunctionObject: f,
|
||||
Details: detail,
|
||||
}
|
||||
logEntries, err := logDB.GetLogs(logFilter)
|
||||
|
||||
buf, err := logDB.GetLogs(ctx, logFilter)
|
||||
if err != nil {
|
||||
fmt.Printf("Error querying logs: %v", err)
|
||||
responseChan <- struct{}{}
|
||||
return
|
||||
}
|
||||
for _, logEntry := range logEntries {
|
||||
if input.Bool(flagkey.FnLogDetail) {
|
||||
fmt.Printf("Timestamp: %s\nNamespace: %s\nFunction Name: %s\nFunction ID: %s\nPod: %s\nContainer: %s\nStream: %s\nLog: %s\n---\n",
|
||||
logEntry.Timestamp, logEntry.Namespace, logEntry.FuncName, logEntry.FuncUid, logEntry.Pod, logEntry.Container, logEntry.Stream, logEntry.Message)
|
||||
} else {
|
||||
fmt.Printf("[%s] %s\n", logEntry.Timestamp, logEntry.Message)
|
||||
}
|
||||
t = logEntry.Timestamp
|
||||
_, err = io.Copy(os.Stdout, buf)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
t = time.Now().UTC() // next time fetch values from this time
|
||||
if dbType == logdb.KUBERNETES { //in case of Kubernetes log we print pods info only once. And then print new logs
|
||||
detail = false
|
||||
}
|
||||
responseChan <- struct{}{}
|
||||
case <-ctx.Done():
|
||||
|
||||
Reference in New Issue
Block a user