docker_legacy_service.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*
  2. Copyright 2016 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 dockershim
  14. import (
  15. "context"
  16. "fmt"
  17. "io"
  18. "strconv"
  19. "time"
  20. "github.com/armon/circbuf"
  21. dockertypes "github.com/docker/docker/api/types"
  22. "k8s.io/api/core/v1"
  23. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  24. kubetypes "k8s.io/apimachinery/pkg/types"
  25. kubecontainer "k8s.io/kubernetes/pkg/kubelet/container"
  26. "k8s.io/kubernetes/pkg/kubelet/kuberuntime"
  27. "k8s.io/kubernetes/pkg/kubelet/dockershim/libdocker"
  28. )
  29. // DockerLegacyService interface embeds some legacy methods for backward compatibility.
  30. // This file/interface will be removed in the near future. Do not modify or add
  31. // more functions.
  32. type DockerLegacyService interface {
  33. // GetContainerLogs gets logs for a specific container.
  34. GetContainerLogs(context.Context, *v1.Pod, kubecontainer.ContainerID, *v1.PodLogOptions, io.Writer, io.Writer) error
  35. // IsCRISupportedLogDriver checks whether the logging driver used by docker is
  36. // supported by native CRI integration.
  37. // TODO(resouer): remove this when deprecating unsupported log driver
  38. IsCRISupportedLogDriver() (bool, error)
  39. kuberuntime.LegacyLogProvider
  40. }
  41. // GetContainerLogs get container logs directly from docker daemon.
  42. func (d *dockerService) GetContainerLogs(_ context.Context, pod *v1.Pod, containerID kubecontainer.ContainerID, logOptions *v1.PodLogOptions, stdout, stderr io.Writer) error {
  43. container, err := d.client.InspectContainer(containerID.ID)
  44. if err != nil {
  45. return err
  46. }
  47. var since int64
  48. if logOptions.SinceSeconds != nil {
  49. t := metav1.Now().Add(-time.Duration(*logOptions.SinceSeconds) * time.Second)
  50. since = t.Unix()
  51. }
  52. if logOptions.SinceTime != nil {
  53. since = logOptions.SinceTime.Unix()
  54. }
  55. opts := dockertypes.ContainerLogsOptions{
  56. ShowStdout: true,
  57. ShowStderr: true,
  58. Since: strconv.FormatInt(since, 10),
  59. Timestamps: logOptions.Timestamps,
  60. Follow: logOptions.Follow,
  61. }
  62. if logOptions.TailLines != nil {
  63. opts.Tail = strconv.FormatInt(*logOptions.TailLines, 10)
  64. }
  65. sopts := libdocker.StreamOptions{
  66. OutputStream: stdout,
  67. ErrorStream: stderr,
  68. RawTerminal: container.Config.Tty,
  69. }
  70. return d.client.Logs(containerID.ID, opts, sopts)
  71. }
  72. // GetContainerLogTail attempts to read up to MaxContainerTerminationMessageLogLength
  73. // from the end of the log when docker is configured with a log driver other than json-log.
  74. // It reads up to MaxContainerTerminationMessageLogLines lines.
  75. func (d *dockerService) GetContainerLogTail(uid kubetypes.UID, name, namespace string, containerId kubecontainer.ContainerID) (string, error) {
  76. value := int64(kubecontainer.MaxContainerTerminationMessageLogLines)
  77. buf, _ := circbuf.NewBuffer(kubecontainer.MaxContainerTerminationMessageLogLength)
  78. // Although this is not a full spec pod, dockerLegacyService.GetContainerLogs() currently completely ignores its pod param
  79. pod := &v1.Pod{
  80. ObjectMeta: metav1.ObjectMeta{
  81. UID: uid,
  82. Name: name,
  83. Namespace: namespace,
  84. },
  85. }
  86. err := d.GetContainerLogs(context.Background(), pod, containerId, &v1.PodLogOptions{TailLines: &value}, buf, buf)
  87. if err != nil {
  88. return "", err
  89. }
  90. return buf.String(), nil
  91. }
  92. // criSupportedLogDrivers are log drivers supported by native CRI integration.
  93. var criSupportedLogDrivers = []string{"json-file"}
  94. // IsCRISupportedLogDriver checks whether the logging driver used by docker is
  95. // supported by native CRI integration.
  96. func (d *dockerService) IsCRISupportedLogDriver() (bool, error) {
  97. info, err := d.client.Info()
  98. if err != nil {
  99. return false, fmt.Errorf("failed to get docker info: %v", err)
  100. }
  101. for _, driver := range criSupportedLogDrivers {
  102. if info.LoggingDriver == driver {
  103. return true, nil
  104. }
  105. }
  106. return false, nil
  107. }