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 56 57 58 59
|
package resources
import (
"github.com/gogo/protobuf/proto"
"github.com/mesos/mesos-go/api/v1/lib"
)
func Validate(resources ...mesos.Resource) error {
type withSpec interface {
WithSpec(mesos.Resource)
}
for i := range resources {
err := resources[i].Validate()
if err != nil {
// augment resourceError's with the resource that failed to validate
if resourceError, ok := err.(withSpec); ok {
r := proto.Clone(&resources[i]).(*mesos.Resource)
resourceError.WithSpec(*r)
}
return err
}
}
return nil
}
func Equivalent(subject, that []mesos.Resource) bool {
return ContainsAll(subject, that) && ContainsAll(that, subject)
}
func Contains(subject []mesos.Resource, that mesos.Resource) bool {
// NOTE: We must validate 'that' because invalid resources can lead
// to false positives here (e.g., "cpus:-1" will return true). This
// is because 'contains' assumes resources are valid.
return that.Validate() == nil && contains(subject, that)
}
func contains(subject []mesos.Resource, that mesos.Resource) bool {
for i := range subject {
if subject[i].Contains(that) {
return true
}
}
return false
}
// ContainsAll returns true if this set of resources contains that set of (presumably pre-validated) resources.
func ContainsAll(subject, that []mesos.Resource) bool {
remaining := mesos.Resources(subject).Clone()
for i := range that {
// NOTE: We use contains() because resources only contain valid
// Resource objects, and we don't want the performance hit of the
// validity check.
if !contains(remaining, that[i]) {
return false
}
remaining.Subtract1(that[i])
}
return true
}
|