Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Kubeconfig not written when output format is json #553

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions docs/stackit_ske_kubeconfig_create.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,15 @@ stackit ske kubeconfig create CLUSTER_NAME [flags]
Create a kubeconfig for the SKE cluster with name "my-cluster" in a custom filepath
$ stackit ske kubeconfig create my-cluster --filepath /path/to/config
Get a kubeconfig for the SKE cluster with name "my-cluster" without writing it to a file.
```

### Options

```
--disable-writing Disable writing to the kubeconfig file.
-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
--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.
-h, --help Help for "stackit ske kubeconfig create"
Expand Down
25 changes: 20 additions & 5 deletions internal/cmd/ske/kubeconfig/create/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@ import (
const (
clusterNameArg = "CLUSTER_NAME"

loginFlag = "login"
expirationFlag = "expiration"
filepathFlag = "filepath"
loginFlag = "login"
expirationFlag = "expiration"
filepathFlag = "filepath"
disableWritingFlag = "disable-writing"
)

type inputModel struct {
Expand All @@ -33,6 +34,7 @@ type inputModel struct {
Filepath *string
ExpirationTime *string
Login bool
DisableWriting bool
}

func NewCmd(p *print.Printer) *cobra.Command {
Expand Down Expand Up @@ -63,6 +65,9 @@ func NewCmd(p *print.Printer) *cobra.Command {
examples.NewExample(
`Create a kubeconfig for the SKE cluster with name "my-cluster" in a custom filepath`,
"$ stackit ske kubeconfig create my-cluster --filepath /path/to/config"),
examples.NewExample(
`Get a kubeconfig for the SKE cluster with name "my-cluster" without writing it to a file. `,
""),
marceljk marked this conversation as resolved.
Show resolved Hide resolved
),
RunE: func(cmd *cobra.Command, args []string) error {
ctx := context.Background()
Expand All @@ -77,7 +82,7 @@ func NewCmd(p *print.Printer) *cobra.Command {
return err
}

if !model.AssumeYes {
if !model.AssumeYes && !model.DisableWriting {
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)
err = p.PromptForConfirmation(prompt)
if err != nil {
Expand Down Expand Up @@ -131,7 +136,7 @@ func NewCmd(p *print.Printer) *cobra.Command {
kubeconfigPath = *model.Filepath
}

if model.OutputFormat != print.JSONOutputFormat {
if !model.DisableWriting {
err = skeUtils.WriteConfigFile(kubeconfigPath, kubeconfig)
if err != nil {
return fmt.Errorf("write kubeconfig file: %w", err)
Expand All @@ -149,6 +154,7 @@ func configureFlags(cmd *cobra.Command) {
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.")
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")
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.")
cmd.Flags().Bool(disableWritingFlag, false, fmt.Sprintf("Disable the writing of kubeconfig. Add --%s to display the kubeconfig.", globalflags.OutputFormatFlag))
marceljk marked this conversation as resolved.
Show resolved Hide resolved

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

disableWriting := flags.FlagToBoolValue(p, cmd, disableWritingFlag)

isDefaultOutputFormat := globalFlags.OutputFormat != print.JSONOutputFormat && globalFlags.OutputFormat != print.YAMLOutputFormat
marceljk marked this conversation as resolved.
Show resolved Hide resolved
if disableWriting && isDefaultOutputFormat {
return nil, fmt.Errorf("when setting the flag --%s, you must specify --%s as one of the values: %s",
disableWritingFlag, globalflags.OutputFormatFlag, fmt.Sprintf("%s, %s", print.JSONOutputFormat, print.YAMLOutputFormat))
}

model := inputModel{
GlobalFlagModel: globalFlags,
ClusterName: clusterName,
Filepath: flags.FlagToStringPointer(p, cmd, filepathFlag),
ExpirationTime: expTime,
Login: flags.FlagToBoolValue(p, cmd, loginFlag),
DisableWriting: disableWriting,
}

if p.IsVerbosityDebug() {
Expand Down
21 changes: 21 additions & 0 deletions internal/cmd/ske/kubeconfig/create/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,27 @@ func TestParseInput(t *testing.T) {
}),
isValid: false,
},
{
description: "disable writing and invalid output format",
argValues: fixtureArgValues(),
flagValues: fixtureFlagValues(func(flagValues map[string]string) {
flagValues[disableWritingFlag] = "true"
}),
isValid: false,
},
{
description: "disable writing and valid output format",
argValues: fixtureArgValues(),
flagValues: fixtureFlagValues(func(flagValues map[string]string) {
flagValues[disableWritingFlag] = "true"
flagValues[globalflags.OutputFormatFlag] = print.YAMLOutputFormat
}),
expectedModel: fixtureInputModel(func(model *inputModel) {
model.DisableWriting = true
model.OutputFormat = print.YAMLOutputFormat
}),
isValid: true,
},
}

for _, tt := range tests {
Expand Down
Loading