Add bind address server config

This commit is contained in:
galal-hussein 2019-03-31 02:10:23 +02:00
parent 797eed6605
commit d255574150
3 changed files with 24 additions and 6 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 {