Skip to content

Commit 34fb659

Browse files
committed
fix(aboutme): prevent IP from being blank
This addresses #3 by falling back to IP searching, improving IP searching, and returning 0.0.0.0 as a last resort. This also updates the Glide configuration to be more flexible with versioning, and to add a Glide.lock file.
1 parent f63d971 commit 34fb659

4 files changed

Lines changed: 694 additions & 3 deletions

File tree

aboutme/aboutme.go

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ package aboutme
1212

1313
import (
1414
"errors"
15+
"fmt"
1516
"net"
1617
"os"
1718
"strings"
@@ -120,6 +121,16 @@ func (me *Me) init() error {
120121
me.Labels = p.Labels
121122
me.Annotations = me.Annotations
122123

124+
// FIXME: It appears that sometimes the k8s API server does not set the
125+
// PodIP, even though the pod is issued an IP. We need to figure out why,
126+
// and if this is an expected case. In the meantime, we get the IP by
127+
// scanning interfaces.
128+
if me.IP == "" {
129+
// We swallow the error, letting me.IP set the interface address to
130+
// 0.0.0.0.
131+
me.IP, _ = MyIP()
132+
}
133+
123134
return nil
124135
}
125136

@@ -164,7 +175,21 @@ func (me *Me) findPodInNamespaces() (*api.Pod, string, error) {
164175
// Because this queries the interfaces, not the Kube API server, this could,
165176
// in theory, return an IP address different from Me.IP.
166177
func MyIP() (string, error) {
167-
iface, err := net.InterfaceByName("eth0")
178+
179+
maxIface := 5
180+
var err error
181+
for i := 0; i < maxIface; i++ {
182+
var ip string
183+
ip, err = IPByInterface(fmt.Sprintf("eth%d", i))
184+
if err == nil {
185+
return ip, nil
186+
}
187+
}
188+
189+
return "0.0.0.0", err
190+
}
191+
func IPByInterface(name string) (string, error) {
192+
iface, err := net.InterfaceByName(name)
168193
if err != nil {
169194
return "", err
170195
}

aboutme/aboutme_test.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package aboutme
33
import (
44
"net"
55
"os"
6+
"strings"
67
"testing"
78

89
"k8s.io/kubernetes/pkg/client/unversioned"
@@ -43,6 +44,57 @@ func TestShuntEnv(t *testing.T) {
4344
}
4445
}
4546

47+
func TestMyIPLocal(t *testing.T) {
48+
// This version does not require running inside of k8s.
49+
ip, _ := MyIP()
50+
if len(ip) == 0 {
51+
t.Error("Expected a string, got empty")
52+
}
53+
octets := strings.Split(ip, ".")
54+
if len(octets) != 4 {
55+
t.Errorf("Expected 4 octets, got %d", len(octets))
56+
}
57+
}
58+
59+
func TestByInterfaceEth0(t *testing.T) {
60+
if _, err := net.InterfaceByName("eth0"); err != nil {
61+
t.Skip("Host operating system does not have an eth0 device to test.")
62+
}
63+
64+
ip, err := IPByInterface("eth0")
65+
if err != nil {
66+
t.Errorf("Failed to get eth0: %s", err)
67+
}
68+
if len(ip) == 0 {
69+
t.Error("IP address is empty.")
70+
}
71+
72+
if ip == "0.0.0.0" {
73+
t.Error("Got generic IP instead of real one.")
74+
}
75+
76+
}
77+
78+
func TestByInterfaceEn0(t *testing.T) {
79+
// This works on most Macs.
80+
if _, err := net.InterfaceByName("en0"); err != nil {
81+
t.Skip("Host operating system does not have an en0 device to test.")
82+
}
83+
84+
ip, err := IPByInterface("en0")
85+
if err != nil {
86+
t.Errorf("Failed to get en0: %s", err)
87+
}
88+
if len(ip) == 0 {
89+
t.Error("IP address is empty.")
90+
}
91+
92+
if ip == "0.0.0.0" {
93+
t.Error("Got generic IP instead of real one.")
94+
}
95+
96+
}
97+
4698
func TestMyIP(t *testing.T) {
4799
if _, err := net.InterfaceByName("eth0"); err != nil {
48100
t.Skip("Host operating system does not have an eth0 device to test.")

0 commit comments

Comments
 (0)