Skip to content

network.type support added #62

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: add/network-args
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions scompose/config/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ def validate_config(filepath):
"properties": {
"allocate_ip": {"type": "boolean"},
"enable": {"type": "boolean"},
"type": {"type": "string"},
# --network-args
"args": string_list,
},
Expand Down
20 changes: 17 additions & 3 deletions scompose/project/instance.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,11 @@ def set_network(self, params):
# if not specified, set the default value for the property
for key in ["enable", "allocate_ip"]:
self.network[key] = self.network.get(key, True)

fakeroot = "--fakeroot" in self.start_opts or "-f" in self.start_opts
default_network_value = "fakeroot" if fakeroot else "bridge"

self.network["type"] = self.network.get("type", default_network_value if self.network["enable"] is True else "none")

def set_ports(self, params):
"""
Expand Down Expand Up @@ -221,7 +226,7 @@ def network_args(self):
"""
return self.params.get("network", {}).get("args", [])

def _get_network_commands(self, ip_address=None):
def _get_network_commands(self, ip_address=None, network_type=None):
"""
Take a list of ports, return the list of --network-args to
ensure they are bound correctly.
Expand All @@ -236,7 +241,11 @@ def _get_network_commands(self, ip_address=None):
for arg in network_args:
ports += ["--network-args", arg]

if not network_args and (not self.sudo and not fakeroot):
if network_type is not None:
# network_type is "bridge" by default when network.enable is True
ports += ["--network", network_type]

if network_type is None:# and (not self.sudo and not fakeroot):
ports += ["--network", "none"]

for pair in self.ports:
Expand Down Expand Up @@ -604,7 +613,12 @@ def create(self, ip_address=None, sudo=False, writable_tmpfs=False):

# Network configuration + Ports
if self.network["enable"]:
options += self._get_network_commands(ip_address)
# if network.enable is true a --network must be always added
# using bridge or fakeroot as default
fakeroot = "--fakeroot" in self.start_opts or "-f" in self.start_opts
network_type = self.network["type"] or ("fakeroot" if fakeroot else "bridge")
bot.debug("network_type is: " + network_type)
options += self._get_network_commands(ip_address, network_type)

# Start options
options += self.start_opts
Expand Down
25 changes: 18 additions & 7 deletions scompose/project/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

"""

import traceback
from scompose.templates import get_template
from scompose.logger import bot
from scompose.utils import read_file, write_file
Expand Down Expand Up @@ -241,6 +242,21 @@ def _sort_instances(self, instances):

# Networking

def subnet_from_cni_config(self, config_name="bridge"):
if config_name == "bridge" and os.path.exists("/usr/local/etc/singularity/network/00_bridge.conflist"):
bridge_file = open("/usr/local/etc/singularity/network/00_bridge.conflist")
bridge_config = json.load(bridge_file)
return bridge_config["plugins"][0]["ipam"]["subnet"] or "10.22.0.0/16"
else:
if not os.path.exists("/usr/local/etc/singularity/network"):
return "10.22.0.0/16"

for config_file in os.listdir("/usr/local/etc/singularity/network"):
config = json.load(open("/usr/local/etc/singularity/network/" + config_file))
if config["name"] == config_name:
return config["plugins"][0]["ipam"].get("subnet") or config["plugins"][0]["ipam"].get("addresses")[0]["address"] or "10.22.0.0/16"
return "10.22.0.0/16"

def get_ip_lookup(self, names, bridge="10.22.0.0/16"):
"""
Generate a pre-determined address for each container.
Expand Down Expand Up @@ -514,13 +530,6 @@ def _create(
created = set()
circular_dep = False

# Generate ip addresses for each
lookup = self.get_ip_lookup(names, bridge)

if not no_resolv:
# Generate shared hosts file
hosts_file = self.create_hosts(lookup)

for instance in self.iter_instances(names):
depends_on = instance.params.get("depends_on", [])
for dep in depends_on:
Expand All @@ -532,6 +541,8 @@ def _create(
resolv = self.generate_resolv_conf()
instance.volumes.append("%s:/etc/resolv.conf" % resolv)

lookup = self.get_ip_lookup(names, self.subnet_from_cni_config(instance.network.get("type", "bridge")))
hosts_file = self.create_hosts(lookup)
# Create a hosts file for the instance based, add as volume
instance.volumes.append("%s:/etc/hosts" % hosts_file)

Expand Down