Debian Codegraph @

api/v1/lib/resources/compare.go

raw symbols SCIP
1 package resources
2
3 import (
4 "github.com/gogo/protobuf/proto"
5 "github.com/mesos/mesos-go/api/v1/lib"
6 )
7
8 func Validate(resources ...mesos.Resource) error {
9 type withSpec interface {
10 WithSpec(mesos.Resource)
11 }
12 for i := range resources {
13 err := resources[i].Validate()
14 if err != nil {
15 // augment resourceError's with the resource that failed to validate
16 if resourceError, ok := err.(withSpec); ok {
17 r := proto.Clone(&resources[i]).(*mesos.Resource)
18 resourceError.WithSpec(*r)
19 }
20 return err
21 }
22 }
23 return nil
24 }
25
26 func Equivalent(subject, that []mesos.Resource) bool {
27 return ContainsAll(subject, that) && ContainsAll(that, subject)
28 }
29
30 func Contains(subject []mesos.Resource, that mesos.Resource) bool {
31 // NOTE: We must validate 'that' because invalid resources can lead
32 // to false positives here (e.g., "cpus:-1" will return true). This
33 // is because 'contains' assumes resources are valid.
34 return that.Validate() == nil && contains(subject, that)
35 }
36
37 func contains(subject []mesos.Resource, that mesos.Resource) bool {
38 for i := range subject {
39 if subject[i].Contains(that) {
40 return true
41 }
42 }
43 return false
44 }
45
46 // ContainsAll returns true if this set of resources contains that set of (presumably pre-validated) resources.
47 func ContainsAll(subject, that []mesos.Resource) bool {
48 remaining := mesos.Resources(subject).Clone()
49 for i := range that {
50 // NOTE: We use contains() because resources only contain valid
51 // Resource objects, and we don't want the performance hit of the
52 // validity check.
53 if !contains(remaining, that[i]) {
54 return false
55 }
56 remaining.Subtract1(that[i])
57 }
58 return true
59 }