// Copyright 2019 Yunion // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package runtime import ( "time" runtimeapi "k8s.io/cri-api/pkg/apis/runtime/v1" ) // Runtime interface defines the interfaces that should be implemented // by a container runtime. // Thread safety is required from implementations of this interface. type Runtime interface { // Type returns the type of the container runtime. Type() string // GetPods returns a list of containers grouped by pods. The boolean parameter // specifies whether the runtime returns all containers including those already // exited and dead containers (used for garbage collection). GetPods(all bool) ([]*Pod, error) // GetPodStatus retrieves the status of the pod, including the // information of all containers in the pod that are visible in Runtime. GetPodStatus(uid, name, namespace string) (*PodStatus, error) } // Pod is a group of containers. type Pod struct { // The ID of the pod, which can be used to retrieve a particular pod // from the pod list returned by GetPods(). Id string CRIId string // The name and namespace of the pod, which is readable by human. Name string Namespace string // List of containers that belongs to the pod. It may contain only // running containers, or mixed with dead ones (when GetPods(true)). Containers []*Container // List of sandboxes associated with this pod. The sandboxes are converted // to Container temporarily to avoid substantial changes to other // components. This is only populated by kuberuntime. Sandboxes []*Container } // ContainerID is a type that identifies a container. type ContainerID struct { // The type of the container runtime. e.g. 'docker'. Type string // The identification of the container, this is comsumable by // the underlying container runtime. (Note that the container // runtime interface still takes the whole struct as input). ID string } // State represents the state of a container type State string const ( // ContainerStateCreated indicates a container that has been created (e.g. with docker create) but not started. ContainerStateCreated State = "created" // ContainerStateRunning indicates a currently running container. ContainerStateRunning State = "running" // ContainerStateExited indicates a container that ran and completed ("stopped" in other contexts, although a created container is technically also "stopped"). ContainerStateExited State = "exited" // ContainerStateUnknown encompasses all the states that we currently don't care about (like restarting, paused, dead). ContainerStateUnknown State = "unknown" ) // Container provides the runtime information for a container, such as ID, hash, // state of the container. type Container struct { // The ID of the container, used by the container runtime to identify // a container. ID ContainerID // The name of the container, which should be the same as specified by // v1.Container. Name string // The image name of the container, this also includes the tag of the image, // the expected form is "NAME:TAG". Image string // The id of the image used by the container. ImageID string // State is the state of the container. State State } // Pods represents the list of pods type Pods []*Pod // FindPodByID finds and returns a pod in the pod list by UID. It will return an empty pod // if not found. func (p Pods) FindPodByID(podUID string) Pod { for i := range p { if p[i].Id == podUID { return *p[i] } } return Pod{} } // FindPodByFullName finds and returns a pod in the pod list by the full name. // It will return an empty pod if not found. func (p Pods) FindPodByFullName(podFullName string) Pod { for i := range p { if BuildPodFullName(p[i].Name, p[i].Namespace) == podFullName { return *p[i] } } return Pod{} } // FindPod combines FindPodByID and FindPodByFullName, it finds and returns a pod in the // pod list either by the full name or the pod ID. It will return an empty pod // if not found. func (p Pods) FindPod(podFullName string, podUID string) Pod { if len(podFullName) > 0 { return p.FindPodByFullName(podFullName) } return p.FindPodByID(podUID) } // FindContainerByName returns a container in the pod with the given name. // When there are multiple containers with the same name, the first match will // be returned. func (p *Pod) FindContainerByName(containerName string) *Container { for _, c := range p.Containers { if c.Name == containerName { return c } } return nil } // FindContainerByID returns a container in the pod with the given ContainerID. func (p *Pod) FindContainerByID(id ContainerID) *Container { for _, c := range p.Containers { if c.ID == id { return c } } return nil } // FindSandboxByID returns a sandbox in the pod with the given ContainerID. func (p *Pod) FindSandboxByID(id ContainerID) *Container { for _, c := range p.Sandboxes { if c.ID == id { return c } } return nil } // BuildPodFullName builds the pod full name from pod name and namespace. func BuildPodFullName(name, namespace string) string { return name + "_" + namespace } type PodStatus struct { ID string Name string Namespace string IPs []string ContainerStatuses []*Status SandboxStatuses []*runtimeapi.PodSandboxStatus } func (ps PodStatus) GetContainerStatus(ctrId string) *Status { for i := range ps.ContainerStatuses { cs := ps.ContainerStatuses[i] if cs.ID.ID == ctrId { return cs } } return nil } // Status represents the status of a container. type Status struct { // ID of the container. ID ContainerID // Name of the container. Name string // ID of the sandbox to which this container belongs. PodSandboxID string // Status of the container. State State // Creation time of the container. CreatedAt time.Time // Start time of the container. StartedAt time.Time // Finish time of the container. FinishedAt time.Time // Exit code of the container. ExitCode int // Name of the image, this also includes the tag of the image, // the expected form is "NAME:TAG". Image string // ID of the image. ImageID string // Hash of the container, used for comparison. Hash uint64 // Number of times that the container has been restarted. RestartCount int // A string explains why container is in such a status. Reason string // Message written by the container before exiting (stored in // TerminationMessagePath). Message string }