Skip to content

Commit

Permalink
fix dns_setup as zone was already a method
Browse files Browse the repository at this point in the history
also changed from setting the project name in the instance labels to a
instance tag aka network tag, as the setup for allowing traffic in the FW
is bassed on tag not label
  • Loading branch information
Samuel Beaulieu committed Jan 6, 2022
1 parent 2cb2550 commit b9b01f1
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 55 deletions.
20 changes: 11 additions & 9 deletions lib/vmpooler/providers/gce.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ def connection

def dns
@dns ||= Google::Cloud::Dns.new(project_id: project)
@dns
end

# main configuration options
Expand Down Expand Up @@ -196,7 +197,8 @@ def create_vm(pool_name, new_vmname)
machine_type: pool['machine_type'],
disks: [disk],
network_interfaces: [network_interfaces],
labels: { 'vm' => new_vmname, 'pool' => pool_name, project => nil }
labels: { 'vm' => new_vmname, 'pool' => pool_name },
tags: Google::Apis::ComputeV1::Tags.new(items: [project])
)

debug_logger('trigger insert_instance')
Expand Down Expand Up @@ -555,26 +557,26 @@ def tag_vm_user(pool, vm_name)
# END BASE METHODS

def dns_setup(created_instance)
zone = dns.zone dns_zone_resource_name if dns_zone_resource_name
return unless zone && created_instance && created_instance['name'] && created_instance['ip']
dns_zone = dns.zone(dns_zone_resource_name) if dns_zone_resource_name
return unless dns_zone && created_instance && created_instance['name'] && created_instance['ip']

name = created_instance['name']
begin
change = zone.add name, 'A', 60, [created_instance['ip']]
change = dns_zone.add(name, 'A', 60, [created_instance['ip']])
debug_logger("#{change.id} - #{change.started_at} - #{change.status}") if change
rescue AlreadyExistsError => _e
rescue Google::Cloud::AlreadyExistsError => _e
# DNS setup is done only for new instances, so in the rare case where a DNS record already exists (it is stale) and we replace it.
# the error is Google::Cloud::AlreadyExistsError: alreadyExists: The resource 'entity.change.additions[0]' named 'instance-8.test.vmpooler.net. (A)' already exists
zone.replace(name, 'A', 60, [created_instance['ip']])
dns_zone.replace(name, 'A', 60, [created_instance['ip']])
end
end

def dns_teardown(created_instance)
zone = dns.zone dns_zone_resource_name if dns_zone_resource_name
return unless zone && created_instance
dns_zone = dns.zone(dns_zone_resource_name) if dns_zone_resource_name
return unless dns_zone && created_instance

name = created_instance['name']
change = zone.remove name, 'A'
change = dns_zone.remove(name, 'A')
debug_logger("#{change.id} - #{change.started_at} - #{change.status}") if change
end

Expand Down
1 change: 1 addition & 0 deletions scripts/GCE_custom_role_for_SA.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ includedPermissions:
- compute.instances.get
- compute.instances.list
- compute.instances.setLabels
- compute.instances.setTags
- compute.instances.start
- compute.instances.stop
- compute.snapshots.create
Expand Down
94 changes: 48 additions & 46 deletions spec/unit/providers/gce_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,26 @@
let(:provider_options) { { 'param' => 'value' } }
let(:project) { 'vmpooler-test' }
let(:zone) { 'us-west1-b' }
let(:config) { YAML.load(<<-EOT
---
:config:
max_tries: 3
retry_factor: 10
:providers:
:gce:
connection_pool_timeout: 1
project: '#{project}'
zone: '#{zone}'
network_name: global/networks/default
:pools:
- name: '#{poolname}'
alias: [ 'mockpool' ]
template: 'projects/debian-cloud/global/images/family/debian-9'
size: 5
timeout: 10
ready_ttl: 1440
provider: 'gce'
machine_type: 'zones/#{zone}/machineTypes/e2-micro'
let(:config) { YAML.load(<<~EOT
---
:config:
max_tries: 3
retry_factor: 10
:providers:
:gce:
connection_pool_timeout: 1
project: '#{project}'
zone: '#{zone}'
network_name: global/networks/default
:pools:
- name: '#{poolname}'
alias: [ 'mockpool' ]
template: 'projects/debian-cloud/global/images/family/debian-9'
size: 5
timeout: 10
ready_ttl: 1440
provider: 'gce'
machine_type: 'zones/#{zone}/machineTypes/e2-micro'
EOT
)
}
Expand Down Expand Up @@ -61,36 +61,38 @@

describe '#manual tests live' do
context 'in itsysops' do
let(:vmname) { "instance-15" }
before(:each) { allow(subject).to receive(:dns).and_call_original }
let(:vmname) { "instance-24" }
let(:project) { 'vmpooler-test' }
let(:config) { YAML.load(<<-EOT
---
:config:
max_tries: 3
retry_factor: 10
:providers:
:gce:
connection_pool_timeout: 1
project: '#{project}'
zone: '#{zone}'
network_name: 'projects/itsysopsnetworking/global/networks/shared1'
dns_zone_resource_name: 'test-vmpooler-puppet-net'
domain: 'test.vmpooler.puppet.net'
:pools:
- name: '#{poolname}'
alias: [ 'mockpool' ]
template: 'projects/debian-cloud/global/images/family/debian-9'
size: 5
timeout: 10
ready_ttl: 1440
provider: 'gce'
subnetwork_name: 'projects/itsysopsnetworking/regions/us-west1/subnetworks/vmpooler-test'
machine_type: 'zones/#{zone}/machineTypes/e2-micro'
EOT
let(:config) { YAML.load(<<~EOT
---
:config:
max_tries: 3
retry_factor: 10
:providers:
:gce:
connection_pool_timeout: 1
project: '#{project}'
zone: '#{zone}'
network_name: 'projects/itsysopsnetworking/global/networks/shared1'
dns_zone_resource_name: 'test-vmpooler-puppet-net'
domain: 'test.vmpooler.puppet.net'
:pools:
- name: '#{poolname}'
alias: [ 'mockpool' ]
template: 'projects/debian-cloud/global/images/family/debian-9'
size: 5
timeout: 10
ready_ttl: 1440
provider: 'gce'
subnetwork_name: 'projects/itsysopsnetworking/regions/us-west1/subnetworks/vmpooler-test'
machine_type: 'zones/#{zone}/machineTypes/e2-micro'
EOT
) }
skip 'gets a vm' do
result = subject.create_vm(poolname, vmname)
#subject.get_vm(poolname, vmname)
#result = subject.destroy_vm(poolname, vmname)
subject.get_vm(poolname, vmname)
#subject.dns_teardown({'name' => vmname})
# subject.dns_setup({'name' => vmname, 'ip' => '1.2.3.5'})
end
Expand Down

0 comments on commit b9b01f1

Please sign in to comment.