Skip to content

Commit

Permalink
feat: Set IP masquerade in the UPF configuration (#67)
Browse files Browse the repository at this point in the history
  • Loading branch information
ghislainbourgeois authored Dec 7, 2023
1 parent 77b2234 commit d487cd8
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 6 deletions.
5 changes: 5 additions & 0 deletions src/charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -411,11 +411,13 @@ def _configure_bessd_workload(self) -> None:
Writes configuration file, creates routes, creates iptable rule and pebble layer.
"""
restart = False
core_ip_address = self._get_core_network_ip_config()
content = render_bessd_config_file(
upf_hostname=self._upf_hostname,
upf_mode=UPF_MODE,
access_interface_name=ACCESS_INTERFACE_NAME,
core_interface_name=CORE_INTERFACE_NAME,
core_ip_address=core_ip_address.split("/")[0] if core_ip_address else "",
dnn=self._get_dnn_config(), # type: ignore[arg-type]
pod_share_path=POD_SHARE_PATH,
)
Expand Down Expand Up @@ -909,6 +911,7 @@ def render_bessd_config_file(
upf_mode: str,
access_interface_name: str,
core_interface_name: str,
core_ip_address: Optional[str],
dnn: str,
pod_share_path: str,
) -> str:
Expand All @@ -919,6 +922,7 @@ def render_bessd_config_file(
upf_mode: UPF mode
access_interface_name: Access network interface name
core_interface_name: Core network interface name
core_ip_address: Core network IP address
dnn: Data Network Name (DNN)
pod_share_path: pod_share path
"""
Expand All @@ -929,6 +933,7 @@ def render_bessd_config_file(
mode=upf_mode,
access_interface_name=access_interface_name,
core_interface_name=core_interface_name,
core_ip_address=core_ip_address,
dnn=dnn,
pod_share_path=pod_share_path,
)
Expand Down
3 changes: 2 additions & 1 deletion src/templates/upf.json.j2
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
"ifname": "{{ access_interface_name }}"
},
"core": {
"ifname": "{{ core_interface_name }}"
"ifname": "{{ core_interface_name }}",
"ip_masquerade": "{{ core_ip_address }}"
},
"cpiface": {
"dnn": "{{ dnn }}",
Expand Down
1 change: 1 addition & 0 deletions test-requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ flake8-docstrings
flake8-builtins
isort
juju
macaroonbakery==1.3.2
mypy
pep8-naming
pyproject-flake8
Expand Down
5 changes: 3 additions & 2 deletions tests/unit/expected_upf.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
"ifname": "access"
},
"core": {
"ifname": "core"
"ifname": "core",
"ip_masquerade": "192.168.250.3"
},
"cpiface": {
"dnn": "internet",
Expand Down Expand Up @@ -43,4 +44,4 @@
"sessionQERLookup": 100000
},
"workers": 1
}
}
7 changes: 4 additions & 3 deletions tests/unit/test_charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ def setUp(self, patch_k8s_client):
self.harness.set_model_name(name=self.namespace)
self.harness.set_leader(is_leader=True)

self.maxDiff = None
self.root = self.harness.get_filesystem_root("bessd")
(self.root / "etc/bess/conf").mkdir(parents=True)
self.addCleanup(self.harness.cleanup)
Expand All @@ -88,7 +89,7 @@ def test_given_bessd_config_file_not_yet_written_when_bessd_pebble_ready_then_co
self.harness.handle_exec("bessd", [], result=0)
self.harness.container_pebble_ready(container_name="bessd")

expected_config_file_content = read_file("tests/unit/expected_upf.json")
expected_config_file_content = read_file("tests/unit/expected_upf.json").strip()

self.assertEqual(
(self.root / "etc/bess/conf/upf.json").read_text(), expected_config_file_content
Expand All @@ -105,7 +106,7 @@ def test_given_bessd_config_file_not_yet_written_when_config_storage_attached_th
self.harness.add_storage(storage_name="config", count=1)
self.harness.attach_storage(storage_id="config/0")

expected_config_file_content = read_file("tests/unit/expected_upf.json")
expected_config_file_content = read_file("tests/unit/expected_upf.json").strip()

self.assertEqual(
(self.root / "etc/bess/conf/upf.json").read_text(), expected_config_file_content
Expand All @@ -118,7 +119,7 @@ def test_given_bessd_config_file_matches_when_bessd_pebble_ready_then_config_fil
):
self.harness.handle_exec("bessd", [], result=0)
patch_is_ready.return_value = True
expected_upf_content = read_file("tests/unit/expected_upf.json")
expected_upf_content = read_file("tests/unit/expected_upf.json").strip()
(self.root / "etc/bess/conf/upf.json").write_text(expected_upf_content)

self.harness.container_pebble_ready(container_name="bessd")
Expand Down

0 comments on commit d487cd8

Please sign in to comment.