KatharaFramework / Kathara

A lightweight container-based network emulation system.
https://www.kathara.org/
GNU General Public License v3.0
433 stars 64 forks source link

Can't undeploy link on running lab using Kathara.get_instance().undeploy_link() with the python api #284

Closed BMG-DYNAMIT closed 4 months ago

BMG-DYNAMIT commented 5 months ago

Operating System

Ubuntu 22.04

Kathará Version

3.7.4

Bug Description

If I deploy a lab and remove a link with Kathara.get_instance().undeploy_link() the link does not get removed and still exists after that.

Steps To Reproduce

Try the following test script:

import logging

from Kathara.manager.Kathara import Kathara
from Kathara.model.Lab import Lab

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

if __name__ == '__main__':
    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)

    # Link "A" exists
    print("Existing Links before undeploy:")
    for linkName, link in lab.links.items():
        print(linkName)

    Kathara.get_instance().undeploy_link(link)

    # Link "A" still exists, but should be removed!
    print("Existing Links after undeploy:")
    for linkName, link in lab.links.items():
        print(linkName)

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

The Link with the name "A" does not get removed by undeploy_link().

It does not work neither with docker or kubernetes.

Expected Behavior

The Link with the name "A" should be undeployed.

Check Command Output

┌──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
│                                                     System Check                                                     │
└──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
Current Manager is:             Docker (Kathara)
Manager version is:             26.0.0
Python version is:              3.10.12 (main, Nov 20 2023, 15:14:05) [GCC 11.4.0]
Kathara version is:             3.7.4
Operating System version is:    Linux-5.15.0-101-generic-x86_64
[Deploying devices]   ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 1/1
[Deleting devices]   ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 1/1
✓ Container run successfully.
tcaiazzi commented 5 months ago

Hi @BMG-DYNAMIT,

Thanks for opening the issue!

To undeploy a link, first you must undeploy all the devices attached to it.

To disconnect a device from a link, you can use the disconnect_machine_from_link method: https://github.com/KatharaFramework/Kathara/blob/362ba16f96c753a8f48cfea98115be5375eb38d9/src/Kathara/manager/Kathara.py#L108-L123

After all the devices connected to the link are disconnected, you can undeploy the link.

We will update the undeploy_link method's documentation to make that clear.

Thanks, Tommaso

BMG-DYNAMIT commented 5 months ago

Thanks for the help!

It works now,

Moritz