|
| 1 | +package startrotation |
| 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/globalflags" |
| 12 | + "github.com/stackitcloud/stackit-cli/internal/pkg/services/ske/client" |
| 13 | + "github.com/stackitcloud/stackit-cli/internal/pkg/spinner" |
| 14 | + |
| 15 | + "github.com/spf13/cobra" |
| 16 | + "github.com/stackitcloud/stackit-sdk-go/services/ske" |
| 17 | + "github.com/stackitcloud/stackit-sdk-go/services/ske/wait" |
| 18 | +) |
| 19 | + |
| 20 | +const ( |
| 21 | + clusterNameArg = "CLUSTER_NAME" |
| 22 | +) |
| 23 | + |
| 24 | +type inputModel struct { |
| 25 | + *globalflags.GlobalFlagModel |
| 26 | + ClusterName string |
| 27 | +} |
| 28 | + |
| 29 | +func NewCmd() *cobra.Command { |
| 30 | + cmd := &cobra.Command{ |
| 31 | + Use: fmt.Sprintf("start-rotation %s", clusterNameArg), |
| 32 | + Short: "Starts the rotation of the credentials associated to a SKE cluster", |
| 33 | + Long: fmt.Sprintf("%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n\n%s\n%s", |
| 34 | + "Starts the rotation of the credentials associated to a STACKIT Kubernetes Engine (SKE) cluster.", |
| 35 | + "This is step 1 of a 2-step process to rotate all SKE cluster credentials. Tasks accomplished in this phase include:", |
| 36 | + " - Rolling recreation of all worker nodes", |
| 37 | + " - A new Certificate Authority (CA) will be established and incorporated into the existing CA bundle.", |
| 38 | + " - A new etcd encryption key is generated and added to the Certificate Authority (CA) bundle.", |
| 39 | + " - A new signing key will be generated for the service account and added to the Certificate Authority (CA) bundle.", |
| 40 | + " - The kube-apiserver will rewrite all secrets in the cluster, encrypting them with the new encryption key.", |
| 41 | + "The old CA, encryption key and signing key will be retained until the rotation is completed.", |
| 42 | + "Complete the rotation by running:", |
| 43 | + " $ stackit ske credentials complete-rotation my-cluster"), |
| 44 | + Args: args.SingleArg(clusterNameArg, nil), |
| 45 | + Example: examples.Build( |
| 46 | + examples.NewExample( |
| 47 | + `Start the rotation of the credentials associated to the SKE cluster with name "my-cluster"`, |
| 48 | + "$ stackit ske credentials start-rotation my-cluster"), |
| 49 | + ), |
| 50 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 51 | + ctx := context.Background() |
| 52 | + model, err := parseInput(cmd, args) |
| 53 | + if err != nil { |
| 54 | + return err |
| 55 | + } |
| 56 | + |
| 57 | + // Configure API client |
| 58 | + apiClient, err := client.ConfigureClient(cmd) |
| 59 | + if err != nil { |
| 60 | + return err |
| 61 | + } |
| 62 | + |
| 63 | + if !model.AssumeYes { |
| 64 | + prompt := fmt.Sprintf("Are you sure you want to start the rotation of the credentials for SKE cluster %q?", model.ClusterName) |
| 65 | + err = confirm.PromptForConfirmation(cmd, prompt) |
| 66 | + if err != nil { |
| 67 | + return err |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + // Call API |
| 72 | + req := buildRequest(ctx, model, apiClient) |
| 73 | + _, err = req.Execute() |
| 74 | + if err != nil { |
| 75 | + return fmt.Errorf("start rotation of SKE credentials: %w", err) |
| 76 | + } |
| 77 | + |
| 78 | + // Wait for async operation, if async mode not enabled |
| 79 | + if !model.Async { |
| 80 | + s := spinner.New(cmd) |
| 81 | + s.Start("Starting credentials rotation") |
| 82 | + _, err = wait.StartCredentialsRotationWaitHandler(ctx, apiClient, model.ProjectId, model.ClusterName).WaitWithContext(ctx) |
| 83 | + if err != nil { |
| 84 | + return fmt.Errorf("wait for start SKE credentials rotation %w", err) |
| 85 | + } |
| 86 | + s.Stop() |
| 87 | + } |
| 88 | + |
| 89 | + operationState := "Rotation of credentials is ready to be completed" |
| 90 | + if model.Async { |
| 91 | + operationState = "Triggered start of credentials rotation" |
| 92 | + } |
| 93 | + cmd.Printf("%s for cluster %q\n", operationState, model.ClusterName) |
| 94 | + cmd.Printf("Complete the rotation by running:\n $ stackit ske credentials complete-rotation %s\n", model.ClusterName) |
| 95 | + return nil |
| 96 | + }, |
| 97 | + } |
| 98 | + return cmd |
| 99 | +} |
| 100 | + |
| 101 | +func parseInput(cmd *cobra.Command, inputArgs []string) (*inputModel, error) { |
| 102 | + clusterName := inputArgs[0] |
| 103 | + |
| 104 | + globalFlags := globalflags.Parse(cmd) |
| 105 | + if globalFlags.ProjectId == "" { |
| 106 | + return nil, &errors.ProjectIdError{} |
| 107 | + } |
| 108 | + |
| 109 | + return &inputModel{ |
| 110 | + GlobalFlagModel: globalFlags, |
| 111 | + ClusterName: clusterName, |
| 112 | + }, nil |
| 113 | +} |
| 114 | + |
| 115 | +func buildRequest(ctx context.Context, model *inputModel, apiClient *ske.APIClient) ske.ApiStartCredentialsRotationRequest { |
| 116 | + req := apiClient.StartCredentialsRotation(ctx, model.ProjectId, model.ClusterName) |
| 117 | + return req |
| 118 | +} |
0 commit comments