Skip to content

Commit 496fc66

Browse files
fix: Avoid hash collisions for instance type zone info (#9023)
1 parent dae2c18 commit 496fc66

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

pkg/providers/instancetype/instancetype.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ func (p *DefaultProvider) get(ctx context.Context, nodeClass NodeClass, name ec2
218218

219219
func (p *DefaultProvider) cacheKey(nodeClass NodeClass) string {
220220
// Compute fully initialized instance types hash key
221-
subnetZonesHash, _ := hashstructure.Hash(nodeClass.ZoneInfo(), hashstructure.FormatV2, &hashstructure.HashOptions{SlicesAsSets: true})
221+
subnetZonesHash := hashZoneInfo(nodeClass.ZoneInfo())
222222
// Compute hash key against node class AMIs (used to force cache rebuild when AMIs change)
223223
amiHash, _ := hashstructure.Hash(nodeClass.AMIs(), hashstructure.FormatV2, &hashstructure.HashOptions{SlicesAsSets: true})
224224
return fmt.Sprintf("%016x-%016x-%s",
@@ -361,3 +361,13 @@ func discoveredCapacityCacheKey(instanceType string, nodeClass NodeClass) string
361361
amiHash, _ := hashstructure.Hash(nodeClass.AMIs(), hashstructure.FormatV2, &hashstructure.HashOptions{SlicesAsSets: true})
362362
return fmt.Sprintf("%s-%016x", instanceType, amiHash)
363363
}
364+
365+
// hashZoneInfo hashes each ZoneInfo element individually and collects the hashes
366+
// into a set, avoiding the hashstructure SlicesAsSets bug from https://github.com/mitchellh/hashstructure/issues/36
367+
func hashZoneInfo(zoneInfo []v1.ZoneInfo) uint64 {
368+
zoneInfoHashes := sets.New[uint64]()
369+
for i := range zoneInfo {
370+
zoneInfoHashes.Insert(lo.Must(hashstructure.Hash(zoneInfo[i], hashstructure.FormatV2, nil)))
371+
}
372+
return lo.Must(hashstructure.Hash(zoneInfoHashes, hashstructure.FormatV2, nil))
373+
}

0 commit comments

Comments
 (0)