Skip to content

Commit 3ac46a6

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 119f829 commit 3ac46a6

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"),
@@ -165,6 +173,9 @@ func (a *API) getVMParameters(name, userdata, sshkey, storageAccountURI string,
165173
}
166174

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

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

189-
future, err := a.compClient.CreateOrUpdate(context.TODO(), resourceGroup, name, vmParams)
200+
clean := func() {
201+
_, _ = a.compClient.Delete(context.TODO(), vmResourceGroup, name, &forceDelete)
202+
_, _ = a.intClient.Delete(context.TODO(), resourceGroup, *nic.Name)
203+
_, _ = a.ipClient.Delete(context.TODO(), resourceGroup, *ip.Name)
204+
}
205+
206+
future, err := a.compClient.CreateOrUpdate(context.TODO(), vmResourceGroup, name, vmParams)
190207
if err != nil {
208+
clean()
191209
return nil, err
192210
}
193211
err = future.WaitForCompletionRef(context.TODO(), a.compClient.Client)
194212
if err != nil {
213+
clean()
195214
return nil, err
196215
}
197216
_, err = future.Result(a.compClient)
198217
if err != nil {
218+
clean()
199219
return nil, err
200220
}
201221
plog.Infof("Instance %s created", name)
202222

203223
err = util.WaitUntilReady(5*time.Minute, 10*time.Second, func() (bool, error) {
204-
vm, err := a.compClient.Get(context.TODO(), resourceGroup, name, "")
224+
vm, err := a.compClient.Get(context.TODO(), vmResourceGroup, name, "")
205225
if err != nil {
206226
return false, err
207227
}
@@ -214,13 +234,11 @@ func (a *API) CreateInstance(name, userdata, sshkey, resourceGroup, storageAccou
214234
})
215235
plog.Infof("Instance %s ready", name)
216236
if err != nil {
217-
_, _ = a.compClient.Delete(context.TODO(), resourceGroup, name, &forceDelete)
218-
_, _ = a.intClient.Delete(context.TODO(), resourceGroup, *nic.Name)
219-
_, _ = a.ipClient.Delete(context.TODO(), resourceGroup, *ip.Name)
237+
clean()
220238
return nil, fmt.Errorf("waiting for machine to become active: %v", err)
221239
}
222240

223-
vm, err := a.compClient.Get(context.TODO(), resourceGroup, name, "")
241+
vm, err := a.compClient.Get(context.TODO(), vmResourceGroup, name, "")
224242
if err != nil {
225243
return nil, err
226244
}
@@ -250,6 +268,7 @@ func (a *API) CreateInstance(name, userdata, sshkey, resourceGroup, storageAccou
250268
// TerminateInstance deletes a VM created by CreateInstance. Public IP, NIC and
251269
// OS disk are deleted automatically together with the VM.
252270
func (a *API) TerminateInstance(machine *Machine, resourceGroup string) error {
271+
resourceGroup = a.getVMRG(resourceGroup)
253272
future, err := a.compClient.Delete(context.TODO(), resourceGroup, machine.ID, &forceDelete)
254273
if err != nil {
255274
return err
@@ -277,7 +296,8 @@ func (a *API) GetConsoleOutput(name, resourceGroup, storageAccount string) ([]by
277296
k := *kr.Keys
278297
key := *k[0].Value
279298

280-
vm, err := a.compClient.Get(context.TODO(), resourceGroup, name, compute.InstanceViewTypesInstanceView)
299+
vmResourceGroup := a.getVMRG(resourceGroup)
300+
vm, err := a.compClient.Get(context.TODO(), vmResourceGroup, name, compute.InstanceViewTypesInstanceView)
281301
if err != nil {
282302
return nil, fmt.Errorf("could not get VM: %v", err)
283303
}

0 commit comments

Comments
 (0)