diff --git a/pkg/cloudprovider/controller/nodecontroller.go b/pkg/cloudprovider/controller/nodecontroller.go index 4c60e1c3ecfc3..de2a619de81ea 100644 --- a/pkg/cloudprovider/controller/nodecontroller.go +++ b/pkg/cloudprovider/controller/nodecontroller.go @@ -18,6 +18,7 @@ package controller import ( "fmt" + "net" "time" "github.com/GoogleCloudPlatform/kubernetes/pkg/api" @@ -73,12 +74,26 @@ func (s *NodeController) SyncStatic(period time.Duration) error { if registered.Has(nodeID) { continue } - _, err := s.kubeClient.Nodes().Create(&api.Node{ + node := &api.Node{ ObjectMeta: api.ObjectMeta{Name: nodeID}, Spec: api.NodeSpec{ Capacity: s.staticResources.Capacity, }, - }) + } + addr := net.ParseIP(nodeID) + if addr != nil { + node.Status.HostIP = nodeID + } else { + addrs, err := net.LookupIP(nodeID) + if err != nil { + glog.Errorf("Can't get ip address of node %v", nodeID) + } else if len(addrs) == 0 { + glog.Errorf("No ip address for node %v", nodeID) + } else { + node.Status.HostIP = addrs[0].String() + } + } + _, err := s.kubeClient.Nodes().Create(node) if err == nil { registered.Insert(nodeID) } diff --git a/pkg/cloudprovider/controller/nodecontroller_test.go b/pkg/cloudprovider/controller/nodecontroller_test.go index d5fe597de5705..5bc3b882032a8 100644 --- a/pkg/cloudprovider/controller/nodecontroller_test.go +++ b/pkg/cloudprovider/controller/nodecontroller_test.go @@ -102,6 +102,25 @@ func TestSyncStaticCreateNode(t *testing.T) { } } +func TestSyncStaticCreateNodeWithHostIP(t *testing.T) { + fakeNodeHandler := &FakeNodeHandler{ + CreateHook: func(fake *FakeNodeHandler, node *api.Node) bool { + return true + }, + } + nodeController := NewNodeController(nil, ".*", []string{"10.0.0.1"}, &api.NodeResources{}, fakeNodeHandler) + if err := nodeController.SyncStatic(time.Millisecond); err != nil { + t.Errorf("unexpected error: %v", err) + } + + if fakeNodeHandler.CreatedNodes[0].Name != "10.0.0.1" { + t.Errorf("unexpect node %v created", fakeNodeHandler.CreatedNodes[0].Name) + } + if fakeNodeHandler.CreatedNodes[0].Status.HostIP != "10.0.0.1" { + t.Errorf("unexpect nil node HostIP for node %v", fakeNodeHandler.CreatedNodes[0].Name) + } +} + func TestSyncStaticCreateNodeWithError(t *testing.T) { fakeNodeHandler := &FakeNodeHandler{ CreateHook: func(fake *FakeNodeHandler, node *api.Node) bool {