Skip to content
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

Unable to add link to deployed lab #285

Closed
BMG-DYNAMIT opened this issue Apr 12, 2024 · 5 comments
Closed

Unable to add link to deployed lab #285

BMG-DYNAMIT opened this issue Apr 12, 2024 · 5 comments

Comments

@BMG-DYNAMIT
Copy link

BMG-DYNAMIT commented Apr 12, 2024

Operating System

Ubuntu 22.04

Kathará Version

3.7.4

Bug Description

Hi,

I am trying to add a link between machines to an already deployed lab.
Maybe I am doing something wrong, but it does not for me.

Take a look at my example code.

import logging

from Kathara.manager.Kathara import Kathara
from Kathara.model.Lab import Lab
from Kathara.setting.Setting import Setting

logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)

if __name__ == '__main__':
    Setting.get_instance().load_from_disk()

    logger.info("Creating Lab BGP Announcement...")
    lab = Lab("BGP Announcement")

    logger.info("Creating router1...")
    # Create router1 with image "kathara/frr"
    router1 = lab.new_machine("router1", **{"image": "kathara/frr"})

    # link = lab.get_or_new_link("A")

    # Create and connect router1 interfaces
    # lab.connect_machine_to_link(router1.name, link.name)

    logger.info("Creating router2...")
    # Create router2 with image "kathara/frr"
    router2 = lab.new_machine("router2", **{"image": "kathara/frr"})

    # Create and connect router1 interfaces
    # lab.connect_machine_to_link(router2.name, link.name)

    logger.info("Deploying BGP Announcement lab...")
    Kathara.get_instance().deploy_lab(lab)

    Kathara.get_instance().connect_tty(machine_name=router1.name, lab_hash=lab.hash)

    link = lab.get_or_new_link("A")

    Kathara.get_instance().deploy_link(link)

    # I am unable to add the link, the interface does not show up with ip add
    # If I use connect_machine_to_link and add_interface I get an exception that both machines are already connected

    lab.connect_machine_to_link(router1.name, link.name)
    # router1.add_interface(link)

    lab.connect_machine_to_link(router2.name, link.name)
    # router2.add_interface(link)

    # The interface to the link does not show up!
    Kathara.get_instance().connect_tty(machine_name=router1.name, lab_hash=lab.hash)

    logger.info("Undeploying BGP Announcement lab...")
    Kathara.get_instance().undeploy_lab(lab_name=lab.name)

If I connect into the node with connect_tty, the new interface for the link beween the nodes does not show up with ip addr.
If I try to add the interface with add_interface after connecting the machine to the link, I get an exception.

If I only try to add the interface without connect_machine_to_link is also does not work.

Maybe you can tell me what I am doing wrong.

Thanks,

Moritz

Steps To Reproduce

Deploy a link to an already deployed lab and connect a machine to it.

Expected Behavior

A new interface should show up with ip addr after connecting it to the link.

Check Command Output

┌──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
│                                                     System Check                                                     │
└──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
Current Manager is:             Docker (Kathara)
Manager version is:             25.0.5
Python version is:              3.10.12 (main, Nov 20 2023, 15:14:05) [GCC 11.4.0]
Kathara version is:             3.7.3
Operating System version is:    Linux-5.15.150.1-microsoft-standard-WSL2+-x86_64
[Deploying devices]   ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 1/1
[Deleting devices]   ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 1/1
✓ Container run successfully.
@tcaiazzi
Copy link
Member

Dear @BMG-DYNAMIT,

Thanks for opening the issue 😄

As for the #284, the Lab's methods only interact with the static representation of the network scenario and not with the running one.

To attach a new collision domain to a running device, instead of deploy_link, you should use the connect_machine_to_link method of the manager:

def connect_machine_to_link(self, machine: Machine, link: Link, mac_address: Optional[str] = None) -> None:
"""Connect a Kathara device to a collision domain.
Args:
machine (Kathara.model.Machine): A Kathara machine object.
link (Kathara.model.Link): A Kathara collision domain object.
mac_address (Optional[str]): The MAC address to assign to the interface.
Returns:
None
Raises:
LabNotFoundError: If the device specified is not associated to any network scenario.
LabNotFoundError: If the collision domain is not associated to any network scenario.
MachineCollisionDomainConflictError: If the device is already connected to the collision domain.
"""
self.manager.connect_machine_to_link(machine, link, mac_address)

We will update the documentation to make this clear.

Please let me know if this helps!

Thanks for contributing,
Tommaso

@BMG-DYNAMIT
Copy link
Author

Thank you very much for your Help.

It works now.

Thanks,
Moritz

@BMG-DYNAMIT
Copy link
Author

BMG-DYNAMIT commented Apr 17, 2024

Hi

I noticed that the usage of Kathara.get_instance().connect_machine_to_link() expects that the user called api_object.reload() before adding a machine to a running scenario. If api_object.reload() isn't called I receive a "Device is not running" exception. Is this intended?

I have an example

import logging

from Kathara.manager.Kathara import Kathara
from Kathara.model.Lab import Lab
from Kathara.setting.Setting import Setting

logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)

if __name__ == '__main__':
    Setting.get_instance().load_from_disk()

    logger.info("Creating Lab BGP Announcement...")
    lab = Lab("BGP Announcement")

    logger.info("Creating router1...")
    # Create router1 with image "kathara/frr"
    router1 = lab.new_machine("router1", **{"image": "kathara/frr"})

    logger.info("Creating router2...")
    # Create router2 with image "kathara/frr"
    router2 = lab.new_machine("router2", **{"image": "kathara/frr"})

    logger.info("Deploying BGP Announcement lab...")
    Kathara.get_instance().deploy_lab(lab)

    link = lab.get_or_new_link("A")

   # Without this I receive a "Device is not running" exception
   router1.api_object.reload()
   router2.api_object.reload()

    # I am unable to add the link, the interface does not show up with ip add
    # If I use connect_machine_to_link and add_interface I get an exception that both machines are already connected

    lab.connect_machine_to_link(router1.name, link.name)
    # router1.add_interface(link)

    lab.connect_machine_to_link(router2.name, link.name)
    # router2.add_interface(link)

    # The interface to the link does not show up!
    Kathara.get_instance().connect_tty(machine_name=router1.name, lab_hash=lab.hash)

    logger.info("Undeploying BGP Announcement lab...")
    Kathara.get_instance().undeploy_lab(lab_name=lab.name)

@BMG-DYNAMIT
Copy link
Author

BMG-DYNAMIT commented Apr 17, 2024

I think the DockerManager should call api_object.reload() before checking if the Device is running in connect_machine_to_link?

if not machine.api_object or machine.api_object.status != "running":
            raise MachineNotRunningError(machine.name)
machine.api_object.reload()
if not machine.api_object or machine.api_object.status != "running":
            raise MachineNotRunningError(machine.name)

tcaiazzi added a commit that referenced this issue Apr 18, 2024
- Reload metadata after a device is started
- Reload api_objects before connecting disconnecting links from a running device
@tcaiazzi tcaiazzi added the bug label Apr 18, 2024
@tcaiazzi
Copy link
Member

Dear @BMG-DYNAMIT,

Many thanks for all your help! You are right, there is a problem with how and where we reload the api_objects.

We are working on a solution in: https://github.com/KatharaFramework/Kathara/tree/285-reload-api-objects

However, note that you cannot connect links to a running machine created without any interface. Indeed, if you start a device with no interfaces, Docker won't add the networking stack on it, and so it will not be possible to attach new interfaces.

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Development

No branches or pull requests

2 participants