mirror of
				https://github.com/k3s-io/kubernetes.git
				synced 2025-10-31 05:40:42 +00:00 
			
		
		
		
	Pulling the CreateKubeConfig function from the expensive to build test/utils/apiserver package had a considerable impact on the overall build time because that package depends on a lot of other packages. Because only that one function is needed by the framework, that extra build time can be avoided by moving it into its own package.
		
			
				
	
	
		
			63 lines
		
	
	
		
			2.0 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			63 lines
		
	
	
		
			2.0 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
| /*
 | |
| Copyright 2022 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.
 | |
| */
 | |
| 
 | |
| package kubeconfig
 | |
| 
 | |
| import (
 | |
| 	"k8s.io/client-go/rest"
 | |
| 	clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
 | |
| )
 | |
| 
 | |
| // CreateKubeConfig converts a [rest.Config] into a [clientcmdapi.Config]
 | |
| // which then can be written to a file with [clientcmd.WriteToFile].
 | |
| func CreateKubeConfig(clientCfg *rest.Config) *clientcmdapi.Config {
 | |
| 	clusterNick := "cluster"
 | |
| 	userNick := "user"
 | |
| 	contextNick := "context"
 | |
| 
 | |
| 	config := clientcmdapi.NewConfig()
 | |
| 
 | |
| 	credentials := clientcmdapi.NewAuthInfo()
 | |
| 	credentials.Token = clientCfg.BearerToken
 | |
| 	credentials.TokenFile = clientCfg.BearerTokenFile
 | |
| 	credentials.ClientCertificate = clientCfg.TLSClientConfig.CertFile
 | |
| 	if len(credentials.ClientCertificate) == 0 {
 | |
| 		credentials.ClientCertificateData = clientCfg.TLSClientConfig.CertData
 | |
| 	}
 | |
| 	credentials.ClientKey = clientCfg.TLSClientConfig.KeyFile
 | |
| 	if len(credentials.ClientKey) == 0 {
 | |
| 		credentials.ClientKeyData = clientCfg.TLSClientConfig.KeyData
 | |
| 	}
 | |
| 	config.AuthInfos[userNick] = credentials
 | |
| 
 | |
| 	cluster := clientcmdapi.NewCluster()
 | |
| 	cluster.Server = clientCfg.Host
 | |
| 	cluster.CertificateAuthority = clientCfg.CAFile
 | |
| 	if len(cluster.CertificateAuthority) == 0 {
 | |
| 		cluster.CertificateAuthorityData = clientCfg.CAData
 | |
| 	}
 | |
| 	cluster.InsecureSkipTLSVerify = clientCfg.Insecure
 | |
| 	config.Clusters[clusterNick] = cluster
 | |
| 
 | |
| 	context := clientcmdapi.NewContext()
 | |
| 	context.Cluster = clusterNick
 | |
| 	context.AuthInfo = userNick
 | |
| 	config.Contexts[contextNick] = context
 | |
| 	config.CurrentContext = contextNick
 | |
| 
 | |
| 	return config
 | |
| }
 |