Skip to content

Commit

Permalink
Merge pull request #674 from HappyTobi/ephemeral_os_disk
Browse files Browse the repository at this point in the history
Ephemeral os disk
  • Loading branch information
rkoster authored Nov 24, 2022
2 parents e0379ce + 08411e5 commit 13019a1
Show file tree
Hide file tree
Showing 14 changed files with 371 additions and 24 deletions.
3 changes: 3 additions & 0 deletions src/bosh_azure_cpi/Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ GEM
digest
net-protocol
timeout
nokogiri (1.13.9-aarch64-linux)
racc (~> 1.4)
nokogiri (1.13.9-arm64-darwin)
racc (~> 1.4)
nokogiri (1.13.9-x86_64-darwin)
Expand Down Expand Up @@ -137,6 +139,7 @@ GEM
hashdiff (>= 0.4.0, < 2.0.0)

PLATFORMS
aarch64-linux
arm64-darwin
x86_64-darwin-21
x86_64-darwin-22
Expand Down
29 changes: 29 additions & 0 deletions src/bosh_azure_cpi/lib/cloud/azure/disk/disk_manager2.rb
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,15 @@ def generate_ephemeral_disk_name(vm_name)
"#{MANAGED_OS_DISK_PREFIX}-#{vm_name}-#{EPHEMERAL_DISK_POSTFIX}"
end

def os_disk_placement(placement)
case placement
when 'resource-disk'
'ResourceDisk'
when 'cache-disk'
'CacheDisk'
end
end

def os_disk(vm_name, stemcell_info, size, caching, use_root_disk_as_ephemeral)
validate_disk_caching(caching)

Expand All @@ -167,6 +176,26 @@ def os_disk(vm_name, stemcell_info, size, caching, use_root_disk_as_ephemeral)
}
end

def ephemeral_os_disk(vm_name, stemcell_info, root_disk_size, ephemeral_disk_size, use_root_disk_as_ephemeral, placement)
disk_size = if use_root_disk_as_ephemeral && !ephemeral_disk_size.nil? && root_disk_size.nil?
# when no size was specified at the root disk, we have to use the default stemcell image size based on the os type. For linux we will use 3g and 128gb for windows.
stemcell_info.image_size / 1024
else
disk_size = get_os_disk_size(root_disk_size, stemcell_info, use_root_disk_as_ephemeral)
end

# when a epehemeral os disk size was configured we add the size of the disk to the root disk to get the same size for the user content as expected.
disk_size += ephemeral_disk_size / 1024 if use_root_disk_as_ephemeral && !ephemeral_disk_size.nil?

disk_placement = os_disk_placement(placement)
{
disk_name: generate_os_disk_name(vm_name),
disk_size: disk_size,
disk_caching: 'ReadOnly',
disk_placement: disk_placement
}
end

def ephemeral_disk(vm_name, instance_type, size, type, use_root_disk_as_ephemeral)
return nil if use_root_disk_as_ephemeral

Expand Down
5 changes: 3 additions & 2 deletions src/bosh_azure_cpi/lib/cloud/azure/models/root_disk.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@

module Bosh::AzureCloud
class RootDisk
attr_reader :size, :type
attr_reader :size, :type, :placement

def initialize(size, type)
def initialize(size, type, placement)
@size = size
@type = type
@placement = placement
end
end
end
22 changes: 21 additions & 1 deletion src/bosh_azure_cpi/lib/cloud/azure/models/vm_cloud_props.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,13 @@ def initialize(vm_properties, global_azure_config)
cloud_error("You need to specify one of 'vm_type/instance_type' or 'vm_resources'.") if @instance_type.nil? && (@instance_types.nil? || @instance_types.empty?)

root_disk_hash = vm_properties.fetch('root_disk', {})
@root_disk = Bosh::AzureCloud::RootDisk.new(root_disk_hash['size'], root_disk_hash['type'])
@root_disk = Bosh::AzureCloud::RootDisk.new(
root_disk_hash['size'],
root_disk_hash['type'],
_default_root_disk_placement(root_disk_hash['placement'])
)

cloud_error("Only one of 'type' and 'placement' is allowed to be configured for the root_disk when 'placement' is not set to persistent") if @root_disk.placement != 'remote' && !@root_disk.type.nil? && !global_azure_config.use_managed_disks

ephemeral_disk_hash = vm_properties.fetch('ephemeral_disk', {})
@ephemeral_disk = Bosh::AzureCloud::EphemeralDisk.new(
Expand Down Expand Up @@ -86,6 +92,20 @@ def initialize(vm_properties, global_azure_config)

private

# In Azure you can place your root disk at:
# temp disk - resource-disk
# cache disk - chache-disk
# persistent (default) - persistent os disk
def _default_root_disk_placement(root_disk_placement)
if root_disk_placement.nil?
'remote'
else
valid_placements = %w[resource-disk cache-disk remote]
cloud_error("root_disk 'placement' must be one of 'resource-disk','cache-disk','remote'") unless valid_placements.include?(root_disk_placement)
root_disk_placement
end
end

# In AzureStack, availability sets can only be configured with 1 update domain.
# In Azure, the max update domain count of a managed/unmanaged availability set is 5.
def _default_update_domain_count(global_azure_config)
Expand Down
36 changes: 30 additions & 6 deletions src/bosh_azure_cpi/lib/cloud/azure/restapi/azure_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,13 @@ def get_resource_group(resource_group_name)
# * +:disk_size+ - Integer. The size in GiB of the ephemeral disk.
# * +:disk_type+ - String. The disk type of the ephemeral disk.
#
# When managed is true and root_disk type is not 'remote' below parameters are required
# * +:image_id+ - String. The id of the image to create the virtual machine.
# * +:ephemeral_os_disk+ - Hash. Azure Ephemeral OS Disk for the virtual machine instance.
# * +:disk_name+ - String. The name of the ephemeral disk.
# * +:disk_size+ - Integer. The size in GiB of the OS disk. It could be nil.
# * +:disk_placement+ - String. Where Ephemeral OS Disk is placed. Possible values: remote, resource-disk, cache-disk.
#
# When managed is false or nil, below parameters are required
# * +:image_uri+ - String. The URI of the image.
# * +:os_disk+ - Hash. OS Disk for the virtual machine instance.
Expand Down Expand Up @@ -342,12 +349,28 @@ def create_virtual_machine(resource_group_name, vm_params, network_interfaces, a

vm['zones'] = [vm_params[:zone]] unless vm_params[:zone].nil?

os_disk = {
'name' => vm_params[:os_disk][:disk_name],
'createOption' => 'FromImage',
'caching' => vm_params[:os_disk][:disk_caching]
}
os_disk['diskSizeGB'] = vm_params[:os_disk][:disk_size] unless vm_params[:os_disk][:disk_size].nil?
os_disk = {}
unless vm_params[:os_disk].nil?
os_disk = {
'name' => vm_params[:os_disk][:disk_name],
'createOption' => 'FromImage',
'caching' => vm_params[:os_disk][:disk_caching]
}
os_disk['diskSizeGB'] = vm_params[:os_disk][:disk_size] unless vm_params[:os_disk][:disk_size].nil?
end

unless vm_params[:ephemeral_os_disk].nil?
os_disk = {
'diffDiskSettings' => {
'option' => 'Local',
'placement' => vm_params[:ephemeral_os_disk][:disk_placement]
},
'caching' => vm_params[:ephemeral_os_disk][:disk_caching],
'createOption' => 'FromImage',
'name' => vm_params[:ephemeral_os_disk][:disk_name]
}
os_disk['diskSizeGB'] = vm_params[:ephemeral_os_disk][:disk_size] unless vm_params[:ephemeral_os_disk][:disk_size].nil?
end

if vm_params[:image_reference].nil?
if vm_params[:managed]
Expand Down Expand Up @@ -390,6 +413,7 @@ def create_virtual_machine(resource_group_name, vm_params, network_interfaces, a
'product' => vm_params[:image_reference]['offer']
}
end

unless vm_params[:ephemeral_disk].nil?
vm['properties']['storageProfile']['dataDisks'] = [{
'name' => vm_params[:ephemeral_disk][:disk_name],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ def initial_agent_settings(agent_id, network_spec, environment, vm_params, confi
}

unless vm_params[:ephemeral_disk].nil?
# Azure uses a data disk as the ephermeral disk and the lun is 0
# Azure uses a data disk as the ephemeral disk and the lun is 0
settings['disks']['ephemeral'] = {
'lun' => '0',
'host_device_id' => Bosh::AzureCloud::Helpers::AZURE_SCSI_HOST_DEVICE_ID
Expand Down
2 changes: 1 addition & 1 deletion src/bosh_azure_cpi/lib/cloud/azure/utils/helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ module Helpers
'resourceManagerEndpointUrl' => 'https://management.azure.com/',
'activeDirectoryEndpointUrl' => 'https://login.microsoftonline.com',
'apiVersion' => {
AZURE_RESOURCE_PROVIDER_COMPUTE => '2018-04-01',
AZURE_RESOURCE_PROVIDER_COMPUTE => '2021-04-01',
AZURE_RESOURCE_PROVIDER_NETWORK => '2017-09-01',
AZURE_RESOURCE_PROVIDER_STORAGE => '2017-10-01',
AZURE_RESOURCE_PROVIDER_GROUP => '2016-06-01',
Expand Down
23 changes: 13 additions & 10 deletions src/bosh_azure_cpi/lib/cloud/azure/vms/vm_manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ def create(bosh_vm_meta, location, vm_props, disk_cids, network_configurator, en
}
end
vm_params[:zone] = zone.to_s unless zone.nil?
vm_params[:os_disk], vm_params[:ephemeral_disk] = _build_disks(instance_id, stemcell_info, vm_props)
vm_params[:os_disk], vm_params[:ephemeral_disk], vm_params[:ephemeral_os_disk] = _build_disks(instance_id, stemcell_info, vm_props)
vm_params[:os_type] = stemcell_info.os_type

if stemcell_info.is_light_stemcell?
Expand Down Expand Up @@ -530,16 +530,19 @@ def _create_virtual_machine(instance_id, vm_params, network_interfaces, availabi
@azure_client.delete_virtual_machine(resource_group_name, vm_name)
end

if @use_managed_disks
os_disk_name = @disk_manager2.generate_os_disk_name(vm_name)
@disk_manager2.delete_disk(resource_group_name, os_disk_name)
else
storage_account_name = instance_id.storage_account_name
os_disk_name = @disk_manager.generate_os_disk_name(vm_name)
@disk_manager.delete_disk(storage_account_name, os_disk_name)
# ephemeral_os_disk delete automatically, run for os_disk and ephemeral_disk only
unless vm_params[:os_disk].nil?
if @use_managed_disks
os_disk_name = @disk_manager2.generate_os_disk_name(vm_name)
@disk_manager2.delete_disk(resource_group_name, os_disk_name)
else
storage_account_name = instance_id.storage_account_name
os_disk_name = @disk_manager.generate_os_disk_name(vm_name)
@disk_manager.delete_disk(storage_account_name, os_disk_name)

# Cleanup invalid VM status file
@disk_manager.delete_vm_status_files(storage_account_name, vm_name)
# Cleanup invalid VM status file
@disk_manager.delete_vm_status_files(storage_account_name, vm_name)
end
end

unless vm_params[:ephemeral_disk].nil?
Expand Down
10 changes: 8 additions & 2 deletions src/bosh_azure_cpi/lib/cloud/azure/vms/vm_manager_disk.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,20 @@ class VMManager

def _build_disks(instance_id, stemcell_info, vm_props)
if @use_managed_disks
os_disk = @disk_manager2.os_disk(instance_id.vm_name, stemcell_info, vm_props.root_disk.size, vm_props.caching, vm_props.ephemeral_disk.use_root_disk)
ephemeral_disk = @disk_manager2.ephemeral_disk(instance_id.vm_name, vm_props.instance_type, vm_props.ephemeral_disk.size, vm_props.ephemeral_disk.type, vm_props.ephemeral_disk.use_root_disk)

# check if disk has the default behavior
if vm_props.root_disk.placement == 'remote'
os_disk = @disk_manager2.os_disk(instance_id.vm_name, stemcell_info, vm_props.root_disk.size, vm_props.caching, vm_props.ephemeral_disk.use_root_disk)
else
ephemeral_os_disk = @disk_manager2.ephemeral_os_disk(instance_id.vm_name, stemcell_info, vm_props.root_disk.size, vm_props.ephemeral_disk.size, vm_props.ephemeral_disk.use_root_disk, vm_props.root_disk.placement)
end
else
storage_account_name = instance_id.storage_account_name
os_disk = @disk_manager.os_disk(storage_account_name, instance_id.vm_name, stemcell_info, vm_props.root_disk.size, vm_props.caching, vm_props.ephemeral_disk.use_root_disk)
ephemeral_disk = @disk_manager.ephemeral_disk(storage_account_name, instance_id.vm_name, vm_props.instance_type, vm_props.ephemeral_disk.size, vm_props.ephemeral_disk.use_root_disk)
end
[os_disk, ephemeral_disk]
[os_disk, ephemeral_disk, ephemeral_os_disk]
end

def _get_root_disk_type(vm_props)
Expand Down
2 changes: 1 addition & 1 deletion src/bosh_azure_cpi/spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
AZURE_CHINA_API_VERSION = '2015-06-15'
AZURE_USGOV_API_VERSION = '2015-06-15'
AZURE_GERMAN_API_VERSION = '2015-06-15'
AZURE_RESOURCE_PROVIDER_COMPUTE = '2018-04-01'
AZURE_RESOURCE_PROVIDER_COMPUTE = '2021-04-01'
AZURE_RESOURCE_PROVIDER_GROUP = '2016-06-01'
AZURE_RESOURCE_PROVIDER_NETWORK = '2017-09-01'
AZURE_RESOURCE_PROVIDER_STORAGE = '2017-10-01'
Expand Down
Loading

0 comments on commit 13019a1

Please sign in to comment.