From 186576549e8a59fc2290fc37fc1df59a618e4fab Mon Sep 17 00:00:00 2001 From: scicco Date: Thu, 25 Aug 2022 14:40:45 +0200 Subject: [PATCH 1/7] network.type support added Signed-off-by: scicco --- scompose/project/instance.py | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/scompose/project/instance.py b/scompose/project/instance.py index f79d3bb..7581efd 100644 --- a/scompose/project/instance.py +++ b/scompose/project/instance.py @@ -221,7 +221,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. @@ -236,7 +236,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: @@ -604,7 +608,10 @@ 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 as default + network_type = self.network["type"] or "bridge" + options += self._get_network_commands(ip_address, network_type) # Start options options += self.start_opts From 640fc7d8f3515d1312e347bdd82a717908d494aa Mon Sep 17 00:00:00 2001 From: scicco Date: Thu, 25 Aug 2022 15:15:20 +0200 Subject: [PATCH 2/7] fix missing colon Signed-off-by: scicco --- scompose/project/instance.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scompose/project/instance.py b/scompose/project/instance.py index 7581efd..c2b227a 100644 --- a/scompose/project/instance.py +++ b/scompose/project/instance.py @@ -236,7 +236,7 @@ def _get_network_commands(self, ip_address=None, network_type=None): for arg in network_args: ports += ["--network-args", arg] - if network_type is not None + if network_type is not None: # network_type is "bridge" by default when network.enable is True ports += ["--network", network_type] From 25bd22491967217949cd3caae74c7f7b212055ca Mon Sep 17 00:00:00 2001 From: scicco Date: Thu, 25 Aug 2022 15:27:00 +0200 Subject: [PATCH 3/7] fix missing schema declaration for network.type (KeyError) Signed-off-by: scicco --- scompose/config/schema.py | 1 + scompose/project/instance.py | 12 +++++++++--- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/scompose/config/schema.py b/scompose/config/schema.py index 560641a..4f11462 100644 --- a/scompose/config/schema.py +++ b/scompose/config/schema.py @@ -56,6 +56,7 @@ def validate_config(filepath): "properties": { "allocate_ip": {"type": "boolean"}, "enable": {"type": "boolean"}, + "type": {"type": "string"}, # --network-args "args": string_list, }, diff --git a/scompose/project/instance.py b/scompose/project/instance.py index c2b227a..6d5c17c 100644 --- a/scompose/project/instance.py +++ b/scompose/project/instance.py @@ -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): """ @@ -240,7 +245,7 @@ def _get_network_commands(self, ip_address=None, network_type=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): + if network_type is None:# and (not self.sudo and not fakeroot): ports += ["--network", "none"] for pair in self.ports: @@ -609,8 +614,9 @@ def create(self, ip_address=None, sudo=False, writable_tmpfs=False): # Network configuration + Ports if self.network["enable"]: # if network.enable is true a --network must be always added - # using bridge as default - network_type = self.network["type"] or "bridge" + # 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" options += self._get_network_commands(ip_address, network_type) # Start options From 0a7eb15a16181214c7916969148208027959a4c6 Mon Sep 17 00:00:00 2001 From: scicco Date: Mon, 29 Aug 2022 15:14:20 +0200 Subject: [PATCH 4/7] network.type improvements Signed-off-by: scicco --- scompose/project/project.py | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/scompose/project/project.py b/scompose/project/project.py index 2070480..b6c9fa7 100644 --- a/scompose/project/project.py +++ b/scompose/project/project.py @@ -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 @@ -241,6 +242,17 @@ def _sort_instances(self, instances): # Networking + def subnet_from_cni_config(self, config_name="bridge"): + if config_name == "bridge": + 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 self.get_bridge_address() + else: + 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 self.get_bridge_address() + def get_ip_lookup(self, names, bridge="10.22.0.0/16"): """ Generate a pre-determined address for each container. @@ -514,13 +526,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: @@ -532,6 +537,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) From 2208c0b24bd0bf3519a5f3cb571a72812f5ada90 Mon Sep 17 00:00:00 2001 From: scicco Date: Mon, 29 Aug 2022 17:35:35 +0200 Subject: [PATCH 5/7] checking config file path existence Signed-off-by: scicco --- scompose/project/project.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/scompose/project/project.py b/scompose/project/project.py index b6c9fa7..464ea03 100644 --- a/scompose/project/project.py +++ b/scompose/project/project.py @@ -243,11 +243,14 @@ def _sort_instances(self, instances): # Networking def subnet_from_cni_config(self, config_name="bridge"): - if 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 self.get_bridge_address() else: + if not os.path.exists("/usr/local/etc/singularity/network"): + return self.get_bridge_address() + 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: From d9853d6556cbe9e4db66853fe12c96fd4026c37c Mon Sep 17 00:00:00 2001 From: scicco Date: Tue, 30 Aug 2022 08:53:09 +0200 Subject: [PATCH 6/7] using default subnet as fallback Signed-off-by: scicco --- scompose/project/project.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/scompose/project/project.py b/scompose/project/project.py index 464ea03..74e14b0 100644 --- a/scompose/project/project.py +++ b/scompose/project/project.py @@ -246,16 +246,17 @@ 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 self.get_bridge_address() + 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 self.get_bridge_address() + 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 self.get_bridge_address() - + return config["plugins"][0]["ipam"].get("subnet") or config["plugins"][0]["ipam"].get("addresses")[0]["address"] or "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. From 4ad4c0130ae5f93d9a62551a133c7fcf6969584a Mon Sep 17 00:00:00 2001 From: scicco Date: Wed, 31 Aug 2022 14:12:10 +0200 Subject: [PATCH 7/7] fixing network type selection --- scompose/project/instance.py | 3 ++- scompose/project/project.py | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/scompose/project/instance.py b/scompose/project/instance.py index 6d5c17c..70acd53 100644 --- a/scompose/project/instance.py +++ b/scompose/project/instance.py @@ -616,7 +616,8 @@ def create(self, ip_address=None, sudo=False, writable_tmpfs=False): # 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" + 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 diff --git a/scompose/project/project.py b/scompose/project/project.py index 74e14b0..6af1601 100644 --- a/scompose/project/project.py +++ b/scompose/project/project.py @@ -255,7 +255,7 @@ def subnet_from_cni_config(self, config_name="bridge"): 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"): """