Skip to content

Commit e7ff926

Browse files
authored
Fallback to kube context if KUBECONFIG is not set (#85)
* Fallback to kube context if KUBECONFIG is not set Signed-off-by: Jonathan Ogilvie <jonathan.ogilvie@sumologic.com> * Fallback to kube context if KUBECONFIG is not set Signed-off-by: Jonathan Ogilvie <jonathan.ogilvie@sumologic.com> --------- Signed-off-by: Jonathan Ogilvie <jonathan.ogilvie@sumologic.com>
1 parent b17032d commit e7ff926

File tree

2 files changed

+15
-11
lines changed

2 files changed

+15
-11
lines changed

cmd/diff/diff_test.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -989,7 +989,9 @@ func TestGetRestConfig(t *testing.T) {
989989
"EmptyKubeconfigEnvVar": {
990990
kubeconfigPath: "",
991991
expectError: true,
992-
errorContains: "KUBECONFIG environment variable is not set",
992+
// With standard loading rules, when KUBECONFIG is empty it tries ~/.kube/config
993+
// If that doesn't exist, it returns "invalid configuration"
994+
errorContains: "invalid configuration",
993995
},
994996
"ValidKubeconfigPath": {
995997
setupFile: func() string {
@@ -1026,8 +1028,9 @@ users:
10261028
},
10271029
"InvalidKubeconfigPath": {
10281030
kubeconfigPath: "/invalid/nonexistent/path",
1029-
expectError: true, // Will error when file doesn't exist
1030-
errorContains: "no such file or directory",
1031+
expectError: true,
1032+
// With standard loading rules, invalid path results in "invalid configuration"
1033+
errorContains: "invalid configuration",
10311034
},
10321035
}
10331036

cmd/diff/main.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,13 @@ limitations under the License.
1818
package main
1919

2020
import (
21-
"os"
2221
"time"
2322

2423
"github.com/alecthomas/kong"
2524
"k8s.io/client-go/rest"
2625
"k8s.io/client-go/tools/clientcmd"
2726
"sigs.k8s.io/controller-runtime/pkg/log/zap"
2827

29-
"github.com/crossplane/crossplane-runtime/v2/pkg/errors"
3028
"github.com/crossplane/crossplane-runtime/v2/pkg/logging"
3129

3230
"github.com/crossplane/crossplane/v2/cmd/crank/version"
@@ -92,10 +90,13 @@ func main() {
9290
}
9391

9492
func getRestConfig() (*rest.Config, error) {
95-
kubeconfig := os.Getenv("KUBECONFIG")
96-
if kubeconfig == "" {
97-
return nil, errors.New("KUBECONFIG environment variable is not set. Please set KUBECONFIG to point to your kubeconfig file")
98-
}
99-
100-
return clientcmd.BuildConfigFromFlags("", kubeconfig)
93+
// Use the standard client-go loading rules:
94+
// 1. If KUBECONFIG env var is set, use that
95+
// 2. Otherwise, use ~/.kube/config
96+
// 3. Respects current context from the kubeconfig
97+
loadingRules := clientcmd.NewDefaultClientConfigLoadingRules()
98+
configOverrides := &clientcmd.ConfigOverrides{}
99+
kubeConfig := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(loadingRules, configOverrides)
100+
101+
return kubeConfig.ClientConfig()
101102
}

0 commit comments

Comments
 (0)