github.com/caos/orbos@v1.5.14-0.20221103111702-e6cd0cea7ad4/internal/operator/orbiter/kinds/providers/gce/destroy.go (about) 1 package gce 2 3 import ( 4 "strings" 5 6 "github.com/caos/orbos/internal/helpers" 7 "github.com/caos/orbos/internal/operator/orbiter/kinds/clusters/core/infra" 8 "github.com/caos/orbos/mntr" 9 uuid "github.com/satori/go.uuid" 10 "google.golang.org/api/compute/v1" 11 ) 12 13 func destroy(svc *machinesService, delegates map[string]interface{}) error { 14 return helpers.Fanout([]func() error{ 15 func() error { 16 destroyLB, err := queryLB(svc.context, nil) 17 if err != nil { 18 return err 19 } 20 return destroyLB() 21 }, 22 func() error { 23 pools, err := svc.ListPools() 24 if err != nil { 25 return err 26 } 27 var delFuncs []func() error 28 for _, pool := range pools { 29 machines, err := svc.List(pool) 30 if err != nil { 31 return err 32 } 33 for _, machine := range machines { 34 delFuncs = append(delFuncs, func(machine infra.Machine) func() error { 35 return func() error { 36 remove, err := machine.Destroy() 37 if err != nil { 38 return err 39 } 40 return remove() 41 } 42 }(machine)) 43 } 44 } 45 if err := helpers.Fanout(delFuncs)(); err != nil { 46 return err 47 } 48 49 return helpers.Fanout([]func() error{ 50 func() error { 51 var deleteDisks []func() error 52 53 deleteMonitor := svc.context.monitor.WithField("type", "persistent disk") 54 currentVolumesList, err := svc.context.client.Disks.AggregatedList(svc.context.projectID).Do() 55 if err != nil { 56 return err 57 } 58 disks := make([]*compute.Disk, 0) 59 region, err := svc.context.client.Regions.Get(svc.context.projectID, svc.context.desired.Region).Do() 60 if err != nil { 61 return err 62 } 63 diskList, ok := currentVolumesList.Items["regions/"+svc.context.desired.Region] 64 if ok { 65 for _, disk := range diskList.Disks { 66 disks = append(disks, disk) 67 } 68 } 69 for zoneURLI := range region.Zones { 70 zoneURL := region.Zones[zoneURLI] 71 zoneURLParts := strings.Split(zoneURL, "/") 72 zone := zoneURLParts[(len(zoneURLParts) - 1)] 73 diskList, ok := currentVolumesList.Items["zones/"+zone] 74 if ok { 75 for _, disk := range diskList.Disks { 76 disks = append(disks, disk) 77 } 78 } 79 } 80 81 for _, delegate := range delegates { 82 volumes, ok := delegate.([]infra.Volume) 83 if ok { 84 for idx := range volumes { 85 diskName := volumes[idx].Name 86 found := false 87 zone := "" 88 for _, currentVolume := range diskList.Disks { 89 if currentVolume.Name == diskName { 90 found = true 91 zoneURLParts := strings.Split(currentVolume.Zone, "/") 92 zone = zoneURLParts[(len(zoneURLParts) - 1)] 93 break 94 } 95 } 96 if found { 97 deleteDisks = append(deleteDisks, deleteDiskFunc(svc.context, deleteMonitor.WithField("id", diskName), zone, diskName)) 98 } 99 } 100 } 101 } 102 return helpers.Fanout(deleteDisks)() 103 }, 104 func() error { 105 _, deleteFirewalls, err := queryFirewall(svc.context, nil) 106 if err != nil { 107 return err 108 } 109 return destroyNetwork(svc.context, deleteFirewalls) 110 }, 111 })() 112 }, 113 })() 114 } 115 116 func deleteDiskFunc(context *context, monitor mntr.Monitor, zone, id string) func() error { 117 return func() error { 118 return operateFunc( 119 func() { monitor.Debug("Removing resource") }, 120 computeOpCall(context.client.Disks.Delete(context.projectID, zone, id).RequestId(uuid.NewV1().String()).Do), 121 func() error { monitor.Info("Resource removed"); return nil }, 122 )() 123 } 124 }