|
| 1 | +package create |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + "github.com/stackitcloud/stackit-cli/internal/pkg/args" |
| 8 | + "github.com/stackitcloud/stackit-cli/internal/pkg/confirm" |
| 9 | + "github.com/stackitcloud/stackit-cli/internal/pkg/errors" |
| 10 | + "github.com/stackitcloud/stackit-cli/internal/pkg/examples" |
| 11 | + "github.com/stackitcloud/stackit-cli/internal/pkg/flags" |
| 12 | + "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" |
| 13 | + "github.com/stackitcloud/stackit-cli/internal/pkg/services/ske/client" |
| 14 | + skeUtils "github.com/stackitcloud/stackit-cli/internal/pkg/services/ske/utils" |
| 15 | + |
| 16 | + "github.com/spf13/cobra" |
| 17 | + "github.com/stackitcloud/stackit-sdk-go/services/ske" |
| 18 | +) |
| 19 | + |
| 20 | +const ( |
| 21 | + clusterNameArg = "CLUSTER_NAME" |
| 22 | + |
| 23 | + expirationFlag = "expiration" |
| 24 | + locationFlag = "location" |
| 25 | +) |
| 26 | + |
| 27 | +type inputModel struct { |
| 28 | + *globalflags.GlobalFlagModel |
| 29 | + ClusterName string |
| 30 | + Location *string |
| 31 | + ExpirationTime *string |
| 32 | +} |
| 33 | + |
| 34 | +func NewCmd() *cobra.Command { |
| 35 | + cmd := &cobra.Command{ |
| 36 | + Use: fmt.Sprintf("create %s", clusterNameArg), |
| 37 | + Short: "Creates a kubeconfig for an SKE cluster", |
| 38 | + Long: fmt.Sprintf("%s\n%s", |
| 39 | + "Creates a kubeconfig for a STACKIT Kubernetes Engine (SKE) cluster.", |
| 40 | + "By default the kubeconfig is created in the .kube folder, in the user's home directory. The kubeconfig file will be overwritten if it already exists."), |
| 41 | + Args: args.SingleArg(clusterNameArg, nil), |
| 42 | + Example: examples.Build( |
| 43 | + examples.NewExample( |
| 44 | + `Create a kubeconfig for the SKE cluster with name "my-cluster"`, |
| 45 | + "$ stackit ske kubeconfig create my-cluster"), |
| 46 | + examples.NewExample( |
| 47 | + `Create a kubeconfig for the SKE cluster with name "my-cluster" and set the expiration time to 30 days`, |
| 48 | + "$ stackit ske kubeconfig create my-cluster --expiration 30d"), |
| 49 | + examples.NewExample( |
| 50 | + `Create a kubeconfig for the SKE cluster with name "my-cluster" and set the expiration time to 2 months`, |
| 51 | + "$ stackit ske kubeconfig create my-cluster --expiration 2M"), |
| 52 | + examples.NewExample( |
| 53 | + `Create a kubeconfig for the SKE cluster with name "my-cluster" in a custom location`, |
| 54 | + "$ stackit ske kubeconfig create my-cluster --location /path/to/config"), |
| 55 | + ), |
| 56 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 57 | + ctx := context.Background() |
| 58 | + model, err := parseInput(cmd, args) |
| 59 | + if err != nil { |
| 60 | + return err |
| 61 | + } |
| 62 | + |
| 63 | + // Configure API client |
| 64 | + apiClient, err := client.ConfigureClient(cmd) |
| 65 | + if err != nil { |
| 66 | + return err |
| 67 | + } |
| 68 | + |
| 69 | + if !model.AssumeYes { |
| 70 | + prompt := fmt.Sprintf("Are you sure you want to create a kubeconfig for SKE cluster %q? This will OVERWRITE your current configuration, if it exists.", model.ClusterName) |
| 71 | + err = confirm.PromptForConfirmation(cmd, prompt) |
| 72 | + if err != nil { |
| 73 | + return err |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + // Call API |
| 78 | + req, err := buildRequest(ctx, model, apiClient) |
| 79 | + if err != nil { |
| 80 | + return fmt.Errorf("build kubeconfig create request: %w", err) |
| 81 | + } |
| 82 | + resp, err := req.Execute() |
| 83 | + if err != nil { |
| 84 | + return fmt.Errorf("create kubeconfig for SKE cluster: %w", err) |
| 85 | + } |
| 86 | + |
| 87 | + // Create the config file |
| 88 | + if resp.Kubeconfig == nil { |
| 89 | + return fmt.Errorf("no kubeconfig returned from the API") |
| 90 | + } |
| 91 | + |
| 92 | + var kubeconfigPath string |
| 93 | + if model.Location == nil { |
| 94 | + kubeconfigPath, err = skeUtils.GetDefaultKubeconfigLocation() |
| 95 | + if err != nil { |
| 96 | + return fmt.Errorf("get default kubeconfig location: %w", err) |
| 97 | + } |
| 98 | + } else { |
| 99 | + kubeconfigPath = *model.Location |
| 100 | + } |
| 101 | + |
| 102 | + err = skeUtils.WriteConfigFile(kubeconfigPath, *resp.Kubeconfig) |
| 103 | + if err != nil { |
| 104 | + return fmt.Errorf("write kubeconfig file: %w", err) |
| 105 | + } |
| 106 | + |
| 107 | + fmt.Printf("Created kubeconfig file for cluster %s in %q, with expiration date %v (UTC)\n", model.ClusterName, kubeconfigPath, *resp.ExpirationTimestamp) |
| 108 | + |
| 109 | + return nil |
| 110 | + }, |
| 111 | + } |
| 112 | + configureFlags(cmd) |
| 113 | + return cmd |
| 114 | +} |
| 115 | + |
| 116 | +func configureFlags(cmd *cobra.Command) { |
| 117 | + 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") |
| 118 | + cmd.Flags().String(locationFlag, "", "Folder location to store the kubeconfig file. By default, the kubeconfig is created in the .kube folder, in the user's home directory.") |
| 119 | +} |
| 120 | + |
| 121 | +func parseInput(cmd *cobra.Command, inputArgs []string) (*inputModel, error) { |
| 122 | + clusterName := inputArgs[0] |
| 123 | + |
| 124 | + globalFlags := globalflags.Parse(cmd) |
| 125 | + if globalFlags.ProjectId == "" { |
| 126 | + return nil, &errors.ProjectIdError{} |
| 127 | + } |
| 128 | + |
| 129 | + return &inputModel{ |
| 130 | + GlobalFlagModel: globalFlags, |
| 131 | + ClusterName: clusterName, |
| 132 | + Location: flags.FlagToStringPointer(cmd, locationFlag), |
| 133 | + ExpirationTime: flags.FlagToStringPointer(cmd, expirationFlag), |
| 134 | + }, nil |
| 135 | +} |
| 136 | + |
| 137 | +func buildRequest(ctx context.Context, model *inputModel, apiClient *ske.APIClient) (ske.ApiCreateKubeconfigRequest, error) { |
| 138 | + req := apiClient.CreateKubeconfig(ctx, model.ProjectId, model.ClusterName) |
| 139 | + |
| 140 | + payload := ske.CreateKubeconfigPayload{} |
| 141 | + |
| 142 | + if model.ExpirationTime != nil { |
| 143 | + expirationTime, err := skeUtils.ConvertToSeconds(*model.ExpirationTime) |
| 144 | + if err != nil { |
| 145 | + return req, fmt.Errorf("parse expiration time: %w", err) |
| 146 | + } |
| 147 | + |
| 148 | + payload.ExpirationSeconds = expirationTime |
| 149 | + } |
| 150 | + |
| 151 | + return req.CreateKubeconfigPayload(payload), nil |
| 152 | +} |
0 commit comments