Skip to content

Commit

Permalink
api/azure: Reuse resource group only for VMs
Browse files Browse the repository at this point in the history
The remaining objects stay in a garbage collected kola managed group.
This allows us to use an availability set for the vms, which requires
that VMs be in the same rg as the avset. Kola correctly removes the VMs
through TerminateInstance() separately from any RG that it creates.

Signed-off-by: Jeremi Piotrowski <[email protected]>
  • Loading branch information
jepio committed Apr 5, 2024
1 parent fa39c84 commit 7f3319e
Showing 1 changed file with 27 additions and 7 deletions.
34 changes: 27 additions & 7 deletions platform/api/azure/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,14 @@ func (a *API) getAvset() string {
return fmt.Sprintf("/subscriptions/%s/resourceGroups/%s/providers/Microsoft.Compute/availabilitySets/%s", a.Opts.SubscriptionID, a.Opts.ResourceGroup, a.Opts.AvailabilitySet)
}

func (a *API) getVMRG(rg string) string {
vmrg := rg
if a.Opts.ResourceGroup != "" {
vmrg = a.Opts.ResourceGroup
}
return vmrg
}

func (a *API) getVMParameters(name, userdata, sshkey, storageAccountURI string, ip *network.PublicIPAddress, nic *network.Interface) compute.VirtualMachine {
osProfile := compute.OSProfile{
AdminUsername: util.StrToPtr("core"),
Expand Down Expand Up @@ -172,6 +180,9 @@ func (a *API) getVMParameters(name, userdata, sshkey, storageAccountURI string,
}

func (a *API) CreateInstance(name, userdata, sshkey, resourceGroup, storageAccount string, network Network) (*Machine, error) {
// only VMs are created in the user supplied resource group, kola still manages a resource group
// for the gallery and storage account.
vmResourceGroup := a.getVMRG(resourceGroup)
subnet := network.subnet

ip, err := a.createPublicIP(resourceGroup)
Expand All @@ -193,22 +204,31 @@ func (a *API) CreateInstance(name, userdata, sshkey, resourceGroup, storageAccou
vmParams := a.getVMParameters(name, userdata, sshkey, fmt.Sprintf("https://%s.blob.core.windows.net/", storageAccount), ip, nic)
plog.Infof("Creating Instance %s", name)

future, err := a.compClient.CreateOrUpdate(context.TODO(), resourceGroup, name, vmParams)
clean := func() {
_, _ = a.compClient.Delete(context.TODO(), vmResourceGroup, name, &forceDelete)
_, _ = a.intClient.Delete(context.TODO(), resourceGroup, *nic.Name)
_, _ = a.ipClient.Delete(context.TODO(), resourceGroup, *ip.Name)
}

future, err := a.compClient.CreateOrUpdate(context.TODO(), vmResourceGroup, name, vmParams)
if err != nil {
clean()
return nil, err
}
err = future.WaitForCompletionRef(context.TODO(), a.compClient.Client)
if err != nil {
clean()
return nil, err
}
_, err = future.Result(a.compClient)
if err != nil {
clean()
return nil, err
}
plog.Infof("Instance %s created", name)

err = util.WaitUntilReady(5*time.Minute, 10*time.Second, func() (bool, error) {
vm, err := a.compClient.Get(context.TODO(), resourceGroup, name, "")
vm, err := a.compClient.Get(context.TODO(), vmResourceGroup, name, "")
if err != nil {
return false, err
}
Expand All @@ -221,13 +241,11 @@ func (a *API) CreateInstance(name, userdata, sshkey, resourceGroup, storageAccou
})
plog.Infof("Instance %s ready", name)
if err != nil {
_, _ = a.compClient.Delete(context.TODO(), resourceGroup, name, &forceDelete)
_, _ = a.intClient.Delete(context.TODO(), resourceGroup, *nic.Name)
_, _ = a.ipClient.Delete(context.TODO(), resourceGroup, *ip.Name)
clean()
return nil, fmt.Errorf("waiting for machine to become active: %v", err)
}

vm, err := a.compClient.Get(context.TODO(), resourceGroup, name, "")
vm, err := a.compClient.Get(context.TODO(), vmResourceGroup, name, "")
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -257,6 +275,7 @@ func (a *API) CreateInstance(name, userdata, sshkey, resourceGroup, storageAccou
// TerminateInstance deletes a VM created by CreateInstance. Public IP, NIC and
// OS disk are deleted automatically together with the VM.
func (a *API) TerminateInstance(machine *Machine, resourceGroup string) error {
resourceGroup = a.getVMRG(resourceGroup)
future, err := a.compClient.Delete(context.TODO(), resourceGroup, machine.ID, &forceDelete)
if err != nil {
return err
Expand Down Expand Up @@ -284,7 +303,8 @@ func (a *API) GetConsoleOutput(name, resourceGroup, storageAccount string) ([]by
k := *kr.Keys
key := *k[0].Value

vm, err := a.compClient.Get(context.TODO(), resourceGroup, name, compute.InstanceViewTypesInstanceView)
vmResourceGroup := a.getVMRG(resourceGroup)
vm, err := a.compClient.Get(context.TODO(), vmResourceGroup, name, compute.InstanceViewTypesInstanceView)
if err != nil {
return nil, fmt.Errorf("could not get VM: %v", err)
}
Expand Down

0 comments on commit 7f3319e

Please sign in to comment.