Merge pull request #289 from galal-hussein/add_bind_address

Add bind address server config
This commit is contained in:
Darren Shepherd 2019-04-08 22:36:58 -07:00 committed by GitHub
commit 9e80177443
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 48 additions and 21 deletions

View File

@ -17,6 +17,7 @@ type Server struct {
KubeConfigOutput string
KubeConfigMode string
KnownIPs cli.StringSlice
BindAddress string
}
var ServerConfig Server
@ -28,6 +29,11 @@ func NewServerCommand(action func(*cli.Context) error) cli.Command {
UsageText: appName + " server [OPTIONS]",
Action: action,
Flags: []cli.Flag{
cli.StringFlag{
Name: "bind-address",
Usage: "k3s bind address",
Destination: &ServerConfig.BindAddress,
},
cli.IntFlag{
Name: "https-listen-port",
Usage: "HTTPS listen port",

View File

@ -78,6 +78,7 @@ func run(app *cli.Context, cfg *cmds.Server) error {
serverConfig.TLSConfig.HTTPSPort = cfg.HTTPSPort
serverConfig.TLSConfig.HTTPPort = cfg.HTTPPort
serverConfig.TLSConfig.KnownIPs = knownIPs(cfg.KnownIPs)
serverConfig.TLSConfig.BindAddress = cfg.BindAddress
_, serverConfig.ControlConfig.ClusterIPRange, err = net2.ParseCIDR(cfg.ClusterCIDR)
if err != nil {
@ -133,8 +134,11 @@ func run(app *cli.Context, cfg *cmds.Server) error {
<-ctx.Done()
return nil
}
url := fmt.Sprintf("https://localhost:%d", serverConfig.TLSConfig.HTTPSPort)
ip := serverConfig.TLSConfig.BindAddress
if ip == "" {
ip = "localhost"
}
url := fmt.Sprintf("https://%s:%d", ip, serverConfig.TLSConfig.HTTPSPort)
token := server.FormatToken(serverConfig.ControlConfig.Runtime.NodeToken, certs)
agentConfig := cmds.AgentConfig

View File

@ -49,6 +49,7 @@ func resolveDataDir(dataDir string) (string, error) {
}
func StartServer(ctx context.Context, config *Config) (string, error) {
if err := setupDataDirAndChdir(&config.ControlConfig); err != nil {
return "", err
}
@ -62,9 +63,12 @@ func StartServer(ctx context.Context, config *Config) (string, error) {
return "", errors.Wrap(err, "starting tls server")
}
ip, err := net.ChooseHostInterface()
if err != nil {
ip = net2.ParseIP("127.0.0.1")
ip := net2.ParseIP(config.TLSConfig.BindAddress)
if ip == nil {
ip, err = net.ChooseHostInterface()
if err != nil {
ip = net2.ParseIP("127.0.0.1")
}
}
printTokens(certs, ip.String(), &config.TLSConfig, &config.ControlConfig)
@ -192,7 +196,11 @@ func printTokens(certs, advertiseIP string, tlsConfig *dynamiclistener.UserConfi
func writeKubeConfig(certs string, tlsConfig *dynamiclistener.UserConfig, config *config.Control) {
clientToken := FormatToken(config.Runtime.ClientToken, certs)
url := fmt.Sprintf("https://localhost:%d", tlsConfig.HTTPSPort)
ip := tlsConfig.BindAddress
if ip == "" {
ip = "localhost"
}
url := fmt.Sprintf("https://%s:%d", ip, tlsConfig.HTTPSPort)
kubeConfig, err := HomeKubeConfig(true)
def := true
if err != nil {

View File

@ -11,7 +11,7 @@ package=github.com/opencontainers/runc/contrib/cmd/recvtty
k8s.io/kubernetes v1.13.5-k3s.1 https://github.com/rancher/k3s.git transitive=true,staging=true
github.com/rancher/norman f75e3607e96e1a5d3cbaf4ee7cea1459cc727f61 https://github.com/ibuildthecloud/norman.git
github.com/rancher/norman efb72b594a2a34f2573b9565c6cd9926a1f6ae08 https://github.com/ibuildthecloud/norman.git
github.com/coreos/flannel 823afe66b2266bf71f5bec24e6e28b26d70cfc7c https://github.com/ibuildthecloud/flannel.git
github.com/natefinch/lumberjack aee4629129445bbdfb69aa565537dcfa16544311
github.com/gorilla/mux v1.6.2

View File

@ -196,7 +196,10 @@ func (s *server) userConfigure() error {
s.ips.Add(ip, netIP)
}
}
bindAddress := net.ParseIP(s.userConfig.BindAddress)
if bindAddress != nil {
s.ips.Add(s.userConfig.BindAddress, bindAddress)
}
return nil
}
@ -445,7 +448,7 @@ func (s *server) serveHTTPS() error {
PreferServerCipherSuites: true,
}
listener, err := s.newListener(s.userConfig.HTTPSPort, conf)
listener, err := s.newListener(s.userConfig.BindAddress, s.userConfig.HTTPSPort, conf)
if err != nil {
return err
}
@ -460,7 +463,7 @@ func (s *server) serveHTTPS() error {
s.startServer(listener, server)
if s.userConfig.HTTPPort > 0 {
httpListener, err := s.newListener(s.userConfig.HTTPPort, nil)
httpListener, err := s.newListener(s.userConfig.BindAddress, s.userConfig.HTTPPort, nil)
if err != nil {
return err
}
@ -524,8 +527,8 @@ func (s *server) Handler() http.Handler {
return s.userConfig.Handler
}
func (s *server) newListener(port int, config *tls.Config) (net.Listener, error) {
addr := fmt.Sprintf(":%d", port)
func (s *server) newListener(ip string, port int, config *tls.Config) (net.Listener, error) {
addr := fmt.Sprintf("%s:%d", ip, port)
l, err := net.Listen("tcp", addr)
if err != nil {
return nil, err
@ -561,7 +564,7 @@ func (s *server) serveACME() error {
}
if s.userConfig.HTTPPort > 0 {
httpListener, err := s.newListener(s.userConfig.HTTPPort, nil)
httpListener, err := s.newListener(s.userConfig.BindAddress, s.userConfig.HTTPPort, nil)
if err != nil {
return err
}
@ -579,7 +582,7 @@ func (s *server) serveACME() error {
}
httpsListener, err := s.newListener(s.userConfig.HTTPSPort, conf)
httpsListener, err := s.newListener(s.userConfig.BindAddress, s.userConfig.HTTPSPort, conf)
if err != nil {
return err
}

View File

@ -26,13 +26,14 @@ type UserConfig struct {
// Optional fields
KnownIPs []string
Domains []string
Mode string
NoCACerts bool
CACerts string
Cert string
Key string
KnownIPs []string
Domains []string
Mode string
NoCACerts bool
CACerts string
Cert string
Key string
BindAddress string
}
type ListenerStatus struct {

View File

@ -27,6 +27,11 @@ func Resolve(s string) (string, error) {
}
func getHomeDir() (string, error) {
home := os.Getenv("HOME")
if home != "" {
return home, nil
}
if os.Getuid() == 0 {
return "/root", nil
}