Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -371,5 +371,12 @@ func UnsafeLocationFilter(location string) func(*SKU) bool {
}
}

// IncludesFilter returns a FilterFn that checks if the SKU is included in the provided list of SKUs.
func IncludesFilter(skuList []SKU) func(*SKU) bool {
return func(s *SKU) bool {
return s.MemberOf(skuList)
}
}

// MapFn is a convenience type for mapping.
type MapFn func(*SKU) SKU
10 changes: 10 additions & 0 deletions sku.go
Original file line number Diff line number Diff line change
Expand Up @@ -572,3 +572,13 @@ func (s *SKU) Equal(other *SKU) bool {
localErr != nil &&
otherErr != nil
}

// MemberOf returns true if the SKU's name is in the list of SKUs.
func (s *SKU) MemberOf(skuList []SKU) bool {
for _, sku := range skuList {
if s.GetName() == sku.GetName() {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

EqualFold might be safer here, depending on where the list is coming from?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The list would be populated thusly:

https://github.com/Azure/skewer/pull/25/files#diff-3cdaf65cd294be89999f768c30e5dedd31813ba12b2f5f05c82cf7ba2eaf90a4R72-R78

And then moved over to the karpenter provider repo for manual curation. I think Name is sufficient if we're going to want to have an easily manageable data structure to maintain.

return true
}
}
return false
}
51 changes: 51 additions & 0 deletions sku_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -517,3 +517,54 @@ func Test_SKU_HasCapabilityInZone(t *testing.T) {
})
}
}

// Test_SKU_MemberOf tests the SKU MemberOf method
func Test_SKU_Includes(t *testing.T) {
cases := map[string]struct {
skuList []SKU
sku SKU
expect bool
}{
"empty list should not include": {
skuList: []SKU{},
sku: SKU{
Name: to.StringPtr("foo"),
},
expect: false,
},
"missing name should not include": {
skuList: []SKU{
{
Name: to.StringPtr("foo"),
},
},
sku: SKU{
Name: to.StringPtr("bar"),
},
expect: false,
},
"name is included": {
skuList: []SKU{
{
Name: to.StringPtr("foo"),
},
{
Name: to.StringPtr("bar"),
},
},
sku: SKU{
Name: to.StringPtr("bar"),
},
expect: true,
},
}
for name, tc := range cases {
tc := tc
t.Run(name, func(t *testing.T) {
sku := SKU(tc.sku)
if diff := cmp.Diff(tc.expect, sku.MemberOf(tc.skuList)); diff != "" {
t.Error(diff)
}
})
}
}