mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-25 20:53:33 +00:00
Merge pull request #53924 from dims/use-in-cluster-config-before-default
Automatic merge from submit-queue. If you want to cherry-pick this change to another branch, please follow the instructions <a href="https://github.com/kubernetes/community/blob/master/contributors/devel/cherry-picks.md">here</a>. Try in-cluster config before using localhost:8080 **What this PR does / why we need it**: When starting an e2e test in a pod in a cluster, if the host is not specified in the command line, we default to using 'http://127.0.0.1:8080' currently. We should be discovering the host/port using the in-cluster config and using that if possible. **Which issue this PR fixes** *(optional, in `fixes #<issue number>(, fixes #<issue_number>, ...)` format, will close that issue when PR gets merged)*: fixes # Fixes #53894 **Special notes for your reviewer**: **Release note**: ```release-note NONE ```
This commit is contained in:
commit
cf84e37c2c
@ -19,12 +19,16 @@ package framework
|
|||||||
import (
|
import (
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/golang/glog"
|
||||||
"github.com/onsi/ginkgo/config"
|
"github.com/onsi/ginkgo/config"
|
||||||
"github.com/spf13/viper"
|
"github.com/spf13/viper"
|
||||||
|
restclient "k8s.io/client-go/rest"
|
||||||
"k8s.io/client-go/tools/clientcmd"
|
"k8s.io/client-go/tools/clientcmd"
|
||||||
|
clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
|
||||||
"k8s.io/kubernetes/pkg/cloudprovider"
|
"k8s.io/kubernetes/pkg/cloudprovider"
|
||||||
"k8s.io/kubernetes/pkg/kubelet/apis/kubeletconfig"
|
"k8s.io/kubernetes/pkg/kubelet/apis/kubeletconfig"
|
||||||
"k8s.io/kubernetes/pkg/kubemark"
|
"k8s.io/kubernetes/pkg/kubemark"
|
||||||
@ -296,13 +300,62 @@ func ViperizeFlags() {
|
|||||||
AfterReadingAllFlags(&TestContext)
|
AfterReadingAllFlags(&TestContext)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func createKubeConfig(clientCfg *restclient.Config) *clientcmdapi.Config {
|
||||||
|
clusterNick := "cluster"
|
||||||
|
userNick := "user"
|
||||||
|
contextNick := "context"
|
||||||
|
|
||||||
|
config := clientcmdapi.NewConfig()
|
||||||
|
|
||||||
|
credentials := clientcmdapi.NewAuthInfo()
|
||||||
|
credentials.Token = clientCfg.BearerToken
|
||||||
|
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
|
||||||
|
}
|
||||||
|
|
||||||
// AfterReadingAllFlags makes changes to the context after all flags
|
// AfterReadingAllFlags makes changes to the context after all flags
|
||||||
// have been read.
|
// have been read.
|
||||||
func AfterReadingAllFlags(t *TestContextType) {
|
func AfterReadingAllFlags(t *TestContextType) {
|
||||||
// Only set a default host if one won't be supplied via kubeconfig
|
// Only set a default host if one won't be supplied via kubeconfig
|
||||||
if len(t.Host) == 0 && len(t.KubeConfig) == 0 {
|
if len(t.Host) == 0 && len(t.KubeConfig) == 0 {
|
||||||
|
// Check if we can use the in-cluster config
|
||||||
|
if clusterConfig, err := restclient.InClusterConfig(); err == nil {
|
||||||
|
if tempFile, err := ioutil.TempFile(os.TempDir(), "kubeconfig-"); err == nil {
|
||||||
|
kubeConfig := createKubeConfig(clusterConfig)
|
||||||
|
clientcmd.WriteToFile(*kubeConfig, tempFile.Name())
|
||||||
|
t.KubeConfig = tempFile.Name()
|
||||||
|
glog.Infof("Using a temporary kubeconfig file from in-cluster config : %s", tempFile.Name())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if len(t.KubeConfig) == 0 {
|
||||||
|
glog.Warningf("Unable to find in-cluster config, using default host : %s", defaultHost)
|
||||||
t.Host = defaultHost
|
t.Host = defaultHost
|
||||||
}
|
}
|
||||||
|
}
|
||||||
// Reset the cluster IP range flag to CLUSTER_IP_RANGE env var, if defined.
|
// Reset the cluster IP range flag to CLUSTER_IP_RANGE env var, if defined.
|
||||||
if clusterIPRange := os.Getenv("CLUSTER_IP_RANGE"); clusterIPRange != "" {
|
if clusterIPRange := os.Getenv("CLUSTER_IP_RANGE"); clusterIPRange != "" {
|
||||||
t.CloudConfig.ClusterIPRange = clusterIPRange
|
t.CloudConfig.ClusterIPRange = clusterIPRange
|
||||||
|
Loading…
Reference in New Issue
Block a user