Skip to content

Instantly share code, notes, and snippets.

@bertinatto
Created January 9, 2020 12:43
Show Gist options
  • Save bertinatto/672e4df7eaa6e62cdf47b631bdd1f7c5 to your computer and use it in GitHub Desktop.
Save bertinatto/672e4df7eaa6e62cdf47b631bdd1f7c5 to your computer and use it in GitHub Desktop.
// ensureTopologyRequirements sets nodeSelection affinity according to given topology keys for drivers that provide them.
// If no node with the given topology key is found, this function will skip the test directly.
func ensureTopologyRequirements(nodeSelection *e2epod.NodeSelection, cs clientset.Interface, topologyKeys []string, minCount int) error {
nodes, err := e2enode.GetReadySchedulableNodes(cs)
if err != nil {
return err
}
// Get the number of nodes per zone per topology key.
// Example: map["topology.ebs.csi.aws.com/zone"]["us-east-1"] = 2
topologyCount := map[string]map[string]int{}
for _, node := range nodes.Items {
for _, topologyKey := range topologyKeys {
if zone, ok := node.Labels[topologyKey]; ok {
if _, ok := topologyCount[topologyKey]; !ok {
topologyCount[topologyKey] = make(map[string]int)
}
topologyCount[topologyKey][zone]++
}
}
}
// Once we find a topology with at least minCount of nodes, we use it in the tests
for topologyKey, zoneCount := range topologyCount {
for zone, count := range zoneCount {
if count >= minCount {
e2epod.SetNodeAffinityTopologyRequirement(nodeSelection, topology{topologyKey: zone})
return nil
}
}
}
framework.Skipf("No topology with at least %d nodes found - skipping", minCount)
return nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment