Skip to content

Commit

Permalink
Move the logic out of ticker case statement
Browse files Browse the repository at this point in the history
Signed-off-by: Nino Kodabande <[email protected]>
  • Loading branch information
Nino-K committed Nov 12, 2024
1 parent da1ac1c commit 153f9f9
Showing 1 changed file with 52 additions and 52 deletions.
104 changes: 52 additions & 52 deletions src/go/guestagent/pkg/iptables/iptables.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,67 +43,67 @@ func ForwardPorts(ctx context.Context, tracker tracker.Tracker, updateInterval t

for {
select {
case <-ctx.Done():
return nil
case <-ticker.C:
// Detect ports for forward
newPorts, err := iptables.GetPorts()
if err != nil {
// iptables exiting with an exit status of 4 means there
// is a resource problem. For example, something else is
// running iptables. In that case, we can skip trying it for
// this loop. You can find the exit code in the iptables
// source at https://git.netfilter.org/iptables/tree/include/xtables.h
if strings.Contains(err.Error(), "exit status 4") {
log.Debug("iptables exited with status 4 (resource error). Retrying...")
continue // Retry in the next iteration
}
return err
}
// Detect ports for forward
newPorts, err := iptables.GetPorts()
if err != nil {
// iptables exiting with an exit status of 4 means there
// is a resource problem. For example, something else is
// running iptables. In that case, we can skip trying it for
// this loop. You can find the exit code in the iptables
// source at https://git.netfilter.org/iptables/tree/include/xtables.h
if strings.Contains(err.Error(), "exit status 4") {
log.Debug("iptables exited with status 4 (resource error). Retrying...")
continue // Retry in the next iteration
}
log.Debugf("iptable found ports %+v", newPorts)
return err
}
log.Debugf("iptable found ports %+v", newPorts)

// Diff from existing forwarded ports
added, removed := comparePorts(ports, newPorts)
ports = newPorts
// Diff from existing forwarded ports
added, removed := comparePorts(ports, newPorts)
ports = newPorts

// Remove old forwards
for _, p := range removed {
name := entryToString(p)
if err := tracker.Remove(generateID(name)); err != nil {
log.Warnf("failed to close listener %q: %w", name, err)
}
// Remove old forwards
for _, p := range removed {
name := entryToString(p)
if err := tracker.Remove(generateID(name)); err != nil {
log.Warnf("failed to close listener %q: %w", name, err)
}
}

portMap := make(nat.PortMap)
portMap := make(nat.PortMap)

// Add new forwards
for _, p := range added {
if p.TCP {
port := strconv.Itoa(p.Port)
portMapKey, err := nat.NewPort("tcp", port)
if err != nil {
log.Errorf("failed to create a corresponding key for the portMap: %s", err)
continue
}
portBinding := nat.PortBinding{
// We can set the hostIP to INADDR_ANY the API Tracker will determine
// the admin installation and can adjust this to localhost accordingly
HostIP: "0.0.0.0",
HostPort: port,
}
if pb, ok := portMap[portMapKey]; ok {
portMap[portMapKey] = append(pb, portBinding)
} else {
portMap[portMapKey] = []nat.PortBinding{portBinding}
}
name := entryToString(p)
if err := tracker.Add(generateID(name), portMap); err != nil {
log.Errorf("failed to listen %q: %w", name, err)
} else {
log.Infof("opened listener for %q", name)
}
// Add new forwards
for _, p := range added {
if p.TCP {
port := strconv.Itoa(p.Port)
portMapKey, err := nat.NewPort("tcp", port)
if err != nil {
log.Errorf("failed to create a corresponding key for the portMap: %s", err)
continue
}
portBinding := nat.PortBinding{
// We can set the hostIP to INADDR_ANY the API Tracker will determine
// the admin installation and can adjust this to localhost accordingly
HostIP: "0.0.0.0",
HostPort: port,
}
if pb, ok := portMap[portMapKey]; ok {
portMap[portMapKey] = append(pb, portBinding)
} else {
portMap[portMapKey] = []nat.PortBinding{portBinding}
}
name := entryToString(p)
if err := tracker.Add(generateID(name), portMap); err != nil {
log.Errorf("failed to listen %q: %w", name, err)
} else {
log.Infof("opened listener for %q", name)
}
}
case <-ctx.Done():
return nil
}
}
}
Expand Down

0 comments on commit 153f9f9

Please sign in to comment.