Skip to content

Commit 59a1223

Browse files
authored
Don't attempt to remove proxy containers when network isolation is off (#994)
This extends the work started in #967 by explicitly setting the network isolation label to false when a new workload is created with network isolation disabled. Workloads created before this change will not have the label, and in order to ensure that it is cleaned up properly - we treat all of those legacy workloads as having network isolation enabled. To minimize the impact of this, we log the proxy container not found errors at debug level.
1 parent 6782365 commit 59a1223

File tree

3 files changed

+40
-35
lines changed

3 files changed

+40
-35
lines changed

pkg/container/docker/client.go

Lines changed: 23 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -466,11 +466,9 @@ func (c *Client) DeployWorkload(
466466
return "", 0, fmt.Errorf("failed to create external networks: %v", err)
467467
}
468468

469+
networkIsolation := false
469470
if isolateNetwork {
470-
// Add a label to the MCP server indicating network isolation.
471-
// This allows other methods to determine whether it needs to care
472-
// about ingress/egress/dns containers.
473-
lb.AddNetworkIsolationLabel(labels)
471+
networkIsolation = true
474472

475473
internalNetworkLabels := map[string]string{}
476474
lb.AddNetworkLabels(internalNetworkLabels, networkName)
@@ -516,6 +514,11 @@ func (c *Client) DeployWorkload(
516514
return "", 0, fmt.Errorf("failed to generate port bindings: %v", err)
517515
}
518516

517+
// Add a label to the MCP server indicating network isolation.
518+
// This allows other methods to determine whether it needs to care
519+
// about ingress/egress/dns containers.
520+
lb.AddNetworkIsolationLabel(labels, networkIsolation)
521+
519522
containerId, err := c.createMcpContainer(
520523
ctx,
521524
name,
@@ -639,15 +642,12 @@ func (c *Client) StopWorkload(ctx context.Context, workloadID string) error {
639642
}
640643

641644
// If network isolation is not enabled, then there is nothing else to do.
642-
// TODO: This check is currently commented out because we need to ensure
643-
// that workloads created by older versions of ToolHive get cleaned up
644-
// properly. Once we are confident that all workloads with network isolation
645-
// have the label set, we can uncomment this check.
646-
/*
647-
if !lb.HasNetworkIsolation(info.Labels) {
648-
return nil
649-
}
650-
*/
645+
// NOTE: This check treats all workloads created before the introduction of
646+
// this label as having network isolation enabled. This is to ensure that they
647+
// get cleaned up properly during stop/rm.
648+
if !lb.HasNetworkIsolation(info.Labels) {
649+
return nil
650+
}
651651

652652
// remove / from container name
653653
containerName := strings.TrimPrefix(info.Name, "/")
@@ -668,11 +668,11 @@ func (c *Client) StopWorkload(ctx context.Context, workloadID string) error {
668668
func (c *Client) stopProxyContainer(ctx context.Context, containerName string, timeoutSeconds int) {
669669
containerId, err := c.findExistingContainer(ctx, containerName)
670670
if err != nil {
671-
logger.Warnf("Failed to find internal container %s: %v", containerName, err)
671+
logger.Debugf("Failed to find internal container %s: %v", containerName, err)
672672
} else {
673673
err = c.client.ContainerStop(ctx, containerId, container.StopOptions{Timeout: &timeoutSeconds})
674674
if err != nil {
675-
logger.Warnf("Failed to stop internal container %s: %v", containerName, err)
675+
logger.Debugf("Failed to stop internal container %s: %v", containerName, err)
676676
}
677677
}
678678
}
@@ -729,16 +729,13 @@ func (c *Client) RemoveWorkload(ctx context.Context, workloadID string) error {
729729
}
730730

731731
// If network isolation is not enabled, then there is nothing else to do.
732-
// TODO: This check is currently commented out because we need to ensure
733-
// that workloads created by older versions of ToolHive get cleaned up
734-
// properly. Once we are confident that all workloads with network isolation
735-
// have the label set, we can uncomment this check.
736-
// If network isolation is not enabled, then there is nothing else to do.
737-
/*
738-
if containerResponse.Config != nil && !lb.HasNetworkIsolation(containerResponse.Config.Labels) {
739-
return nil
740-
}
741-
*/
732+
// NOTE: This check treats all workloads created before the introduction of
733+
// this label as having network isolation enabled. This is to ensure that they
734+
// get cleaned up properly during stop/rm. There may be some spurious warnings
735+
// from the following code, but they can be ignored.
736+
if containerResponse.Config != nil && !lb.HasNetworkIsolation(containerResponse.Config.Labels) {
737+
return nil
738+
}
742739

743740
// remove egress, ingress, and dns containers
744741
suffixes := []string{"egress", "ingress", "dns"}
@@ -747,7 +744,7 @@ func (c *Client) RemoveWorkload(ctx context.Context, workloadID string) error {
747744
containerName := fmt.Sprintf("%s-%s", containerName, suffix)
748745
containerId, err := c.findExistingContainer(ctx, containerName)
749746
if err != nil {
750-
logger.Warnf("Failed to find %s container %s: %v", suffix, containerName, err)
747+
logger.Debugf("Failed to find %s container %s: %v", suffix, containerName, err)
751748
continue
752749
}
753750
if containerId == "" {

pkg/labels/labels.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ package labels
44

55
import (
66
"fmt"
7+
"strconv"
78
"strings"
89
)
910

@@ -34,9 +35,6 @@ const (
3435

3536
// LabelToolHiveValue is the value for the LabelToolHive label
3637
LabelToolHiveValue = "true"
37-
38-
// LabelNetworkIsolationValue is the value for the LabelNetworkIsolation label
39-
LabelNetworkIsolationValue = "true"
4038
)
4139

4240
// AddStandardLabels adds standard labels to a container
@@ -59,8 +57,8 @@ func AddNetworkLabels(labels map[string]string, networkName string) {
5957
}
6058

6159
// AddNetworkIsolationLabel adds the network isolation label to a container
62-
func AddNetworkIsolationLabel(labels map[string]string) {
63-
labels[LabelNetworkIsolation] = LabelNetworkIsolationValue
60+
func AddNetworkIsolationLabel(labels map[string]string, networkIsolation bool) {
61+
labels[LabelNetworkIsolation] = strconv.FormatBool(networkIsolation)
6462
}
6563

6664
// FormatToolHiveFilter formats a filter for ToolHive containers
@@ -77,7 +75,10 @@ func IsToolHiveContainer(labels map[string]string) bool {
7775
// HasNetworkIsolation checks if a container has network isolation enabled.
7876
func HasNetworkIsolation(labels map[string]string) bool {
7977
value, ok := labels[LabelNetworkIsolation]
80-
return ok && strings.ToLower(value) == LabelNetworkIsolationValue
78+
// If the label is not present, assume that network isolation is enabled.
79+
// This is to ensure that workloads created before this label was introduced
80+
// will be properly cleaned up during stop/rm.
81+
return !ok || strings.ToLower(value) == "true"
8182
}
8283

8384
// GetContainerName gets the container name from labels

pkg/labels/labels_test.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -314,15 +314,22 @@ func TestHasNetworkIsolation(t *testing.T) {
314314
{
315315
name: "Network isolation enabled",
316316
labels: map[string]string{
317-
LabelNetworkIsolation: LabelNetworkIsolationValue,
317+
LabelNetworkIsolation: "true",
318318
},
319319
expected: true,
320320
},
321321
{
322-
name: "Network isolation not enabled",
323-
labels: map[string]string{},
322+
name: "Network isolation disabled",
323+
labels: map[string]string{
324+
LabelNetworkIsolation: "false",
325+
},
324326
expected: false,
325327
},
328+
{
329+
name: "Legacy workload without label",
330+
labels: map[string]string{},
331+
expected: true,
332+
},
326333
}
327334

328335
for _, tc := range tests {

0 commit comments

Comments
 (0)