cloud-cleaner: clean up GCE instances in all regions and zones
Since the `api.sh` test case is using random GCE zone from a random GCE region which name starts with the `GCP_REGION` CI environment variable. Since the used region name is not known to the `cloud-cleaner`, it has to iterate over all potential GCE regions and their zones. We can not simply filter the VM instance name a list of instances, because any `instances` API call requires a zone name to be provided. Add a new internal `cloud/gcp` package method to list existing GCE regions based on a provided filter.
This commit is contained in:
parent
18dfa9d9c9
commit
1017aee438
2 changed files with 65 additions and 39 deletions
|
|
@ -22,7 +22,7 @@ func cleanupGCP(testID string, wg *sync.WaitGroup) {
|
|||
|
||||
log.Println("[GCP] Running clean up")
|
||||
|
||||
GCPRegion, ok := os.LookupEnv("GCP_REGION")
|
||||
GCPRegionName, ok := os.LookupEnv("GCP_REGION")
|
||||
if !ok {
|
||||
log.Println("[GCP] Error: 'GCP_REGION' is not set in the environment.")
|
||||
return
|
||||
|
|
@ -61,25 +61,31 @@ func cleanupGCP(testID string, wg *sync.WaitGroup) {
|
|||
ctx := context.Background()
|
||||
|
||||
// Try to delete potentially running instance
|
||||
// api.sh chooses a random GCP Zone from the set Region. Since we
|
||||
// don't know which one it is, iterate over all Zones in the Region
|
||||
// and try to delete the instance. Unless the instance has set
|
||||
// "VmDnsSetting:ZonalOnly", which we don't do, this is safe and the
|
||||
// instance name must be unique for the whole GCP project.
|
||||
GCPZones, err := g.ComputeZonesInRegion(ctx, GCPRegion)
|
||||
// api.sh chooses a random GCP Zone from regions which names start with the
|
||||
// `GCPRegionName` value. Since we don't know which one it is, iterate over
|
||||
// all Regions and Zones in them and try to delete the instance. Unless the
|
||||
// instance has set "VmDnsSetting:ZonalOnly", which we don't do, this is
|
||||
// safe and the instance name must be unique for the whole GCP project.
|
||||
GCPRegions, err := g.ComputeRegionsList(ctx, fmt.Sprintf("name:%s* AND status=UP", GCPRegionName))
|
||||
if err != nil {
|
||||
log.Printf("[GCP] Error: Failed to get available Zones for the '%s' Region: %v", GCPRegion, err)
|
||||
return
|
||||
log.Printf("[GCP] Error: Failed to list GCE regions starting with %q: %v", GCPRegionName, err)
|
||||
}
|
||||
for _, GCPZone := range GCPZones {
|
||||
log.Printf("[GCP] 🧹 Deleting VM instance %s in %s. "+
|
||||
"This should fail if the test succeeded.", GCPInstance, GCPZone)
|
||||
err = g.ComputeInstanceDelete(ctx, GCPZone, GCPInstance)
|
||||
if err == nil {
|
||||
// If an instance with the given name was successfully deleted in one of the Zones, we are done.
|
||||
for _, GCPRegion := range GCPRegions {
|
||||
GCPZones, err := g.ComputeUpZonesInRegion(ctx, GCPRegion)
|
||||
if err != nil {
|
||||
log.Printf("[GCP] Error: Failed to get available Zones for the '%s' Region: %v", GCPRegionName, err)
|
||||
break
|
||||
} else {
|
||||
log.Printf("[GCP] Error: %v", err)
|
||||
}
|
||||
for _, GCPZone := range GCPZones {
|
||||
log.Printf("[GCP] 🧹 Deleting VM instance %s in %s. "+
|
||||
"This should fail if the test succeeded.", GCPInstance, GCPZone.GetName())
|
||||
err = g.ComputeInstanceDelete(ctx, GCPZone.GetName(), GCPInstance)
|
||||
if err == nil {
|
||||
// If an instance with the given name was successfully deleted in one of the Zones, we are done.
|
||||
break
|
||||
} else {
|
||||
log.Printf("[GCP] Error: %v", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue