|
| 1 | +/* |
| 2 | +Copyright 2017 The Kubernetes Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +// this file contains parallel methods for getting a client-go clientconfig |
| 18 | +// The types are not reasonably compatibly between the client-go and kubernetes restclient.Interface, |
| 19 | +// restclient.Config, or typed clients, so this is the simplest solution during the transition |
| 20 | +package app |
| 21 | + |
| 22 | +import ( |
| 23 | + "fmt" |
| 24 | + "net/http" |
| 25 | + |
| 26 | + "github.com/golang/glog" |
| 27 | + |
| 28 | + "k8s.io/client-go/rest" |
| 29 | + clientauth "k8s.io/client-go/tools/auth" |
| 30 | + "k8s.io/client-go/tools/clientcmd" |
| 31 | + clientcmdapi "k8s.io/client-go/tools/clientcmd/api" |
| 32 | + |
| 33 | + "k8s.io/kubernetes/cmd/kubelet/app/options" |
| 34 | + "k8s.io/kubernetes/pkg/client/chaosclient" |
| 35 | +) |
| 36 | + |
| 37 | +func kubeconfigClientGoConfig(s *options.KubeletServer) (*rest.Config, error) { |
| 38 | + if s.RequireKubeConfig { |
| 39 | + // Ignores the values of s.APIServerList |
| 40 | + return clientcmd.NewNonInteractiveDeferredLoadingClientConfig( |
| 41 | + &clientcmd.ClientConfigLoadingRules{ExplicitPath: s.KubeConfig.Value()}, |
| 42 | + &clientcmd.ConfigOverrides{}, |
| 43 | + ).ClientConfig() |
| 44 | + } |
| 45 | + return clientcmd.NewNonInteractiveDeferredLoadingClientConfig( |
| 46 | + &clientcmd.ClientConfigLoadingRules{ExplicitPath: s.KubeConfig.Value()}, |
| 47 | + &clientcmd.ConfigOverrides{ClusterInfo: clientcmdapi.Cluster{Server: s.APIServerList[0]}}, |
| 48 | + ).ClientConfig() |
| 49 | +} |
| 50 | + |
| 51 | +// createClientConfig creates a client configuration from the command line |
| 52 | +// arguments. If --kubeconfig is explicitly set, it will be used. If it is |
| 53 | +// not set, we attempt to load the default kubeconfig file, and if we cannot, |
| 54 | +// we fall back to the default client with no auth - this fallback does not, in |
| 55 | +// and of itself, constitute an error. |
| 56 | +func createClientGoConfig(s *options.KubeletServer) (*rest.Config, error) { |
| 57 | + if s.RequireKubeConfig { |
| 58 | + return kubeconfigClientGoConfig(s) |
| 59 | + } |
| 60 | + |
| 61 | + // TODO: handle a new --standalone flag that bypasses kubeconfig loading and returns no error. |
| 62 | + // DEPRECATED: all subsequent code is deprecated |
| 63 | + if len(s.APIServerList) == 0 { |
| 64 | + return nil, fmt.Errorf("no api servers specified") |
| 65 | + } |
| 66 | + // TODO: adapt Kube client to support LB over several servers |
| 67 | + if len(s.APIServerList) > 1 { |
| 68 | + glog.Infof("Multiple api servers specified. Picking first one") |
| 69 | + } |
| 70 | + |
| 71 | + if s.KubeConfig.Provided() { |
| 72 | + return kubeconfigClientGoConfig(s) |
| 73 | + } |
| 74 | + // If KubeConfig was not provided, try to load the default file, then fall back |
| 75 | + // to a default auth config. |
| 76 | + clientConfig, err := kubeconfigClientGoConfig(s) |
| 77 | + if err != nil { |
| 78 | + glog.Warningf("Could not load kubeconfig file %s: %v. Using default client config instead.", s.KubeConfig, err) |
| 79 | + |
| 80 | + authInfo := &clientauth.Info{} |
| 81 | + authConfig, err := authInfo.MergeWithConfig(rest.Config{}) |
| 82 | + if err != nil { |
| 83 | + return nil, err |
| 84 | + } |
| 85 | + authConfig.Host = s.APIServerList[0] |
| 86 | + clientConfig = &authConfig |
| 87 | + } |
| 88 | + return clientConfig, nil |
| 89 | +} |
| 90 | + |
| 91 | +// createAPIServerClientGoConfig generates a client.Config from command line flags, |
| 92 | +// including api-server-list, via createClientConfig and then injects chaos into |
| 93 | +// the configuration via addChaosToClientConfig. This func is exported to support |
| 94 | +// integration with third party kubelet extensions (e.g. kubernetes-mesos). |
| 95 | +func createAPIServerClientGoConfig(s *options.KubeletServer) (*rest.Config, error) { |
| 96 | + clientConfig, err := createClientGoConfig(s) |
| 97 | + if err != nil { |
| 98 | + return nil, err |
| 99 | + } |
| 100 | + |
| 101 | + clientConfig.ContentType = s.ContentType |
| 102 | + // Override kubeconfig qps/burst settings from flags |
| 103 | + clientConfig.QPS = float32(s.KubeAPIQPS) |
| 104 | + clientConfig.Burst = int(s.KubeAPIBurst) |
| 105 | + |
| 106 | + addChaosToClientGoConfig(s, clientConfig) |
| 107 | + return clientConfig, nil |
| 108 | +} |
| 109 | + |
| 110 | +// addChaosToClientConfig injects random errors into client connections if configured. |
| 111 | +func addChaosToClientGoConfig(s *options.KubeletServer, config *rest.Config) { |
| 112 | + if s.ChaosChance != 0.0 { |
| 113 | + config.WrapTransport = func(rt http.RoundTripper) http.RoundTripper { |
| 114 | + seed := chaosclient.NewSeed(1) |
| 115 | + // TODO: introduce a standard chaos package with more tunables - this is just a proof of concept |
| 116 | + // TODO: introduce random latency and stalls |
| 117 | + return chaosclient.NewChaosRoundTripper(rt, chaosclient.LogChaos, seed.P(s.ChaosChance, chaosclient.ErrSimulatedConnectionResetByPeer)) |
| 118 | + } |
| 119 | + } |
| 120 | +} |
0 commit comments