log.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*
  2. Copyright 2015 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package rkt
  14. import (
  15. "fmt"
  16. "io"
  17. "strings"
  18. "time"
  19. "github.com/golang/glog"
  20. "golang.org/x/net/context"
  21. rktapi "github.com/coreos/rkt/api/v1alpha"
  22. "k8s.io/kubernetes/pkg/api"
  23. "k8s.io/kubernetes/pkg/api/unversioned"
  24. kubecontainer "k8s.io/kubernetes/pkg/kubelet/container"
  25. "k8s.io/kubernetes/pkg/kubelet/util/format"
  26. )
  27. const (
  28. journalTimestampLayout = "2006-01-02 15:04:05 -0700 MST"
  29. )
  30. // processLines write the lines into stdout in the required format.
  31. func processLines(lines []string, logOptions *api.PodLogOptions, stdout, stderr io.Writer) {
  32. msgKey := "MESSAGE="
  33. for _, line := range lines {
  34. msgStart := strings.Index(line, msgKey)
  35. if msgStart < 0 {
  36. glog.Warningf("rkt: Invalid log line %q, missing %q", line, msgKey)
  37. continue
  38. }
  39. tss := strings.TrimSpace(line[:msgStart])
  40. msg := strings.TrimPrefix(line[msgStart:], msgKey)
  41. t, err := time.Parse(journalTimestampLayout, tss)
  42. if err != nil {
  43. glog.Warningf("rkt: Failed to parse the timestamp %q: %v", tss, err)
  44. continue
  45. }
  46. var result string
  47. if logOptions.Timestamps {
  48. result = fmt.Sprintf("%s %s\n", t.Format(time.RFC3339), msg)
  49. } else {
  50. result = fmt.Sprintf("%s\n", msg)
  51. }
  52. if _, err := io.WriteString(stdout, result); err != nil {
  53. glog.Warningf("rkt: Cannot write log line %q to output: %v", result, err)
  54. }
  55. }
  56. }
  57. // GetContainerLogs uses rkt's GetLogs API to get the logs of the container.
  58. // By default, it returns a snapshot of the container log. Set |follow| to true to
  59. // stream the log. Set |follow| to false and specify the number of lines (e.g.
  60. // "100" or "all") to tail the log.
  61. //
  62. // TODO(yifan): This doesn't work with lkvm stage1 yet.
  63. func (r *Runtime) GetContainerLogs(pod *api.Pod, containerID kubecontainer.ContainerID, logOptions *api.PodLogOptions, stdout, stderr io.Writer) error {
  64. id, err := parseContainerID(containerID)
  65. if err != nil {
  66. return err
  67. }
  68. var since int64
  69. if logOptions.SinceSeconds != nil {
  70. t := unversioned.Now().Add(-time.Duration(*logOptions.SinceSeconds) * time.Second)
  71. since = t.Unix()
  72. }
  73. if logOptions.SinceTime != nil {
  74. since = logOptions.SinceTime.Unix()
  75. }
  76. getLogsRequest := &rktapi.GetLogsRequest{
  77. PodId: id.uuid,
  78. AppName: id.appName,
  79. Follow: logOptions.Follow,
  80. SinceTime: since,
  81. }
  82. if logOptions.TailLines != nil {
  83. getLogsRequest.Lines = int32(*logOptions.TailLines)
  84. }
  85. stream, err := r.apisvc.GetLogs(context.Background(), getLogsRequest)
  86. if err != nil {
  87. glog.Errorf("rkt: Failed to create log stream for pod %q: %v", format.Pod(pod), err)
  88. return err
  89. }
  90. for {
  91. log, err := stream.Recv()
  92. if err == io.EOF {
  93. break
  94. }
  95. if err != nil {
  96. glog.Errorf("rkt: Failed to receive log for pod %q: %v", format.Pod(pod), err)
  97. return err
  98. }
  99. processLines(log.Lines, logOptions, stdout, stderr)
  100. }
  101. return nil
  102. }