This commit is contained in:
Pavel Sviderski
2025-04-20 11:15:07 +10:00
parent 948201cb6f
commit 8ee551ca8f
3 changed files with 59 additions and 298 deletions
-114
View File
@@ -193,46 +193,6 @@ func (s *VolumeScheduler) Schedule() (map[string][]api.VolumeSpec, error) {
}
return scheduledVolumes, nil
//// For each missing volume (should be created only on one machine)
//machineToVolumeSpecs := make(map[string][]api.VolumeSpec)
//for volumeName, serviceNames := range missingVolumes {
// // Get the intersection of candidate machines for all services using this volume
// var intersection []string
// for i, serviceName := range serviceNames {
// if i == 0 {
// intersection = serviceEligibleMachines[serviceName]
// } else {
// intersection = s.intersectMachines(intersection, serviceEligibleMachines[serviceName])
// }
// }
//
// if len(intersection) == 0 {
// return nil, fmt.Errorf("unable to find a machine where services %v can be placed together to share the missing volume %s",
// serviceNames, volumeName)
// }
//
// // Sort the intersection to ensure deterministic behavior
// sortedIntersection := make([]string, len(intersection))
// copy(sortedIntersection, intersection)
// slices.Sort(sortedIntersection)
//
// // Choose the first machine in the sorted intersection to create the volume on
// machineID := sortedIntersection[0]
//
// // Get the volume spec for this volume name
// volumeSpec := volumeSpecs[volumeName]
//
// // Add the volume spec to the machine's list
// machineToVolumeSpecs[machineID] = append(machineToVolumeSpecs[machineID], volumeSpec)
//
// // Update the candidate machines for all services using this volume
// for _, serviceName := range serviceNames {
// serviceEligibleMachines[serviceName] = intersection
// }
//}
//
//return machineToVolumeSpecs, nil
}
// serviceEligibleMachinesWithoutVolumes returns a set of machine IDs where the service can be scheduled
@@ -254,77 +214,3 @@ func (s *VolumeScheduler) serviceEligibleMachinesWithoutVolumes(spec api.Service
return machineIDs, nil
}
// getAllVolumesAndSpecs returns a map of all volume names used by services and a map of volume names to their specs.
func (s *VolumeScheduler) getAllVolumesAndSpecs() (map[string]struct{}, map[string]api.VolumeSpec) {
volumes := make(map[string]struct{})
volumeSpecs := make(map[string]api.VolumeSpec)
for _, serviceSpec := range s.serviceSpecs {
for _, mount := range serviceSpec.Container.VolumeMounts {
if v, ok := serviceSpec.Volume(mount.VolumeName); ok && v.Type == api.VolumeTypeVolume {
volumeName := v.DockerVolumeName()
volumes[volumeName] = struct{}{}
volumeSpecs[volumeName] = v
}
}
}
return volumes, volumeSpecs
}
// getVolumeLocations returns a map of volume names to the list of machine IDs where they exist.
func (s *VolumeScheduler) getVolumeLocations(allVolumes map[string]struct{}) map[string][]string {
volumeLocations := make(map[string][]string)
// Initialize the map with empty slices for all volumes
for volumeName := range allVolumes {
volumeLocations[volumeName] = []string{}
}
// Populate the map with machine IDs where each volume exists
for _, machine := range s.machines {
for _, vol := range machine.Volumes {
if _, ok := volumeLocations[vol.Name]; ok {
volumeLocations[vol.Name] = append(volumeLocations[vol.Name], machine.Info.Id)
}
}
}
return volumeLocations
}
// getVolumeServices returns a map of volume names to the list of service names that use them.
func (s *VolumeScheduler) getVolumeServices(allVolumes map[string]struct{}) map[string][]string {
volumeServices := make(map[string][]string)
// Initialize the map with empty slices for all volumes
for volumeName := range allVolumes {
volumeServices[volumeName] = []string{}
}
for _, serviceSpec := range s.serviceSpecs {
serviceName := serviceSpec.Name
for _, mount := range serviceSpec.Container.VolumeMounts {
if v, ok := serviceSpec.Volume(mount.VolumeName); ok && v.Type == api.VolumeTypeVolume {
volumeName := v.DockerVolumeName()
if _, ok := allVolumes[volumeName]; ok {
volumeServices[volumeName] = append(volumeServices[volumeName], serviceName)
}
}
}
}
return volumeServices
}
// intersectMachines returns the intersection of two slices of machine IDs.
func (s *VolumeScheduler) intersectMachines(a, b []string) []string {
var result []string
for _, id := range a {
if slices.Contains(b, id) {
result = append(result, id)
}
}
return result
}