From ab7e4d568ef5f3aee7494ca1f8143869d669bd7d Mon Sep 17 00:00:00 2001 From: deads2k Date: Thu, 2 Feb 2017 09:57:58 -0500 Subject: [PATCH] remove unnecessarily duplication since types collapsed --- cmd/kubelet/app/BUILD | 1 - cmd/kubelet/app/clientgo.go | 120 ------------------------------------ cmd/kubelet/app/server.go | 25 ++------ 3 files changed, 4 insertions(+), 142 deletions(-) delete mode 100644 cmd/kubelet/app/clientgo.go diff --git a/cmd/kubelet/app/BUILD b/cmd/kubelet/app/BUILD index 6a149d91d67..cc1960815c9 100644 --- a/cmd/kubelet/app/BUILD +++ b/cmd/kubelet/app/BUILD @@ -29,7 +29,6 @@ go_library( srcs = [ "auth.go", "bootstrap.go", - "clientgo.go", "plugins.go", "server.go", "server_linux.go", diff --git a/cmd/kubelet/app/clientgo.go b/cmd/kubelet/app/clientgo.go deleted file mode 100644 index bb35c60f37f..00000000000 --- a/cmd/kubelet/app/clientgo.go +++ /dev/null @@ -1,120 +0,0 @@ -/* -Copyright 2017 The Kubernetes Authors. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ - -// this file contains parallel methods for getting a client-go clientconfig -// The types are not reasonably compatibly between the client-go and kubernetes restclient.Interface, -// restclient.Config, or typed clients, so this is the simplest solution during the transition -package app - -import ( - "fmt" - "net/http" - - "github.com/golang/glog" - - "k8s.io/client-go/rest" - clientauth "k8s.io/client-go/tools/auth" - "k8s.io/client-go/tools/clientcmd" - clientcmdapi "k8s.io/client-go/tools/clientcmd/api" - - "k8s.io/kubernetes/cmd/kubelet/app/options" - "k8s.io/kubernetes/pkg/client/chaosclient" -) - -func kubeconfigClientGoConfig(s *options.KubeletServer) (*rest.Config, error) { - if s.RequireKubeConfig { - // Ignores the values of s.APIServerList - return clientcmd.NewNonInteractiveDeferredLoadingClientConfig( - &clientcmd.ClientConfigLoadingRules{ExplicitPath: s.KubeConfig.Value()}, - &clientcmd.ConfigOverrides{}, - ).ClientConfig() - } - return clientcmd.NewNonInteractiveDeferredLoadingClientConfig( - &clientcmd.ClientConfigLoadingRules{ExplicitPath: s.KubeConfig.Value()}, - &clientcmd.ConfigOverrides{ClusterInfo: clientcmdapi.Cluster{Server: s.APIServerList[0]}}, - ).ClientConfig() -} - -// createClientConfig creates a client configuration from the command line -// arguments. If --kubeconfig is explicitly set, it will be used. If it is -// not set, we attempt to load the default kubeconfig file, and if we cannot, -// we fall back to the default client with no auth - this fallback does not, in -// and of itself, constitute an error. -func createClientGoConfig(s *options.KubeletServer) (*rest.Config, error) { - if s.RequireKubeConfig { - return kubeconfigClientGoConfig(s) - } - - // TODO: handle a new --standalone flag that bypasses kubeconfig loading and returns no error. - // DEPRECATED: all subsequent code is deprecated - if len(s.APIServerList) == 0 { - return nil, fmt.Errorf("no api servers specified") - } - // TODO: adapt Kube client to support LB over several servers - if len(s.APIServerList) > 1 { - glog.Infof("Multiple api servers specified. Picking first one") - } - - if s.KubeConfig.Provided() { - return kubeconfigClientGoConfig(s) - } - // If KubeConfig was not provided, try to load the default file, then fall back - // to a default auth config. - clientConfig, err := kubeconfigClientGoConfig(s) - if err != nil { - glog.Warningf("Could not load kubeconfig file %s: %v. Using default client config instead.", s.KubeConfig, err) - - authInfo := &clientauth.Info{} - authConfig, err := authInfo.MergeWithConfig(rest.Config{}) - if err != nil { - return nil, err - } - authConfig.Host = s.APIServerList[0] - clientConfig = &authConfig - } - return clientConfig, nil -} - -// createAPIServerClientGoConfig generates a client.Config from command line flags, -// including api-server-list, via createClientConfig and then injects chaos into -// the configuration via addChaosToClientConfig. This func is exported to support -// integration with third party kubelet extensions (e.g. kubernetes-mesos). -func createAPIServerClientGoConfig(s *options.KubeletServer) (*rest.Config, error) { - clientConfig, err := createClientGoConfig(s) - if err != nil { - return nil, err - } - - clientConfig.ContentType = s.ContentType - // Override kubeconfig qps/burst settings from flags - clientConfig.QPS = float32(s.KubeAPIQPS) - clientConfig.Burst = int(s.KubeAPIBurst) - - addChaosToClientGoConfig(s, clientConfig) - return clientConfig, nil -} - -// addChaosToClientConfig injects random errors into client connections if configured. -func addChaosToClientGoConfig(s *options.KubeletServer, config *rest.Config) { - if s.ChaosChance != 0.0 { - config.WrapTransport = func(rt http.RoundTripper) http.RoundTripper { - seed := chaosclient.NewSeed(1) - // TODO: introduce a standard chaos package with more tunables - this is just a proof of concept - // TODO: introduce random latency and stalls - return chaosclient.NewChaosRoundTripper(rt, chaosclient.LogChaos, seed.P(s.ChaosChance, chaosclient.ErrSimulatedConnectionResetByPeer)) - } - } -} diff --git a/cmd/kubelet/app/server.go b/cmd/kubelet/app/server.go index 780e8dab39d..51e12915075 100644 --- a/cmd/kubelet/app/server.go +++ b/cmd/kubelet/app/server.go @@ -396,6 +396,10 @@ func run(s *options.KubeletServer, kubeDeps *kubelet.KubeletDeps) (err error) { if err != nil { glog.Warningf("New kubeClient from clientConfig error: %v", err) } + externalKubeClient, err = clientgoclientset.NewForConfig(clientConfig) + if err != nil { + glog.Warningf("New kubeClient from clientConfig error: %v", err) + } // make a separate client for events eventClientConfig := *clientConfig eventClientConfig.QPS = float32(s.EventRecordQPS) @@ -413,27 +417,6 @@ func run(s *options.KubeletServer, kubeDeps *kubelet.KubeletDeps) (err error) { } } - // client-go and kuberenetes generated clients are incompatible because the runtime - // and runtime/serializer types have been duplicated in client-go. This means that - // you can't reasonably convert from one to the other and its impossible for a single - // type to fulfill both interfaces. Because of that, we have to build the clients - // up from scratch twice. - // TODO eventually the kubelet should only use the client-go library - clientGoConfig, err := createAPIServerClientGoConfig(s) - if err == nil { - externalKubeClient, err = clientgoclientset.NewForConfig(clientGoConfig) - if err != nil { - glog.Warningf("New kubeClient from clientConfig error: %v", err) - } - } else { - if s.RequireKubeConfig { - return fmt.Errorf("invalid kubeconfig: %v", err) - } - if standaloneMode { - glog.Warningf("No API client: %v", err) - } - } - kubeDeps, err = UnsecuredKubeletDeps(s) if err != nil { return err