func createRollOut()

in cli_tools/gce_image_publish/publish/publish.go [639:684]


func createRollOut(zones []*compute.Zone, rolloutStartTime time.Time, rolloutRate int) *computeAlpha.RolloutPolicy {
	rp := computeAlpha.RolloutPolicy{}

	var regions map[string][]string
	regions = make(map[string][]string)
	maxRegionLength := 0

	// Build a map of all the regions and determine the max number of zones in a region.
	for _, z := range zones {
		regions[z.Region] = append(regions[z.Region], z.Name)
		if len(regions[z.Region]) > maxRegionLength {
			maxRegionLength = len(regions[z.Region])
		}
	}

	// Order the list of zones in each region.
	for _, value := range regions {
		sort.Strings(value)
	}

	// zoneList is the ordered list of zones to apply the rollout policy to.
	var zoneList []string

	// zoneList's order should be the first zone from each region, then second zone from each region, third zone from each region, etc.
	// us-central1-a, us-central2-b, us-central3-c, us-central1-a, us-central2-b, us-central3-c
	for zoneCount := 0; zoneCount < maxRegionLength; zoneCount++ {
		for _, regionZones := range regions {
			// If the region has a zone at the current zoneCount, add that zone to the zoneList.
			if zoneCount < len(regionZones) {
				zoneList = append(zoneList, regionZones[zoneCount])
			}
		}
	}

	var rolloutPolicy map[string]string
	rolloutPolicy = make(map[string]string)

	for i, zone := range zoneList {
		rolloutPolicy[fmt.Sprintf("zones/%s", zone)] = rolloutStartTime.Add(time.Duration(rolloutRate*i) * time.Minute).Format(time.RFC3339)
	}

	// Set the default time to be the same as the last zone.
	rp.DefaultRolloutTime = rolloutStartTime.Add(time.Duration(rolloutRate*(len(zoneList)-1)) * time.Minute).Format(time.RFC3339)
	rp.LocationRolloutPolicies = rolloutPolicy
	return &rp
}