-
Notifications
You must be signed in to change notification settings - Fork 40
SKE Credentials Rotation: start-rotation command #179
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
Merged
DiogoFerrao
merged 5 commits into
feature-ske-new-credentials-rotation
from
df/ske-start-credentials-rotation
Apr 1, 2024
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
2c38837
initial start rotation command implementation
DiogoFerrao b678d20
update go mod, rename files, generate docs
DiogoFerrao 726110b
start-rotation update long description
DiogoFerrao d713f64
Improve start-rotation documentation
DiogoFerrao c1f9ffe
Update internal/cmd/ske/credentials/start-rotation/start_rotation.go
DiogoFerrao File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| ## stackit ske credentials start-rotation | ||
|
|
||
| Starts the rotation of the credentials associated to a SKE cluster | ||
|
|
||
| ### Synopsis | ||
|
|
||
| Starts the rotation of the credentials associated to a STACKIT Kubernetes Engine (SKE) cluster. This is step 1 of a two-step process. | ||
| Complete the rotation using the 'stackit ske credentials complete-rotation' command. | ||
|
|
||
| ``` | ||
| stackit ske credentials start-rotation CLUSTER_NAME [flags] | ||
| ``` | ||
|
|
||
| ### Examples | ||
|
|
||
| ``` | ||
| Start the rotation of the credentials associated to the SKE cluster with name "my-cluster" | ||
| $ stackit ske credentials start-rotation my-cluster | ||
| ``` | ||
|
|
||
| ### Options | ||
|
|
||
| ``` | ||
| -h, --help Help for "stackit ske credentials start-rotation" | ||
| ``` | ||
|
|
||
| ### Options inherited from parent commands | ||
|
|
||
| ``` | ||
| -y, --assume-yes If set, skips all confirmation prompts | ||
| --async If set, runs the command asynchronously | ||
| -o, --output-format string Output format, one of ["json" "pretty"] | ||
| -p, --project-id string Project ID | ||
| ``` | ||
|
|
||
| ### SEE ALSO | ||
|
|
||
| * [stackit ske credentials](./stackit_ske_credentials.md) - Provides functionality for SKE credentials | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
118 changes: 118 additions & 0 deletions
118
internal/cmd/ske/credentials/start-rotation/start_rotation.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,118 @@ | ||
| package startrotation | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
|
|
||
| "github.com/stackitcloud/stackit-cli/internal/pkg/args" | ||
| "github.com/stackitcloud/stackit-cli/internal/pkg/confirm" | ||
| "github.com/stackitcloud/stackit-cli/internal/pkg/errors" | ||
| "github.com/stackitcloud/stackit-cli/internal/pkg/examples" | ||
| "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" | ||
| "github.com/stackitcloud/stackit-cli/internal/pkg/services/ske/client" | ||
| "github.com/stackitcloud/stackit-cli/internal/pkg/spinner" | ||
|
|
||
| "github.com/spf13/cobra" | ||
| "github.com/stackitcloud/stackit-sdk-go/services/ske" | ||
| "github.com/stackitcloud/stackit-sdk-go/services/ske/wait" | ||
| ) | ||
|
|
||
| const ( | ||
| clusterNameArg = "CLUSTER_NAME" | ||
| ) | ||
|
|
||
| type inputModel struct { | ||
| *globalflags.GlobalFlagModel | ||
| ClusterName string | ||
| } | ||
|
|
||
| func NewCmd() *cobra.Command { | ||
| cmd := &cobra.Command{ | ||
| Use: fmt.Sprintf("start-rotation %s", clusterNameArg), | ||
| Short: "Starts the rotation of the credentials associated to a SKE cluster", | ||
| Long: fmt.Sprintf("%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n\n%s\n%s", | ||
| "Starts the rotation of the credentials associated to a STACKIT Kubernetes Engine (SKE) cluster.", | ||
| "This is step 1 of a 2-step process to rotate all SKE cluster credentials. Tasks accomplished in this phase include:", | ||
| " - Rolling recreation of all worker nodes", | ||
| " - A new Certificate Authority (CA) will be established and incorporated into the existing CA bundle.", | ||
| " - A new etcd encryption key is generated and added to the Certificate Authority (CA) bundle.", | ||
| " - A new signing key will be generated for the service account and added to the Certificate Authority (CA) bundle.", | ||
| " - The kube-apiserver will rewrite all secrets in the cluster, encrypting them with the new encryption key.", | ||
| "The old CA, encryption key and signing key will be retained until the rotation is completed.", | ||
| "Complete the rotation by running:", | ||
| " $ stackit ske credentials complete-rotation my-cluster"), | ||
| Args: args.SingleArg(clusterNameArg, nil), | ||
| Example: examples.Build( | ||
| examples.NewExample( | ||
| `Start the rotation of the credentials associated to the SKE cluster with name "my-cluster"`, | ||
| "$ stackit ske credentials start-rotation my-cluster"), | ||
| ), | ||
| RunE: func(cmd *cobra.Command, args []string) error { | ||
| ctx := context.Background() | ||
| model, err := parseInput(cmd, args) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| // Configure API client | ||
| apiClient, err := client.ConfigureClient(cmd) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| if !model.AssumeYes { | ||
| prompt := fmt.Sprintf("Are you sure you want to start the rotation of the credentials for SKE cluster %q?", model.ClusterName) | ||
| err = confirm.PromptForConfirmation(cmd, prompt) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| } | ||
|
|
||
| // Call API | ||
| req := buildRequest(ctx, model, apiClient) | ||
| _, err = req.Execute() | ||
| if err != nil { | ||
| return fmt.Errorf("start rotation of SKE credentials: %w", err) | ||
| } | ||
|
|
||
| // Wait for async operation, if async mode not enabled | ||
| if !model.Async { | ||
| s := spinner.New(cmd) | ||
| s.Start("Starting credentials rotation") | ||
| _, err = wait.StartCredentialsRotationWaitHandler(ctx, apiClient, model.ProjectId, model.ClusterName).WaitWithContext(ctx) | ||
| if err != nil { | ||
| return fmt.Errorf("wait for start SKE credentials rotation %w", err) | ||
| } | ||
| s.Stop() | ||
| } | ||
|
|
||
| operationState := "Rotation of credentials is ready to be completed" | ||
| if model.Async { | ||
| operationState = "Triggered start of credentials rotation" | ||
| } | ||
| cmd.Printf("%s for cluster %q\n", operationState, model.ClusterName) | ||
| cmd.Printf("Complete the rotation by running:\n $ stackit ske credentials complete-rotation %s\n", model.ClusterName) | ||
| return nil | ||
| }, | ||
| } | ||
| return cmd | ||
| } | ||
|
|
||
| func parseInput(cmd *cobra.Command, inputArgs []string) (*inputModel, error) { | ||
| clusterName := inputArgs[0] | ||
|
|
||
| globalFlags := globalflags.Parse(cmd) | ||
| if globalFlags.ProjectId == "" { | ||
| return nil, &errors.ProjectIdError{} | ||
| } | ||
|
|
||
| return &inputModel{ | ||
| GlobalFlagModel: globalFlags, | ||
| ClusterName: clusterName, | ||
| }, nil | ||
| } | ||
|
|
||
| func buildRequest(ctx context.Context, model *inputModel, apiClient *ske.APIClient) ske.ApiStartCredentialsRotationRequest { | ||
| req := apiClient.StartCredentialsRotation(ctx, model.ProjectId, model.ClusterName) | ||
| return req | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.