From 6b93e5ba9d360e1a258ea1fb93dd68194117f3bf Mon Sep 17 00:00:00 2001 From: Eric Tune Date: Thu, 16 Apr 2015 23:42:54 -0700 Subject: [PATCH] Kube-proxy learns to read a kubeconfig --master flag is still supported for distros that need it. But now, --kubeconfig flag can be used instead, or in addition, to specify the auth info, and/or the location of the master. A subsequent PR will change salt to generate a kubeconfig, and to make kube-proxy use it, for salt-based clouds. --- cmd/kube-proxy/app/server.go | 30 +++++++++++++++++++++++++----- 1 file changed, 25 insertions(+), 5 deletions(-) diff --git a/cmd/kube-proxy/app/server.go b/cmd/kube-proxy/app/server.go index 8610486b09b..7af846c6e45 100644 --- a/cmd/kube-proxy/app/server.go +++ b/cmd/kube-proxy/app/server.go @@ -22,11 +22,13 @@ import ( "net" "net/http" _ "net/http/pprof" + "os" "strconv" "time" "github.com/GoogleCloudPlatform/kubernetes/pkg/api" "github.com/GoogleCloudPlatform/kubernetes/pkg/client" + "github.com/GoogleCloudPlatform/kubernetes/pkg/client/clientcmd" "github.com/GoogleCloudPlatform/kubernetes/pkg/proxy" "github.com/GoogleCloudPlatform/kubernetes/pkg/proxy/config" "github.com/GoogleCloudPlatform/kubernetes/pkg/util" @@ -40,11 +42,12 @@ import ( // ProxyServer contains configures and runs a Kubernetes proxy server type ProxyServer struct { BindAddress util.IP - ClientConfig client.Config HealthzPort int HealthzBindAddress util.IP OOMScoreAdj int ResourceContainer string + Host string + Kubeconfig string } // NewProxyServer creates a new ProxyServer object with default parameters @@ -61,11 +64,12 @@ func NewProxyServer() *ProxyServer { // AddFlags adds flags for a specific ProxyServer to the specified FlagSet func (s *ProxyServer) AddFlags(fs *pflag.FlagSet) { fs.Var(&s.BindAddress, "bind_address", "The IP address for the proxy server to serve on (set to 0.0.0.0 for all interfaces)") - client.BindClientConfigFlags(fs, &s.ClientConfig) + fs.StringVar(&s.Host, "master", s.Host, "The address of the Kubernetes API server (overrides any value in kubeconfig)") fs.IntVar(&s.HealthzPort, "healthz_port", s.HealthzPort, "The port to bind the health check server. Use 0 to disable.") fs.Var(&s.HealthzBindAddress, "healthz_bind_address", "The IP address for the health check server to serve on, defaulting to 127.0.0.1 (set to 0.0.0.0 for all interfaces)") fs.IntVar(&s.OOMScoreAdj, "oom_score_adj", s.OOMScoreAdj, "The oom_score_adj value for kube-proxy process. Values must be within the range [-1000, 1000]") fs.StringVar(&s.ResourceContainer, "resource_container", s.ResourceContainer, "Absolute name of the resource-only container to create and run the Kube-proxy in (Default: /kube-proxy).") + fs.StringVar(&s.Kubeconfig, "kubeconfig", s.Kubeconfig, "Path to kubeconfig file with authorization and master location information.") } // Run runs the specified ProxyServer. This should never exit. @@ -105,9 +109,25 @@ func (s *ProxyServer) Run(_ []string) error { // are registered yet. // define api config source - if s.ClientConfig.Host != "" { - glog.Infof("Using API calls to get config %v", s.ClientConfig.Host) - client, err := client.New(&s.ClientConfig) + if s.Host != "" || s.Kubeconfig != "" { + var kubeconfig *client.Config + if s.Kubeconfig != "" { + var err error + loader := clientcmd.NewNonInteractiveDeferredLoadingClientConfig( + &clientcmd.ClientConfigLoadingRules{ExplicitPath: s.Kubeconfig}, + &clientcmd.ConfigOverrides{}) + kubeconfig, err = loader.ClientConfig() + if os.IsNotExist(err) { + glog.Fatalf("Could not find kubeconfig file at %s", s.Kubeconfig) + } + if err != nil { + glog.Fatalf("Error loading kubeconfig file \"%s\": %v", s.Kubeconfig, err) + } + } + if s.Host != "" { + kubeconfig.Host = s.Host + } + client, err := client.New(kubeconfig) if err != nil { glog.Fatalf("Invalid API configuration: %v", err) }