Skip to content

Commit

Permalink
Implement translation of instance network ips to existing subnets
Browse files Browse the repository at this point in the history
  • Loading branch information
sulksamino authored and alerei committed Feb 13, 2024
1 parent cc0dda3 commit 6d028af
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
2 changes: 2 additions & 0 deletions daemon/common/deployment/deployment.h
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,8 @@ class deployment_t
-> std::string_view;
auto default_network_gateway() const //
-> std::string_view;
auto transfer_ip_to_network(const network_t& network, std::string_view ip_address) const //
-> std::optional<ip_addr_t>;
auto get_base_ip(std::string_view cidr_subnet) const //
-> std::optional<ip_addr_t>;
auto get_subnet_size(std::string_view cidr_subnet) const //
Expand Down
32 changes: 32 additions & 0 deletions daemon/common/deployment/src/deployment.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,19 @@ auto deployment_t::import_instance(std::shared_ptr<instances::instance_t> instan

base_dir /= instance->id().hex();

for (auto& network : instance->networks()) {
auto net = query_network(network.network_name);
if (!net.has_value()) {
return {-1, "Could not find network " + network.network_name};
}
auto result_address = transfer_ip_to_network(net.value(), network.ip_address);
if (result_address.has_value()) {
network.ip_address = to_string(result_address.value());
} else {
return {-1, "Could not transfer ip " + network.ip_address + " to network " + net.value().name};
}
}

auto [res, additional_info] = import_volumes(instance, base_dir / "volumes");
if (res != 0) {
return {res, additional_info};
Expand Down Expand Up @@ -741,6 +754,25 @@ auto deployment_t::default_network_gateway() const //
return do_default_network_gateway();
}

auto deployment_t::transfer_ip_to_network(const network_t& network, std::string_view ip_address) const //
-> std::optional<ip_addr_t>
{
auto base_ip = get_base_ip(network.cidr_subnet);
if (!base_ip.has_value()) {
return {};
}
auto net_size = get_subnet_size(network.cidr_subnet);
if (!net_size.has_value()) {
return {};
}
auto num = ip_addr_t{ip_address}.addr_v4();
// Remove network part from ip
num.s_addr &= ~0u << net_size.value();
// Combine with new network base
num.s_addr |= base_ip.value().addr_v4().s_addr;
return ip_addr_t{num};
}

auto deployment_t::get_base_ip(std::string_view cidr_subnet) const //
-> std::optional<ip_addr_t>
{
Expand Down

0 comments on commit 6d028af

Please # to comment.