chore: implement VolumesConstraint to only schedule on machines with required volumes

This commit is contained in:
Pavel Sviderski
2025-04-17 21:34:52 +10:00
parent 03c2681792
commit 1acb0ff110
5 changed files with 288 additions and 5 deletions
+9
View File
@@ -52,6 +52,15 @@ type ServiceSpec struct {
Volumes []VolumeSpec
}
func (s *ServiceSpec) Volume(name string) (VolumeSpec, bool) {
for _, v := range s.Volumes {
if v.Name == name {
return v, true
}
}
return VolumeSpec{}, false
}
func (s *ServiceSpec) SetDefaults() ServiceSpec {
spec := s.Clone()
+13 -3
View File
@@ -14,7 +14,7 @@ import (
const (
// VolumeTypeBind is the type for mounting a host path.
VolumeTypeBind = "bind"
// VolumeTypeVolume is the type for mounting a managed volume.
// 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"
@@ -42,7 +42,7 @@ type BindOptions struct {
Recursive string `json:",omitempty"`
}
// VolumeOptions represents options for a managed volume.
// VolumeOptions represents options for a named Docker volume.
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.
@@ -50,7 +50,7 @@ type VolumeOptions struct {
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 managed volume to use. If not specified, defaults to the VolumeSpec.Name.
// 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"`
@@ -58,6 +58,16 @@ type VolumeOptions struct {
SubPath string `json:",omitempty"`
}
func (v *VolumeSpec) DockerVolumeName() string {
if v.Type != VolumeTypeVolume {
return ""
}
if v.VolumeOptions != nil && v.VolumeOptions.Name != "" {
return v.VolumeOptions.Name
}
return v.Name
}
func (v *VolumeSpec) SetDefaults() VolumeSpec {
spec := v.Clone()
+61 -1
View File
@@ -1,8 +1,10 @@
package scheduler
import (
"slices"
"strings"
"github.com/docker/docker/api/types/volume"
"github.com/psviderski/uncloud/pkg/api"
)
@@ -15,6 +17,7 @@ type Constraint interface {
Description() string
}
// constraintsFromSpec derives scheduling constraints from the service specification.
func constraintsFromSpec(spec api.ServiceSpec) []Constraint {
var constraints []Constraint
@@ -24,7 +27,18 @@ func constraintsFromSpec(spec api.ServiceSpec) []Constraint {
})
}
// TODO: inspect and add VolumeConstraint.
// Add a VolumesConstraint for named Docker volumes that are mounted in the container.
var volumes []api.VolumeSpec
for _, m := range spec.Container.VolumeMounts {
if v, ok := spec.Volume(m.VolumeName); ok && v.Type == api.VolumeTypeVolume {
volumes = append(volumes, v)
}
}
if len(volumes) > 0 {
constraints = append(constraints, &VolumesConstraint{
Volumes: volumes,
})
}
return constraints
}
@@ -45,5 +59,51 @@ func (c *PlacementConstraint) Evaluate(machine *Machine) bool {
}
func (c *PlacementConstraint) Description() string {
slices.Sort(c.Machines)
return "Placement constraint by machines: " + strings.Join(c.Machines, ", ")
}
// VolumesConstraint restricts container placement to machines that have the required named Docker volumes.
type VolumesConstraint struct {
// Volumes is a list of named Docker volumes of type api.VolumeTypeVolume that must exist on the machine.
Volumes []api.VolumeSpec
}
// Evaluate determines if a machine has all the required volumes.
// Returns true if all required volumes exist on the machine or if there are no required volumes.
func (c *VolumesConstraint) Evaluate(machine *Machine) bool {
if len(c.Volumes) == 0 {
return true
}
for _, v := range c.Volumes {
if v.Type != api.VolumeTypeVolume {
continue
}
// TODO: should we check the volume driver to be local or any matched volume by name is ok?
if !slices.ContainsFunc(machine.Volumes, func(vol volume.Volume) bool {
return vol.Name == v.DockerVolumeName()
}) {
return false
}
}
return true
}
func (c *VolumesConstraint) Description() string {
volumeNames := make([]string, 0, len(c.Volumes))
for _, v := range c.Volumes {
if v.Type == api.VolumeTypeVolume {
volumeNames = append(volumeNames, v.DockerVolumeName())
}
}
slices.Sort(volumeNames)
if len(volumeNames) == 0 {
return "No volumes constraint"
}
return "Volumes: " + strings.Join(volumeNames, ", ")
}
+2
View File
@@ -53,5 +53,7 @@ func (s *ServiceScheduler) evaluateConstraints(machine *Machine) bool {
}
func (s *ServiceScheduler) ScheduleContainer() ([]*pb.MachineInfo, error) {
// TODO: organise machines in a heap and supply a sort function from the strategy. Each scheduled container
// should update the machine and reorder it in the heap.
return nil, errors.New("not implemented")
}