Skip to content

Commit 7f3319e

Browse files
committed
api/azure: Reuse resource group only for VMs
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]>
1 parent fa39c84 commit 7f3319e

File tree

1 file changed

+27
-7
lines changed

1 file changed

+27
-7
lines changed

platform/api/azure/instance.go

+27-7
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,14 @@ func (a *API) getAvset() string {
4646
return fmt.Sprintf("/subscriptions/%s/resourceGroups/%s/providers/Microsoft.Compute/availabilitySets/%s", a.Opts.SubscriptionID, a.Opts.ResourceGroup, a.Opts.AvailabilitySet)
4747
}
4848

49+
func (a *API) getVMRG(rg string) string {
50+
vmrg := rg
51+
if a.Opts.ResourceGroup != "" {
52+
vmrg = a.Opts.ResourceGroup
53+
}
54+
return vmrg
55+
}
56+
4957
func (a *API) getVMParameters(name, userdata, sshkey, storageAccountURI string, ip *network.PublicIPAddress, nic *network.Interface) compute.VirtualMachine {
5058
osProfile := compute.OSProfile{
5159
AdminUsername: util.StrToPtr("core"),
@@ -172,6 +180,9 @@ func (a *API) getVMParameters(name, userdata, sshkey, storageAccountURI string,
172180
}
173181

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

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

196-
future, err := a.compClient.CreateOrUpdate(context.TODO(), resourceGroup, name, vmParams)
207+
clean := func() {
208+
_, _ = a.compClient.Delete(context.TODO(), vmResourceGroup, name, &forceDelete)
209+
_, _ = a.intClient.Delete(context.TODO(), resourceGroup, *nic.Name)
210+
_, _ = a.ipClient.Delete(context.TODO(), resourceGroup, *ip.Name)
211+
}
212+
213+
future, err := a.compClient.CreateOrUpdate(context.TODO(), vmResourceGroup, name, vmParams)
197214
if err != nil {
215+
clean()
198216
return nil, err
199217
}
200218
err = future.WaitForCompletionRef(context.TODO(), a.compClient.Client)
201219
if err != nil {
220+
clean()
202221
return nil, err
203222
}
204223
_, err = future.Result(a.compClient)
205224
if err != nil {
225+
clean()
206226
return nil, err
207227
}
208228
plog.Infof("Instance %s created", name)
209229

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

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

287-
vm, err := a.compClient.Get(context.TODO(), resourceGroup, name, compute.InstanceViewTypesInstanceView)
306+
vmResourceGroup := a.getVMRG(resourceGroup)
307+
vm, err := a.compClient.Get(context.TODO(), vmResourceGroup, name, compute.InstanceViewTypesInstanceView)
288308
if err != nil {
289309
return nil, fmt.Errorf("could not get VM: %v", err)
290310
}

0 commit comments

Comments
 (0)