Skip to content

Commit

Permalink
Move hostIP detection from master to server
Browse files Browse the repository at this point in the history
Add PublicAddress in test files

Move valid public addr into util
  • Loading branch information
resouer committed Nov 30, 2015
1 parent fc927e8 commit 477da92
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 18 deletions.
9 changes: 8 additions & 1 deletion cmd/integration/integration.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,13 @@ func startComponents(firstManifestURL, secondManifestURL string) (string, string
glog.Fatalf("No public address for %s", host)
}

// The caller of master.New should guarantee pulicAddress is properly set
hostIP, err := util.ValidPublicAddrForMaster(publicAddress)
if err != nil {
glog.Fatalf("Unable to find suitable network address.error='%v' . "+
"Fail to get a valid public address for master.", err)
}

// Create a master and install handlers into mux.
m := master.New(&master.Config{
StorageDestinations: storageDestinations,
Expand All @@ -182,7 +189,7 @@ func startComponents(firstManifestURL, secondManifestURL string) (string, string
Authorizer: apiserver.NewAlwaysAllowAuthorizer(),
AdmissionControl: admit.NewAlwaysAdmit(),
ReadWritePort: portNumber,
PublicAddress: publicAddress,
PublicAddress: hostIP,
CacheTimeout: 2 * time.Second,
StorageVersions: storageVersions,
})
Expand Down
13 changes: 9 additions & 4 deletions cmd/kube-apiserver/app/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -367,12 +367,17 @@ func (s *APIServer) Run(_ []string) error {
s.verifyClusterIPFlags()

// If advertise-address is not specified, use bind-address. If bind-address
// is not usable (unset, 0.0.0.0, or loopback), setDefaults() in
// pkg/master/master.go will do the right thing and use the host's default
// interface.
// is not usable (unset, 0.0.0.0, or loopback), we will use the host's default
// interface as valid public addr for master (see: util#ValidPublicAddrForMaster)
if s.AdvertiseAddress == nil || s.AdvertiseAddress.IsUnspecified() {
s.AdvertiseAddress = s.BindAddress
hostIP, err := util.ValidPublicAddrForMaster(s.BindAddress)
if err != nil {
glog.Fatalf("Unable to find suitable network address.error='%v' . "+
"Try to set the AdvertiseAddress directly or provide a valid BindAddress to fix this.", err)
}
s.AdvertiseAddress = hostIP
}
glog.Infof("Will report %v as public IP address.", s.AdvertiseAddress)

if (s.EtcdConfigFile != "" && len(s.EtcdServerList) != 0) || (s.EtcdConfigFile == "" && len(s.EtcdServerList) == 0) {
glog.Fatalf("Specify either --etcd-servers or --etcd-config")
Expand Down
13 changes: 0 additions & 13 deletions pkg/master/master.go
Original file line number Diff line number Diff line change
Expand Up @@ -401,19 +401,6 @@ func setDefaults(c *Config) {
if c.CacheTimeout == 0 {
c.CacheTimeout = 5 * time.Second
}
for c.PublicAddress == nil || c.PublicAddress.IsUnspecified() || c.PublicAddress.IsLoopback() {
// TODO: This should be done in the caller and just require a
// valid value to be passed in.
hostIP, err := util.ChooseHostInterface()
if err != nil {
glog.Fatalf("Unable to find suitable network address.error='%v' . "+
"Will try again in 5 seconds. Set the public address directly to avoid this wait.", err)
time.Sleep(5 * time.Second)
continue
}
c.PublicAddress = hostIP
glog.Infof("Will report %v as public IP address.", c.PublicAddress)
}
if c.RequestContextMapper == nil {
c.RequestContextMapper = api.NewRequestContextMapper()
}
Expand Down
1 change: 1 addition & 0 deletions pkg/master/master_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ func setUp(t *testing.T) (Master, *etcdtesting.EtcdTestServer, Config, *assert.A
storageVersions[""] = testapi.Default.Version()
storageVersions["extensions"] = testapi.Extensions.GroupAndVersion()
config.StorageVersions = storageVersions
config.PublicAddress = net.ParseIP("192.168.10.4")
master.nodeRegistry = registrytest.NewNodeRegistry([]string{"node1", "node2"}, api.NodeResources{})

return master, server, config, assert.New(t)
Expand Down
14 changes: 14 additions & 0 deletions pkg/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -512,3 +512,17 @@ func ReadDirNoExit(dirname string) ([]os.FileInfo, []error, error) {

return list, errs, nil
}

// If bind-address is usable, return it directly
// If bind-address is not usable (unset, 0.0.0.0, or loopback), we will use the host's default
// interface.
func ValidPublicAddrForMaster(bindAddress net.IP) (net.IP, error) {
if bindAddress == nil || bindAddress.IsUnspecified() || bindAddress.IsLoopback() {
hostIP, err := ChooseHostInterface()
if err != nil {
return nil, err
}
bindAddress = hostIP
}
return bindAddress, nil
}
10 changes: 10 additions & 0 deletions test/integration/auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"errors"
"fmt"
"io/ioutil"
"net"
"net/http"
"net/http/httptest"
"net/url"
Expand Down Expand Up @@ -420,6 +421,7 @@ func TestAuthModeAlwaysAllow(t *testing.T) {
Authorizer: apiserver.NewAlwaysAllowAuthorizer(),
AdmissionControl: admit.NewAlwaysAdmit(),
StorageVersions: storageVersions,
PublicAddress: net.ParseIP("192.168.10.4"),
})

transport := http.DefaultTransport
Expand Down Expand Up @@ -549,6 +551,7 @@ func TestAuthModeAlwaysDeny(t *testing.T) {
Authorizer: apiserver.NewAlwaysDenyAuthorizer(),
AdmissionControl: admit.NewAlwaysAdmit(),
StorageVersions: storageVersions,
PublicAddress: net.ParseIP("192.168.10.4"),
})

transport := http.DefaultTransport
Expand Down Expand Up @@ -630,6 +633,7 @@ func TestAliceNotForbiddenOrUnauthorized(t *testing.T) {
Authorizer: allowAliceAuthorizer{},
AdmissionControl: admit.NewAlwaysAdmit(),
StorageVersions: storageVersions,
PublicAddress: net.ParseIP("192.168.10.4"),
})

previousResourceVersion := make(map[string]float64)
Expand Down Expand Up @@ -730,6 +734,7 @@ func TestBobIsForbidden(t *testing.T) {
Authorizer: allowAliceAuthorizer{},
AdmissionControl: admit.NewAlwaysAdmit(),
StorageVersions: storageVersions,
PublicAddress: net.ParseIP("192.168.10.4"),
})

transport := http.DefaultTransport
Expand Down Expand Up @@ -804,6 +809,7 @@ func TestUnknownUserIsUnauthorized(t *testing.T) {
Authorizer: allowAliceAuthorizer{},
AdmissionControl: admit.NewAlwaysAdmit(),
StorageVersions: storageVersions,
PublicAddress: net.ParseIP("192.168.10.4"),
})

transport := http.DefaultTransport
Expand Down Expand Up @@ -903,6 +909,7 @@ func TestAuthorizationAttributeDetermination(t *testing.T) {
Authorizer: trackingAuthorizer,
AdmissionControl: admit.NewAlwaysAdmit(),
StorageVersions: storageVersions,
PublicAddress: net.ParseIP("192.168.10.4"),
})

transport := http.DefaultTransport
Expand Down Expand Up @@ -997,6 +1004,7 @@ func TestNamespaceAuthorization(t *testing.T) {
Authorizer: a,
AdmissionControl: admit.NewAlwaysAdmit(),
StorageVersions: storageVersions,
PublicAddress: net.ParseIP("192.168.10.4"),
})

previousResourceVersion := make(map[string]float64)
Expand Down Expand Up @@ -1125,6 +1133,7 @@ func TestKindAuthorization(t *testing.T) {
Authorizer: a,
AdmissionControl: admit.NewAlwaysAdmit(),
StorageVersions: storageVersions,
PublicAddress: net.ParseIP("192.168.10.4"),
})

previousResourceVersion := make(map[string]float64)
Expand Down Expand Up @@ -1240,6 +1249,7 @@ func TestReadOnlyAuthorization(t *testing.T) {
Authorizer: a,
AdmissionControl: admit.NewAlwaysAdmit(),
StorageVersions: storageVersions,
PublicAddress: net.ParseIP("192.168.10.4"),
})

transport := http.DefaultTransport
Expand Down
3 changes: 3 additions & 0 deletions test/integration/scheduler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ package integration

import (
"fmt"
"net"
"net/http"
"net/http/httptest"
"sync"
Expand Down Expand Up @@ -91,6 +92,7 @@ func TestUnschedulableNodes(t *testing.T) {
Authorizer: apiserver.NewAlwaysAllowAuthorizer(),
AdmissionControl: admit.NewAlwaysAdmit(),
StorageVersions: storageVersions,
PublicAddress: net.ParseIP("192.168.10.4"),
})

restClient := client.NewOrDie(&client.Config{Host: s.URL, GroupVersion: testapi.Default.GroupVersion()})
Expand Down Expand Up @@ -341,6 +343,7 @@ func BenchmarkScheduling(b *testing.B) {
Authorizer: apiserver.NewAlwaysAllowAuthorizer(),
AdmissionControl: admit.NewAlwaysAdmit(),
StorageVersions: storageVersions,
PublicAddress: net.ParseIP("192.168.10.4"),
})

c := client.NewOrDie(&client.Config{
Expand Down
2 changes: 2 additions & 0 deletions test/integration/secret_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ package integration
// This file tests use of the secrets API resource.

import (
"net"
"net/http"
"net/http/httptest"
"testing"
Expand Down Expand Up @@ -81,6 +82,7 @@ func TestSecrets(t *testing.T) {
Authorizer: apiserver.NewAlwaysAllowAuthorizer(),
AdmissionControl: admit.NewAlwaysAdmit(),
StorageVersions: storageVersions,
PublicAddress: net.ParseIP("192.168.10.4"),
})

framework.DeleteAllEtcdKeys()
Expand Down

0 comments on commit 477da92

Please sign in to comment.