Skip to content

Commit 7ba5ffc

Browse files
authored
feat(ske): respect KUBECONFIG environment variable (#876)
relates to #875
1 parent db88d79 commit 7ba5ffc

File tree

4 files changed

+54
-5
lines changed

4 files changed

+54
-5
lines changed

docs/stackit_ske_kubeconfig_create.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ Creates or update a kubeconfig for a SKE cluster
77
Creates a kubeconfig for a STACKIT Kubernetes Engine (SKE) cluster, if the config exists in the kubeconfig file the information will be updated.
88

99
By default, the kubeconfig information of the SKE cluster is merged into the default kubeconfig file of the current user. If the kubeconfig file doesn't exist, a new one will be created.
10-
You can override this behavior by specifying a custom filepath with the --filepath flag.
10+
You can override this behavior by specifying a custom filepath using the --filepath flag or by setting the KUBECONFIG env variable (fallback).
1111

1212
An expiration time can be set for the kubeconfig. The expiration time is set in seconds(s), minutes(m), hours(h), days(d) or months(M). Default is 1h.
1313

@@ -47,7 +47,7 @@ stackit ske kubeconfig create CLUSTER_NAME [flags]
4747
```
4848
--disable-writing Disable the writing of kubeconfig. Set the output format to json or yaml using the --output-format flag to display the kubeconfig.
4949
-e, --expiration string Expiration time for the kubeconfig in seconds(s), minutes(m), hours(h), days(d) or months(M). Example: 30d. By default, expiration time is 1h
50-
--filepath string Path to create the kubeconfig file. By default, the kubeconfig is created as 'config' in the .kube folder, in the user's home directory.
50+
--filepath string Path to create the kubeconfig file. Will fall back to KUBECONFIG env variable if not set. In case both aren't set, the kubeconfig is created as file named 'config' in the .kube folder in the user's home directory.
5151
-h, --help Help for "stackit ske kubeconfig create"
5252
-l, --login Create a login kubeconfig that obtains valid credentials via the STACKIT CLI. This flag is mutually exclusive with the expiration flag.
5353
--overwrite Overwrite the kubeconfig file.

internal/cmd/ske/kubeconfig/create/create.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ func NewCmd(params *params.CmdParams) *cobra.Command {
4848
Long: fmt.Sprintf("%s\n\n%s\n%s\n%s\n%s",
4949
"Creates a kubeconfig for a STACKIT Kubernetes Engine (SKE) cluster, if the config exists in the kubeconfig file the information will be updated.",
5050
"By default, the kubeconfig information of the SKE cluster is merged into the default kubeconfig file of the current user. If the kubeconfig file doesn't exist, a new one will be created.",
51-
"You can override this behavior by specifying a custom filepath with the --filepath flag.\n",
51+
"You can override this behavior by specifying a custom filepath using the --filepath flag or by setting the KUBECONFIG env variable (fallback).\n",
5252
"An expiration time can be set for the kubeconfig. The expiration time is set in seconds(s), minutes(m), hours(h), days(d) or months(M). Default is 1h.\n",
5353
"Note that the format is <value><unit>, e.g. 30d for 30 days and you can't combine units."),
5454
Args: args.SingleArg(clusterNameArg, nil),
@@ -170,7 +170,7 @@ func NewCmd(params *params.CmdParams) *cobra.Command {
170170
func configureFlags(cmd *cobra.Command) {
171171
cmd.Flags().Bool(disableWritingFlag, false, fmt.Sprintf("Disable the writing of kubeconfig. Set the output format to json or yaml using the --%s flag to display the kubeconfig.", globalflags.OutputFormatFlag))
172172
cmd.Flags().BoolP(loginFlag, "l", false, "Create a login kubeconfig that obtains valid credentials via the STACKIT CLI. This flag is mutually exclusive with the expiration flag.")
173-
cmd.Flags().String(filepathFlag, "", "Path to create the kubeconfig file. By default, the kubeconfig is created as 'config' in the .kube folder, in the user's home directory.")
173+
cmd.Flags().String(filepathFlag, "", "Path to create the kubeconfig file. Will fall back to KUBECONFIG env variable if not set. In case both aren't set, the kubeconfig is created as file named 'config' in the .kube folder in the user's home directory.")
174174
cmd.Flags().StringP(expirationFlag, "e", "", "Expiration time for the kubeconfig in seconds(s), minutes(m), hours(h), days(d) or months(M). Example: 30d. By default, expiration time is 1h")
175175
cmd.Flags().Bool(overwriteFlag, false, "Overwrite the kubeconfig file.")
176176
cmd.MarkFlagsMutuallyExclusive(loginFlag, expirationFlag)

internal/pkg/services/ske/utils/utils.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff 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.
288288
func 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)

internal/pkg/services/ske/utils/utils_test.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -698,3 +698,48 @@ 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+
err := os.Setenv("KUBECONFIG", tt.kubeconfigEnvVar)
727+
if err != nil {
728+
t.Errorf("could not set KUBECONFIG environment variable: %s", err)
729+
}
730+
err = os.Setenv("HOME", tt.userHome)
731+
if err != nil {
732+
t.Errorf("could not set HOME environment variable: %s", err)
733+
}
734+
735+
output, err := GetDefaultKubeconfigPath()
736+
737+
if err != nil {
738+
t.Errorf("failed on valid input")
739+
}
740+
if output != tt.expected {
741+
t.Errorf("expected output to be %s, got %s", tt.expected, output)
742+
}
743+
})
744+
}
745+
}

0 commit comments

Comments
 (0)