Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

If the IP is on the lo device already don't fail #205

Merged
merged 4 commits into from
Jan 30, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 6 additions & 13 deletions pkg/loadbalancer/address_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,14 @@ import (
"os/exec"
)

func AddIPToLocalInterface(ip string) error {
func AddIPToLocalInterface(ip string) (string, error) {
// TODO: IPv6
err := exec.Command("ifconfig", "lo0", "alias", ip, "netmask", "255.255.255.255").Run()
if err != nil {
return err
}
return nil
output, err := exec.Command("ifconfig", "lo0", "alias", ip, "netmask", "255.255.255.255").CombinedOutput()
return string(output), err
}

func RemoveIPFromLocalInterface(ip string) error {
func RemoveIPFromLocalInterface(ip string) (string, error) {
// delete the IP address
err := exec.Command("ifconfig", "lo0", "-alias", ip).Run()
if err != nil {
return err
}
return nil

output, err := exec.Command("ifconfig", "lo0", "-alias", ip).CombinedOutput()
return string(output), err
}
26 changes: 6 additions & 20 deletions pkg/loadbalancer/address_other.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,15 @@
package loadbalancer

import (
"bytes"
"errors"
"os/exec"
)

func AddIPToLocalInterface(ip string) error {
outBytes, err := exec.Command("ip", "addr", "add", ip, "dev", "lo").CombinedOutput()

var exerr *exec.ExitError
if errors.As(err, &exerr) && bytes.ContainsAny(outBytes, "File exists") {
// unsure why but `Run()` doesn't capture stderr properly
// if 'File exists' is in the output, then the lo device has the IP already
return nil
} else if err != nil {
return err
}
return nil
func AddIPToLocalInterface(ip string) (string, error) {
output, err := exec.Command("ip", "addr", "add", ip, "dev", "lo").CombinedOutput()
return string(output), err
}

func RemoveIPFromLocalInterface(ip string) error {
err := exec.Command("ip", "addr", "del", ip, "dev", "lo").Run()
if err != nil {
return err
}
return nil
func RemoveIPFromLocalInterface(ip string) (string, error) {
output, err := exec.Command("ip", "addr", "del", ip, "dev", "lo").CombinedOutput()
return string(output), err
}
18 changes: 6 additions & 12 deletions pkg/loadbalancer/address_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,12 @@ import (
"os/exec"
)

func AddIPToLocalInterface(ip string) error {
err := exec.Command("netsh", "interface", "ip", "add", "address", "loopback", ip, "255.255.255.255").Run()
if err != nil {
return err
}
return nil
func AddIPToLocalInterface(ip string) (string, error) {
output, err := exec.Command("netsh", "interface", "ip", "add", "address", "loopback", ip, "255.255.255.255").CombinedOutput()
return string(output), err
}

func RemoveIPFromLocalInterface(ip string) error {
err := exec.Command("netsh", "interface", "ip", "delete", "address", "loopback", ip, "255.255.255.255").Run()
if err != nil {
return err
}
return nil
func RemoveIPFromLocalInterface(ip string) (string, error) {
output, err := exec.Command("netsh", "interface", "ip", "delete", "address", "loopback", ip, "255.255.255.255").CombinedOutput()
return string(output), err
}
8 changes: 4 additions & 4 deletions pkg/loadbalancer/tunnel.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ func (t *tunnelManager) setupTunnels(containerName string) error {
}

klog.V(0).Infof("setting IPv4 address %s associated to container %s", ipv4, containerName)
err = AddIPToLocalInterface(ipv4)
output, err := AddIPToLocalInterface(ipv4)
if err != nil {
return err
klog.V(4).Infof("error adding IP to local interface: %v - %s", err, output)
Copy link
Contributor

@aojea aojea Jan 30, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wrap the error here, I think this tunnel is ok to return the error ... I find better if we swallow it here by logging it at normal level, as the tunnel is an addon for certain environments it should not block the loadbalancer creation but it should be easy for the user to find the error in the logs

if s.tunnelManager != nil {
klog.V(2).Infof("updating loadbalancer tunnels on userspace")
err = s.tunnelManager.setupTunnels(loadBalancerName(clusterName, service))
if err != nil {
return nil, err
}
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pushed - take a look at the edits

}

// create tunnel from the ip:svcport to the localhost:portmap
Expand Down Expand Up @@ -83,9 +83,9 @@ func (t *tunnelManager) removeTunnels(containerName string) error {
}

klog.V(0).Infof("Removing IPv4 address %s associated to local interface", tunnelIP)
err := RemoveIPFromLocalInterface(tunnelIP)
output, err := RemoveIPFromLocalInterface(tunnelIP)
if err != nil {
return err
klog.V(4).Infof("error removing IP from local interface: %v - %s", err, output)
}
return nil
}
Expand Down
Loading