Skip to content

Commit

Permalink
Merge pull request #7781 from rancher-sandbox/remove-listener-for-con…
Browse files Browse the repository at this point in the history
…tainerd-events

Remove Listener Creation for Containerd Events
  • Loading branch information
jandubois authored Nov 15, 2024
2 parents ba0061c + 0753d86 commit aade807
Show file tree
Hide file tree
Showing 3 changed files with 1 addition and 57 deletions.
37 changes: 0 additions & 37 deletions src/go/guestagent/pkg/containerd/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"encoding/json"
"errors"
"fmt"
"net"
"os/exec"
"reflect"
"regexp"
Expand Down Expand Up @@ -124,8 +123,6 @@ func (e *EventMonitor) MonitorPorts(ctx context.Context) {
continue
}

e.updateListener(ctx, ports, e.portTracker.AddListener)

case "/containers/update":
cuEvent := &events.ContainerUpdate{}
err := proto.Unmarshal(envelope.Event.GetValue(), cuEvent)
Expand All @@ -150,15 +147,12 @@ func (e *EventMonitor) MonitorPorts(ctx context.Context) {
log.Errorf("failed to remove port mapping from container update event: %v", err)
}

e.updateListener(ctx, ports, e.portTracker.RemoveListener)
err = e.portTracker.Add(cuEvent.ID, ports)
if err != nil {
log.Errorf("failed to add port mapping from container update event: %v", err)

continue
}

e.updateListener(ctx, ports, e.portTracker.AddListener)
}

continue
Expand All @@ -182,8 +176,6 @@ func (e *EventMonitor) MonitorPorts(ctx context.Context) {
log.Errorf("removing port mapping from tracker failed: %v", err)
}
}

e.updateListener(ctx, portMapToDelete, e.portTracker.RemoveListener)
}

case err := <-errCh:
Expand Down Expand Up @@ -264,7 +256,6 @@ func (e *EventMonitor) initializeRunningContainers(ctx context.Context) {
continue
}

e.updateListener(ctx, ports, e.portTracker.AddListener)
log.Debugf("initialized container %s task status: %+v with ports: %+v", c.ID(), status, ports)
}
}
Expand All @@ -286,34 +277,6 @@ func (e *EventMonitor) Close() error {
return finalErr
}

func (e *EventMonitor) updateListener(
ctx context.Context,
portMappings nat.PortMap,
action func(context.Context, net.IP, int) error,
) {
for _, portBindings := range portMappings {
for _, portBinding := range portBindings {
port, err := strconv.Atoi(portBinding.HostPort)
if err != nil {
log.Errorf("port conversion for [%+v] error: %v", portBinding, err)

continue
}

// We always need to use INADDR_ANY here since any other addresses used here
// can cause a wrong entry in iptables and will not be routable.
if err := action(ctx, net.IPv4zero, port); err != nil {
log.Errorf("updating listener for IP: [%s] and Port: [%s] failed: %v",
net.IPv4zero,
portBinding.HostPort,
err)

continue
}
}
}
}

// execIptablesRules creates an additional DNAT rule to allow service exposure on
// other network addresses if port binding is bound to 127.0.0.1.
func execIptablesRules(portMappings nat.PortMap, containerID, namespace, pid string) error {
Expand Down
2 changes: 0 additions & 2 deletions src/go/guestagent/pkg/tracker/apitracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ type APITracker struct {
tapInterfaceIP string
portStorage *portStorage
apiForwarder *forwarder.APIForwarder
*ListenerTracker
}

// NewAPITracker creates a new instance of APITracker with the specified configuration.
Expand All @@ -75,7 +74,6 @@ func NewAPITracker(ctx context.Context, wslProxyForwarder forwarder.Forwarder, b
tapInterfaceIP: tapIfaceIP,
portStorage: newPortStorage(),
apiForwarder: forwarder.NewAPIForwarder(baseURL),
ListenerTracker: NewListenerTracker(),
}
}

Expand Down
19 changes: 1 addition & 18 deletions src/go/guestagent/pkg/tracker/tracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,7 @@ limitations under the License.
// of the ports during various container event types e.g start, stop
package tracker

import (
"context"
"net"

"github.com/docker/go-connections/nat"
)

// NetTracker is the interface that wraps the methods
// that are used to manage Add/Remove tcp listeners.
type NetTracker interface {
// AddListener creates a TCP listener for a given IP and Port.
AddListener(ctx context.Context, ip net.IP, port int) error

// RemoveListener removes a TCP listener for a given IP and Port.
RemoveListener(ctx context.Context, ip net.IP, port int) error
}
import "github.com/docker/go-connections/nat"

// Tracker is the interface that includes all the functions that
// are used to keep track of the port mappings plus NetTracker methods
Expand All @@ -49,6 +34,4 @@ type Tracker interface {

// RemoveAll removes all the available portMappings in the storage.
RemoveAll() error

NetTracker
}

0 comments on commit aade807

Please sign in to comment.