Skip to content

Commit

Permalink
Remove unnecessary detach logic from Nic
Browse files Browse the repository at this point in the history
Detach nic from the VM logic is not needed since we do not support
managing nics, yet. There is no need to keep it.

I have also moved the cleanup of tunnels to the
association_dependencies.

Furthermore, since we do not bud any child processes anymore, we do not
need to destroy child progs separately at the time of nic destroy.
  • Loading branch information
furkansahin committed May 6, 2024
1 parent 3f871d4 commit fdb74a0
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 74 deletions.
5 changes: 4 additions & 1 deletion model/nic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@ class Nic < Sequel::Model
one_to_many :src_ipsec_tunnels, key: :src_nic_id, class: IpsecTunnel
one_to_many :dst_ipsec_tunnels, key: :dst_nic_id, class: IpsecTunnel
one_to_one :strand, key: :id, class: Strand
plugin :association_dependencies, src_ipsec_tunnels: :destroy, dst_ipsec_tunnels: :destroy

include ResourceMethods
include SemaphoreMethods
semaphore :destroy, :detach_vm, :start_rekey, :trigger_outbound_update,

semaphore :destroy, :start_rekey, :trigger_outbound_update,
:old_state_drop_trigger, :setup_nic, :repopulate

plugin :column_encryption do |enc|
Expand Down
29 changes: 3 additions & 26 deletions prog/vnet/nic_nexus.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

class Prog::Vnet::NicNexus < Prog::Base
subject_is :nic
semaphore :destroy, :detach_vm, :start_rekey, :trigger_outbound_update, :old_state_drop_trigger, :setup_nic, :repopulate
semaphore :destroy, :start_rekey, :trigger_outbound_update, :old_state_drop_trigger, :setup_nic, :repopulate

def self.assemble(private_subnet_id, name: nil, ipv6_addr: nil, ipv4_addr: nil)
unless (subnet = PrivateSubnet[private_subnet_id])
Expand Down Expand Up @@ -51,10 +51,6 @@ def before_run
end

label def wait
when_detach_vm_set? do
hop_detach_vm
end

when_repopulate_set? do
hop_repopulate
end
Expand Down Expand Up @@ -120,32 +116,13 @@ def before_run
end

decr_destroy
strand.children.each { _1.destroy }

DB.transaction do
nic.src_ipsec_tunnels_dataset.destroy
nic.dst_ipsec_tunnels_dataset.destroy
nic.private_subnet.incr_refresh_keys

nic.destroy
end
nic.private_subnet.incr_refresh_keys
nic.destroy

pop "nic deleted"
end

label def detach_vm
DB.transaction do
nic.update(vm_id: nil)
nic.src_ipsec_tunnels_dataset.destroy
nic.dst_ipsec_tunnels_dataset.destroy
nic.private_subnet.incr_refresh_keys
strand.children.map { _1.destroy }
decr_detach_vm
end

hop_wait
end

# Generate a MAC with the "local" (generated, non-manufacturer) bit
# set and the multicast bit cleared in the first octet.
#
Expand Down
47 changes: 0 additions & 47 deletions spec/prog/vnet/nic_nexus_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -127,11 +127,6 @@
expect { nx.wait }.to nap(30)
end

it "hops to detach vm if needed" do
expect(nx).to receive(:when_detach_vm_set?).and_yield
expect { nx.wait }.to hop("detach_vm")
end

it "hops to start rekey if needed" do
expect(nx).to receive(:when_start_rekey_set?).and_yield
expect { nx.wait }.to hop("start_rekey")
Expand Down Expand Up @@ -241,10 +236,6 @@
end

it "destroys nic" do
expect(ipsec_tunnels[0]).to receive(:destroy).and_return(true)
expect(ipsec_tunnels[1]).to receive(:destroy).and_return(true)
expect(nic).to receive(:src_ipsec_tunnels_dataset).and_return(ipsec_tunnels[0])
expect(nic).to receive(:dst_ipsec_tunnels_dataset).and_return(ipsec_tunnels[1])
expect(nic).to receive(:private_subnet).and_return(ps)
expect(ps).to receive(:incr_refresh_keys).and_return(true)
expect(nic).to receive(:destroy).and_return(true)
Expand All @@ -257,44 +248,6 @@
end
end

describe "#detach_vm" do
let(:ps) {
PrivateSubnet.create_with_id(name: "ps", location: "hetzner-hel1", net6: "fd10:9b0b:6b4b:8fbb::/64",
net4: "1.1.1.0/26", state: "waiting").tap { _1.id = "57afa8a7-2357-4012-9632-07fbe13a3133" }
}
let(:nic) {
Nic.new(private_subnet_id: ps.id,
private_ipv6: "fd10:9b0b:6b4b:8fbb:abc::",
private_ipv4: "10.0.0.1",
mac: "00:00:00:00:00:00",
encryption_key: "0x736f6d655f656e6372797074696f6e5f6b6579",
name: "default-nic").tap { _1.id = "0a9a166c-e7e7-4447-ab29-7ea442b5bb0e" }
}
let(:ipsec_tunnels) {
[
instance_double(IpsecTunnel),
instance_double(IpsecTunnel)
]
}

before do
allow(nx).to receive(:nic).and_return(nic)
end

it "detaches vm and refreshes mesh" do
expect(nic).to receive(:update).with(vm_id: nil).and_return(true)
expect(nic).to receive(:src_ipsec_tunnels_dataset).and_return(ipsec_tunnels[0])
expect(ipsec_tunnels[0]).to receive(:destroy).and_return(true)
expect(nic).to receive(:dst_ipsec_tunnels_dataset).and_return(ipsec_tunnels[1])
expect(ipsec_tunnels[1]).to receive(:destroy).and_return(true)

expect(nic).to receive(:private_subnet).and_return(ps)
expect(ps).to receive(:incr_refresh_keys).and_return(true)

expect { nx.detach_vm }.to hop("wait")
end
end

describe "nic fetch" do
let(:nic) {
Nic.new(private_subnet_id: ps.id,
Expand Down

0 comments on commit fdb74a0

Please sign in to comment.