cri

package
v0.0.0-...-5c7ffcf Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 2, 2024 License: Apache-2.0 Imports: 15 Imported by: 0

README

介绍

这个包存放 容器运行时(cri) 的接口/模型/辅助函数. 子目录是一些具体的 cri 实现.

docker/ 标准docker pouch/ alibaba pouch

元数据定制

  1. app 的来源: 用户自定义方式(labels/ENV) > 标准 app 标签
  2. hostname 的来源: 用户自定义方式(labels/ENV) > pod.spec.Hostname

Documentation

Index

Constants

View Source
const (
	Runc = "runc"
	Rund = "rund"

	ContainerEngineFeatureCopy ContainerEngineFeature = iota
)
View Source
const (
	HackInit = iota
	HackIng
	HackOk
	HackSkipped
	HackRetryError
)
View Source
const (
	DefaultSampleOutputLength = 1024
)

Variables

View Source
var (
	ErrMultiBiz        = errors.New("multi biz containers")
	ErrNoBiz           = errors.New("no biz container")
	ErrNoSuchContainer = errors.New("no such container")
)

TODO 我们推出一个规范 让用户按我们规范做 就认为它是主容器

Functions

func NoPodError

func NoPodError(ns, pod string) error

func ShortContainerId

func ShortContainerId(cid string) string

func SortMountPointsByLongSourceFirst

func SortMountPointsByLongSourceFirst(mounts []*MountPoint)

func TransferToHostPathForContainer

func TransferToHostPathForContainer(c *Container, ctrPath string, respectSymbol bool) (string, error)

TransferToHostPathForContainer transfer container path into host path

Types

type AlpineStatus

type AlpineStatus uint8
const (
	AlpineStatusUnknown AlpineStatus = iota
	// AlpineStatusYes indicates that the container is an alpine-based container
	AlpineStatusYes
	// AlpineStatusNo indicates that the container is not an alpine-based container
	AlpineStatusNo
)

func (AlpineStatus) MarshalText

func (a AlpineStatus) MarshalText() ([]byte, error)

type Container

type Container struct {
	// container 可能会依赖底层实现, 因此这里不让它依赖具体的实现类
	Id string

	// 所属的pod
	Pod *Pod `json:"-"`

	// ContainerName example:
	// /k8s_POD_xxx
	// /k8s_xxx
	ContainerName string

	// K8sContainerName k8s container name
	K8sContainerName string

	// 常见状态
	State ContainerState

	// 容器常见配置
	Labels map[string]string
	Env    map[string]string

	// 挂载点 已经转换到hostfs
	Mounts []*MountPoint

	// docker 的 MergedDir 特有字段, 已经转换到hostfs
	MergedDir string

	ContainerRole ContainerRole

	// 该 container 所属的 pod 的 sandbox cid
	SandboxID string

	Tz TzInfo

	// 该容器是否已经被我们hack过
	Hacked int

	// pouch 场景, 只有登录到 container 里 才能获取 daemonset 的 hostname
	Hostname string

	// 运行时
	Runtime string

	// NetworkMode
	NetworkMode string

	// If PidMode is "host", it means that the container uses the pid namespace of the physical machine.
	PidMode string

	// docker json log: https://docs.docker.com/config/containers/logging/json-file/
	LogPath string

	// Attributes can be used to prevent arbitrary extension fields
	Attributes sync.Map

	// The number of zombie processes inside the container
	ZombieCount int

	// pid 1 process name
	Pid1Name string

	// Whether pid 1 has the ability to recycle zombie processes
	Pid1CanRecycleZombieProcesses bool
}

func (*Container) GetTz

func (c *Container) GetTz() *time.Location

GetTz returns the time zone used by this container

func (*Container) GetTzName

func (c *Container) GetTzName() string

func (*Container) IsBiz

func (c *Container) IsBiz() bool

func (*Container) IsRunning

func (c *Container) IsRunning() bool

func (*Container) IsSandbox

func (c *Container) IsSandbox() bool

func (*Container) IsSidecar

func (c *Container) IsSidecar() bool

func (*Container) ShortID

func (c *Container) ShortID() string

type ContainerEngine

type ContainerEngine interface {
	// Init initializes the engine
	Init() error

	// Type returns engine type, such as "docker" "containerd"
	Type() string

	ListAllContainers(ctx context.Context) ([]*EngineSimpleContainer, error)

	GetContainerDetail(ctx context.Context, cid string) (*EngineDetailContainer, error)

	Exec(ctx context.Context, c *Container, req ExecRequest) (ExecResult, error)

	// ExecAsync runs command in target container
	ExecAsync(ctx context.Context, c *Container, req ExecRequest) (ExecAsyncResult, error)

	// CopyToContainer copies file from src(in agent) to dst(in container)
	CopyToContainer(ctx context.Context, c *Container, src, dst string) error

	// CopyFromContainer copies file from src(in container) to dst(in agent)
	CopyFromContainer(ctx context.Context, c *Container, src, dst string) error

	// Supports checks if engine supports the specified feature
	Supports(feature ContainerEngineFeature) bool
}

ContainerEngine is an abstraction for docker/pouch/containerd

type ContainerEngineFeature

type ContainerEngineFeature uint8

type ContainerRole

type ContainerRole string

ContainerRole biz sidecar sandbox

const (
	// ContainerRoleBiz The target container is the pod's biz(main) container.
	ContainerRoleBiz ContainerRole = "biz"
	// ContainerRoleSidecar The target container is the pod's sidecar.
	ContainerRoleSidecar ContainerRole = "sidecar"
	// ContainerRoleSandbox The target container is the pod's sandbox.
	ContainerRoleSandbox ContainerRole = "sandbox"
)

type ContainerState

type ContainerState struct {
	Pid    int
	Status string
}

func (*ContainerState) IsRunning

func (s *ContainerState) IsRunning() bool

type EngineDetailContainer

type EngineDetailContainer struct {
	ID          string
	Name        string
	Labels      map[string]string
	Env         []string
	Source      interface{}
	IsSandbox   bool
	SandboxId   string
	Hostname    string
	Runtime     string
	NetworkMode string
	PidMode     string
	MergedDir   string
	LogPath     string
	Mounts      []*MountPoint
	State       ContainerState
}

EngineDetailContainer Contains the most common container properties, provided by almost all advanced container runtimes.

type EngineSimpleContainer

type EngineSimpleContainer struct {
	ID     string
	Labels map[string]string
	Source interface{}
}

type ExecAsyncResult

type ExecAsyncResult struct {
	Cmd    string
	Result chan ExecAsyncResultCode
	Stdout io.Reader
	Stderr io.Reader
}

type ExecAsyncResultCode

type ExecAsyncResultCode struct {
	Code int
	Err  error
}

type ExecRequest

type ExecRequest struct {
	Cmd        []string `json:"cmd"`
	Env        []string `json:"env"`
	WorkingDir string   `json:"workingDir"`
	Input      io.Reader
	// User is the user passed to docker exec, defaults to 'root'
	User                 string
	FixOut               bool
	NoWrapCmdWithTimeout bool
}

ExecRequest wraps parameters need for exec

type ExecResult

type ExecResult struct {
	Cmd      string
	ExitCode int
	Stdout   *bytes.Buffer
	Stderr   *bytes.Buffer
}

func (*ExecResult) SampleOutput

func (r *ExecResult) SampleOutput() (stdout string, stderr string)

func (*ExecResult) SampleOutputLength

func (r *ExecResult) SampleOutputLength(length int) (stdout string, stderr string)

type Interface

type Interface interface {
	MetaStore

	// CopyToContainer copies file to container
	CopyToContainer(ctx context.Context, c *Container, srcPath, dstPath string) error

	// CopyFromContainer copies file from container
	CopyFromContainer(ctx context.Context, c *Container, srcPath, dstPath string) error

	// Exec runs command in target container
	Exec(ctx context.Context, c *Container, req ExecRequest) (ExecResult, error)

	// ExecAsync runs command in target container
	ExecAsync(ctx context.Context, c *Container, req ExecRequest) (ExecAsyncResult, error)

	Start() error

	Stop()

	// Engine returns the underlying ContainerEngine
	Engine() ContainerEngine
}

Interface is the entrance of accessing pod/container meta and executing container commands.

type LocalAgentMeta

type LocalAgentMeta interface {
	Namespace() string
	PodName() string
	PodIP() string
	NodeName() string
	NodeIP() string
	Node() *v1.Node
	IsLocalPod(pod *v1.Pod) bool
}

LocalAgentMeta providers local agent basic info

type MetaListener

type MetaListener interface {
	OnAnyPodChanged()
}

type MetaStore

type MetaStore interface {
	// GetAllPods returns all local pods
	GetAllPods() []*Pod

	// GetPod queries one local pod by namespace and podName.
	// Returns an error if not found.
	GetPod(namespace, podName string) (*Pod, error)

	// GetPod queries one local pod by its sandbox id.
	// Returns an error if not found.
	GetPodBySandboxId(sandboxId string) (*Pod, error)

	// GetContainerByCid queries one container by cid
	GetContainerByCid(cid string) (*Container, bool)

	LocalAgentMeta() LocalAgentMeta

	Start() error

	Stop()

	AddListener(MetaListener)
	RemoveListener(MetaListener)
}

type MountPoint

type MountPoint struct {
	// Source is the path in agent (already starts with core.GetHostFs())
	Source string
	// Destination is the container path
	Destination string
	RW          bool
}

type NsEnterType

type NsEnterType uint8

type Pod

type Pod struct {
	// 我们依赖k8s的模型是肯定的
	*v1.Pod
	// All containers
	All []*Container `json:"-"`
	// TODO 考虑大小为1, 改名为main
	Biz     []*Container `json:"biz"`
	Sidecar []*Container `json:"sidecar"`
	// Sandbox container
	Sandbox  *Container `json:"sandbox"`
	Hostname string     `json:"hostname"`
}

func (*Pod) GetContainer

func (p *Pod) GetContainer(k8sContainerName string) (*Container, error)

func (*Pod) HostIP

func (p *Pod) HostIP() string

func (*Pod) IP

func (p *Pod) IP() string

func (*Pod) IsRunning

func (p *Pod) IsRunning() bool

func (*Pod) MainBiz

func (p *Pod) MainBiz() *Container

如果有且只有一个 main biz 就直接返回 否则返回 nil 让用户自己检查去

func (*Pod) MainBizE

func (p *Pod) MainBizE() (*Container, error)

type TzInfo

type TzInfo struct {
	Name   string
	TzObj  *time.Location `json:"-"`
	Zone   string
	Offset int

	// EtcLocaltime is timezone name read from /etc/localtime
	EtcLocaltime string
	// EnvTz is timezone name read from Env 'TZ'
	EnvTz string
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL