Skip to content

Commit

Permalink
Merge pull request #7764 from rancher-sandbox/fixes-empty-portmapping…
Browse files Browse the repository at this point in the history
…-forward

Fixes empty portmapping forward
  • Loading branch information
mook-as authored Nov 14, 2024
2 parents b162343 + b2abce0 commit dc4283b
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 40 deletions.
1 change: 1 addition & 0 deletions src/go/guestagent/pkg/docker/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ func (e *EventMonitor) initializeRunningContainers(ctx context.Context) error {

if err := e.portTracker.Add(container.ID, portMap); err != nil {
log.Errorf("registering already running containers failed: %v", err)
continue
}

for _, netSettings := range container.NetworkSettings.Networks {
Expand Down
File renamed without changes.
4 changes: 2 additions & 2 deletions src/go/guestagent/pkg/iptables/iptables.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,9 @@ func ForwardPorts(ctx context.Context, tracker tracker.Tracker, updateInterval t
}
name := entryToString(p)
if err := tracker.Add(generateID(name), portMap); err != nil {
log.Errorf("failed to listen %q: %w", name, err)
log.Errorf("failed to listen %s: %s", name, err)
} else {
log.Infof("opened listener for %q", name)
log.Infof("opened listener for %s", name)
}
}
}
Expand Down
37 changes: 18 additions & 19 deletions src/go/guestagent/pkg/kube/watcher_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,36 +154,35 @@ func WatchForServices(
case event := <-eventCh:
if event.deleted {
if err := portTracker.Remove(string(event.UID)); err != nil {
log.Errorw("failed to delete a port from tracker", log.Fields{
"error": err,
"UID": event.UID,
"ports": event.portMapping,
"namespace": event.namespace,
"name": event.name,
})
log.Errorf("failed to delete port mapping: %v from tracker UID: %v namespace: %s name: %s failed: %s",
event.portMapping,
event.UID,
event.namespace,
event.name,
err)
} else {
log.Debugf("kubernetes service: port mapping deleted %s/%s:%v",
event.namespace, event.name, event.portMapping)
}
} else {
portMapping, err := createPortMapping(event.portMapping, k8sServiceListenerIP)
if err != nil {
log.Errorw("failed to create port mapping", log.Fields{
"error": err,
"ports": event.portMapping,
"namespace": event.namespace,
"name": event.name,
})
log.Errorf("failed to create port mapping: %v from tracker UID: %v namespace: %s name: %s failed: %s",
event.portMapping,
event.UID,
event.namespace,
event.name,
err)

continue
}
if err := portTracker.Add(string(event.UID), portMapping); err != nil {
log.Errorw("failed to add port mapping", log.Fields{
"error": err,
"ports": event.portMapping,
"namespace": event.namespace,
"name": event.name,
})
log.Errorf("failed to add port mapping: %v from tracker UID: %v namespace: %s name: %s failed: %s",
event.portMapping,
event.UID,
event.namespace,
event.name,
err)
} else {
log.Debugf("kubernetes service: port mapping added %s/%s:%v",
event.namespace, event.name, event.portMapping)
Expand Down
43 changes: 24 additions & 19 deletions src/go/guestagent/pkg/tracker/apitracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,19 +116,22 @@ func (a *APITracker) Add(containerID string, portMap nat.PortMap) error {
tmpPortBinding = append(tmpPortBinding, portBinding)
}

successfullyForwarded[portProto] = tmpPortBinding
}

a.portStorage.add(containerID, successfullyForwarded)
portMapping := guestagentTypes.PortMapping{
Remove: false,
Ports: successfullyForwarded,
if len(tmpPortBinding) != 0 {
successfullyForwarded[portProto] = tmpPortBinding
}
}
log.Debugf("forwarding to wsl-proxy to add port mapping: %+v", portMapping)

err := a.wslProxyForwarder.Send(portMapping)
if err != nil {
return fmt.Errorf("sending port mappings to wsl proxy error: %w", err)
if len(successfullyForwarded) != 0 {
a.portStorage.add(containerID, successfullyForwarded)
portMapping := guestagentTypes.PortMapping{
Remove: false,
Ports: successfullyForwarded,
}
log.Debugf("forwarding to wsl-proxy to add port mapping: %+v", portMapping)
err := a.wslProxyForwarder.Send(portMapping)
if err != nil {
return fmt.Errorf("sending port mappings to wsl proxy error: %w", err)
}
}

if len(errs) != 0 {
Expand Down Expand Up @@ -176,14 +179,16 @@ func (a *APITracker) Remove(containerID string) error {
}
}

portMapping := guestagentTypes.PortMapping{
Remove: true,
Ports: portMap,
}
log.Debugf("forwarding to wsl-proxy to remove port mapping: %+v", portMapping)
err := a.wslProxyForwarder.Send(portMapping)
if err != nil {
return fmt.Errorf("sending port mappings to wsl proxy error: %w", err)
if len(portMap) != 0 {
portMapping := guestagentTypes.PortMapping{
Remove: true,
Ports: portMap,
}
log.Debugf("forwarding to wsl-proxy to remove port mapping: %+v", portMapping)
err := a.wslProxyForwarder.Send(portMapping)
if err != nil {
return fmt.Errorf("sending port mappings to wsl proxy error: %w", err)
}
}

if len(errs) != 0 {
Expand Down

0 comments on commit dc4283b

Please sign in to comment.