Skip to content

Commit 745d0b9

Browse files
authored
Merge pull request #497 from flatcar/tormath1/azure+jepio
azure: Add support for using an external availability set and resource group
2 parents 6d3d3e3 + 7f3319e commit 745d0b9

File tree

4 files changed

+50
-8
lines changed

4 files changed

+50
-8
lines changed

cmd/kola/options.go

+2
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,8 @@ func init() {
124124
bv(&kola.AzureOptions.UsePrivateIPs, "azure-use-private-ips", false, "Assume nodes are reachable using private IP addresses")
125125
bv(&kola.AzureOptions.UseIdentity, "azure-identity", false, "Use VM managed identity for authentication (default false)")
126126
sv(&kola.AzureOptions.DiskController, "azure-disk-controller", "default", "Use a specific disk-controller for storage (default \"default\", also \"nvme\" and \"scsi\")")
127+
sv(&kola.AzureOptions.ResourceGroup, "azure-resource-group", "", "Deploy resources in an existing resource group")
128+
sv(&kola.AzureOptions.AvailabilitySet, "azure-availability-set", "", "Deploy instances with an existing availibity set")
127129

128130
// do-specific options
129131
sv(&kola.DOOptions.ConfigPath, "do-config-file", "", "DigitalOcean config file (default \"~/"+auth.DOConfigPath+"\")")

platform/api/azure/api.go

+4
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,10 @@ func New(opts *Options) (*API, error) {
154154
client = management.NewAnonymousClient()
155155
}
156156

157+
if opts.AvailabilitySet != "" && opts.ResourceGroup == "" {
158+
return nil, fmt.Errorf("ResourceGroup must match AvailabilitySet")
159+
}
160+
157161
api := &API{
158162
client: client,
159163
Opts: opts,

platform/api/azure/instance.go

+39-7
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,21 @@ type Machine struct {
3939
PublicIPName string
4040
}
4141

42+
func (a *API) getAvset() string {
43+
if a.Opts.AvailabilitySet == "" {
44+
return ""
45+
}
46+
return fmt.Sprintf("/subscriptions/%s/resourceGroups/%s/providers/Microsoft.Compute/availabilitySets/%s", a.Opts.SubscriptionID, a.Opts.ResourceGroup, a.Opts.AvailabilitySet)
47+
}
48+
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+
4257
func (a *API) getVMParameters(name, userdata, sshkey, storageAccountURI string, ip *network.PublicIPAddress, nic *network.Interface) compute.VirtualMachine {
4358
osProfile := compute.OSProfile{
4459
AdminUsername: util.StrToPtr("core"),
@@ -156,10 +171,18 @@ func (a *API) getVMParameters(name, userdata, sshkey, storageAccountURI string,
156171
}
157172
}
158173

174+
availabilitySetID := a.getAvset()
175+
if availabilitySetID != "" {
176+
vm.VirtualMachineProperties.AvailabilitySet = &compute.SubResource{ID: &availabilitySetID}
177+
}
178+
159179
return vm
160180
}
161181

162182
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)
163186
subnet := network.subnet
164187

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

184-
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)
185214
if err != nil {
215+
clean()
186216
return nil, err
187217
}
188218
err = future.WaitForCompletionRef(context.TODO(), a.compClient.Client)
189219
if err != nil {
220+
clean()
190221
return nil, err
191222
}
192223
_, err = future.Result(a.compClient)
193224
if err != nil {
225+
clean()
194226
return nil, err
195227
}
196228
plog.Infof("Instance %s created", name)
197229

198230
err = util.WaitUntilReady(5*time.Minute, 10*time.Second, func() (bool, error) {
199-
vm, err := a.compClient.Get(context.TODO(), resourceGroup, name, "")
231+
vm, err := a.compClient.Get(context.TODO(), vmResourceGroup, name, "")
200232
if err != nil {
201233
return false, err
202234
}
@@ -209,13 +241,11 @@ func (a *API) CreateInstance(name, userdata, sshkey, resourceGroup, storageAccou
209241
})
210242
plog.Infof("Instance %s ready", name)
211243
if err != nil {
212-
_, _ = a.compClient.Delete(context.TODO(), resourceGroup, name, &forceDelete)
213-
_, _ = a.intClient.Delete(context.TODO(), resourceGroup, *nic.Name)
214-
_, _ = a.ipClient.Delete(context.TODO(), resourceGroup, *ip.Name)
244+
clean()
215245
return nil, fmt.Errorf("waiting for machine to become active: %v", err)
216246
}
217247

218-
vm, err := a.compClient.Get(context.TODO(), resourceGroup, name, "")
248+
vm, err := a.compClient.Get(context.TODO(), vmResourceGroup, name, "")
219249
if err != nil {
220250
return nil, err
221251
}
@@ -245,6 +275,7 @@ func (a *API) CreateInstance(name, userdata, sshkey, resourceGroup, storageAccou
245275
// TerminateInstance deletes a VM created by CreateInstance. Public IP, NIC and
246276
// OS disk are deleted automatically together with the VM.
247277
func (a *API) TerminateInstance(machine *Machine, resourceGroup string) error {
278+
resourceGroup = a.getVMRG(resourceGroup)
248279
future, err := a.compClient.Delete(context.TODO(), resourceGroup, machine.ID, &forceDelete)
249280
if err != nil {
250281
return err
@@ -272,7 +303,8 @@ func (a *API) GetConsoleOutput(name, resourceGroup, storageAccount string) ([]by
272303
k := *kr.Keys
273304
key := *k[0].Value
274305

275-
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)
276308
if err != nil {
277309
return nil, fmt.Errorf("could not get VM: %v", err)
278310
}

platform/api/azure/options.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@ type Options struct {
5151

5252
// Azure Storage API endpoint suffix. If unset, the Azure SDK default will be used.
5353
StorageEndpointSuffix string
54-
// UseUserData can be use to enable custom data only or user-data only.
54+
// UseUserData can be used to enable custom data only or user-data only.
5555
UseUserData bool
56+
// ResourceGroup is an existing resource group to deploy resources in.
57+
ResourceGroup string
58+
// AvailabilitySet is an existing availability set to deploy the instance in.
59+
AvailabilitySet string
5660
}

0 commit comments

Comments
 (0)