Use utilnet for getting the host's public IP, which is more reliable. Also, switch to net.IP and net.IPNet flags for validation

pull/54/head
Lucas Käldström 2016-05-10 17:49:09 +03:00
parent 1fe8a53339
commit a38a9037e2
6 changed files with 34 additions and 38 deletions

View File

@ -17,22 +17,27 @@ limitations under the License.
package cmd
import (
"net"
flag "github.com/spf13/pflag"
"k8s.io/minikube/pkg/localkube"
)
func NewLocalkubeServer() *localkube.LocalkubeServer {
// net.ParseCIDR returns multiple values. Use the IPNet return value
_, defaultServiceClusterIPRange, _ := net.ParseCIDR("10.0.0.1/24")
return &localkube.LocalkubeServer{
Containerized: false,
EnableDNS: true,
DNSDomain: "cluster.local",
DNSIP: "10.0.0.10",
DNSIP: net.ParseIP("10.0.0.10"),
LocalkubeDirectory: "/var/lib/localkube",
ServiceClusterIPRange: "10.0.0.1/24",
APIServerAddress: "0.0.0.0",
ServiceClusterIPRange: *defaultServiceClusterIPRange,
APIServerAddress: net.ParseIP("0.0.0.0"),
APIServerPort: 443,
APIServerInsecureAddress: "127.0.0.1",
APIServerInsecureAddress: net.ParseIP("127.0.0.1"),
APIServerInsecurePort: 8080,
}
}
@ -42,12 +47,12 @@ func AddFlags(s *localkube.LocalkubeServer) {
flag.BoolVar(&s.Containerized, "containerized", s.Containerized, "If kubelet should run in containerized mode")
flag.BoolVar(&s.EnableDNS, "enable-dns", s.EnableDNS, "If dns should be enabled")
flag.StringVar(&s.DNSDomain, "dns-domain", s.DNSDomain, "The cluster dns domain")
flag.StringVar(&s.DNSIP, "dns-ip", s.DNSIP, "The cluster dns IP")
flag.IPVar(&s.DNSIP, "dns-ip", s.DNSIP, "The cluster dns IP")
flag.StringVar(&s.LocalkubeDirectory, "localkube-directory", s.LocalkubeDirectory, "The directory localkube will store files in")
flag.StringVar(&s.ServiceClusterIPRange, "service-cluster-ip-range", s.ServiceClusterIPRange, "The service-cluster-ip-range for the apiserver")
flag.StringVar(&s.APIServerAddress, "apiserver-address", s.APIServerAddress, "The address the apiserver will listen securely on")
flag.IPNetVar(&s.ServiceClusterIPRange, "service-cluster-ip-range", s.ServiceClusterIPRange, "The service-cluster-ip-range for the apiserver")
flag.IPVar(&s.APIServerAddress, "apiserver-address", s.APIServerAddress, "The address the apiserver will listen securely on")
flag.IntVar(&s.APIServerPort, "apiserver-port", s.APIServerPort, "The port the apiserver will listen securely on")
flag.StringVar(&s.APIServerInsecureAddress, "apiserver-insecure-address", s.APIServerInsecureAddress, "The address the apiserver will listen insecurely on")
flag.IPVar(&s.APIServerInsecureAddress, "apiserver-insecure-address", s.APIServerInsecureAddress, "The address the apiserver will listen insecurely on")
flag.IntVar(&s.APIServerInsecurePort, "apiserver-insecure-port", s.APIServerInsecurePort, "The port the apiserver will listen insecurely on")
// These two come from vendor/ packages that use flags. We should hide them

View File

@ -85,7 +85,7 @@ func SetupServer(s *localkube.LocalkubeServer) {
// setup dns if we should
if s.EnableDNS {
dns, err := s.NewDNSServer(s.DNSDomain, s.DNSIP, s.GetAPIServerInsecureURL())
dns, err := s.NewDNSServer(s.DNSDomain, s.DNSIP.String(), s.GetAPIServerInsecureURL())
if err != nil {
panic(err)
}

View File

@ -17,7 +17,6 @@ limitations under the License.
package localkube
import (
"net"
"path/filepath"
"strings"
@ -36,9 +35,9 @@ func (lk LocalkubeServer) NewAPIServer() Server {
func StartAPIServer(lk LocalkubeServer) func() error {
config := options.NewAPIServer()
config.BindAddress = net.ParseIP(lk.APIServerAddress)
config.BindAddress = lk.APIServerAddress
config.SecurePort = lk.APIServerPort
config.InsecureBindAddress = net.ParseIP(lk.APIServerInsecureAddress)
config.InsecureBindAddress = lk.APIServerInsecureAddress
config.InsecurePort = lk.APIServerInsecurePort
config.ClientCAFile = filepath.Join(lk.GetCertificateDirectory(), "ca.crt")
@ -52,11 +51,7 @@ func StartAPIServer(lk LocalkubeServer) func() error {
}
// set Service IP range
_, ipnet, err := net.ParseCIDR(lk.ServiceClusterIPRange)
if err != nil {
panic(err)
}
config.ServiceClusterIPRange = *ipnet
config.ServiceClusterIPRange = lk.ServiceClusterIPRange
// defaults from apiserver command
config.EnableProfiling = true

View File

@ -20,7 +20,6 @@ import (
"fmt"
"net"
"os"
str "strings"
"time"
"k8s.io/minikube/pkg/localkube/kube2sky"
@ -56,23 +55,12 @@ func (lk LocalkubeServer) NewDNSServer(rootDomain, clusterIP, kubeAPIServer stri
peerURLs := []string{"http://localhost:9256"}
DNSEtcdURLs := []string{"http://localhost:9090"}
addrs, err := net.InterfaceAddrs()
publicIP := ""
publicIP, err := lk.GetHostIP()
if err != nil {
return nil, err
}
// TODO: Use "k8s.io/kubernetes/pkg/util/net" to detect the public ip address instead of this
for _, addr := range addrs {
// Cast addr to an IPNet and use one that starts with 192.168. that probably is this machine
if ipnet, ok := addr.(*net.IPNet); ok && str.Contains(addr.String(), "192.168.") {
publicIP = ipnet.IP.String()
break
}
}
serverAddress := fmt.Sprintf("%s:%d", publicIP, 53)
serverAddress := fmt.Sprintf("%s:%d", publicIP.String(), 53)
etcdServer, err := lk.NewEtcd(DNSEtcdURLs, peerURLs, DNSName, lk.GetDNSDataDirectory())
if err != nil {
return nil, err

View File

@ -40,7 +40,7 @@ func StartKubeletServer(lk LocalkubeServer) func() error {
// Networking
config.ClusterDomain = lk.DNSDomain
config.ClusterDNS = lk.DNSIP
config.ClusterDNS = lk.DNSIP.String()
config.HostnameOverride = HostnameOverride
// Use the host's resolver config

View File

@ -19,6 +19,9 @@ package localkube
import (
"fmt"
"path"
"net"
utilnet "k8s.io/kubernetes/pkg/util/net"
)
const serverInterval = 200
@ -32,12 +35,12 @@ type LocalkubeServer struct {
Containerized bool
EnableDNS bool
DNSDomain string
DNSIP string
DNSIP net.IP
LocalkubeDirectory string
ServiceClusterIPRange string
APIServerAddress string
ServiceClusterIPRange net.IPNet
APIServerAddress net.IP
APIServerPort int
APIServerInsecureAddress string
APIServerInsecureAddress net.IP
APIServerInsecurePort int
}
@ -58,9 +61,14 @@ func (lk LocalkubeServer) GetCertificateDirectory() string {
}
func (lk LocalkubeServer) GetAPIServerSecureURL() string {
return fmt.Sprintf("https://%s:%d", lk.APIServerAddress, lk.APIServerPort)
return fmt.Sprintf("https://%s:%d", lk.APIServerAddress.String(), lk.APIServerPort)
}
func (lk LocalkubeServer) GetAPIServerInsecureURL() string {
return fmt.Sprintf("http://%s:%d", lk.APIServerInsecureAddress, lk.APIServerInsecurePort)
return fmt.Sprintf("http://%s:%d", lk.APIServerInsecureAddress.String(), lk.APIServerInsecurePort)
}
// Get the host's public IP address
func (lk LocalkubeServer) GetHostIP() (net.IP, error) {
return utilnet.ChooseBindAddress(net.ParseIP("0.0.0.0"))
}