-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(containers): send one network for each release
the network is set so that it does not have internet access and containers outside the network cannot communicate with containers inside it Signed-off-by: Francesco Noacco <[email protected]>
- Loading branch information
Showing
4 changed files
with
76 additions
and
11 deletions.
There are no files selected for viewing
69 changes: 69 additions & 0 deletions
69
backend/lib/edgehog/containers/changes/create_default_network.ex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
# | ||
# This file is part of Edgehog. | ||
# | ||
# Copyright 2024 SECO Mind Srl | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# | ||
# SPDX-License-Identifier: Apache-2.0 | ||
# | ||
|
||
defmodule Edgehog.Containers.Changes.CreateDefaultNetwork do | ||
@moduledoc false | ||
use Ash.Resource.Change | ||
|
||
alias Edgehog.Containers.ContainerNetwork | ||
alias Edgehog.Containers.Network | ||
|
||
@impl Ash.Resource.Change | ||
def change(changeset, _opts, _ctx) do | ||
Ash.Changeset.after_action(changeset, fn _changeset, release -> | ||
with {:ok, release} <- Ash.load(release, :containers), | ||
{:ok, network} <- create_default_network(release.tenant_id), | ||
:ok <- create_networks(release, network) do | ||
{:ok, release} | ||
end | ||
end) | ||
end | ||
|
||
defp create_networks(release, network) do | ||
Enum.reduce_while(release.containers, :ok, fn container, _acc -> | ||
add_network(container, network) | ||
end) | ||
end | ||
|
||
defp create_default_network(tenant) do | ||
default_network_parameters = %{ | ||
driver: "bridge", | ||
options: ["isolate=true"], | ||
internal: true, | ||
enable_ipv6: false | ||
} | ||
|
||
Network | ||
|> Ash.Changeset.for_create(:create, default_network_parameters) | ||
|> Ash.create(tenant: tenant) | ||
end | ||
|
||
defp add_network(container, network) do | ||
params = %{ | ||
container_id: container.id, | ||
network_id: network.id | ||
} | ||
|
||
case Ash.create(ContainerNetwork, params, tenant: container.tenant_id) do | ||
{:ok, _device} -> {:cont, :ok} | ||
{:error, reason} -> {:halt, {:error, reason}} | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters