api

package
v0.12.0 Latest Latest
Warning

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

Go to latest
Published: Aug 28, 2025 License: Apache-2.0 Imports: 24 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// DockerNetworkName is the name of the Docker network used by uncloud. Keep the value in sync with NetworkName
	// in internal/machine/docker/manager.go.
	DockerNetworkName = "uncloud"
	LabelManaged      = "uncloud.managed"
	LabelServiceID    = "uncloud.service.id"
	LabelServiceName  = "uncloud.service.name"
	LabelServiceMode  = "uncloud.service.mode"
	LabelServicePorts = "uncloud.service.ports"
)
View Source
const (
	PortModeIngress = "ingress"
	PortModeHost    = "host"

	ProtocolHTTP  = "http"
	ProtocolHTTPS = "https"
	ProtocolTCP   = "tcp"
	ProtocolUDP   = "udp"
)
View Source
const (
	MilliCore = 1_000_000
	Core      = 1000 * MilliCore
)
View Source
const (
	ServiceModeReplicated = "replicated"
	ServiceModeGlobal     = "global"

	// PullPolicyAlways means the image is always pulled from the registry.
	PullPolicyAlways = "always"
	// PullPolicyMissing means the image is pulled from the registry only if it's not available on the machine where
	// a container is started. This is the default pull policy.
	// TODO: make each machine aware of the images on other machines and it possible to pull from them.
	// 	Pull from the registry only if the image is missing on all machines.
	PullPolicyMissing = "missing"
	// PullPolicyNever means the image is never pulled from the registry. A service with this pull policy can only be
	// deployed to machines where the image is already available.
	// TODO: see the TODO above for PullPolicyMissing. Pull from other machines in the cluster if available.
	PullPolicyNever = "never"
)
View Source
const (
	// VolumeTypeBind is the type for mounting a host path.
	VolumeTypeBind = "bind"
	// VolumeTypeVolume is the type for mounting a named Docker volume.
	VolumeTypeVolume = "volume"
	// VolumeTypeTmpfs is the type for mounting a temporary file system stored in the host memory.
	VolumeTypeTmpfs = "tmpfs"

	// VolumeDriverLocal is the default volume driver for local named Docker volumes.
	VolumeDriverLocal = "local"
)

Variables

View Source
var ErrNotFound = errors.New("not found")

Functions

func PortsEqual

func PortsEqual(a, b []PortSpec) bool

PortsEqual returns true if the two port sets are equal. The order of the ports is not important.

func ValidateServiceID

func ValidateServiceID(id string) bool

Types

type BindOptions added in v0.6.0

type BindOptions struct {
	// HostPath is the absolute path on the host filesystem.
	HostPath string
	// CreateHostPath indicates whether the host path should be created if it doesn't exist.
	// If false, deployment will fail if the path doesn't exist.
	CreateHostPath bool              `json:",omitempty"`
	Propagation    mount.Propagation `json:",omitempty"`
	Recursive      string            `json:",omitempty"`
}

BindOptions represents options for a bind volume.

type CaddySpec added in v0.11.1

type CaddySpec struct {
	// Config contains the Caddy config (Caddyfile) content. It must not conflict with the Caddy configs
	// of other services.
	Config string
}

CaddySpec is the Caddy reverse proxy configuration for a service.

func (*CaddySpec) Equals added in v0.11.1

func (c *CaddySpec) Equals(other *CaddySpec) bool

type Container

type Container struct {
	types.ContainerJSON
	// contains filtered or unexported fields
}

func (*Container) CreatedTime added in v0.12.0

func (c *Container) CreatedTime() time.Time

CreatedTime returns the time when the container was created parsed from the Created field.

func (*Container) Healthy

func (c *Container) Healthy() bool

Healthy determines if the container is running and healthy. A running container with no health check configured is considered healthy.

func (*Container) HumanState

func (c *Container) HumanState() (string, error)

HumanState returns a human-readable description of the container's state. Based on the Docker implementation: https://github.com/moby/moby/blob/b343d235a0a1f30c8f05b1d651238e72158dc25d/container/state.go#L79-L113

func (*Container) UncloudNetworkIP added in v0.7.0

func (c *Container) UncloudNetworkIP() netip.Addr

UncloudNetworkIP returns the IP address of the container in the uncloud Docker network.

func (*Container) UnmarshalJSON

func (c *Container) UnmarshalJSON(data []byte) error

type ContainerClient

type ContainerClient interface {
	CreateContainer(
		ctx context.Context, serviceID string, spec ServiceSpec, machineID string,
	) (container.CreateResponse, error)
	InspectContainer(ctx context.Context, serviceNameOrID, containerNameOrID string) (MachineServiceContainer, error)
	RemoveContainer(ctx context.Context, serviceNameOrID, containerNameOrID string, opts container.RemoveOptions) error
	StartContainer(ctx context.Context, serviceNameOrID, containerNameOrID string) error
	StopContainer(ctx context.Context, serviceNameOrID, containerNameOrID string, opts container.StopOptions) error
}

type ContainerResources added in v0.7.0

type ContainerResources struct {
	// CPU is the maximum amount of CPU nanocores (1000000000 = 1 CPU core) the container can use.
	CPU int64
	// Memory is the maximum amount of memory (in bytes) the container can use.
	Memory int64
	// MemoryReservation is the minimum amount of memory (in bytes) the container needs to run efficiently.
	// TODO: implement a placement constraint that checks available memory on machines.
	MemoryReservation int64
}

type ContainerSpec

type ContainerSpec struct {
	// Command overrides the default CMD of the image to be executed when running a container.
	Command []string
	// Entrypoint overrides the default ENTRYPOINT of the image.
	Entrypoint []string
	// Env defines the environment variables to set inside the container.
	Env   EnvVars
	Image string
	// Run a custom init inside the container. If nil, use the daemon's configured settings.
	Init *bool
	// LogDriver overrides the default logging driver for the container. Each Docker daemon can have its own default.
	LogDriver *LogDriver
	// Privileged gives extended privileges to the container. This is a security risk and should be used with caution.
	Privileged bool
	// PullPolicy determines when to pull the image from the registry or use the image already available in the cluster.
	// Default is PullPolicyMissing if empty.
	PullPolicy string
	// Resource allocation for the container.
	Resources ContainerResources
	// User overrides the default user of the image used to run the container. Format: user|UID[:group|GID].
	User string
	// VolumeMounts specifies how volumes are mounted into the container filesystem.
	// Each mount references a volume defined in ServiceSpec.Volumes.
	VolumeMounts []VolumeMount
	// Volumes is list of data volumes to mount into the container.
	// TODO(lhf): delete all usage, has been replaced with []VolumeMounts.
	Volumes []string
}

ContainerSpec defines the desired state of a container in a service. ATTENTION: after changing this struct, verify if deploy.EvalContainerSpecChange needs to be updated.

func (*ContainerSpec) Clone

func (s *ContainerSpec) Clone() ContainerSpec

func (*ContainerSpec) Equals

func (s *ContainerSpec) Equals(spec ContainerSpec) bool

func (*ContainerSpec) SetDefaults

func (s *ContainerSpec) SetDefaults() ContainerSpec

SetDefaults returns a copy of the container spec with default values set.

func (*ContainerSpec) Validate

func (s *ContainerSpec) Validate() error

type DNSClient

type DNSClient interface {
	GetDomain(ctx context.Context) (string, error)
}

type EnvVars added in v0.6.0

type EnvVars map[string]string

func (EnvVars) ToSlice added in v0.6.0

func (e EnvVars) ToSlice() []string

ToSlice converts the environment variables to a slice of strings in the format "key=value".

type ImageClient

type ImageClient interface {
	InspectImage(ctx context.Context, id string) ([]MachineImage, error)
	InspectRemoteImage(ctx context.Context, id string) ([]MachineRemoteImage, error)
}

type LogDriver added in v0.7.0

type LogDriver struct {
	// Name of the logging driver to use.
	Name string
	// Options is the configuration options to pass to the logging driver.
	Options map[string]string
}

type MachineClient

type MachineClient interface {
	InspectMachine(ctx context.Context, id string) (*pb.MachineMember, error)
	ListMachines(ctx context.Context, filter *MachineFilter) (MachineMembersList, error)
	UpdateMachine(ctx context.Context, req *pb.UpdateMachineRequest) (*pb.MachineInfo, error)
	RenameMachine(ctx context.Context, nameOrID, newName string) (*pb.MachineInfo, error)
}

type MachineFilter added in v0.6.0

type MachineFilter struct {
	// Available filters machines that are not DOWN.
	Available bool
	// NamesOrIDs filters machines by their names or IDs.
	NamesOrIDs []string
}

MachineFilter defines criteria to filter machines in ListMachines.

type MachineImage

type MachineImage struct {
	Metadata *pb.Metadata
	Image    types.ImageInspect
}

type MachineMembersList added in v0.6.0

type MachineMembersList []*pb.MachineMember

func ProxyMachinesContext

func ProxyMachinesContext(
	ctx context.Context, cli MachineClient, namesOrIDs []string,
) (context.Context, MachineMembersList, error)

ProxyMachinesContext returns a new context that proxies gRPC requests to the specified machines. If namesOrIDs is nil, all machines are included.

func (MachineMembersList) FindByManagementIP added in v0.6.0

func (m MachineMembersList) FindByManagementIP(ip string) *pb.MachineMember

func (MachineMembersList) FindByNameOrID added in v0.6.0

func (m MachineMembersList) FindByNameOrID(nameOrID string) *pb.MachineMember

type MachineRemoteImage

type MachineRemoteImage struct {
	Metadata *pb.Metadata
	Image    RemoteImage
}

MachineRemoteImage represents an image in a remote registry fetched by a particular machine.

type MachineServiceContainer

type MachineServiceContainer struct {
	MachineID string
	Container ServiceContainer
}

type MachineVolume added in v0.6.0

type MachineVolume struct {
	// MachineID is the ID of the machine where the volume exists.
	MachineID string
	// MachineName is the name of the machine where the volume exists.
	MachineName string
	// Volume is the Docker volume model.
	Volume volume.Volume
}

MachineVolume represents a volume on a specific machine.

func (*MachineVolume) MatchesFilter added in v0.6.0

func (v *MachineVolume) MatchesFilter(filter *VolumeFilter) bool

MatchesFilter checks if a volume matches the specified filter criteria.

type Placement added in v0.6.0

type Placement struct {
	// Machines is a list of machine names or IDs where service containers are allowed to be deployed.
	// If empty, containers can be deployed to any available machine in the cluster.
	Machines []string `json:",omitempty"`
}

Placement defines the placement constraints for service containers.

type PortSpec

type PortSpec struct {
	// Hostname specifies the DNS name that will route to this service. Only valid in ingress mode.
	Hostname string
	// HostIP is the host IP to bind the PublishedPort to. Only valid in host mode.
	HostIP netip.Addr
	// PublishedPort is the port number exposed outside the container.
	// In ingress mode, this is the load balancer port. In host mode, this is the port bound on the host.
	PublishedPort uint16
	// ContainerPort is the port inside the container that the service listens on.
	ContainerPort uint16
	// Protocol specifies the network protocol.
	Protocol string
	// Mode specifies how the port is published.
	Mode string
}

func ParsePortSpec

func ParsePortSpec(port string) (PortSpec, error)

func (*PortSpec) String

func (p *PortSpec) String() (string, error)

String returns the port specification in the -p/--publish flag format. Format: [hostname:][load_balancer_port:]container_port/protocol for ingress mode (default) or [host_ip:]:host_port:container_port/protocol@host for host mode.

func (*PortSpec) Validate

func (p *PortSpec) Validate() error

type RemoteImage

type RemoteImage struct {
	Reference     reference.Canonical
	IndexManifest *v1.Index
	ImageManifest *v1.Manifest
}

RemoteImage represents an image in a remote registry. The canonical reference includes the image digest. Either IndexManifest or ImageManifest must be set depending on whether the reference points to an index (multi-platform image) or a single-platform image.

type RunServiceResponse added in v0.6.0

type RunServiceResponse struct {
	ID   string
	Name string
}

type Service

type Service struct {
	ID         string
	Name       string
	Mode       string
	Containers []MachineServiceContainer
}

func ServiceFromProto

func ServiceFromProto(s *pb.Service) (Service, error)

func (*Service) Endpoints

func (s *Service) Endpoints() []string

Endpoints returns the exposed HTTP and HTTPS endpoints of the service.

type ServiceClient

type ServiceClient interface {
	RunService(ctx context.Context, spec ServiceSpec) (RunServiceResponse, error)
	InspectService(ctx context.Context, id string) (Service, error)
	RemoveService(ctx context.Context, id string) error
}

type ServiceContainer

type ServiceContainer struct {
	Container
	ServiceSpec ServiceSpec
}

func (*ServiceContainer) ConflictingServicePorts

func (c *ServiceContainer) ConflictingServicePorts(ports []PortSpec) ([]PortSpec, error)

ConflictingServicePorts returns a list of service ports that conflict with the given ports.

func (*ServiceContainer) ServiceID

func (c *ServiceContainer) ServiceID() string

ServiceID returns the ID of the service this container belongs to.

func (*ServiceContainer) ServiceMode

func (c *ServiceContainer) ServiceMode() string

ServiceMode returns the replication mode of the service this container belongs to.

func (*ServiceContainer) ServiceName

func (c *ServiceContainer) ServiceName() string

ServiceName returns the name of the service this container belongs to.

func (*ServiceContainer) ServicePorts

func (c *ServiceContainer) ServicePorts() ([]PortSpec, error)

ServicePorts returns the ports this container publishes as part of its service.

func (*ServiceContainer) UnmarshalJSON added in v0.12.0

func (c *ServiceContainer) UnmarshalJSON(data []byte) error

UnmarshalJSON implements custom unmarshalling for ServiceContainer to override the custom unmarshaler of the embedded Container field.

type ServiceSpec

type ServiceSpec struct {
	// Caddy is the optional Caddy reverse proxy configuration for the service.
	// Caddy and Ports cannot be specified simultaneously.
	Caddy *CaddySpec `json:",omitempty"`
	// Container defines the desired state of each container in the service.
	Container ContainerSpec
	// Mode is the replication mode of the service. Default is ServiceModeReplicated if empty.
	Mode string
	Name string
	// Placement defines the placement constraints for the service.
	Placement Placement
	// Ports defines what service ports to publish to make the service accessible outside the cluster.
	// Caddy and Ports cannot be specified simultaneously.
	Ports []PortSpec
	// Replicas is the number of containers to run for the service. Only valid for a replicated service.
	Replicas uint `json:",omitempty"`
	// Volumes is list of data volumes that can be mounted into the container.
	Volumes []VolumeSpec
}

ServiceSpec defines the desired state of a service. ATTENTION: after changing this struct, verify if deploy.EvalContainerSpecChange needs to be updated.

func (*ServiceSpec) CaddyConfig added in v0.12.0

func (s *ServiceSpec) CaddyConfig() string

CaddyConfig returns the Caddy reverse proxy configuration for the service or an empty string if it's not defined.

func (*ServiceSpec) Clone

func (s *ServiceSpec) Clone() ServiceSpec

func (*ServiceSpec) MountedDockerVolumes added in v0.6.0

func (s *ServiceSpec) MountedDockerVolumes() []VolumeSpec

MountedDockerVolumes returns the list of volumes of VolumeTypeVolume type that are mounted into the container.

func (*ServiceSpec) SetDefaults

func (s *ServiceSpec) SetDefaults() ServiceSpec

func (*ServiceSpec) Validate

func (s *ServiceSpec) Validate() error

func (*ServiceSpec) Volume added in v0.6.0

func (s *ServiceSpec) Volume(name string) (VolumeSpec, bool)

type VolumeClient added in v0.6.0

type VolumeClient interface {
	CreateVolume(ctx context.Context, machineNameOrID string, opts volume.CreateOptions) (MachineVolume, error)
	ListVolumes(ctx context.Context, filter *VolumeFilter) ([]MachineVolume, error)
	RemoveVolume(ctx context.Context, machineNameOrID, volumeName string, force bool) error
}

type VolumeFilter added in v0.6.0

type VolumeFilter struct {
	// Driver filters volumes by storage driver name.
	Driver string
	// Labels filters volumes by label key-value pairs. Volumes must match all labels.
	Labels map[string]string
	// MachineIDs filters volumes to those on the specified machines (names or IDs).
	Machines []string
	// Names filters volumes by name. Volumes must match one of the names.
	Names []string
}

VolumeFilter defines criteria to filter volumes in ListVolumes.

type VolumeMount added in v0.6.0

type VolumeMount struct {
	// VolumeName references a volume defined in ServiceSpec.Volumes by its Name field.
	VolumeName string
	// ContainerPath is the absolute path where the volume is mounted in the container.
	ContainerPath string
	// ReadOnly indicates whether the volume should be mounted read-only.
	// If false (default), the volume is mounted read-write.
	ReadOnly bool `json:",omitempty"`
}

VolumeMount defines how a volume is mounted into a container.

func (*VolumeMount) Validate added in v0.6.0

func (m *VolumeMount) Validate() error

type VolumeOptions added in v0.6.0

type VolumeOptions struct {
	// Driver specifies the volume driver and its options for volume creation.
	// TODO: It seems we don't really need Driver and Labels if we only support externally managed volumes.
	//  However we may need them in the future if we add support for isolated container-scoped volumes.
	Driver *mount.Driver `json:",omitempty"`
	// Labels are key-value metadata to apply to the volume if creating a new volume.
	Labels map[string]string `json:",omitempty"`
	// Name of the named Docker volume to use. If not specified, defaults to the VolumeSpec.Name.
	Name string `json:",omitempty"`
	// NoCopy prevents automatic copying of data from the container mount path to the volume.
	NoCopy bool `json:",omitempty"`
	// SubPath is the path within the volume to mount instead of its root.
	SubPath string `json:",omitempty"`
}

VolumeOptions represents options for a named Docker volume.

type VolumeSpec added in v0.6.0

type VolumeSpec struct {
	// Name is the volume name used to reference this volume in container mounts.
	Name          string
	Type          string
	BindOptions   *BindOptions        `json:",omitempty"`
	TmpfsOptions  *mount.TmpfsOptions `json:",omitempty"`
	VolumeOptions *VolumeOptions      `json:",omitempty"`
}

VolumeSpec defines a volume mount specification. As of April 2025, the volume must be created before deploying a service using it.

func (*VolumeSpec) Clone added in v0.6.0

func (v *VolumeSpec) Clone() VolumeSpec

func (*VolumeSpec) DockerVolumeName added in v0.6.0

func (v *VolumeSpec) DockerVolumeName() string

func (*VolumeSpec) Equals added in v0.6.0

func (v *VolumeSpec) Equals(other VolumeSpec) bool

func (*VolumeSpec) MatchesDockerVolume added in v0.6.0

func (v *VolumeSpec) MatchesDockerVolume(vol volume.Volume) bool

MatchesDockerVolume checks if this VolumeSpec is compatible with the given named Docker volume. In other words, it checks if the spec could be used to mount the volume.

func (*VolumeSpec) SetDefaults added in v0.6.0

func (v *VolumeSpec) SetDefaults() VolumeSpec

func (*VolumeSpec) Validate added in v0.6.0

func (v *VolumeSpec) Validate() error

Jump to

Keyboard shortcuts

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