Skip to content

Commit

Permalink
Merge pull request #205 from Random-Liu/improve-log-support
Browse files Browse the repository at this point in the history
Improve log support
  • Loading branch information
feiskyer authored Nov 29, 2017
2 parents f2fa9a1 + 2f0a36a commit 80a7c13
Show file tree
Hide file tree
Showing 6 changed files with 217 additions and 3 deletions.
2 changes: 1 addition & 1 deletion cmd/crictl/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -519,7 +519,7 @@ func ListContainers(client pb.RuntimeServiceClient, opts listOptions) error {
filter.PodSandboxId = opts.podID
}
st := &pb.ContainerStateValue{}
if !opts.all {
if !opts.all && opts.state == "" {
st.State = pb.ContainerState_CONTAINER_RUNNING
filter.State = st
}
Expand Down
38 changes: 37 additions & 1 deletion cmd/crictl/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@ import (
"os"
"time"

timetypes "github.com/docker/docker/api/types/time"
"github.com/urfave/cli"
"k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/kubernetes/pkg/kubelet/kuberuntime/logs"
)

Expand All @@ -36,7 +38,7 @@ var logsCommand = cli.Command{
Usage: "Follow log output",
},
cli.Int64Flag{
Name: "tail, t",
Name: "tail",
Value: -1,
Usage: "Number of lines to show from the end of the logs. Defaults to all",
},
Expand All @@ -45,6 +47,15 @@ var logsCommand = cli.Command{
Value: -1,
Usage: "Maximum bytes of logs to return. Defaults to no limit",
},
cli.StringFlag{
Name: "since",
Value: "",
Usage: "Show logs since timestamp (e.g. 2013-01-02T13:23:37) or relative (e.g. 42m for 42 minutes)",
},
cli.BoolFlag{
Name: "timestamps, t",
Usage: "Show timestamps",
},
},
Action: func(context *cli.Context) error {
runtimeService, err := getRuntimeService(context)
Expand All @@ -58,10 +69,17 @@ var logsCommand = cli.Command{
}
tailLines := context.Int64("tail")
limitBytes := context.Int64("limit-bytes")
since, err := parseTimestamp(context.String("since"))
if err != nil {
return err
}
timestamp := context.Bool("timestamps")
logOptions := logs.NewLogOptions(&v1.PodLogOptions{
Follow: context.Bool("follow"),
TailLines: &tailLines,
LimitBytes: &limitBytes,
SinceTime: since,
Timestamps: timestamp,
}, time.Now())
status, err := runtimeService.ContainerStatus(containerID)
if err != nil {
Expand All @@ -75,3 +93,21 @@ var logsCommand = cli.Command{
},
After: closeConnection,
}

// parseTimestamp parses timestamp string as golang duration,
// then RFC3339 time and finally as a Unix timestamp.
func parseTimestamp(value string) (*metav1.Time, error) {
if value == "" {
return nil, nil
}
str, err := timetypes.GetTimestamp(value, time.Now())
if err != nil {
return nil, err
}
s, ns, err := timetypes.ParseTimestamps(str, 0)
if err != nil {
return nil, err
}
t := metav1.NewTime(time.Unix(s, ns))
return &t, nil
}
2 changes: 1 addition & 1 deletion vendor.conf
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ github.com/juju/ratelimit 5b9ff866471762aa2ab2dced63c9fb6f53921342
github.com/mailru/easyjson d5b7844b561a7bc640052f1b935f7b800330d7e0
github.com/onsi/ginkgo 67b9df7f55fe1165fd9ad49aca7754cce01a42b8
github.com/onsi/gomega d59fa0ac68bb5dd932ee8d24eed631cdd519efc3
github.com/opencontainers/selinux b29023b86e4a69d1b46b7e7b4e2b6fda03f0b9cd
github.com/pborman/uuid ca53cad383cad2479bbba7f7a1a05797ec1386e4
github.com/peterbourgon/diskv 5f041e8faa004a95c88a202771f4cc3e991971e6
github.com/PuerkitoBio/purell v1.0.0
Expand All @@ -41,4 +42,3 @@ k8s.io/client-go 72e1c2a1ef30b3f8da039e92d4a6a1f079f374e8
k8s.io/kube-openapi 39a7bf85c140f972372c2a0d1ee40adbf0c8bfe1
k8s.io/kubernetes 164317879bcd810b97e5ebf1c8df041770f2ff1b
k8s.io/utils bf963466fd3fea33c428098b12a89d8ecd012f2
github.com/opencontainers/selinux b29023b86e4a69d1b46b7e7b4e2b6fda03f0b9cd
42 changes: 42 additions & 0 deletions vendor/github.com/docker/docker/api/README.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions vendor/github.com/docker/docker/api/types/time/duration_convert.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

124 changes: 124 additions & 0 deletions vendor/github.com/docker/docker/api/types/time/timestamp.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 80a7c13

Please sign in to comment.