Skip to content

Commit

Permalink
feat(v1beta3): implement AnyOf for attributes
Browse files Browse the repository at this point in the history
Signed-off-by: Artur Troian <[email protected]>
  • Loading branch information
troian committed Jun 15, 2023
1 parent f91f9a8 commit 7fa9c7f
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 1 deletion.
3 changes: 3 additions & 0 deletions go/manifest/v2beta2/manifest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ var randUnits1 = akashtypes.ResourceUnits{
CPU: &akashtypes.CPU{
Units: akashtypes.NewResourceValue(randCPU1),
},
GPU: &akashtypes.GPU{
Units: akashtypes.NewResourceValue(randGPU1),
},
Memory: &akashtypes.Memory{
Quantity: akashtypes.NewResourceValue(randMemory),
},
Expand Down
3 changes: 2 additions & 1 deletion go/node/deployment/v1beta3/groupspec.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ func (g GroupSpec) MatchResourcesRequirements(pattr types.Attributes) bool {
}

pgroup = pattr.GetCapabilitiesMap("gpu")
if !gpu.Attributes.IN(pgroup) {

if !gpu.Attributes.AnyIN(pgroup) {
return false
}
}
Expand Down
25 changes: 25 additions & 0 deletions go/node/types/v1beta3/attribute.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,10 +199,26 @@ loop:
return true
}

func AttributesAnyOf(a, b Attributes) bool {
for _, req := range a {
for _, attr := range b {
if req.SubsetOf(attr) {
return true
}
}
}

return false
}

func (attr Attributes) SubsetOf(b Attributes) bool {
return AttributesSubsetOf(attr, b)
}

func (attr Attributes) AnyOf(b Attributes) bool {
return AttributesAnyOf(attr, b)
}

func (attr Attributes) Find(glob string) AttributeValue {
// todo wildcard

Expand Down Expand Up @@ -335,3 +351,12 @@ func (attr Attributes) IN(group AttributesGroup) bool {
}
return false
}

func (attr Attributes) AnyIN(group AttributesGroup) bool {
for _, group := range group {
if attr.AnyOf(group) {
return true
}
}
return false
}
37 changes: 37 additions & 0 deletions go/node/types/v1beta3/attribute_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,15 @@ func TestAttribute_SubsetOf(t *testing.T) {
require.False(t, attr1.SubsetOf(attr3))
}

func TestAttribute_AnyOf(t *testing.T) {
attr1 := Attribute{Key: "key1", Value: "val1"}
attr2 := Attribute{Key: "key1", Value: "val1"}
attr3 := Attribute{Key: "key1", Value: "val2"}

require.True(t, attr1.SubsetOf(attr2))
require.False(t, attr1.SubsetOf(attr3))
}

func TestAttributes_SubsetOf(t *testing.T) {
attr1 := Attributes{
{Key: "key1", Value: "val1"},
Expand Down Expand Up @@ -86,6 +95,34 @@ func TestAttributes_SubsetOf(t *testing.T) {
require.False(t, attr1.SubsetOf(attr4))
}

func TestAttributes_AnyOf(t *testing.T) {
attr1 := Attributes{
{Key: "key1", Value: "val1"},
}

attr2 := Attributes{
{Key: "key1", Value: "val1"},
{Key: "key2", Value: "val2"},
}

attr3 := Attributes{
{Key: "key1", Value: "val1"},
{Key: "key2", Value: "val2"},
{Key: "key3", Value: "val3"},
{Key: "key4", Value: "val4"},
}

attr4 := Attributes{
{Key: "key3", Value: "val3"},
{Key: "key4", Value: "val4"},
}

require.True(t, attr1.AnyOf(attr2))
require.True(t, attr2.AnyOf(attr1))
require.True(t, attr2.AnyOf(attr3))
require.False(t, attr1.AnyOf(attr4))
}

func TestAttributeRegex(t *testing.T) {
tests := []regexTest{
{
Expand Down

0 comments on commit 7fa9c7f

Please sign in to comment.