Skip to content

Commit

Permalink
Pipe through the ability to set the external hostname for swagger URLs.
Browse files Browse the repository at this point in the history
  • Loading branch information
brendandburns committed Mar 26, 2015
1 parent 8183a48 commit 7c684e4
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 11 deletions.
28 changes: 28 additions & 0 deletions cmd/kube-apiserver/app/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"crypto/tls"
"net"
"net/http"
"os"
"strconv"
"strings"
"time"
Expand All @@ -45,6 +46,7 @@ import (
// APIServer runs a kubernetes api server.
type APIServer struct {
WideOpenPort int
ExternalHost string
Address util.IP
PublicAddressOverride util.IP
ReadOnlyPort int
Expand Down Expand Up @@ -151,6 +153,7 @@ func (s *APIServer) AddFlags(fs *pflag.FlagSet) {
client.BindKubeletClientConfigFlags(fs, &s.KubeletConfig)
fs.StringVar(&s.ClusterName, "cluster_name", s.ClusterName, "The instance prefix for the cluster")
fs.BoolVar(&s.EnableProfiling, "profiling", false, "Enable profiling via web interface host:port/debug/pprof/")
fs.StringVar(&s.ExternalHost, "external_hostname", "", "The hostname to use when generating externalized URLs for this master (e.g. Swagger API Docs.)")
}

// TODO: Longer term we should read this from some config store, rather than a flag.
Expand Down Expand Up @@ -227,6 +230,30 @@ func (s *APIServer) Run(_ []string) error {
admissionControlPluginNames := strings.Split(s.AdmissionControl, ",")
admissionController := admission.NewFromPlugins(client, admissionControlPluginNames, s.AdmissionControlConfigFile)

if len(s.ExternalHost) == 0 {
// TODO: extend for other providers
if s.CloudProvider == "gce" {
instances, supported := cloud.Instances()
if !supported {
glog.Fatalf("gce cloud provider has no instances. this shouldn't happen. exiting.")
}
name, err := os.Hostname()
if err != nil {
glog.Fatalf("failed to get hostname: %v", err)
}
addrs, err := instances.NodeAddresses(name)
if err != nil {
glog.Warningf("unable to obtain external host address from cloud provider: %v", err)
} else {
for _, addr := range addrs {
if addr.Type == api.NodeExternalIP {
s.ExternalHost = addr.Address
}
}
}
}
}

config := &master.Config{
Cloud: cloud,
EtcdHelper: helper,
Expand All @@ -249,6 +276,7 @@ func (s *APIServer) Run(_ []string) error {
EnableV1Beta3: v1beta3,
MasterServiceNamespace: s.MasterServiceNamespace,
ClusterName: s.ClusterName,
ExternalHost: s.ExternalHost,
}
m := master.New(config)

Expand Down
34 changes: 25 additions & 9 deletions pkg/master/master.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,9 @@ type Config struct {
// Defaults to 6443 if not set.
ReadWritePort int

// ExternalHost is the host name to use for external (public internet) facing URLs (e.g. Swagger)
ExternalHost string

// If nil, the first result from net.InterfaceAddrs will be used.
PublicAddress net.IP

Expand Down Expand Up @@ -141,7 +144,10 @@ type Master struct {
v1beta3 bool
requestContextMapper api.RequestContextMapper

publicIP net.IP
// External host is the name that should be used in external (public internet) URLs for this master
externalHost string
// clusterIP is the IP address of the master within the cluster.
clusterIP net.IP
publicReadOnlyPort int
publicReadWritePort int
serviceReadOnlyIP net.IP
Expand Down Expand Up @@ -277,7 +283,8 @@ func New(c *Config) *Master {
cacheTimeout: c.CacheTimeout,

masterCount: c.MasterCount,
publicIP: c.PublicAddress,
externalHost: c.ExternalHost,
clusterIP: c.PublicAddress,
publicReadOnlyPort: c.ReadOnlyPort,
publicReadWritePort: c.ReadWritePort,
serviceReadOnlyIP: serviceReadOnlyIP,
Expand Down Expand Up @@ -494,14 +501,23 @@ func (m *Master) init(c *Config) {
// register their own web services into the Kubernetes mux prior to initialization
// of swagger, so that other resource types show up in the documentation.
func (m *Master) InstallSwaggerAPI() {
webServicesUrl := ""
// Use the secure read write port, if available.
if m.publicReadWritePort != 0 {
webServicesUrl = "https://" + net.JoinHostPort(m.publicIP.String(), strconv.Itoa(m.publicReadWritePort))
} else {
// Use the read only port.
webServicesUrl = "http://" + net.JoinHostPort(m.publicIP.String(), strconv.Itoa(m.publicReadOnlyPort))
hostAndPort := m.externalHost
protocol := "https://"

// TODO: this is kind of messed up, we should just pipe in the full URL from the outside, rather
// than guessing at it.
if len(m.externalHost) == 0 && m.clusterIP != nil {
host := m.clusterIP.String()
if m.publicReadWritePort != 0 {
hostAndPort = net.JoinHostPort(host, strconv.Itoa(m.publicReadWritePort))
} else {
// Use the read only port.
hostAndPort = net.JoinHostPort(host, strconv.Itoa(m.publicReadOnlyPort))
protocol = "http://"
}
}
webServicesUrl := protocol + hostAndPort

// Enable swagger UI and discovery API
swaggerConfig := swagger.Config{
WebServicesUrl: webServicesUrl,
Expand Down
4 changes: 2 additions & 2 deletions pkg/master/publish.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func (m *Master) serviceWriterLoop(stop chan struct{}) {
if err := m.createMasterServiceIfNeeded("kubernetes", m.serviceReadWriteIP, m.serviceReadWritePort); err != nil {
glog.Errorf("Can't create rw service: %v", err)
}
if err := m.ensureEndpointsContain("kubernetes", m.publicIP, m.publicReadWritePort); err != nil {
if err := m.ensureEndpointsContain("kubernetes", m.clusterIP, m.publicReadWritePort); err != nil {
glog.Errorf("Can't create rw endpoints: %v", err)
}
}
Expand All @@ -65,7 +65,7 @@ func (m *Master) roServiceWriterLoop(stop chan struct{}) {
if err := m.createMasterServiceIfNeeded("kubernetes-ro", m.serviceReadOnlyIP, m.serviceReadOnlyPort); err != nil {
glog.Errorf("Can't create ro service: %v", err)
}
if err := m.ensureEndpointsContain("kubernetes-ro", m.publicIP, m.publicReadOnlyPort); err != nil {
if err := m.ensureEndpointsContain("kubernetes-ro", m.clusterIP, m.publicReadOnlyPort); err != nil {
glog.Errorf("Can't create ro endpoints: %v", err)
}
}
Expand Down

0 comments on commit 7c684e4

Please sign in to comment.