virtcontainers

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Nov 28, 2016 License: Apache-2.0 Imports: 20 Imported by: 0

README

Build Status Go Report Card Coverage Status GoDoc

virtcontainers

virtcontainers is a Go library that can be used to build hardware-virtualized container runtimes.

Background

The few existing VM-based container runtimes (Clear Containers, runv, rkt's kvm stage 1) all share the same hardware virtualization semantics but use different code bases to implement them. virtcontainers's goal is to factorize this code into a common Go library.

Ideally, VM-based container runtime implementations would become translation layers from the runtime specification they implement (e.g. the OCI runtime-spec or the Kubernetes CRI) to the virtcontainers API.

Out of scope

Implementing a container runtime tool is out of scope for this project. Any tools or executables in this repository are only provided for demonstration or testing purposes.

virtcontainers and CRI

virtcontainers's API is loosely inspired by the Kubernetes CRI because we believe it provides the right level of abstractions for containerized pods. However, despite the API similarities between the two projects, the goal of virtcontainers is not to build a CRI implementation, but instead to provide a generic, runtime-specification agnostic, hardware-virtualized containers library that other projects could leverage to implement CRI themselves.

Design

Pods

The virtcontainers execution unit is a pod, i.e. virtcontainers users start pods where containers will be running.

virtcontainers creates a pod by starting a virtual machine and setting the pod up within that environment. Starting a pod means launching all containers with the VM pod runtime environment.

Hypervisors

The virtcontainers package relies on hypervisors to start and stop virtual machine where pods will be running. An hypervisor is defined by an Hypervisor interface implementation, and the default implementation is the QEMU one.

Agents

During the lifecycle of a container, the runtime running on the host needs to interact with the virtual machine guest OS in order to start new commands to be executed as part of a given container workload, set new networking routes or interfaces, fetch a container standard or error output, and so on. There are many existing and potential solutions to resolve that problem and virtcontainers abstracts this through the Agent interface.

API

The high level virtcontainers API is the following one:

Pod API
  • CreatePod(podConfig PodConfig) creates a Pod. The Pod is prepared and will run into a virtual machine. It is not started, i.e. the VM is not running after CreatePod() is called.

  • DeletePod(podID string) deletes a Pod. The function will fail if the Pod is running. In that case StopPod() needs to be called first.

  • StartPod(podID string) starts an already created Pod.

  • StopPod(podID string) stops an already running Pod.

  • ListPod() lists all running Pods on the host.

  • EnterPod(cmd Cmd) enters a Pod root filesystem and runs a given command.

  • PodStatus(podID string) returns a detailed Pod status.

Container API
  • CreateContainer(podID string, container ContainerConfig) creates a Container on a given Pod.

  • DeleteContainer(podID, containerID string) deletes a Container from a Pod. If the container is running it needs to be stopped first.

  • StartContainer(podID, containerID string) starts an already created container.

  • StopContainer(podID, containerID string) stops an already running container.

  • EnterContainer(podID, containerID string, cmd Cmd) enters an already running container and runs a given command.

  • ContainerStatus(podID, containerID string) returns a detailed container status.

An example tool using the virtcontainers API is provided in the hack/virtc package.

Documentation

Overview

Package virtcontainers manages hardware virtualized containers. Each container belongs to a set of containers sharing the same networking namespace and storage, also known as a pod.

Virtcontainers pods are hardware virtualized, i.e. they run on virtual machines. Virtcontainers will create one VM per pod, and containers will be created as processes within the pod VM.

The virtcontainers package manages both pods and containers lifecycles.

Example (CreateAndStartPod)

This example creates and starts a single container pod, using qemu as the hypervisor and hyperstart as the VM agent.

package main

import (
	"fmt"
	"strings"

	vc "github.com/sameo/virtcontainers"
)

const containerRootfs = "/var/lib/container/bundle/"

// This example creates and starts a single container pod,
// using qemu as the hypervisor and hyperstart as the VM agent.
func main() {
	envs := []vc.EnvVar{
		{
			Var:   "PATH",
			Value: "/bin:/usr/bin:/sbin:/usr/sbin",
		},
	}

	cmd := vc.Cmd{
		Args:    strings.Split("/bin/sh", " "),
		Envs:    envs,
		WorkDir: "/",
	}

	// Define the container command and bundle.
	container := vc.ContainerConfig{
		ID:     "1",
		RootFs: containerRootfs,
		Cmd:    cmd,
	}

	// Sets the hypervisor configuration.
	hypervisorConfig := vc.HypervisorConfig{
		KernelPath:     "/usr/share/clear-containers/vmlinux.container",
		ImagePath:      "/usr/share/clear-containers/clear-containers.img",
		HypervisorPath: "/usr/bin/qemu-lite-system-x86_64",
	}

	// Use hyperstart default values for the agent.
	agConfig := vc.HyperConfig{}

	// VM resources
	vmConfig := vc.Resources{
		VCPUs:  4,
		Memory: 1024,
	}

	// The pod configuration:
	// - One container
	// - Hypervisor is QEMU
	// - Agent is hyperstart
	podConfig := vc.PodConfig{
		VMConfig: vmConfig,

		HypervisorType:   vc.QemuHypervisor,
		HypervisorConfig: hypervisorConfig,

		AgentType:   vc.HyperstartAgent,
		AgentConfig: agConfig,

		Containers: []vc.ContainerConfig{container},
	}

	_, err := vc.RunPod(podConfig)
	if err != nil {
		fmt.Printf("Could not run pod: %s", err)
	}

	return
}
Output:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func ListPod

func ListPod() error

ListPod is the virtcontainers pod listing entry point.

func StatusContainer

func StatusContainer(podID, containerID string) error

StatusContainer is the virtcontainers container status entry point. StatusContainer returns a detailed container status.

func StatusPod

func StatusPod(podID string) error

StatusPod is the virtcontainers pod status entry point.

Types

type AgentType

type AgentType string

AgentType describes the type of guest agent a Pod should run.

const (
	// NoopAgentType is the No-Op agent.
	NoopAgentType AgentType = "noop"

	// SSHdAgent is the SSH daemon agent.
	SSHdAgent AgentType = "sshd"

	// HyperstartAgent is the Hyper hyperstart agent.
	HyperstartAgent AgentType = "hyperstart"
)

func (*AgentType) Set

func (agentType *AgentType) Set(value string) error

Set sets an agent type based on the input string.

func (*AgentType) String

func (agentType *AgentType) String() string

String converts an agent type to a string.

type Cmd

type Cmd struct {
	Args    []string
	Envs    []EnvVar
	WorkDir string

	User  string
	Group string
}

Cmd represents a command to execute in a running container.

type Container

type Container struct {
	// contains filtered or unexported fields
}

Container is composed of a set of containers and a runtime environment. A Container can be created, deleted, started, stopped, listed, entered, paused and restored.

func CreateContainer

func CreateContainer(podID string, containerConfig ContainerConfig) (*Container, error)

CreateContainer is the virtcontainers container creation entry point. CreateContainer creates a container on a given pod.

func DeleteContainer

func DeleteContainer(podID, containerID string) (*Container, error)

DeleteContainer is the virtcontainers container deletion entry point. DeleteContainer deletes a Container from a Pod. If the container is running, it needs to be stopped first.

func EnterContainer

func EnterContainer(podID, containerID string, cmd Cmd) (*Container, error)

EnterContainer is the virtcontainers container command execution entry point. EnterContainer enters an already running container and runs a given command.

func StartContainer

func StartContainer(podID, containerID string) (*Container, error)

StartContainer is the virtcontainers container starting entry point. StartContainer starts an already created container.

func StopContainer

func StopContainer(podID, containerID string) (*Container, error)

StopContainer is the virtcontainers container stopping entry point. StopContainer stops an already running container.

func (*Container) ID

func (c *Container) ID() string

ID returns the container identifier string.

type ContainerConfig

type ContainerConfig struct {
	ID string

	// RootFs is the container workload image on the host.
	RootFs string

	// Interactive specifies if the container runs in the foreground.
	Interactive bool

	// Console is a console path provided by the caller.
	Console string

	// Cmd specifies the command to run on a container
	Cmd Cmd
}

ContainerConfig describes one container runtime configuration.

type EnvVar

type EnvVar struct {
	Var   string
	Value string
}

EnvVar is a key/value structure representing a command environment variable.

type ExecInfo

type ExecInfo struct {
	Container string            `json:"container"`
	Process   hyperJson.Process `json:"process"`
}

ExecInfo is the structure corresponding to the format expected by hyperstart to execute a command on the guest.

type HyperConfig

type HyperConfig struct {
	SockCtlName string
	SockTtyName string
	Volumes     []Volume
	Sockets     []Socket
}

HyperConfig is a structure storing information needed for hyperstart agent initialization.

type HypervisorConfig

type HypervisorConfig struct {
	// KernelPath is the guest kernel host path.
	KernelPath string

	// ImagePath is the guest image host path.
	ImagePath string

	// HypervisorPath is the hypervisor executable host path.
	HypervisorPath string

	// KernelParams are additional guest kernel parameters.
	KernelParams []Param

	// HypervisorParams are additional hypervisor parameters.
	HypervisorParams []Param
}

HypervisorConfig is the hypervisor configuration.

type HypervisorType

type HypervisorType string

HypervisorType describes an hypervisor type.

const (
	// QemuHypervisor is the QEMU hypervisor.
	QemuHypervisor HypervisorType = "qemu"

	// MockHypervisor is a mock hypervisor for testing purposes
	MockHypervisor HypervisorType = "mock"
)

func (*HypervisorType) Set

func (hType *HypervisorType) Set(value string) error

Set sets an hypervisor type based on the input string.

func (*HypervisorType) String

func (hType *HypervisorType) String() string

String converts an hypervisor type to a string.

type Param

type Param struct {
	// contains filtered or unexported fields
}

Param is a key/value representation for hypervisor and kernel parameters.

type Pod

type Pod struct {
	// contains filtered or unexported fields
}

Pod is composed of a set of containers and a runtime environment. A Pod can be created, deleted, started, stopped, listed, entered, paused and restored.

func CreatePod

func CreatePod(podConfig PodConfig) (*Pod, error)

CreatePod is the virtcontainers pod creation entry point. CreatePod creates a pod and its containers. It does not start them.

func DeletePod

func DeletePod(podID string) (*Pod, error)

DeletePod is the virtcontainers pod deletion entry point. DeletePod will stop an already running container and then delete it.

func RunPod

func RunPod(podConfig PodConfig) (*Pod, error)

RunPod is the virtcontainers pod running entry point. RunPod creates a pod and its containers and then it starts them.

func StartPod

func StartPod(podID string) (*Pod, error)

StartPod is the virtcontainers pod starting entry point. StartPod will talk to the given hypervisor to start an existing pod and all its containers. It returns the pod ID.

func StopPod

func StopPod(podID string) (*Pod, error)

StopPod is the virtcontainers pod stopping entry point. StopPod will talk to the given agent to stop an existing pod and destroy all containers within that pod.

func (*Pod) ID

func (p *Pod) ID() string

ID returns the pod identifier string.

type PodConfig

type PodConfig struct {
	ID string

	// VMConfig is the VM configuration to set for this pod.
	VMConfig Resources

	HypervisorType   HypervisorType
	HypervisorConfig HypervisorConfig

	AgentType   AgentType
	AgentConfig interface{}

	// Rootfs is the pod root file system in the host.
	// This can be left empty if we only have a set of containers
	// workload images and expect the agent to aggregate them into
	// a pod from the guest.
	RootFs string

	// Volumes is a list of shared volumes between the host and the Pod.
	Volumes []Volume

	// Containers describe the list of containers within a Pod.
	// This list can be empty and populated by adding containers
	// to the Pod a posteriori.
	Containers []ContainerConfig
}

PodConfig is a Pod configuration.

type Resources

type Resources struct {
	// VCPUs is the number of available virtual CPUs.
	VCPUs uint

	// Memory is the amount of available memory in MiB.
	Memory uint
}

Resources describes VM resources configuration.

type Socket

type Socket struct {
	DeviceID string
	ID       string
	HostPath string
	Name     string
}

Socket defines a socket to communicate between the host and any process inside the VM.

type Sockets

type Sockets []Socket

Sockets is a Socket list.

func (*Sockets) Set

func (s *Sockets) Set(sockStr string) error

Set assigns socket values from string to a Socket.

func (*Sockets) String

func (s *Sockets) String() string

String converts a Socket to a string.

type SpawnerType

type SpawnerType string

SpawnerType describes the type of guest agent a Pod should run.

const (
	// NsEnter is the nsenter spawner type
	NsEnter SpawnerType = "nsenter"
)

func (*SpawnerType) Set

func (spawnerType *SpawnerType) Set(value string) error

Set sets an agent type based on the input string.

func (*SpawnerType) String

func (spawnerType *SpawnerType) String() string

String converts an agent type to a string.

type SshdConfig

type SshdConfig struct {
	Username    string
	PrivKeyFile string
	Server      string
	Port        string
	Protocol    string

	Spawner SpawnerType
}

SshdConfig is a structure storing information needed for sshd agent initialization.

type State

type State struct {
	State stateString `json:"state"`
}

State is a pod state structure.

type Volume

type Volume struct {
	// MountTag is a label used as a hint to the guest.
	MountTag string

	// HostPath is the host filesystem path for this volume.
	HostPath string
}

Volume is a shared volume between the host and the VM, defined by its mount tag and its host path.

type Volumes

type Volumes []Volume

Volumes is a Volume list.

func (*Volumes) Set

func (v *Volumes) Set(volStr string) error

Set assigns volume values from string to a Volume.

func (*Volumes) String

func (v *Volumes) String() string

String converts a Volume to a string.

Directories

Path Synopsis
hack

Jump to

Keyboard shortcuts

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