File tree Expand file tree Collapse file tree 2 files changed +44
-1
lines changed
internal/pkg/services/ske/utils Expand file tree Collapse file tree 2 files changed +44
-1
lines changed Original file line number Diff line number Diff line change @@ -284,8 +284,12 @@ func WriteConfigFile(configPath, data string) error {
284284 return nil
285285}
286286
287- // GetDefaultKubeconfigPath returns the default location for the kubeconfig file.
287+ // GetDefaultKubeconfigPath returns the default location for the kubeconfig file or the value of KUBECONFIG if set .
288288func GetDefaultKubeconfigPath () (string , error ) {
289+ if kubeconfigEnv := os .Getenv ("KUBECONFIG" ); kubeconfigEnv != "" {
290+ return kubeconfigEnv , nil
291+ }
292+
289293 userHome , err := os .UserHomeDir ()
290294 if err != nil {
291295 return "" , fmt .Errorf ("get user home directory: %w" , err )
Original file line number Diff line number Diff line change @@ -698,3 +698,42 @@ func TestGetDefaultKubeconfigPath(t *testing.T) {
698698 })
699699 }
700700}
701+
702+ func TestGetDefaultKubeconfigPathWithEnvVar (t * testing.T ) {
703+ tests := []struct {
704+ description string
705+ kubeconfigEnvVar string
706+ expected string
707+ userHome string
708+ }{
709+ {
710+ description : "base" ,
711+ kubeconfigEnvVar : "~/.kube/custom/config" ,
712+ expected : "~/.kube/custom/config" ,
713+ userHome : "/home/test-user" ,
714+ },
715+ {
716+ description : "return user home when environment var is empty" ,
717+ kubeconfigEnvVar : "" ,
718+ expected : "/home/test-user/.kube/config" ,
719+ userHome : "/home/test-user" ,
720+ },
721+ }
722+
723+ for _ , tt := range tests {
724+ t .Run (tt .description , func (t * testing.T ) {
725+ // Setup environment variables
726+ os .Setenv ("KUBECONFIG" , tt .kubeconfigEnvVar )
727+ os .Setenv ("HOME" , tt .userHome )
728+
729+ output , err := GetDefaultKubeconfigPath ()
730+
731+ if err != nil {
732+ t .Errorf ("failed on valid input" )
733+ }
734+ if output != tt .expected {
735+ t .Errorf ("expected output to be %s, got %s" , tt .expected , output )
736+ }
737+ })
738+ }
739+ }
You can’t perform that action at this time.
0 commit comments