istio.io/istio@v0.0.0-20240520182934-d79c90f27776/pkg/test/framework/components/cluster/kube/factory.go (about) 1 // Copyright Istio Authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package kube 16 17 import ( 18 "fmt" 19 "net/http" 20 "net/url" 21 22 "k8s.io/client-go/rest" 23 24 istioKube "istio.io/istio/pkg/kube" 25 "istio.io/istio/pkg/test/framework/components/cluster" 26 "istio.io/istio/pkg/test/util/file" 27 ) 28 29 const ( 30 kubeconfigMetaKey = "kubeconfig" 31 vmSupportMetaKey = "fakeVM" 32 ) 33 34 func init() { 35 cluster.RegisterFactory(cluster.Kubernetes, buildKube) 36 } 37 38 func buildKube(origCfg cluster.Config, topology cluster.Topology) (cluster.Cluster, error) { 39 cfg, err := validConfig(origCfg) 40 if err != nil { 41 return nil, err 42 } 43 44 kubeconfigPath := cfg.Meta.String(kubeconfigMetaKey) 45 kubeconfigPath, err = file.NormalizePath(kubeconfigPath) 46 if err != nil { 47 return nil, err 48 } 49 50 var client istioKube.CLIClient 51 if len(cfg.HTTPProxy) > 0 { 52 proxyURL, err := url.Parse(cfg.HTTPProxy) 53 if err != nil { 54 return nil, err 55 } 56 client, err = buildClientWithProxy(kubeconfigPath, proxyURL) 57 if err != nil { 58 return nil, err 59 } 60 } else { 61 client, err = buildClient(kubeconfigPath) 62 if err != nil { 63 return nil, err 64 } 65 } 66 67 // support fake VMs by default 68 vmSupport := true 69 if vmP := cfg.Meta.Bool(vmSupportMetaKey); vmP != nil { 70 vmSupport = *vmP 71 } 72 73 return &Cluster{ 74 filename: kubeconfigPath, 75 CLIClient: client, 76 vmSupport: vmSupport, 77 Topology: topology, 78 }, nil 79 } 80 81 func validConfig(cfg cluster.Config) (cluster.Config, error) { 82 // only include kube-specific validation here 83 if cfg.Meta.String(kubeconfigMetaKey) == "" { 84 return cfg, fmt.Errorf("missing meta.%s for %s", kubeconfigMetaKey, cfg.Name) 85 } 86 return cfg, nil 87 } 88 89 func buildClient(kubeconfig string) (istioKube.CLIClient, error) { 90 rc, err := istioKube.DefaultRestConfig(kubeconfig, "", func(config *rest.Config) { 91 config.QPS = 200 92 config.Burst = 400 93 }) 94 if err != nil { 95 return nil, err 96 } 97 return istioKube.NewCLIClient(istioKube.NewClientConfigForRestConfig(rc)) 98 } 99 100 func buildClientWithProxy(kubeconfig string, proxyURL *url.URL) (istioKube.CLIClient, error) { 101 rc, err := istioKube.DefaultRestConfig(kubeconfig, "", func(config *rest.Config) { 102 config.QPS = 200 103 config.Burst = 400 104 }) 105 if err != nil { 106 return nil, err 107 } 108 rc.Proxy = http.ProxyURL(proxyURL) 109 return istioKube.NewCLIClient(istioKube.NewClientConfigForRestConfig(rc)) 110 }