From 97a4e59f3adbe1fcb0ba0c4940915800b7db786b Mon Sep 17 00:00:00 2001 From: Ansh Rupani Date: Tue, 16 May 2023 13:25:41 +0530 Subject: [PATCH] support instance tags alongside deployment tags --- .../lib/bosh/director/vm_creator.rb | 10 +++++++++ .../spec/unit/vm_creator_spec.rb | 21 ++++++++++++++++++- 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/src/bosh-director/lib/bosh/director/vm_creator.rb b/src/bosh-director/lib/bosh/director/vm_creator.rb index 41d905a6d4c..c334eb178d2 100644 --- a/src/bosh-director/lib/bosh/director/vm_creator.rb +++ b/src/bosh-director/lib/bosh/director/vm_creator.rb @@ -54,6 +54,12 @@ def get_agenda_for_instance_plan(instance_plan, disks, tags, ip_provider, total, instance = instance_plan.instance already_had_active_vm = instance.vm_created? + variables_interpolator = Bosh::Director::ConfigServer::VariablesInterpolator.new + env = variables_interpolator.interpolate_with_versioning(instance.env, instance.desired_variable_set) + env['bosh'] ||= {} + env['bosh'] = Config.agent_env.merge(env['bosh']) + tags = combined_tags(tags, env['bosh']['tags']) + agenda.steps = [ DeploymentPlan::Steps::CreateVmStep.new( instance_plan, @@ -93,5 +99,9 @@ def get_agenda_for_instance_plan(instance_plan, disks, tags, ip_provider, total, def creating_first_create_swap_delete_vm?(instance_plan, already_had_active_vm) instance_plan.should_create_swap_delete? && !already_had_active_vm end + + def combined_tags(deployment_tags, instance_tags) + instance_tags.nil? ? deployment_tags : deployment_tags.merge(instance_tags) + end end end diff --git a/src/bosh-director/spec/unit/vm_creator_spec.rb b/src/bosh-director/spec/unit/vm_creator_spec.rb index 3517461e853..ac1fcaded54 100644 --- a/src/bosh-director/spec/unit/vm_creator_spec.rb +++ b/src/bosh-director/spec/unit/vm_creator_spec.rb @@ -16,6 +16,7 @@ module Director end let(:dns_encoder) { instance_double(DnsEncoder) } let(:ip_provider) { double(:ip_provider) } + let(:variables_interpolator) { instance_double(Bosh::Director::ConfigServer::VariablesInterpolator) } let(:instance) do instance_double(DeploymentPlan::Instance, model: instance_model, vm_created?: false) @@ -33,6 +34,19 @@ module Director ) end + let(:agent_env_bosh_hash) do + { + 'value_1_key' => 'value_1_value_changed', + 'value_6_key' => { + 'smurf' => 'i am here', + }, + 'a' => '12', + 'b' => { + 'c' => '34', + }, + } + end + let(:tags) { { 'mytag' => 'foobar' } } let(:instance_group) do @@ -93,7 +107,9 @@ module Director before do fake_app - + allow(instance).to receive(:env).and_return({ "bosh": {} }) + allow(instance).to receive(:desired_variable_set).and_return({}) + allow(variables_interpolator).to receive(:interpolate_with_versioning).and_return({}) allow(instance_model).to receive(:managed_persistent_disk_cid).and_return('fake-disk-cid') allow(DeploymentPlan::Stages::Report).to receive(:new).and_return(report) @@ -120,6 +136,8 @@ module Director .and_return(commit_networks_step) allow(DeploymentPlan::Steps::ReleaseObsoleteNetworksStep).to receive(:new) .with(ip_provider).and_return(release_networks_step) + allow(ConfigServer::VariablesInterpolator).to receive(:new).and_return(variables_interpolator) + allow(Config).to receive(:agent_env).and_return(agent_env_bosh_hash) end describe '#create_for_instance_plan' do @@ -188,6 +206,7 @@ module Director context 'when the instance uses create-swap-delete strategy' do before do allow(instance_plan).to receive(:should_create_swap_delete?).and_return(true) + allow(instance).to receive(:env).and_return({}) end context 'when there is already an active vm' do