Commit 2fc2e714 authored by Yu-Ju Hong's avatar Yu-Ju Hong

Fake container exec/logs support for in-process docker CRI integration

This is a temporary hack to bypass CRI when getting container logs or running exec in a container. This is necessary to unblock testing and adding other features in the integration.
parent c43a106b
...@@ -25,6 +25,7 @@ import ( ...@@ -25,6 +25,7 @@ import (
runtimeApi "k8s.io/kubernetes/pkg/kubelet/api/v1alpha1/runtime" runtimeApi "k8s.io/kubernetes/pkg/kubelet/api/v1alpha1/runtime"
kubecontainer "k8s.io/kubernetes/pkg/kubelet/container" kubecontainer "k8s.io/kubernetes/pkg/kubelet/container"
"k8s.io/kubernetes/pkg/kubelet/dockertools" "k8s.io/kubernetes/pkg/kubelet/dockertools"
"k8s.io/kubernetes/pkg/util/term"
) )
const ( const (
...@@ -69,6 +70,9 @@ type DockerLegacyService interface { ...@@ -69,6 +70,9 @@ type DockerLegacyService interface {
GetContainerLogs(pod *api.Pod, containerID kubecontainer.ContainerID, logOptions *api.PodLogOptions, stdout, stderr io.Writer) (err error) GetContainerLogs(pod *api.Pod, containerID kubecontainer.ContainerID, logOptions *api.PodLogOptions, stdout, stderr io.Writer) (err error)
kubecontainer.ContainerAttacher kubecontainer.ContainerAttacher
PortForward(pod *kubecontainer.Pod, port uint16, stream io.ReadWriteCloser) error PortForward(pod *kubecontainer.Pod, port uint16, stream io.ReadWriteCloser) error
// TODO: Remove this once exec is properly defined in CRI.
ExecInContainer(containerID kubecontainer.ContainerID, cmd []string, stdin io.Reader, stdout, stderr io.WriteCloser, tty bool, resize <-chan term.Size) error
} }
type dockerService struct { type dockerService struct {
......
...@@ -42,3 +42,16 @@ func (ds *dockerService) GetContainerLogs(pod *api.Pod, containerID kubecontaine ...@@ -42,3 +42,16 @@ func (ds *dockerService) GetContainerLogs(pod *api.Pod, containerID kubecontaine
func (ds *dockerService) PortForward(pod *kubecontainer.Pod, port uint16, stream io.ReadWriteCloser) error { func (ds *dockerService) PortForward(pod *kubecontainer.Pod, port uint16, stream io.ReadWriteCloser) error {
return fmt.Errorf("not implemented") return fmt.Errorf("not implemented")
} }
func (ds *dockerService) ExecInContainer(containerID kubecontainer.ContainerID, cmd []string, stdin io.Reader, stdout, stderr io.WriteCloser, tty bool, resize <-chan term.Size) error {
container, err := ds.client.InspectContainer(containerID.ID)
if err != nil {
return err
}
if !container.State.Running {
return fmt.Errorf("container not running (%s)", container.ID)
}
handler := &dockertools.NativeExecHandler{}
return handler.ExecInContainer(ds.client, container, cmd, stdin, stdout, stderr, tty, resize)
}
...@@ -32,6 +32,7 @@ import ( ...@@ -32,6 +32,7 @@ import (
"k8s.io/kubernetes/pkg/api/unversioned" "k8s.io/kubernetes/pkg/api/unversioned"
runtimeApi "k8s.io/kubernetes/pkg/kubelet/api/v1alpha1/runtime" runtimeApi "k8s.io/kubernetes/pkg/kubelet/api/v1alpha1/runtime"
kubecontainer "k8s.io/kubernetes/pkg/kubelet/container" kubecontainer "k8s.io/kubernetes/pkg/kubelet/container"
"k8s.io/kubernetes/pkg/kubelet/dockershim"
"k8s.io/kubernetes/pkg/kubelet/events" "k8s.io/kubernetes/pkg/kubelet/events"
"k8s.io/kubernetes/pkg/kubelet/util/format" "k8s.io/kubernetes/pkg/kubelet/util/format"
"k8s.io/kubernetes/pkg/types" "k8s.io/kubernetes/pkg/types"
...@@ -513,6 +514,13 @@ func (m *kubeGenericRuntimeManager) AttachContainer(id kubecontainer.ContainerID ...@@ -513,6 +514,13 @@ func (m *kubeGenericRuntimeManager) AttachContainer(id kubecontainer.ContainerID
// GetContainerLogs returns logs of a specific container. // GetContainerLogs returns logs of a specific container.
func (m *kubeGenericRuntimeManager) GetContainerLogs(pod *api.Pod, containerID kubecontainer.ContainerID, logOptions *api.PodLogOptions, stdout, stderr io.Writer) (err error) { func (m *kubeGenericRuntimeManager) GetContainerLogs(pod *api.Pod, containerID kubecontainer.ContainerID, logOptions *api.PodLogOptions, stdout, stderr io.Writer) (err error) {
// Get logs directly from docker for in-process docker integration for
// now to unblock other tests.
// TODO: remove this hack after setting down on how to implement log
// retrieval/management.
if ds, ok := m.runtimeService.(dockershim.DockerLegacyService); ok {
return ds.GetContainerLogs(pod, containerID, logOptions, stdout, stderr)
}
return fmt.Errorf("not implemented") return fmt.Errorf("not implemented")
} }
...@@ -521,6 +529,12 @@ func (m *kubeGenericRuntimeManager) GetContainerLogs(pod *api.Pod, containerID k ...@@ -521,6 +529,12 @@ func (m *kubeGenericRuntimeManager) GetContainerLogs(pod *api.Pod, containerID k
// tty. // tty.
// TODO: handle terminal resizing, refer https://github.com/kubernetes/kubernetes/issues/29579 // TODO: handle terminal resizing, refer https://github.com/kubernetes/kubernetes/issues/29579
func (m *kubeGenericRuntimeManager) ExecInContainer(containerID kubecontainer.ContainerID, cmd []string, stdin io.Reader, stdout, stderr io.WriteCloser, tty bool, resize <-chan term.Size) error { func (m *kubeGenericRuntimeManager) ExecInContainer(containerID kubecontainer.ContainerID, cmd []string, stdin io.Reader, stdout, stderr io.WriteCloser, tty bool, resize <-chan term.Size) error {
// Use `docker exec` directly for in-process docker integration for
// now to unblock other tests.
// TODO: remove this hack after exec is defined in CRI.
if ds, ok := m.runtimeService.(dockershim.DockerLegacyService); ok {
return ds.ExecInContainer(containerID, cmd, stdin, stdout, stderr, tty, resize)
}
return fmt.Errorf("not implemented") return fmt.Errorf("not implemented")
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment