Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Sep 26, 2025

This PR addresses the issue where users cannot destroy labs by name when the topology file has been lost, particularly in CI environments where topology files may be accidentally removed.

Problem

Previously, when using containerlab destroy --name <lab-name>, the command would fail if the topology file referenced in the container labels didn't exist on disk, even when using --keep-mgmt-net. This created issues in scenarios like:

  • CI environments where topology files are not persisted
  • Development workflows where topology files are accidentally deleted
  • Docker containers running containerlab where the topology file is in a lost volume

Solution

When both --name and --keep-mgmt-net flags are used together, containerlab now gracefully handles missing topology files by:

  1. Fallback behavior: If the topology file doesn't exist, containerlab proceeds with "limited cleanup" instead of failing
  2. Direct container deletion: Uses container runtime APIs to delete containers directly rather than requiring parsed topology nodes
  3. Essential cleanup: Still performs SSH config cleanup and host entries cleanup
  4. Clear messaging: Shows warning that topology file is missing and limited cleanup is being performed

Behavior Changes

Before (fails):

$ containerlab destroy --name mylab --keep-mgmt-net
ERROR: Topology file '/path/to/missing.yml' not found

After (succeeds with warning):

$ containerlab destroy --name mylab --keep-mgmt-net  
WARN: Topology file '/path/to/missing.yml' not found, proceeding with limited cleanup (--keep-mgmt-net is set)
INFO: Destroying lab name=mylab
INFO: Deleting 3 containers directly (topology not available)
INFO: Removing SSH config
...

Backward Compatibility

  • Full backward compatibility: Existing behavior unchanged when --keep-mgmt-net is not used
  • No breaking changes: All existing destroy workflows continue to work exactly as before
  • Opt-in behavior: New functionality only activates when both --name and --keep-mgmt-net are specified

Implementation Details

  • Modified makeCopyForDestroy() to handle missing topology files gracefully when --keep-mgmt-net is set
  • Added deleteContainersDirect() method for container deletion without topology node abstraction
  • Enhanced destroy() method to fallback to container listing by lab name when topology parsing fails
  • Maintained all cleanup operations that don't require parsed topology (SSH, hosts file)

Testing

Added comprehensive test case that verifies:

  • Normal destroy behavior unchanged when topology file exists
  • New behavior works correctly when topology file is missing and --keep-mgmt-net is used
  • Error behavior maintained when topology file is missing and --keep-mgmt-net is not used

Fixes issue where destroy operations would fail unnecessarily in CI environments and development workflows.

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • myserver.foo
    • Triggering command: /tmp/go-build3271045625/b1249/utils.test -test.paniconexit0 -test.gocoverdir=/tmp/go-build3271045625/b1249/gocoverdir -test.timeout=10m0s -test.v=true -test.gocoverdir=/tmp/clab-tests/coverage (dns block)
    • Triggering command: /tmp/go-build2041016230/b1249/utils.test -test.paniconexit0 -test.gocoverdir=/tmp/go-build2041016230/b1249/gocoverdir -test.timeout=10m0s -test.v=true -test.gocoverdir=/tmp/clab-tests/coverage (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

Original prompt

This section details on the original issue you should resolve

<issue_title>Destroy with only name specified</issue_title>
<issue_description>The setting is the same as in #2523.

I am using Docker image of containerlab and during development, when something goes wrong I sometimes find myself with lost topology file and a dozen of started containers of container lab. In this case I want to destroy all containers from a lab by the name and clenup ssh if set.

In CI I never need to destroy managment network, so destroying by name should make my CI environment simpler.

WDYT about cutting destroy dependency on topology file if --keep-mgmt-net is set and we have name from #2523.</issue_description>

Comments on the Issue (you are @copilot in this section)

@hellt #2665 adds `--name` support for the destroy command. Does it solve this issue @feldsherov ? @hellt ah, ok. Yeah, I don't think I personally have a use case for this myself. The topo file is backed up now in `~/tmp/.clab/bak` directory if someone needs to cleanup a lab once accidentally removed the topo file in its original location.

But I will reopen this if someone comes up with a PR</comment_new>

Fixes #2524

💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.

Copilot AI changed the title [WIP] Destroy with only name specified Allow destroy by name only when --keep-mgmt-net is set and topology file is missing Sep 26, 2025
Copilot AI requested a review from hellt September 26, 2025 08:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Destroy with only name specified

2 participants