fake_runtime.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. /*
  2. Copyright 2017 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 fake
  14. import (
  15. "context"
  16. "fmt"
  17. "time"
  18. "google.golang.org/grpc"
  19. kubeapi "k8s.io/cri-api/pkg/apis/runtime/v1alpha2"
  20. apitest "k8s.io/cri-api/pkg/apis/testing"
  21. "k8s.io/kubernetes/pkg/kubelet/util"
  22. utilexec "k8s.io/utils/exec"
  23. )
  24. // RemoteRuntime represents a fake remote container runtime.
  25. type RemoteRuntime struct {
  26. server *grpc.Server
  27. // Fake runtime service.
  28. RuntimeService *apitest.FakeRuntimeService
  29. // Fake image service.
  30. ImageService *apitest.FakeImageService
  31. }
  32. // NewFakeRemoteRuntime creates a new RemoteRuntime.
  33. func NewFakeRemoteRuntime() *RemoteRuntime {
  34. fakeRuntimeService := apitest.NewFakeRuntimeService()
  35. fakeImageService := apitest.NewFakeImageService()
  36. f := &RemoteRuntime{
  37. server: grpc.NewServer(),
  38. RuntimeService: fakeRuntimeService,
  39. ImageService: fakeImageService,
  40. }
  41. kubeapi.RegisterRuntimeServiceServer(f.server, f)
  42. kubeapi.RegisterImageServiceServer(f.server, f)
  43. return f
  44. }
  45. // Start starts the fake remote runtime.
  46. func (f *RemoteRuntime) Start(endpoint string) error {
  47. l, err := util.CreateListener(endpoint)
  48. if err != nil {
  49. return fmt.Errorf("failed to listen on %q: %v", endpoint, err)
  50. }
  51. go f.server.Serve(l)
  52. return nil
  53. }
  54. // Stop stops the fake remote runtime.
  55. func (f *RemoteRuntime) Stop() {
  56. f.server.Stop()
  57. }
  58. // Version returns the runtime name, runtime version, and runtime API version.
  59. func (f *RemoteRuntime) Version(ctx context.Context, req *kubeapi.VersionRequest) (*kubeapi.VersionResponse, error) {
  60. return f.RuntimeService.Version(req.Version)
  61. }
  62. // RunPodSandbox creates and starts a pod-level sandbox. Runtimes must ensure
  63. // the sandbox is in the ready state on success.
  64. func (f *RemoteRuntime) RunPodSandbox(ctx context.Context, req *kubeapi.RunPodSandboxRequest) (*kubeapi.RunPodSandboxResponse, error) {
  65. sandboxID, err := f.RuntimeService.RunPodSandbox(req.Config, req.RuntimeHandler)
  66. if err != nil {
  67. return nil, err
  68. }
  69. return &kubeapi.RunPodSandboxResponse{PodSandboxId: sandboxID}, nil
  70. }
  71. // StopPodSandbox stops any running process that is part of the sandbox and
  72. // reclaims network resources (e.g., IP addresses) allocated to the sandbox.
  73. // If there are any running containers in the sandbox, they must be forcibly
  74. // terminated.
  75. func (f *RemoteRuntime) StopPodSandbox(ctx context.Context, req *kubeapi.StopPodSandboxRequest) (*kubeapi.StopPodSandboxResponse, error) {
  76. err := f.RuntimeService.StopPodSandbox(req.PodSandboxId)
  77. if err != nil {
  78. return nil, err
  79. }
  80. return &kubeapi.StopPodSandboxResponse{}, nil
  81. }
  82. // RemovePodSandbox removes the sandbox. If there are any running containers
  83. // in the sandbox, they must be forcibly terminated and removed.
  84. // This call is idempotent, and must not return an error if the sandbox has
  85. // already been removed.
  86. func (f *RemoteRuntime) RemovePodSandbox(ctx context.Context, req *kubeapi.RemovePodSandboxRequest) (*kubeapi.RemovePodSandboxResponse, error) {
  87. err := f.RuntimeService.StopPodSandbox(req.PodSandboxId)
  88. if err != nil {
  89. return nil, err
  90. }
  91. return &kubeapi.RemovePodSandboxResponse{}, nil
  92. }
  93. // PodSandboxStatus returns the status of the PodSandbox. If the PodSandbox is not
  94. // present, returns an error.
  95. func (f *RemoteRuntime) PodSandboxStatus(ctx context.Context, req *kubeapi.PodSandboxStatusRequest) (*kubeapi.PodSandboxStatusResponse, error) {
  96. podStatus, err := f.RuntimeService.PodSandboxStatus(req.PodSandboxId)
  97. if err != nil {
  98. return nil, err
  99. }
  100. return &kubeapi.PodSandboxStatusResponse{Status: podStatus}, nil
  101. }
  102. // ListPodSandbox returns a list of PodSandboxes.
  103. func (f *RemoteRuntime) ListPodSandbox(ctx context.Context, req *kubeapi.ListPodSandboxRequest) (*kubeapi.ListPodSandboxResponse, error) {
  104. items, err := f.RuntimeService.ListPodSandbox(req.Filter)
  105. if err != nil {
  106. return nil, err
  107. }
  108. return &kubeapi.ListPodSandboxResponse{Items: items}, nil
  109. }
  110. // CreateContainer creates a new container in specified PodSandbox
  111. func (f *RemoteRuntime) CreateContainer(ctx context.Context, req *kubeapi.CreateContainerRequest) (*kubeapi.CreateContainerResponse, error) {
  112. containerID, err := f.RuntimeService.CreateContainer(req.PodSandboxId, req.Config, req.SandboxConfig)
  113. if err != nil {
  114. return nil, err
  115. }
  116. return &kubeapi.CreateContainerResponse{ContainerId: containerID}, nil
  117. }
  118. // StartContainer starts the container.
  119. func (f *RemoteRuntime) StartContainer(ctx context.Context, req *kubeapi.StartContainerRequest) (*kubeapi.StartContainerResponse, error) {
  120. err := f.RuntimeService.StartContainer(req.ContainerId)
  121. if err != nil {
  122. return nil, err
  123. }
  124. return &kubeapi.StartContainerResponse{}, nil
  125. }
  126. // StopContainer stops a running container with a grace period (i.e., timeout).
  127. // This call is idempotent, and must not return an error if the container has
  128. // already been stopped.
  129. func (f *RemoteRuntime) StopContainer(ctx context.Context, req *kubeapi.StopContainerRequest) (*kubeapi.StopContainerResponse, error) {
  130. err := f.RuntimeService.StopContainer(req.ContainerId, req.Timeout)
  131. if err != nil {
  132. return nil, err
  133. }
  134. return &kubeapi.StopContainerResponse{}, nil
  135. }
  136. // RemoveContainer removes the container. If the container is running, the
  137. // container must be forcibly removed.
  138. // This call is idempotent, and must not return an error if the container has
  139. // already been removed.
  140. func (f *RemoteRuntime) RemoveContainer(ctx context.Context, req *kubeapi.RemoveContainerRequest) (*kubeapi.RemoveContainerResponse, error) {
  141. err := f.RuntimeService.RemoveContainer(req.ContainerId)
  142. if err != nil {
  143. return nil, err
  144. }
  145. return &kubeapi.RemoveContainerResponse{}, nil
  146. }
  147. // ListContainers lists all containers by filters.
  148. func (f *RemoteRuntime) ListContainers(ctx context.Context, req *kubeapi.ListContainersRequest) (*kubeapi.ListContainersResponse, error) {
  149. items, err := f.RuntimeService.ListContainers(req.Filter)
  150. if err != nil {
  151. return nil, err
  152. }
  153. return &kubeapi.ListContainersResponse{Containers: items}, nil
  154. }
  155. // ContainerStatus returns status of the container. If the container is not
  156. // present, returns an error.
  157. func (f *RemoteRuntime) ContainerStatus(ctx context.Context, req *kubeapi.ContainerStatusRequest) (*kubeapi.ContainerStatusResponse, error) {
  158. status, err := f.RuntimeService.ContainerStatus(req.ContainerId)
  159. if err != nil {
  160. return nil, err
  161. }
  162. return &kubeapi.ContainerStatusResponse{Status: status}, nil
  163. }
  164. // ExecSync runs a command in a container synchronously.
  165. func (f *RemoteRuntime) ExecSync(ctx context.Context, req *kubeapi.ExecSyncRequest) (*kubeapi.ExecSyncResponse, error) {
  166. var exitCode int32
  167. stdout, stderr, err := f.RuntimeService.ExecSync(req.ContainerId, req.Cmd, time.Duration(req.Timeout)*time.Second)
  168. if err != nil {
  169. exitError, ok := err.(utilexec.ExitError)
  170. if !ok {
  171. return nil, err
  172. }
  173. exitCode = int32(exitError.ExitStatus())
  174. return nil, err
  175. }
  176. return &kubeapi.ExecSyncResponse{
  177. Stdout: stdout,
  178. Stderr: stderr,
  179. ExitCode: exitCode,
  180. }, nil
  181. }
  182. // Exec prepares a streaming endpoint to execute a command in the container.
  183. func (f *RemoteRuntime) Exec(ctx context.Context, req *kubeapi.ExecRequest) (*kubeapi.ExecResponse, error) {
  184. return f.RuntimeService.Exec(req)
  185. }
  186. // Attach prepares a streaming endpoint to attach to a running container.
  187. func (f *RemoteRuntime) Attach(ctx context.Context, req *kubeapi.AttachRequest) (*kubeapi.AttachResponse, error) {
  188. return f.RuntimeService.Attach(req)
  189. }
  190. // PortForward prepares a streaming endpoint to forward ports from a PodSandbox.
  191. func (f *RemoteRuntime) PortForward(ctx context.Context, req *kubeapi.PortForwardRequest) (*kubeapi.PortForwardResponse, error) {
  192. return f.RuntimeService.PortForward(req)
  193. }
  194. // ContainerStats returns stats of the container. If the container does not
  195. // exist, the call returns an error.
  196. func (f *RemoteRuntime) ContainerStats(ctx context.Context, req *kubeapi.ContainerStatsRequest) (*kubeapi.ContainerStatsResponse, error) {
  197. stats, err := f.RuntimeService.ContainerStats(req.ContainerId)
  198. if err != nil {
  199. return nil, err
  200. }
  201. return &kubeapi.ContainerStatsResponse{Stats: stats}, nil
  202. }
  203. // ListContainerStats returns stats of all running containers.
  204. func (f *RemoteRuntime) ListContainerStats(ctx context.Context, req *kubeapi.ListContainerStatsRequest) (*kubeapi.ListContainerStatsResponse, error) {
  205. stats, err := f.RuntimeService.ListContainerStats(req.Filter)
  206. if err != nil {
  207. return nil, err
  208. }
  209. return &kubeapi.ListContainerStatsResponse{Stats: stats}, nil
  210. }
  211. // UpdateRuntimeConfig updates the runtime configuration based on the given request.
  212. func (f *RemoteRuntime) UpdateRuntimeConfig(ctx context.Context, req *kubeapi.UpdateRuntimeConfigRequest) (*kubeapi.UpdateRuntimeConfigResponse, error) {
  213. err := f.RuntimeService.UpdateRuntimeConfig(req.RuntimeConfig)
  214. if err != nil {
  215. return nil, err
  216. }
  217. return &kubeapi.UpdateRuntimeConfigResponse{}, nil
  218. }
  219. // Status returns the status of the runtime.
  220. func (f *RemoteRuntime) Status(ctx context.Context, req *kubeapi.StatusRequest) (*kubeapi.StatusResponse, error) {
  221. status, err := f.RuntimeService.Status()
  222. if err != nil {
  223. return nil, err
  224. }
  225. return &kubeapi.StatusResponse{Status: status}, nil
  226. }
  227. // UpdateContainerResources updates ContainerConfig of the container.
  228. func (f *RemoteRuntime) UpdateContainerResources(ctx context.Context, req *kubeapi.UpdateContainerResourcesRequest) (*kubeapi.UpdateContainerResourcesResponse, error) {
  229. err := f.RuntimeService.UpdateContainerResources(req.ContainerId, req.Linux)
  230. if err != nil {
  231. return nil, err
  232. }
  233. return &kubeapi.UpdateContainerResourcesResponse{}, nil
  234. }
  235. // ReopenContainerLog reopens the container log file.
  236. func (f *RemoteRuntime) ReopenContainerLog(ctx context.Context, req *kubeapi.ReopenContainerLogRequest) (*kubeapi.ReopenContainerLogResponse, error) {
  237. err := f.RuntimeService.ReopenContainerLog(req.ContainerId)
  238. if err != nil {
  239. return nil, err
  240. }
  241. return &kubeapi.ReopenContainerLogResponse{}, nil
  242. }