@@ -188,12 +188,14 @@ type clusterProxy struct {
188
188
}
189
189
190
190
// 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).
192
192
func NewClusterProxy (name string , kubeconfigPath string , scheme * runtime.Scheme , options ... Option ) ClusterProxy {
193
193
Expect (scheme ).NotTo (BeNil (), "scheme is required for NewClusterProxy" )
194
194
195
195
if kubeconfigPath == "" {
196
- kubeconfigPath = clientcmd .NewDefaultClientConfigLoadingRules ().GetDefaultFilename ()
196
+ if _ , err := rest .InClusterConfig (); err != nil {
197
+ kubeconfigPath = clientcmd .NewDefaultClientConfigLoadingRules ().GetDefaultFilename ()
198
+ }
197
199
}
198
200
199
201
proxy := & clusterProxy {
@@ -354,11 +356,18 @@ func (p *clusterProxy) CreateOrUpdate(ctx context.Context, resources []byte, opt
354
356
}
355
357
356
358
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
+ }
362
371
363
372
restConfig .UserAgent = "cluster-api-e2e"
364
373
0 commit comments