in projects/k8s-hybrid-neg-controller/pkg/config/zones.go [86:121]
func getNEGZones(ctx context.Context, logger logr.Logger, projectID string, regionZonesClient *compute.RegionZonesClient) ([]string, error) {
var zones []string
var err error
switch {
case len(negZonesFlag) > 0:
// Use the values from the supplied `neg-zones` flag.
zones = strings.Split(negZonesFlag, ",")
case len(zoneMappingFlag) > 0:
// Use the target values from the `zone-mapping` flag.
zoneMapping := parseZoneMappingFlag()
for _, negZone := range zoneMapping {
zones = append(zones, negZone)
}
case metadata.OnGCE():
// Look up zones from the Compute Engine or Google Kubernetes Engine metadata server, and
// using the Compute Engine API.
// 1. Look up the zone of the current Kubernetes Node from the metadata server.
// 2. Determine the region of that zone.
// 3. Use the Compute Engine API to list the zones of that region.
logger.V(4).Info("No NEG zones provided, attempting to fetch zones from the metadata server")
zones, err = lookupNEGZones(ctx, projectID, regionZonesClient)
if err != nil {
return nil, err
}
}
if len(zones) == 0 {
return nil, errAtLeastOneZoneRequired
}
slices.Sort(zones)
for _, zone := range zones {
if !isValidNEGZone(zone) {
return nil, fmt.Errorf("%w zone=%s", errInvalidNEGZone, zone)
}
}
return zones, nil
}