Skip to content

Commit a3a0754

Browse files
authored
Fix: Kubeconfig not written when output format is json (#553)
* Fix: Kubeconfig doesn't get written when output format is json - Add flag to disable the writing of kubeconfig
1 parent 5ba6565 commit a3a0754

File tree

3 files changed

+45
-5
lines changed

3 files changed

+45
-5
lines changed

docs/stackit_ske_kubeconfig_create.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,15 @@ stackit ske kubeconfig create CLUSTER_NAME [flags]
3232
3333
Create a kubeconfig for the SKE cluster with name "my-cluster" in a custom filepath
3434
$ stackit ske kubeconfig create my-cluster --filepath /path/to/config
35+
36+
Get a kubeconfig for the SKE cluster with name "my-cluster" without writing it to a file.
37+
3538
```
3639

3740
### Options
3841

3942
```
43+
--disable-writing Disable writing to the kubeconfig file.
4044
-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
4145
--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.
4246
-h, --help Help for "stackit ske kubeconfig create"

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

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

25-
loginFlag = "login"
26-
expirationFlag = "expiration"
27-
filepathFlag = "filepath"
25+
loginFlag = "login"
26+
expirationFlag = "expiration"
27+
filepathFlag = "filepath"
28+
disableWritingFlag = "disable-writing"
2829
)
2930

3031
type inputModel struct {
@@ -33,6 +34,7 @@ type inputModel struct {
3334
Filepath *string
3435
ExpirationTime *string
3536
Login bool
37+
DisableWriting bool
3638
}
3739

3840
func NewCmd(p *print.Printer) *cobra.Command {
@@ -63,6 +65,9 @@ func NewCmd(p *print.Printer) *cobra.Command {
6365
examples.NewExample(
6466
`Create a kubeconfig for the SKE cluster with name "my-cluster" in a custom filepath`,
6567
"$ stackit ske kubeconfig create my-cluster --filepath /path/to/config"),
68+
examples.NewExample(
69+
`Get a kubeconfig for the SKE cluster with name "my-cluster" without writing it to a file and format the output as json`,
70+
"$ stackit ske kubeconfig create my-cluster --disable-writing --output-format json"),
6671
),
6772
RunE: func(cmd *cobra.Command, args []string) error {
6873
ctx := context.Background()
@@ -77,7 +82,7 @@ func NewCmd(p *print.Printer) *cobra.Command {
7782
return err
7883
}
7984

80-
if !model.AssumeYes {
85+
if !model.AssumeYes && !model.DisableWriting {
8186
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)
8287
err = p.PromptForConfirmation(prompt)
8388
if err != nil {
@@ -131,7 +136,7 @@ func NewCmd(p *print.Printer) *cobra.Command {
131136
kubeconfigPath = *model.Filepath
132137
}
133138

134-
if model.OutputFormat != print.JSONOutputFormat {
139+
if !model.DisableWriting {
135140
err = skeUtils.WriteConfigFile(kubeconfigPath, kubeconfig)
136141
if err != nil {
137142
return fmt.Errorf("write kubeconfig file: %w", err)
@@ -149,6 +154,7 @@ func configureFlags(cmd *cobra.Command) {
149154
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.")
150155
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")
151156
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.")
157+
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))
152158

153159
cmd.MarkFlagsMutuallyExclusive(loginFlag, expirationFlag)
154160
}
@@ -174,12 +180,21 @@ func parseInput(p *print.Printer, cmd *cobra.Command, inputArgs []string) (*inpu
174180
}
175181
}
176182

183+
disableWriting := flags.FlagToBoolValue(p, cmd, disableWritingFlag)
184+
185+
isInvalidOutputFormat := globalFlags.OutputFormat == "" || globalFlags.OutputFormat == print.NoneOutputFormat || globalFlags.OutputFormat == print.PrettyOutputFormat
186+
if disableWriting && isInvalidOutputFormat {
187+
return nil, fmt.Errorf("when setting the flag --%s, you must specify --%s as one of the values: %s",
188+
disableWritingFlag, globalflags.OutputFormatFlag, fmt.Sprintf("%s, %s", print.JSONOutputFormat, print.YAMLOutputFormat))
189+
}
190+
177191
model := inputModel{
178192
GlobalFlagModel: globalFlags,
179193
ClusterName: clusterName,
180194
Filepath: flags.FlagToStringPointer(p, cmd, filepathFlag),
181195
ExpirationTime: expTime,
182196
Login: flags.FlagToBoolValue(p, cmd, loginFlag),
197+
DisableWriting: disableWriting,
183198
}
184199

185200
if p.IsVerbosityDebug() {

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,27 @@ func TestParseInput(t *testing.T) {
156156
}),
157157
isValid: false,
158158
},
159+
{
160+
description: "disable writing and invalid output format",
161+
argValues: fixtureArgValues(),
162+
flagValues: fixtureFlagValues(func(flagValues map[string]string) {
163+
flagValues[disableWritingFlag] = "true"
164+
}),
165+
isValid: false,
166+
},
167+
{
168+
description: "disable writing and valid output format",
169+
argValues: fixtureArgValues(),
170+
flagValues: fixtureFlagValues(func(flagValues map[string]string) {
171+
flagValues[disableWritingFlag] = "true"
172+
flagValues[globalflags.OutputFormatFlag] = print.YAMLOutputFormat
173+
}),
174+
expectedModel: fixtureInputModel(func(model *inputModel) {
175+
model.DisableWriting = true
176+
model.OutputFormat = print.YAMLOutputFormat
177+
}),
178+
isValid: true,
179+
},
159180
}
160181

161182
for _, tt := range tests {

0 commit comments

Comments
 (0)