Skip to content

Commit 198b3ed

Browse files
committed
feat: ske kubeconfig merge add flag overwrite
Signed-off-by: Javier Vela <fjvela@gmail.com>
1 parent d964e77 commit 198b3ed

File tree

4 files changed

+36
-24
lines changed

4 files changed

+36
-24
lines changed

docs/stackit_ske_kubeconfig_create.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ stackit ske kubeconfig create CLUSTER_NAME [flags]
2020
### Examples
2121

2222
```
23-
Create a kubeconfig for the SKE cluster with name "my-cluster. If the config exits in the kubeconfig file the information will be updated."
23+
Create or update a kubeconfig for the SKE cluster with name "my-cluster. If the config exits in the kubeconfig file the information will be updated."
2424
$ stackit ske kubeconfig create my-cluster
2525
2626
Get a login kubeconfig for the SKE cluster with name "my-cluster". This kubeconfig does not contain any credentials and instead obtains valid credentials via the `stackit ske kubeconfig login` command.
@@ -37,6 +37,9 @@ stackit ske kubeconfig create CLUSTER_NAME [flags]
3737
3838
Get a kubeconfig for the SKE cluster with name "my-cluster" without writing it to a file and format the output as json
3939
$ stackit ske kubeconfig create my-cluster --disable-writing --output-format json
40+
41+
Create a kubeconfig for the SKE cluster with name "my-cluster. It will OVERWRITE your current kubeconfig file."
42+
$ stackit ske kubeconfig create my-cluster --overwrite true
4043
```
4144

4245
### Options
@@ -47,6 +50,7 @@ stackit ske kubeconfig create CLUSTER_NAME [flags]
4750
--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.
4851
-h, --help Help for "stackit ske kubeconfig create"
4952
-l, --login Create a login kubeconfig that obtains valid credentials via the STACKIT CLI. This flag is mutually exclusive with the expiration flag.
53+
--overwrite Overwrite the kubeconfig file.
5054
```
5155

5256
### Options inherited from parent commands

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

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,21 @@ import (
2222
const (
2323
clusterNameArg = "CLUSTER_NAME"
2424

25-
loginFlag = "login"
25+
disableWritingFlag = "disable-writing"
2626
expirationFlag = "expiration"
2727
filepathFlag = "filepath"
28-
disableWritingFlag = "disable-writing"
28+
loginFlag = "login"
29+
overwrite = "overwrite"
2930
)
3031

3132
type inputModel struct {
3233
*globalflags.GlobalFlagModel
3334
ClusterName string
34-
Filepath *string
35+
DisableWriting bool
3536
ExpirationTime *string
37+
Filepath *string
3638
Login bool
37-
DisableWriting bool
39+
Overwrite bool
3840
}
3941

4042
func NewCmd(p *print.Printer) *cobra.Command {
@@ -50,7 +52,7 @@ func NewCmd(p *print.Printer) *cobra.Command {
5052
Args: args.SingleArg(clusterNameArg, nil),
5153
Example: examples.Build(
5254
examples.NewExample(
53-
`Create a kubeconfig for the SKE cluster with name "my-cluster. If the config exits in the kubeconfig file the information will be updated."`,
55+
`Create or update a kubeconfig for the SKE cluster with name "my-cluster. If the config exits in the kubeconfig file the information will be updated."`,
5456
"$ stackit ske kubeconfig create my-cluster"),
5557
examples.NewExample(
5658
`Get a login kubeconfig for the SKE cluster with name "my-cluster". `+
@@ -68,6 +70,9 @@ func NewCmd(p *print.Printer) *cobra.Command {
6870
examples.NewExample(
6971
`Get a kubeconfig for the SKE cluster with name "my-cluster" without writing it to a file and format the output as json`,
7072
"$ stackit ske kubeconfig create my-cluster --disable-writing --output-format json"),
73+
examples.NewExample(
74+
`Create a kubeconfig for the SKE cluster with name "my-cluster. It will OVERWRITE your current kubeconfig file."`,
75+
"$ stackit ske kubeconfig create my-cluster --overwrite true"),
7176
),
7277
RunE: func(cmd *cobra.Command, args []string) error {
7378
ctx := context.Background()
@@ -83,7 +88,12 @@ func NewCmd(p *print.Printer) *cobra.Command {
8388
}
8489

8590
if !model.AssumeYes && !model.DisableWriting {
86-
prompt := fmt.Sprintf("Are you sure you want to update your kubeconfig for SKE cluster %q? This will update your kubeconfig file. \n If it the kubeconfig file doesn´t exists, it will create a new one.", model.ClusterName)
91+
var prompt string
92+
if model.Overwrite {
93+
prompt = fmt.Sprintf("Are you sure you want to create a kubeconfig for SKE cluster %q? This will OVERWRITE your current kubeconfig file, if it exists.", model.ClusterName)
94+
} else {
95+
prompt = fmt.Sprintf("Are you sure you want to update your kubeconfig for SKE cluster %q? This will update your kubeconfig file. \n If it the kubeconfig file doesn't exists, it will create a new one.", model.ClusterName)
96+
}
8797
err = p.PromptForConfirmation(prompt)
8898
if err != nil {
8999
return err
@@ -137,7 +147,11 @@ func NewCmd(p *print.Printer) *cobra.Command {
137147
}
138148

139149
if !model.DisableWriting {
140-
err = skeUtils.MergeKubeConfig(kubeconfigPath, kubeconfig)
150+
if model.Overwrite {
151+
err = skeUtils.WriteConfigFile(kubeconfigPath, kubeconfig)
152+
} else {
153+
err = skeUtils.MergeKubeConfig(kubeconfigPath, kubeconfig)
154+
}
141155
if err != nil {
142156
return fmt.Errorf("write kubeconfig file: %w", err)
143157
}
@@ -152,11 +166,11 @@ func NewCmd(p *print.Printer) *cobra.Command {
152166
}
153167

154168
func configureFlags(cmd *cobra.Command) {
169+
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))
155170
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.")
156-
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")
157171
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.")
158-
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))
159-
172+
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")
173+
cmd.Flags().Bool(overwrite, false, "Overwrite the kubeconfig file.")
160174
cmd.MarkFlagsMutuallyExclusive(loginFlag, expirationFlag)
161175
}
162176

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,12 @@ import (
44
"context"
55
"testing"
66

7-
"github.com/stackitcloud/stackit-cli/internal/pkg/globalflags"
8-
"github.com/stackitcloud/stackit-cli/internal/pkg/print"
9-
"github.com/stackitcloud/stackit-cli/internal/pkg/utils"
10-
117
"github.com/google/go-cmp/cmp"
128
"github.com/google/go-cmp/cmp/cmpopts"
139
"github.com/google/uuid"
10+
"github.com/stackitcloud/stackit-cli/internal/pkg/globalflags"
11+
"github.com/stackitcloud/stackit-cli/internal/pkg/print"
12+
"github.com/stackitcloud/stackit-cli/internal/pkg/utils"
1413
"github.com/stackitcloud/stackit-sdk-go/services/ske"
1514
)
1615

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

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package utils
33
import (
44
"context"
55
"fmt"
6+
"maps"
67
"os"
78
"path/filepath"
89
"strconv"
@@ -251,15 +252,9 @@ func MergeKubeConfig(pathDestionationKubeConfig, contentNewKubeConfig string) er
251252
return fmt.Errorf("error loading existing kubeconfig: %w", err)
252253
}
253254

254-
for name, authInfo := range newConfig.AuthInfos {
255-
existingConfig.AuthInfos[name] = authInfo
256-
}
257-
for name, context := range newConfig.Contexts {
258-
existingConfig.Contexts[name] = context
259-
}
260-
for name, cluster := range newConfig.Clusters {
261-
existingConfig.Clusters[name] = cluster
262-
}
255+
maps.Copy(existingConfig.AuthInfos, newConfig.AuthInfos)
256+
maps.Copy(existingConfig.Contexts, newConfig.Contexts)
257+
maps.Copy(existingConfig.Clusters, newConfig.Clusters)
263258

264259
err = clientcmd.WriteToFile(*existingConfig, pathDestionationKubeConfig)
265260
if err != nil {

0 commit comments

Comments
 (0)