From 84bbf1c705a845a59676fe9788c7ed40448fc433 Mon Sep 17 00:00:00 2001 From: Felix Moehler Date: Tue, 26 Nov 2024 20:29:22 +0100 Subject: [PATCH 1/3] enable dual stack support --- .../lib/cloud/aws/instance_param_mapper.rb | 20 +++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/src/bosh_aws_cpi/lib/cloud/aws/instance_param_mapper.rb b/src/bosh_aws_cpi/lib/cloud/aws/instance_param_mapper.rb index 0c25179c..950e2354 100644 --- a/src/bosh_aws_cpi/lib/cloud/aws/instance_param_mapper.rb +++ b/src/bosh_aws_cpi/lib/cloud/aws/instance_param_mapper.rb @@ -93,13 +93,8 @@ def instance_params nic[:subnet_id] = subnet_id if subnet_id # only supporting one ip address for now (either ipv4 or ipv6) - if private_ip_address - if ipv6_address?(private_ip_address) - nic[:ipv_6_addresses] = [{ipv_6_address: private_ip_address}] - else - nic[:private_ip_address] = private_ip_address - end - end + nic[:ipv_6_addresses] = [{ipv_6_address: private_ipv6_address}] if !private_ipv6_address.nil? + nic[:private_ip_address] = private_ip_address if !private_ip_address.nil? nic[:associate_public_ip_address] = vm_type.auto_assign_public_ip unless vm_type.auto_assign_public_ip.nil? @@ -149,7 +144,16 @@ def ipv6_address?(addr) def private_ip_address first_manual_network = networks_cloud_props.filter('manual').first - first_manual_network.ip unless first_manual_network.nil? + return first_manual_network.ip if !first_manual_network.nil? && !ipv6_address?(first_manual_network.ip) + second_manual_network = networks_cloud_props.filter('manual')[1] + second_manual_network.ip if !second_manual_network.nil? && !ipv6_address?(second_manual_network.ip) + end + + def private_ipv6_address + first_manual_network = networks_cloud_props.filter('manual').first + return first_manual_network.ip if !first_manual_network.nil? && ipv6_address?(first_manual_network.ip) + second_manual_network = networks_cloud_props.filter('manual')[1] + second_manual_network.ip if !second_manual_network.nil? && ipv6_address?(second_manual_network.ip) end # NOTE: do NOT lookup the subnet (from EC2 client) anymore. We just need to From ed83b1bafa68570dffcfdbe6d5bc4af3336a92be Mon Sep 17 00:00:00 2001 From: Felix Moehler Date: Mon, 2 Dec 2024 11:04:19 +0100 Subject: [PATCH 2/3] add second network interface card --- .../lib/cloud/aws/instance_manager.rb | 2 +- .../lib/cloud/aws/instance_param_mapper.rb | 35 +++++++++++++++---- 2 files changed, 29 insertions(+), 8 deletions(-) diff --git a/src/bosh_aws_cpi/lib/cloud/aws/instance_manager.rb b/src/bosh_aws_cpi/lib/cloud/aws/instance_manager.rb index e8c58145..df82f7e8 100644 --- a/src/bosh_aws_cpi/lib/cloud/aws/instance_manager.rb +++ b/src/bosh_aws_cpi/lib/cloud/aws/instance_manager.rb @@ -12,7 +12,7 @@ def initialize(ec2, logger) @imds_v2_enable = {} security_group_mapper = SecurityGroupMapper.new(@ec2) - @param_mapper = InstanceParamMapper.new(security_group_mapper) + @param_mapper = InstanceParamMapper.new(security_group_mapper, logger) end def create(stemcell_id, vm_cloud_props, networks_cloud_props, disk_locality, default_security_groups, block_device_mappings, user_data, tags, metadata_options) diff --git a/src/bosh_aws_cpi/lib/cloud/aws/instance_param_mapper.rb b/src/bosh_aws_cpi/lib/cloud/aws/instance_param_mapper.rb index 950e2354..9c9a19b9 100644 --- a/src/bosh_aws_cpi/lib/cloud/aws/instance_param_mapper.rb +++ b/src/bosh_aws_cpi/lib/cloud/aws/instance_param_mapper.rb @@ -4,8 +4,9 @@ module Bosh::AwsCloud class InstanceParamMapper attr_accessor :manifest_params - def initialize(security_group_mapper) + def initialize(security_group_mapper, logger) @manifest_params = {} + @logger = logger @security_group_mapper = security_group_mapper end @@ -86,21 +87,31 @@ def instance_params placement[:tenancy] = vm_type.tenancy.dedicated if vm_type.tenancy.dedicated? params[:placement] = placement unless placement.empty? - sg = @security_group_mapper.map_to_ids(security_groups, subnet_id) + subnet_ids = get_all_subnet_ids + subnet_id1 = subnet_ids[0].subnet + subnet_id2 = subnet_ids[1].subnet + + sg = @security_group_mapper.map_to_ids(security_groups, subnet_id1) + sg2 = @security_group_mapper.map_to_ids(security_groups, subnet_id2) nic = {} + nic2 = {} nic[:groups] = sg unless sg.nil? || sg.empty? - nic[:subnet_id] = subnet_id if subnet_id + nic2[:groups] = sg2 unless sg2.nil? || sg2.empty? - # only supporting one ip address for now (either ipv4 or ipv6) - nic[:ipv_6_addresses] = [{ipv_6_address: private_ipv6_address}] if !private_ipv6_address.nil? - nic[:private_ip_address] = private_ip_address if !private_ip_address.nil? + nic[:subnet_id] = subnet_id1 if subnet_id1 + nic2[:subnet_id] = subnet_id2 if subnet_id2 nic[:associate_public_ip_address] = vm_type.auto_assign_public_ip unless vm_type.auto_assign_public_ip.nil? nic[:device_index] = 0 unless nic.empty? - params[:network_interfaces] = [nic] unless nic.empty? + nic2[:device_index] = 1 unless nic2.empty? + + nic2[:ipv_6_addresses] = [{ipv_6_address: private_ipv6_address}] + nic[:private_ip_address] = private_ip_address + params[:network_interfaces] = [nic, nic2] + params.delete_if { |_k, v| v.nil? } end @@ -158,6 +169,16 @@ def private_ipv6_address # NOTE: do NOT lookup the subnet (from EC2 client) anymore. We just need to # pass along the subnet_id anyway, and we have that. + def get_all_subnet_ids + subnet_network_spec = networks_cloud_props.filter('manual', 'dynamic').reject do |net| + net.subnet.nil? + end + + @logger.info(">>>>>subnet_network_spec: '#{subnet_network_spec}'") + + subnet_network_spec unless subnet_network_spec.nil? + end + def subnet_id subnet_network_spec = networks_cloud_props.filter('manual', 'dynamic').reject do |net| net.subnet.nil? From 46da1e1bb11914d9e00959eeadb60aea3d628244 Mon Sep 17 00:00:00 2001 From: Felix Moehler Date: Tue, 3 Dec 2024 15:46:24 +0100 Subject: [PATCH 3/3] use only single network interface card if only one network is applied --- .../lib/cloud/aws/instance_param_mapper.rb | 55 +++++++++++++------ 1 file changed, 39 insertions(+), 16 deletions(-) diff --git a/src/bosh_aws_cpi/lib/cloud/aws/instance_param_mapper.rb b/src/bosh_aws_cpi/lib/cloud/aws/instance_param_mapper.rb index 9c9a19b9..41161cab 100644 --- a/src/bosh_aws_cpi/lib/cloud/aws/instance_param_mapper.rb +++ b/src/bosh_aws_cpi/lib/cloud/aws/instance_param_mapper.rb @@ -88,29 +88,52 @@ def instance_params params[:placement] = placement unless placement.empty? subnet_ids = get_all_subnet_ids - subnet_id1 = subnet_ids[0].subnet - subnet_id2 = subnet_ids[1].subnet + if subnet_ids.length == 1 + subnet_id1 = subnet_ids[0].subnet + sg = @security_group_mapper.map_to_ids(security_groups, subnet_id1) + + nic = {} + nic[:groups] = sg unless sg.nil? || sg.empty? + nic[:subnet_id] = subnet_id1 if subnet_id1 + + # only supporting one ip address for now (either ipv4 or ipv6) + if private_ip_address + if ipv6_address?(private_ip_address) + nic[:ipv_6_addresses] = [{ipv_6_address: private_ip_address}] + else + nic[:private_ip_address] = private_ip_address + end + end + + nic[:associate_public_ip_address] = vm_type.auto_assign_public_ip unless vm_type.auto_assign_public_ip.nil? - sg = @security_group_mapper.map_to_ids(security_groups, subnet_id1) - sg2 = @security_group_mapper.map_to_ids(security_groups, subnet_id2) + nic[:device_index] = 0 unless nic.empty? + params[:network_interfaces] = [nic] unless nic.empty? + else + subnet_id1 = subnet_ids[0].subnet + subnet_id2 = subnet_ids[1].subnet - nic = {} - nic2 = {} - nic[:groups] = sg unless sg.nil? || sg.empty? - nic2[:groups] = sg2 unless sg2.nil? || sg2.empty? + sg = @security_group_mapper.map_to_ids(security_groups, subnet_id1) + sg2 = @security_group_mapper.map_to_ids(security_groups, subnet_id2) - nic[:subnet_id] = subnet_id1 if subnet_id1 - nic2[:subnet_id] = subnet_id2 if subnet_id2 + nic = {} + nic2 = {} + nic[:groups] = sg unless sg.nil? || sg.empty? + nic2[:groups] = sg2 unless sg2.nil? || sg2.empty? - nic[:associate_public_ip_address] = vm_type.auto_assign_public_ip unless vm_type.auto_assign_public_ip.nil? + nic[:subnet_id] = subnet_id1 if subnet_id1 + nic2[:subnet_id] = subnet_id2 if subnet_id2 - nic[:device_index] = 0 unless nic.empty? - nic2[:device_index] = 1 unless nic2.empty? + nic[:associate_public_ip_address] = vm_type.auto_assign_public_ip unless vm_type.auto_assign_public_ip.nil? - nic2[:ipv_6_addresses] = [{ipv_6_address: private_ipv6_address}] - nic[:private_ip_address] = private_ip_address + nic[:device_index] = 0 unless nic.empty? + nic2[:device_index] = 1 unless nic2.empty? - params[:network_interfaces] = [nic, nic2] + nic2[:ipv_6_addresses] = [{ipv_6_address: private_ipv6_address}] + nic[:private_ip_address] = private_ip_address + + params[:network_interfaces] = [nic, nic2] + end params.delete_if { |_k, v| v.nil? } end