File: find.go

package info (click to toggle)
golang-github-mesos-mesos-go 0.0.6%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 11,724 kB
  • sloc: makefile: 163
file content (55 lines) | stat: -rw-r--r-- 1,401 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package resources

import (
	"github.com/mesos/mesos-go/api/v1/lib"
	"github.com/mesos/mesos-go/api/v1/lib/resourcefilters"
)

func Find(wants mesos.Resources, from ...mesos.Resource) (total mesos.Resources) {
	for i := range wants {
		found := find(wants[i], from...)

		// each want *must* be found
		if len(found) == 0 {
			return nil
		}

		total.Add(found...)
	}
	return total
}

func find(want mesos.Resource, from ...mesos.Resource) mesos.Resources {
	var (
		total      = mesos.Resources(from).Clone()
		remaining  = mesos.Resources(Flatten(mesos.Resources{want}))
		found      mesos.Resources
		predicates = resourcefilters.Filters{
			resourcefilters.ReservedByRole(want.GetRole()),
			resourcefilters.Unreserved,
			resourcefilters.Any,
		}
	)
	for _, predicate := range predicates {
		filtered := resourcefilters.Select(predicate, total...)
		for i := range filtered {
			// need to flatten to ignore the roles in ContainsAll()
			flattened := Flatten(mesos.Resources{filtered[i]})
			if ContainsAll(flattened, remaining) {
				// want has been found, return the result
				return found.Add(Flatten(
					remaining,
					Role(filtered[i].GetRole()).Assign(),
					filtered[i].Reservation.Assign())...,
				)
			}
			if ContainsAll(remaining, flattened) {
				found.Add1(filtered[i])
				total.Subtract1(filtered[i])
				remaining.Subtract(flattened...)
				break
			}
		}
	}
	return nil
}