Browse code

Allow listen address to be overriden on api start

Also disables etcd. Needed to allow one config to vary along
network identity lines.

Clayton Coleman authored on 2015/08/22 03:54:26
Showing 3 changed files
... ...
@@ -3,6 +3,7 @@ package start
3 3
 import (
4 4
 	"fmt"
5 5
 	"io"
6
+	"net"
6 7
 	"os"
7 8
 
8 9
 	"github.com/golang/glog"
... ...
@@ -66,6 +67,7 @@ func NewCommandStartMasterAPI(name, basename string, out io.Writer) (*cobra.Comm
66 66
 		},
67 67
 	}
68 68
 
69
+	// allow the master IP address to be overriden on a per process basis
69 70
 	masterAddr := flagtypes.Addr{
70 71
 		Value:         "127.0.0.1:8443",
71 72
 		DefaultScheme: "https",
... ...
@@ -73,11 +75,31 @@ func NewCommandStartMasterAPI(name, basename string, out io.Writer) (*cobra.Comm
73 73
 		AllowPrefix:   true,
74 74
 	}.Default()
75 75
 
76
+	// allow the listen address to be overriden on a per process basis
77
+	listenArg := &ListenArg{
78
+		ListenAddr: flagtypes.Addr{
79
+			Value:         "127.0.0.1:8444",
80
+			DefaultScheme: "https",
81
+			DefaultPort:   8444,
82
+			AllowPrefix:   true,
83
+		}.Default(),
84
+	}
85
+
76 86
 	options.MasterArgs = NewDefaultMasterArgs()
77 87
 	options.MasterArgs.StartAPI = true
78 88
 	options.MasterArgs.OverrideConfig = func(config *configapi.MasterConfig) error {
79
-		if config.KubernetesMasterConfig != nil && masterAddr.Provided {
80
-			config.KubernetesMasterConfig.MasterIP = masterAddr.Host
89
+		// we do not currently enable multi host etcd for the cluster
90
+		config.EtcdConfig = nil
91
+		if config.KubernetesMasterConfig != nil {
92
+			if masterAddr.Provided {
93
+				glog.V(2).Infof("Using a master address override %q", masterAddr.Host)
94
+				config.KubernetesMasterConfig.MasterIP = masterAddr.Host
95
+			}
96
+			if listenArg.ListenAddr.Provided {
97
+				addr := listenArg.ListenAddr.URL.Host
98
+				glog.V(2).Infof("Using a listen address override %q", addr)
99
+				applyBindAddressOverride(addr, config)
100
+			}
81 101
 		}
82 102
 		return nil
83 103
 	}
... ...
@@ -86,7 +108,51 @@ func NewCommandStartMasterAPI(name, basename string, out io.Writer) (*cobra.Comm
86 86
 	// This command only supports reading from config and the override master address
87 87
 	flags.StringVar(&options.ConfigFile, "config", "", "Location of the master configuration file to run from. Required")
88 88
 	cmd.MarkFlagFilename("config", "yaml", "yml")
89
-	flags.Var(&masterAddr, "master-ip", "The IP address the master should register for itself. Defaults to the master address from the config.")
89
+	flags.Var(&masterAddr, "master", "The address the master should register for itself. Defaults to the master address from the config.")
90
+	BindListenArg(listenArg, flags, "")
90 91
 
91 92
 	return cmd, options
92 93
 }
94
+
95
+// applyBindAddressOverride takes a given address and overrides the relevant sections of a MasterConfig
96
+// TODO: move into helpers
97
+func applyBindAddressOverride(addr string, config *configapi.MasterConfig) {
98
+	defaultHost, defaultPort, err := net.SplitHostPort(addr)
99
+	if err != nil {
100
+		// is just a host
101
+		defaultHost = addr
102
+	}
103
+	config.ServingInfo.BindAddress = overrideAddress(config.ServingInfo.BindAddress, defaultHost, defaultPort)
104
+	if config.EtcdConfig != nil {
105
+		config.EtcdConfig.ServingInfo.BindAddress = overrideAddress(config.EtcdConfig.ServingInfo.BindAddress, defaultHost, "")
106
+		config.EtcdConfig.PeerServingInfo.BindAddress = overrideAddress(config.EtcdConfig.PeerServingInfo.BindAddress, defaultHost, "")
107
+	}
108
+	if config.DNSConfig != nil {
109
+		config.DNSConfig.BindAddress = overrideAddress(config.DNSConfig.BindAddress, defaultHost, "")
110
+	}
111
+	if config.AssetConfig != nil {
112
+		config.AssetConfig.ServingInfo.BindAddress = overrideAddress(config.AssetConfig.ServingInfo.BindAddress, defaultHost, defaultPort)
113
+	}
114
+}
115
+
116
+// overrideAddress applies an optional host or port override to a incoming addr. If host or port are empty they will
117
+// not override the existing addr values.
118
+func overrideAddress(addr, host, port string) string {
119
+	existingHost, existingPort, err := net.SplitHostPort(addr)
120
+	if err != nil {
121
+		if len(host) > 0 {
122
+			return host
123
+		}
124
+		return addr
125
+	}
126
+	if len(host) > 0 {
127
+		existingHost = host
128
+	}
129
+	if len(port) > 0 {
130
+		existingPort = port
131
+	}
132
+	if len(existingPort) == 0 {
133
+		return existingHost
134
+	}
135
+	return net.JoinHostPort(existingHost, existingPort)
136
+}
... ...
@@ -108,7 +108,7 @@ func NewCommandStartMaster(basename string, out io.Writer) (*cobra.Command, *Mas
108 108
 	options.MasterArgs.StartControllers = true
109 109
 	options.MasterArgs.OverrideConfig = func(config *configapi.MasterConfig) error {
110 110
 		if config.KubernetesMasterConfig != nil && options.MasterArgs.MasterAddr.Provided {
111
-			config.KubernetesMasterConfig.MasterIP = options.MasterArgs.MasterAddr.URL.Host
111
+			config.KubernetesMasterConfig.MasterIP = options.MasterArgs.MasterAddr.Host
112 112
 		}
113 113
 		return nil
114 114
 	}
... ...
@@ -171,7 +171,8 @@ _openshift_start_master_api()
171 171
     flags_completion+=("__handle_filename_extension_flag yaml|yml")
172 172
     flags+=("--help")
173 173
     flags+=("-h")
174
-    flags+=("--master-ip=")
174
+    flags+=("--listen=")
175
+    flags+=("--master=")
175 176
 
176 177
     must_have_one_flag=()
177 178
     must_have_one_noun=()