k8s.io/kubernetes@v1.29.3/pkg/kubelet/cri/remote/fake/fake_runtime.go (about) 1 /* 2 Copyright 2017 The Kubernetes Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package fake 18 19 import ( 20 "context" 21 "fmt" 22 "time" 23 24 "google.golang.org/grpc" 25 kubeapi "k8s.io/cri-api/pkg/apis/runtime/v1" 26 apitest "k8s.io/cri-api/pkg/apis/testing" 27 "k8s.io/kubernetes/pkg/kubelet/util" 28 utilexec "k8s.io/utils/exec" 29 ) 30 31 // RemoteRuntime represents a fake remote container runtime. 32 type RemoteRuntime struct { 33 server *grpc.Server 34 // Fake runtime service. 35 RuntimeService *apitest.FakeRuntimeService 36 // Fake image service. 37 ImageService *apitest.FakeImageService 38 } 39 40 // NewFakeRemoteRuntime creates a new RemoteRuntime. 41 func NewFakeRemoteRuntime() *RemoteRuntime { 42 fakeRuntimeService := apitest.NewFakeRuntimeService() 43 fakeImageService := apitest.NewFakeImageService() 44 45 f := &RemoteRuntime{ 46 server: grpc.NewServer(), 47 RuntimeService: fakeRuntimeService, 48 ImageService: fakeImageService, 49 } 50 kubeapi.RegisterRuntimeServiceServer(f.server, f) 51 kubeapi.RegisterImageServiceServer(f.server, f) 52 53 return f 54 } 55 56 // Start starts the fake remote runtime. 57 func (f *RemoteRuntime) Start(endpoint string) error { 58 l, err := util.CreateListener(endpoint) 59 if err != nil { 60 return fmt.Errorf("failed to listen on %q: %v", endpoint, err) 61 } 62 63 go f.server.Serve(l) 64 65 // Set runtime and network conditions ready. 66 f.RuntimeService.FakeStatus = &kubeapi.RuntimeStatus{ 67 Conditions: []*kubeapi.RuntimeCondition{ 68 {Type: kubeapi.RuntimeReady, Status: true}, 69 {Type: kubeapi.NetworkReady, Status: true}, 70 }, 71 } 72 73 return nil 74 } 75 76 // Stop stops the fake remote runtime. 77 func (f *RemoteRuntime) Stop() { 78 f.server.Stop() 79 } 80 81 // Version returns the runtime name, runtime version, and runtime API version. 82 func (f *RemoteRuntime) Version(ctx context.Context, req *kubeapi.VersionRequest) (*kubeapi.VersionResponse, error) { 83 return f.RuntimeService.Version(ctx, req.Version) 84 } 85 86 // RunPodSandbox creates and starts a pod-level sandbox. Runtimes must ensure 87 // the sandbox is in the ready state on success. 88 func (f *RemoteRuntime) RunPodSandbox(ctx context.Context, req *kubeapi.RunPodSandboxRequest) (*kubeapi.RunPodSandboxResponse, error) { 89 sandboxID, err := f.RuntimeService.RunPodSandbox(ctx, req.Config, req.RuntimeHandler) 90 if err != nil { 91 return nil, err 92 } 93 94 return &kubeapi.RunPodSandboxResponse{PodSandboxId: sandboxID}, nil 95 } 96 97 // StopPodSandbox stops any running process that is part of the sandbox and 98 // reclaims network resources (e.g., IP addresses) allocated to the sandbox. 99 // If there are any running containers in the sandbox, they must be forcibly 100 // terminated. 101 func (f *RemoteRuntime) StopPodSandbox(ctx context.Context, req *kubeapi.StopPodSandboxRequest) (*kubeapi.StopPodSandboxResponse, error) { 102 err := f.RuntimeService.StopPodSandbox(ctx, req.PodSandboxId) 103 if err != nil { 104 return nil, err 105 } 106 107 return &kubeapi.StopPodSandboxResponse{}, nil 108 } 109 110 // RemovePodSandbox removes the sandbox. If there are any running containers 111 // in the sandbox, they must be forcibly terminated and removed. 112 // This call is idempotent, and must not return an error if the sandbox has 113 // already been removed. 114 func (f *RemoteRuntime) RemovePodSandbox(ctx context.Context, req *kubeapi.RemovePodSandboxRequest) (*kubeapi.RemovePodSandboxResponse, error) { 115 err := f.RuntimeService.StopPodSandbox(ctx, req.PodSandboxId) 116 if err != nil { 117 return nil, err 118 } 119 120 return &kubeapi.RemovePodSandboxResponse{}, nil 121 } 122 123 // PodSandboxStatus returns the status of the PodSandbox. If the PodSandbox is not 124 // present, returns an error. 125 func (f *RemoteRuntime) PodSandboxStatus(ctx context.Context, req *kubeapi.PodSandboxStatusRequest) (*kubeapi.PodSandboxStatusResponse, error) { 126 resp, err := f.RuntimeService.PodSandboxStatus(ctx, req.PodSandboxId, false) 127 if err != nil { 128 return nil, err 129 } 130 131 return resp, nil 132 } 133 134 // ListPodSandbox returns a list of PodSandboxes. 135 func (f *RemoteRuntime) ListPodSandbox(ctx context.Context, req *kubeapi.ListPodSandboxRequest) (*kubeapi.ListPodSandboxResponse, error) { 136 items, err := f.RuntimeService.ListPodSandbox(ctx, req.Filter) 137 if err != nil { 138 return nil, err 139 } 140 141 return &kubeapi.ListPodSandboxResponse{Items: items}, nil 142 } 143 144 // CreateContainer creates a new container in specified PodSandbox 145 func (f *RemoteRuntime) CreateContainer(ctx context.Context, req *kubeapi.CreateContainerRequest) (*kubeapi.CreateContainerResponse, error) { 146 containerID, err := f.RuntimeService.CreateContainer(ctx, req.PodSandboxId, req.Config, req.SandboxConfig) 147 if err != nil { 148 return nil, err 149 } 150 151 return &kubeapi.CreateContainerResponse{ContainerId: containerID}, nil 152 } 153 154 // StartContainer starts the container. 155 func (f *RemoteRuntime) StartContainer(ctx context.Context, req *kubeapi.StartContainerRequest) (*kubeapi.StartContainerResponse, error) { 156 err := f.RuntimeService.StartContainer(ctx, req.ContainerId) 157 if err != nil { 158 return nil, err 159 } 160 161 return &kubeapi.StartContainerResponse{}, nil 162 } 163 164 // StopContainer stops a running container with a grace period (i.e., timeout). 165 // This call is idempotent, and must not return an error if the container has 166 // already been stopped. 167 func (f *RemoteRuntime) StopContainer(ctx context.Context, req *kubeapi.StopContainerRequest) (*kubeapi.StopContainerResponse, error) { 168 err := f.RuntimeService.StopContainer(ctx, req.ContainerId, req.Timeout) 169 if err != nil { 170 return nil, err 171 } 172 173 return &kubeapi.StopContainerResponse{}, nil 174 } 175 176 // RemoveContainer removes the container. If the container is running, the 177 // container must be forcibly removed. 178 // This call is idempotent, and must not return an error if the container has 179 // already been removed. 180 func (f *RemoteRuntime) RemoveContainer(ctx context.Context, req *kubeapi.RemoveContainerRequest) (*kubeapi.RemoveContainerResponse, error) { 181 err := f.RuntimeService.RemoveContainer(ctx, req.ContainerId) 182 if err != nil { 183 return nil, err 184 } 185 186 return &kubeapi.RemoveContainerResponse{}, nil 187 } 188 189 // ListContainers lists all containers by filters. 190 func (f *RemoteRuntime) ListContainers(ctx context.Context, req *kubeapi.ListContainersRequest) (*kubeapi.ListContainersResponse, error) { 191 items, err := f.RuntimeService.ListContainers(ctx, req.Filter) 192 if err != nil { 193 return nil, err 194 } 195 196 return &kubeapi.ListContainersResponse{Containers: items}, nil 197 } 198 199 // ContainerStatus returns status of the container. If the container is not 200 // present, returns an error. 201 func (f *RemoteRuntime) ContainerStatus(ctx context.Context, req *kubeapi.ContainerStatusRequest) (*kubeapi.ContainerStatusResponse, error) { 202 resp, err := f.RuntimeService.ContainerStatus(ctx, req.ContainerId, false) 203 if err != nil { 204 return nil, err 205 } 206 207 return resp, nil 208 } 209 210 // ExecSync runs a command in a container synchronously. 211 func (f *RemoteRuntime) ExecSync(ctx context.Context, req *kubeapi.ExecSyncRequest) (*kubeapi.ExecSyncResponse, error) { 212 var exitCode int32 213 stdout, stderr, err := f.RuntimeService.ExecSync(ctx, req.ContainerId, req.Cmd, time.Duration(req.Timeout)*time.Second) 214 if err != nil { 215 exitError, ok := err.(utilexec.ExitError) 216 if !ok { 217 return nil, err 218 } 219 exitCode = int32(exitError.ExitStatus()) 220 } 221 222 return &kubeapi.ExecSyncResponse{ 223 Stdout: stdout, 224 Stderr: stderr, 225 ExitCode: exitCode, 226 }, nil 227 } 228 229 // Exec prepares a streaming endpoint to execute a command in the container. 230 func (f *RemoteRuntime) Exec(ctx context.Context, req *kubeapi.ExecRequest) (*kubeapi.ExecResponse, error) { 231 return f.RuntimeService.Exec(ctx, req) 232 } 233 234 // Attach prepares a streaming endpoint to attach to a running container. 235 func (f *RemoteRuntime) Attach(ctx context.Context, req *kubeapi.AttachRequest) (*kubeapi.AttachResponse, error) { 236 return f.RuntimeService.Attach(ctx, req) 237 } 238 239 // PortForward prepares a streaming endpoint to forward ports from a PodSandbox. 240 func (f *RemoteRuntime) PortForward(ctx context.Context, req *kubeapi.PortForwardRequest) (*kubeapi.PortForwardResponse, error) { 241 return f.RuntimeService.PortForward(ctx, req) 242 } 243 244 // ContainerStats returns stats of the container. If the container does not 245 // exist, the call returns an error. 246 func (f *RemoteRuntime) ContainerStats(ctx context.Context, req *kubeapi.ContainerStatsRequest) (*kubeapi.ContainerStatsResponse, error) { 247 stats, err := f.RuntimeService.ContainerStats(ctx, req.ContainerId) 248 if err != nil { 249 return nil, err 250 } 251 252 return &kubeapi.ContainerStatsResponse{Stats: stats}, nil 253 } 254 255 // ListContainerStats returns stats of all running containers. 256 func (f *RemoteRuntime) ListContainerStats(ctx context.Context, req *kubeapi.ListContainerStatsRequest) (*kubeapi.ListContainerStatsResponse, error) { 257 stats, err := f.RuntimeService.ListContainerStats(ctx, req.Filter) 258 if err != nil { 259 return nil, err 260 } 261 262 return &kubeapi.ListContainerStatsResponse{Stats: stats}, nil 263 } 264 265 // PodSandboxStats returns stats of the pod. If the pod does not 266 // exist, the call returns an error. 267 func (f *RemoteRuntime) PodSandboxStats(ctx context.Context, req *kubeapi.PodSandboxStatsRequest) (*kubeapi.PodSandboxStatsResponse, error) { 268 stats, err := f.RuntimeService.PodSandboxStats(ctx, req.PodSandboxId) 269 if err != nil { 270 return nil, err 271 } 272 273 return &kubeapi.PodSandboxStatsResponse{Stats: stats}, nil 274 } 275 276 // ListPodSandboxStats returns stats of all running pods. 277 func (f *RemoteRuntime) ListPodSandboxStats(ctx context.Context, req *kubeapi.ListPodSandboxStatsRequest) (*kubeapi.ListPodSandboxStatsResponse, error) { 278 stats, err := f.RuntimeService.ListPodSandboxStats(ctx, req.Filter) 279 if err != nil { 280 return nil, err 281 } 282 283 return &kubeapi.ListPodSandboxStatsResponse{Stats: stats}, nil 284 } 285 286 // UpdateRuntimeConfig updates the runtime configuration based on the given request. 287 func (f *RemoteRuntime) UpdateRuntimeConfig(ctx context.Context, req *kubeapi.UpdateRuntimeConfigRequest) (*kubeapi.UpdateRuntimeConfigResponse, error) { 288 err := f.RuntimeService.UpdateRuntimeConfig(ctx, req.RuntimeConfig) 289 if err != nil { 290 return nil, err 291 } 292 293 return &kubeapi.UpdateRuntimeConfigResponse{}, nil 294 } 295 296 // Status returns the status of the runtime. 297 func (f *RemoteRuntime) Status(ctx context.Context, req *kubeapi.StatusRequest) (*kubeapi.StatusResponse, error) { 298 resp, err := f.RuntimeService.Status(ctx, false) 299 if err != nil { 300 return nil, err 301 } 302 303 return resp, nil 304 } 305 306 // UpdateContainerResources updates ContainerConfig of the container. 307 func (f *RemoteRuntime) UpdateContainerResources(ctx context.Context, req *kubeapi.UpdateContainerResourcesRequest) (*kubeapi.UpdateContainerResourcesResponse, error) { 308 err := f.RuntimeService.UpdateContainerResources(ctx, req.ContainerId, &kubeapi.ContainerResources{Linux: req.Linux}) 309 if err != nil { 310 return nil, err 311 } 312 313 return &kubeapi.UpdateContainerResourcesResponse{}, nil 314 } 315 316 // ReopenContainerLog reopens the container log file. 317 func (f *RemoteRuntime) ReopenContainerLog(ctx context.Context, req *kubeapi.ReopenContainerLogRequest) (*kubeapi.ReopenContainerLogResponse, error) { 318 err := f.RuntimeService.ReopenContainerLog(ctx, req.ContainerId) 319 if err != nil { 320 return nil, err 321 } 322 323 return &kubeapi.ReopenContainerLogResponse{}, nil 324 } 325 326 // CheckpointContainer checkpoints the given container. 327 func (f *RemoteRuntime) CheckpointContainer(ctx context.Context, req *kubeapi.CheckpointContainerRequest) (*kubeapi.CheckpointContainerResponse, error) { 328 err := f.RuntimeService.CheckpointContainer(ctx, &kubeapi.CheckpointContainerRequest{}) 329 if err != nil { 330 return nil, err 331 } 332 333 return &kubeapi.CheckpointContainerResponse{}, nil 334 } 335 336 func (f *RemoteRuntime) GetContainerEvents(req *kubeapi.GetEventsRequest, ces kubeapi.RuntimeService_GetContainerEventsServer) error { 337 return nil 338 } 339 340 // ListMetricDescriptors gets the descriptors for the metrics that will be returned in ListPodSandboxMetrics. 341 func (f *RemoteRuntime) ListMetricDescriptors(ctx context.Context, req *kubeapi.ListMetricDescriptorsRequest) (*kubeapi.ListMetricDescriptorsResponse, error) { 342 descs, err := f.RuntimeService.ListMetricDescriptors(ctx) 343 if err != nil { 344 return nil, err 345 } 346 347 return &kubeapi.ListMetricDescriptorsResponse{Descriptors: descs}, nil 348 } 349 350 // ListPodSandboxMetrics retrieves the metrics for all pod sandboxes. 351 func (f *RemoteRuntime) ListPodSandboxMetrics(ctx context.Context, req *kubeapi.ListPodSandboxMetricsRequest) (*kubeapi.ListPodSandboxMetricsResponse, error) { 352 podMetrics, err := f.RuntimeService.ListPodSandboxMetrics(ctx) 353 if err != nil { 354 return nil, err 355 } 356 357 return &kubeapi.ListPodSandboxMetricsResponse{PodMetrics: podMetrics}, nil 358 } 359 360 // RuntimeConfig returns the configuration information of the runtime. 361 func (f *RemoteRuntime) RuntimeConfig(ctx context.Context, req *kubeapi.RuntimeConfigRequest) (*kubeapi.RuntimeConfigResponse, error) { 362 resp, err := f.RuntimeService.RuntimeConfig(ctx) 363 if err != nil { 364 return nil, err 365 } 366 367 return resp, nil 368 }