remote_runtime.go 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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 remote
  14. import (
  15. "io"
  16. "time"
  17. "fmt"
  18. "github.com/golang/glog"
  19. "google.golang.org/grpc"
  20. internalApi "k8s.io/kubernetes/pkg/kubelet/api"
  21. runtimeApi "k8s.io/kubernetes/pkg/kubelet/api/v1alpha1/runtime"
  22. )
  23. // RemoteRuntimeService is a gRPC implementation of internalApi.RuntimeService.
  24. type RemoteRuntimeService struct {
  25. timeout time.Duration
  26. runtimeClient runtimeApi.RuntimeServiceClient
  27. }
  28. // NewRemoteRuntimeService creates a new internalApi.RuntimeService.
  29. func NewRemoteRuntimeService(addr string, connectionTimout time.Duration) (internalApi.RuntimeService, error) {
  30. glog.V(3).Infof("Connecting to runtime service %s", addr)
  31. conn, err := grpc.Dial(addr, grpc.WithInsecure(), grpc.WithDialer(dial))
  32. if err != nil {
  33. glog.Errorf("Connect remote runtime %s failed: %v", addr, err)
  34. return nil, err
  35. }
  36. return &RemoteRuntimeService{
  37. timeout: connectionTimout,
  38. runtimeClient: runtimeApi.NewRuntimeServiceClient(conn),
  39. }, nil
  40. }
  41. // Version returns the runtime name, runtime version and runtime API version.
  42. func (r *RemoteRuntimeService) Version(apiVersion string) (*runtimeApi.VersionResponse, error) {
  43. ctx, cancel := getContextWithTimeout(r.timeout)
  44. defer cancel()
  45. typedVersion, err := r.runtimeClient.Version(ctx, &runtimeApi.VersionRequest{
  46. Version: &apiVersion,
  47. })
  48. if err != nil {
  49. glog.Errorf("Version from runtime service failed: %v", err)
  50. return nil, err
  51. }
  52. return typedVersion, err
  53. }
  54. // CreatePodSandbox creates a pod-level sandbox.
  55. func (r *RemoteRuntimeService) CreatePodSandbox(config *runtimeApi.PodSandboxConfig) (string, error) {
  56. ctx, cancel := getContextWithTimeout(r.timeout)
  57. defer cancel()
  58. resp, err := r.runtimeClient.CreatePodSandbox(ctx, &runtimeApi.CreatePodSandboxRequest{
  59. Config: config,
  60. })
  61. if err != nil {
  62. glog.Errorf("CreatePodSandbox from runtime service failed: %v", err)
  63. return "", err
  64. }
  65. return resp.GetPodSandboxId(), nil
  66. }
  67. // StopPodSandbox stops the sandbox. If there are any running containers in the
  68. // sandbox, they should be forced to termination.
  69. func (r *RemoteRuntimeService) StopPodSandbox(podSandBoxID string) error {
  70. ctx, cancel := getContextWithTimeout(r.timeout)
  71. defer cancel()
  72. _, err := r.runtimeClient.StopPodSandbox(ctx, &runtimeApi.StopPodSandboxRequest{
  73. PodSandboxId: &podSandBoxID,
  74. })
  75. if err != nil {
  76. glog.Errorf("StopPodSandbox %q from runtime service failed: %v", podSandBoxID, err)
  77. return err
  78. }
  79. return nil
  80. }
  81. // RemovePodSandbox removes the sandbox. If there are any containers in the
  82. // sandbox, they should be forcibly removed.
  83. func (r *RemoteRuntimeService) RemovePodSandbox(podSandBoxID string) error {
  84. ctx, cancel := getContextWithTimeout(r.timeout)
  85. defer cancel()
  86. _, err := r.runtimeClient.RemovePodSandbox(ctx, &runtimeApi.RemovePodSandboxRequest{
  87. PodSandboxId: &podSandBoxID,
  88. })
  89. if err != nil {
  90. glog.Errorf("RemovePodSandbox %q from runtime service failed: %v", podSandBoxID, err)
  91. return err
  92. }
  93. return nil
  94. }
  95. // PodSandboxStatus returns the status of the PodSandbox.
  96. func (r *RemoteRuntimeService) PodSandboxStatus(podSandBoxID string) (*runtimeApi.PodSandboxStatus, error) {
  97. ctx, cancel := getContextWithTimeout(r.timeout)
  98. defer cancel()
  99. resp, err := r.runtimeClient.PodSandboxStatus(ctx, &runtimeApi.PodSandboxStatusRequest{
  100. PodSandboxId: &podSandBoxID,
  101. })
  102. if err != nil {
  103. glog.Errorf("PodSandboxStatus %q from runtime service failed: %v", podSandBoxID, err)
  104. return nil, err
  105. }
  106. return resp.Status, nil
  107. }
  108. // ListPodSandbox returns a list of PodSandboxes.
  109. func (r *RemoteRuntimeService) ListPodSandbox(filter *runtimeApi.PodSandboxFilter) ([]*runtimeApi.PodSandbox, error) {
  110. ctx, cancel := getContextWithTimeout(r.timeout)
  111. defer cancel()
  112. resp, err := r.runtimeClient.ListPodSandbox(ctx, &runtimeApi.ListPodSandboxRequest{
  113. Filter: filter,
  114. })
  115. if err != nil {
  116. glog.Errorf("ListPodSandbox with filter %q from runtime service failed: %v", filter, err)
  117. return nil, err
  118. }
  119. return resp.Items, nil
  120. }
  121. // CreateContainer creates a new container in the specified PodSandbox.
  122. func (r *RemoteRuntimeService) CreateContainer(podSandBoxID string, config *runtimeApi.ContainerConfig, sandboxConfig *runtimeApi.PodSandboxConfig) (string, error) {
  123. ctx, cancel := getContextWithTimeout(r.timeout)
  124. defer cancel()
  125. resp, err := r.runtimeClient.CreateContainer(ctx, &runtimeApi.CreateContainerRequest{
  126. PodSandboxId: &podSandBoxID,
  127. Config: config,
  128. SandboxConfig: sandboxConfig,
  129. })
  130. if err != nil {
  131. glog.Errorf("CreateContainer in sandbox %q from runtime service failed: %v", podSandBoxID, err)
  132. return "", err
  133. }
  134. return resp.GetContainerId(), nil
  135. }
  136. // StartContainer starts the container.
  137. func (r *RemoteRuntimeService) StartContainer(containerID string) error {
  138. ctx, cancel := getContextWithTimeout(r.timeout)
  139. defer cancel()
  140. _, err := r.runtimeClient.StartContainer(ctx, &runtimeApi.StartContainerRequest{
  141. ContainerId: &containerID,
  142. })
  143. if err != nil {
  144. glog.Errorf("StartContainer %q from runtime service failed: %v", containerID, err)
  145. return err
  146. }
  147. return nil
  148. }
  149. // StopContainer stops a running container with a grace period (i.e., timeout).
  150. func (r *RemoteRuntimeService) StopContainer(containerID string, timeout int64) error {
  151. ctx, cancel := getContextWithTimeout(r.timeout)
  152. defer cancel()
  153. _, err := r.runtimeClient.StopContainer(ctx, &runtimeApi.StopContainerRequest{
  154. ContainerId: &containerID,
  155. Timeout: &timeout,
  156. })
  157. if err != nil {
  158. glog.Errorf("StopContainer %q from runtime service failed: %v", containerID, err)
  159. return err
  160. }
  161. return nil
  162. }
  163. // RemoveContainer removes the container. If the container is running, the container
  164. // should be forced to removal.
  165. func (r *RemoteRuntimeService) RemoveContainer(containerID string) error {
  166. ctx, cancel := getContextWithTimeout(r.timeout)
  167. defer cancel()
  168. _, err := r.runtimeClient.RemoveContainer(ctx, &runtimeApi.RemoveContainerRequest{
  169. ContainerId: &containerID,
  170. })
  171. if err != nil {
  172. glog.Errorf("RemoveContainer %q from runtime service failed: %v", containerID, err)
  173. return err
  174. }
  175. return nil
  176. }
  177. // ListContainers lists containers by filters.
  178. func (r *RemoteRuntimeService) ListContainers(filter *runtimeApi.ContainerFilter) ([]*runtimeApi.Container, error) {
  179. ctx, cancel := getContextWithTimeout(r.timeout)
  180. defer cancel()
  181. resp, err := r.runtimeClient.ListContainers(ctx, &runtimeApi.ListContainersRequest{
  182. Filter: filter,
  183. })
  184. if err != nil {
  185. glog.Errorf("ListContainers with filter %q from runtime service failed: %v", filter, err)
  186. return nil, err
  187. }
  188. return resp.Containers, nil
  189. }
  190. // ContainerStatus returns the container status.
  191. func (r *RemoteRuntimeService) ContainerStatus(containerID string) (*runtimeApi.ContainerStatus, error) {
  192. ctx, cancel := getContextWithTimeout(r.timeout)
  193. defer cancel()
  194. resp, err := r.runtimeClient.ContainerStatus(ctx, &runtimeApi.ContainerStatusRequest{
  195. ContainerId: &containerID,
  196. })
  197. if err != nil {
  198. glog.Errorf("ContainerStatus %q from runtime service failed: %v", containerID, err)
  199. return nil, err
  200. }
  201. return resp.Status, nil
  202. }
  203. // Exec executes a command in the container.
  204. // TODO: support terminal resizing for exec, refer https://github.com/kubernetes/kubernetes/issues/29579.
  205. func (r *RemoteRuntimeService) Exec(containerID string, cmd []string, tty bool, stdin io.Reader, stdout, stderr io.WriteCloser) error {
  206. return fmt.Errorf("Not implemented")
  207. }