No Proxy for Dashboard and moving logic to proxy package

pull/4229/head
Medya Gh 2019-05-14 21:45:35 -07:00
parent ddb95e1488
commit 8120cdd505
4 changed files with 27 additions and 24 deletions

View File

@ -59,7 +59,7 @@ var dashboardCmd = &cobra.Command{
if err != nil && !os.IsNotExist(err) { if err != nil && !os.IsNotExist(err) {
console.ErrLn("Error loading profile config: %v", err) console.ErrLn("Error loading profile config: %v", err)
} }
proxy.UpdateEnv(cc.KubernetesConfig.NodeIP, "NO_PROXY") proxy.UpdateEnv(cc.KubernetesConfig.NodeIP, "NO_PROXY") // to be used for http get calls
kubectl, err := exec.LookPath("kubectl") kubectl, err := exec.LookPath("kubectl")
if err != nil { if err != nil {

View File

@ -19,10 +19,13 @@ package proxy
import ( import (
"fmt" "fmt"
"net" "net"
"net/http"
"os" "os"
"strings" "strings"
"github.com/golang/glog"
"github.com/pkg/errors" "github.com/pkg/errors"
"k8s.io/client-go/rest"
) )
// EnvVars are variables we plumb through to the underlying container runtime // EnvVars are variables we plumb through to the underlying container runtime
@ -97,3 +100,21 @@ func isValidEnv(env string) bool {
} }
return false return false
} }
// SetNoProxyK8s takes a k8s config and upadates the proxy
func SetNoProxyK8s(cfg *rest.Config) *rest.Config {
wt := cfg.WrapTransport // Config might already have a transport wrapper
cfg.WrapTransport = func(rt http.RoundTripper) http.RoundTripper {
if wt != nil {
rt = wt(rt)
}
if ht, ok := rt.(*http.Transport); ok {
ht.Proxy = nil
rt = ht
} else {
glog.Errorf("Error while casting RoundTripper to *http.Transport : %v", ok)
}
return rt
}
return cfg
}

View File

@ -29,7 +29,7 @@ import (
"github.com/pkg/browser" "github.com/pkg/browser"
"github.com/pkg/errors" "github.com/pkg/errors"
"github.com/spf13/viper" "github.com/spf13/viper"
"k8s.io/api/core/v1" v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels" "k8s.io/apimachinery/pkg/labels"
"k8s.io/client-go/kubernetes" "k8s.io/client-go/kubernetes"
@ -40,6 +40,7 @@ import (
"k8s.io/minikube/pkg/minikube/config" "k8s.io/minikube/pkg/minikube/config"
"k8s.io/minikube/pkg/minikube/console" "k8s.io/minikube/pkg/minikube/console"
"k8s.io/minikube/pkg/minikube/constants" "k8s.io/minikube/pkg/minikube/constants"
"k8s.io/minikube/pkg/minikube/proxy"
"k8s.io/minikube/pkg/util" "k8s.io/minikube/pkg/util"
) )
@ -84,6 +85,7 @@ func (*K8sClientGetter) GetClientset(timeout time.Duration) (*kubernetes.Clients
return nil, fmt.Errorf("Error creating kubeConfig: %v", err) return nil, fmt.Errorf("Error creating kubeConfig: %v", err)
} }
clientConfig.Timeout = timeout clientConfig.Timeout = timeout
clientConfig = proxy.SetNoProxyK8s(clientConfig)
client, err := kubernetes.NewForConfig(clientConfig) client, err := kubernetes.NewForConfig(clientConfig)
if err != nil { if err != nil {
return nil, errors.Wrap(err, "Error creating new client from kubeConfig.ClientConfig()") return nil, errors.Wrap(err, "Error creating new client from kubeConfig.ClientConfig()")

View File

@ -18,7 +18,6 @@ package util
import ( import (
"fmt" "fmt"
"net/http"
"time" "time"
"github.com/golang/glog" "github.com/golang/glog"
@ -34,10 +33,10 @@ import (
"k8s.io/apimachinery/pkg/util/wait" "k8s.io/apimachinery/pkg/util/wait"
"k8s.io/apimachinery/pkg/watch" "k8s.io/apimachinery/pkg/watch"
"k8s.io/client-go/kubernetes" "k8s.io/client-go/kubernetes"
rest "k8s.io/client-go/rest"
"k8s.io/client-go/tools/cache" "k8s.io/client-go/tools/cache"
"k8s.io/client-go/tools/clientcmd" "k8s.io/client-go/tools/clientcmd"
"k8s.io/kubernetes/cmd/kubeadm/app/constants" "k8s.io/kubernetes/cmd/kubeadm/app/constants"
"k8s.io/minikube/pkg/minikube/proxy"
) )
var ( var (
@ -69,25 +68,6 @@ func (s *PodStore) Stop() {
close(s.stopCh) close(s.stopCh)
} }
// setNoProxyConfig takes a k8s config and returns a config without proxy
// to avoid connectivity issues when http(s) proxy is used.
func setNoProxyConfig(cfg *rest.Config) *rest.Config {
wt := cfg.WrapTransport // Config might already have a transport wrapper
cfg.WrapTransport = func(rt http.RoundTripper) http.RoundTripper {
if wt != nil {
rt = wt(rt)
}
if ht, ok := rt.(*http.Transport); ok {
ht.Proxy = nil
rt = ht
} else {
glog.Errorf("Error while casting RoundTripper to *http.Transport : %v", ok)
}
return rt
}
return cfg
}
// GetClient gets the client from config // GetClient gets the client from config
func GetClient() (kubernetes.Interface, error) { func GetClient() (kubernetes.Interface, error) {
loadingRules := clientcmd.NewDefaultClientConfigLoadingRules() loadingRules := clientcmd.NewDefaultClientConfigLoadingRules()
@ -97,7 +77,7 @@ func GetClient() (kubernetes.Interface, error) {
if err != nil { if err != nil {
return nil, fmt.Errorf("Error creating kubeConfig: %v", err) return nil, fmt.Errorf("Error creating kubeConfig: %v", err)
} }
config = setNoProxyConfig(config) config = proxy.SetNoProxyK8s(config)
client, err := kubernetes.NewForConfig(config) client, err := kubernetes.NewForConfig(config)
if err != nil { if err != nil {
return nil, errors.Wrap(err, "Error creating new client from kubeConfig.ClientConfig()") return nil, errors.Wrap(err, "Error creating new client from kubeConfig.ClientConfig()")