docker_legacy_service.go 4.5 KB

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