Skip to content

Commit f27917c

Browse files
authored
Merge pull request #11865 from m-messiah/allow-to-use-in-cluster-kubeconfig
🌱Use inCluster kubeconfig if empty var provided
2 parents 2b06a59 + d8b6f51 commit f27917c

File tree

1 file changed

+16
-7
lines changed

1 file changed

+16
-7
lines changed

test/framework/cluster_proxy.go

+16-7
Original file line numberDiff line numberDiff line change
@@ -188,12 +188,14 @@ type clusterProxy struct {
188188
}
189189

190190
// NewClusterProxy returns a clusterProxy given a KubeconfigPath and the scheme defining the types hosted in the cluster.
191-
// If a kubeconfig file isn't provided, standard kubeconfig locations will be used (kubectl loading rules apply).
191+
// If a kubeconfig file isn't provided, standard kubeconfig locations will be used (first with in-cluster, then kubectl loading rules apply).
192192
func NewClusterProxy(name string, kubeconfigPath string, scheme *runtime.Scheme, options ...Option) ClusterProxy {
193193
Expect(scheme).NotTo(BeNil(), "scheme is required for NewClusterProxy")
194194

195195
if kubeconfigPath == "" {
196-
kubeconfigPath = clientcmd.NewDefaultClientConfigLoadingRules().GetDefaultFilename()
196+
if _, err := rest.InClusterConfig(); err != nil {
197+
kubeconfigPath = clientcmd.NewDefaultClientConfigLoadingRules().GetDefaultFilename()
198+
}
197199
}
198200

199201
proxy := &clusterProxy{
@@ -354,11 +356,18 @@ func (p *clusterProxy) CreateOrUpdate(ctx context.Context, resources []byte, opt
354356
}
355357

356358
func (p *clusterProxy) GetRESTConfig() *rest.Config {
357-
config, err := clientcmd.LoadFromFile(p.kubeconfigPath)
358-
Expect(err).ToNot(HaveOccurred(), "Failed to load Kubeconfig file from %q", p.kubeconfigPath)
359-
360-
restConfig, err := clientcmd.NewDefaultClientConfig(*config, &clientcmd.ConfigOverrides{}).ClientConfig()
361-
Expect(err).ToNot(HaveOccurred(), "Failed to get ClientConfig from %q", p.kubeconfigPath)
359+
var restConfig *rest.Config
360+
var err error
361+
if p.kubeconfigPath == "" {
362+
restConfig, err = rest.InClusterConfig()
363+
Expect(err).NotTo(HaveOccurred(), "Failed to get in-cluster config")
364+
} else {
365+
config, err := clientcmd.LoadFromFile(p.kubeconfigPath)
366+
Expect(err).ToNot(HaveOccurred(), "Failed to load Kubeconfig file from %q", p.kubeconfigPath)
367+
368+
restConfig, err = clientcmd.NewDefaultClientConfig(*config, &clientcmd.ConfigOverrides{}).ClientConfig()
369+
Expect(err).ToNot(HaveOccurred(), "Failed to get ClientConfig from %q", p.kubeconfigPath)
370+
}
362371

363372
restConfig.UserAgent = "cluster-api-e2e"
364373

0 commit comments

Comments
 (0)