1/*
2Copyright 2016 The Kubernetes Authors.
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8    http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16
17package cri
18
19import (
20	"time"
21
22	runtimeapi "k8s.io/cri-api/pkg/apis/runtime/v1alpha2"
23)
24
25// RuntimeVersioner contains methods for runtime name, version and API version.
26type RuntimeVersioner interface {
27	// Version returns the runtime name, runtime version and runtime API version
28	Version(apiVersion string) (*runtimeapi.VersionResponse, error)
29}
30
31// ContainerManager contains methods to manipulate containers managed by a
32// container runtime. The methods are thread-safe.
33type ContainerManager interface {
34	// CreateContainer creates a new container in specified PodSandbox.
35	CreateContainer(podSandboxID string, config *runtimeapi.ContainerConfig, sandboxConfig *runtimeapi.PodSandboxConfig) (string, error)
36	// StartContainer starts the container.
37	StartContainer(containerID string) error
38	// StopContainer stops a running container with a grace period (i.e., timeout).
39	StopContainer(containerID string, timeout int64) error
40	// RemoveContainer removes the container.
41	RemoveContainer(containerID string) error
42	// ListContainers lists all containers by filters.
43	ListContainers(filter *runtimeapi.ContainerFilter) ([]*runtimeapi.Container, error)
44	// ContainerStatus returns the status of the container.
45	ContainerStatus(containerID string) (*runtimeapi.ContainerStatus, error)
46	// UpdateContainerResources updates the cgroup resources for the container.
47	UpdateContainerResources(containerID string, resources *runtimeapi.LinuxContainerResources) error
48	// ExecSync executes a command in the container, and returns the stdout output.
49	// If command exits with a non-zero exit code, an error is returned.
50	ExecSync(containerID string, cmd []string, timeout time.Duration) (stdout []byte, stderr []byte, err error)
51	// Exec prepares a streaming endpoint to execute a command in the container, and returns the address.
52	Exec(*runtimeapi.ExecRequest) (*runtimeapi.ExecResponse, error)
53	// Attach prepares a streaming endpoint to attach to a running container, and returns the address.
54	Attach(req *runtimeapi.AttachRequest) (*runtimeapi.AttachResponse, error)
55	// ReopenContainerLog asks runtime to reopen the stdout/stderr log file
56	// for the container. If it returns error, new container log file MUST NOT
57	// be created.
58	ReopenContainerLog(ContainerID string) error
59}
60
61// PodSandboxManager contains methods for operating on PodSandboxes. The methods
62// are thread-safe.
63type PodSandboxManager interface {
64	// RunPodSandbox creates and starts a pod-level sandbox. Runtimes should ensure
65	// the sandbox is in ready state.
66	RunPodSandbox(config *runtimeapi.PodSandboxConfig, runtimeHandler string) (string, error)
67	// StopPodSandbox stops the sandbox. If there are any running containers in the
68	// sandbox, they should be force terminated.
69	StopPodSandbox(podSandboxID string) error
70	// RemovePodSandbox removes the sandbox. If there are running containers in the
71	// sandbox, they should be forcibly removed.
72	RemovePodSandbox(podSandboxID string) error
73	// PodSandboxStatus returns the Status of the PodSandbox.
74	PodSandboxStatus(podSandboxID string) (*runtimeapi.PodSandboxStatus, error)
75	// ListPodSandbox returns a list of Sandbox.
76	ListPodSandbox(filter *runtimeapi.PodSandboxFilter) ([]*runtimeapi.PodSandbox, error)
77	// PortForward prepares a streaming endpoint to forward ports from a PodSandbox, and returns the address.
78	PortForward(*runtimeapi.PortForwardRequest) (*runtimeapi.PortForwardResponse, error)
79}
80
81// ContainerStatsManager contains methods for retrieving the container
82// statistics.
83type ContainerStatsManager interface {
84	// ContainerStats returns stats of the container. If the container does not
85	// exist, the call returns an error.
86	ContainerStats(containerID string) (*runtimeapi.ContainerStats, error)
87	// ListContainerStats returns stats of all running containers.
88	ListContainerStats(filter *runtimeapi.ContainerStatsFilter) ([]*runtimeapi.ContainerStats, error)
89}
90
91// RuntimeService interface should be implemented by a container runtime.
92// The methods should be thread-safe.
93type RuntimeService interface {
94	RuntimeVersioner
95	ContainerManager
96	PodSandboxManager
97	ContainerStatsManager
98
99	// UpdateRuntimeConfig updates runtime configuration if specified
100	UpdateRuntimeConfig(runtimeConfig *runtimeapi.RuntimeConfig) error
101	// Status returns the status of the runtime.
102	Status() (*runtimeapi.RuntimeStatus, error)
103}
104
105// ImageManagerService interface should be implemented by a container image
106// manager.
107// The methods should be thread-safe.
108type ImageManagerService interface {
109	// ListImages lists the existing images.
110	ListImages(filter *runtimeapi.ImageFilter) ([]*runtimeapi.Image, error)
111	// ImageStatus returns the status of the image.
112	ImageStatus(image *runtimeapi.ImageSpec) (*runtimeapi.Image, error)
113	// PullImage pulls an image with the authentication config.
114	PullImage(image *runtimeapi.ImageSpec, auth *runtimeapi.AuthConfig, podSandboxConfig *runtimeapi.PodSandboxConfig) (string, error)
115	// RemoveImage removes the image.
116	RemoveImage(image *runtimeapi.ImageSpec) error
117	// ImageFsInfo returns information of the filesystem that is used to store images.
118	ImageFsInfo() ([]*runtimeapi.FilesystemUsage, error)
119}
120