Skip to content

Commit 135d338

Browse files
authored
SKE Credentials Rotation: start-rotation command (#179)
* initial start rotation command implementation * update go mod, rename files, generate docs * start-rotation update long description * Improve start-rotation documentation
1 parent e9135e9 commit 135d338

8 files changed

Lines changed: 367 additions & 4 deletions

File tree

docs/stackit_postgresflex_instance_clone.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ stackit postgresflex instance clone INSTANCE_ID [flags]
2727

2828
```
2929
-h, --help Help for "stackit postgresflex instance clone"
30-
--recovery-timestamp string Recovery timestamp for the instance, specified in UTC time following the format, e.g. 2024-03-12T09:28:00+00:00
30+
--recovery-timestamp string Recovery timestamp for the instance, in a date-time with the RFC3339 layout format, e.g. 2024-01-01T00:00:00Z
3131
--storage-class string Storage class. If not specified, storage class from the existing instance will be used.
3232
--storage-size int Storage size (in GB). If not specified, storage size from the existing instance will be used.
3333
```

docs/stackit_ske_credentials.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,5 @@ stackit ske credentials [flags]
3030
* [stackit ske](./stackit_ske.md) - Provides functionality for SKE
3131
* [stackit ske credentials describe](./stackit_ske_credentials_describe.md) - Shows details of the credentials associated to a SKE cluster
3232
* [stackit ske credentials rotate](./stackit_ske_credentials_rotate.md) - Rotates credentials associated to a SKE cluster
33+
* [stackit ske credentials start-rotation](./stackit_ske_credentials_start-rotation.md) - Starts the rotation of the credentials associated to a SKE cluster
3334

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
## stackit ske credentials start-rotation
2+
3+
Starts the rotation of the credentials associated to a SKE cluster
4+
5+
### Synopsis
6+
7+
Starts the rotation of the credentials associated to a STACKIT Kubernetes Engine (SKE) cluster. This is step 1 of a two-step process.
8+
Complete the rotation using the 'stackit ske credentials complete-rotation' command.
9+
10+
```
11+
stackit ske credentials start-rotation CLUSTER_NAME [flags]
12+
```
13+
14+
### Examples
15+
16+
```
17+
Start the rotation of the credentials associated to the SKE cluster with name "my-cluster"
18+
$ stackit ske credentials start-rotation my-cluster
19+
```
20+
21+
### Options
22+
23+
```
24+
-h, --help Help for "stackit ske credentials start-rotation"
25+
```
26+
27+
### Options inherited from parent commands
28+
29+
```
30+
-y, --assume-yes If set, skips all confirmation prompts
31+
--async If set, runs the command asynchronously
32+
-o, --output-format string Output format, one of ["json" "pretty"]
33+
-p, --project-id string Project ID
34+
```
35+
36+
### SEE ALSO
37+
38+
* [stackit ske credentials](./stackit_ske_credentials.md) - Provides functionality for SKE credentials
39+

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ require (
1919
github.com/stackitcloud/stackit-sdk-go/services/resourcemanager v0.7.7
2020
github.com/stackitcloud/stackit-sdk-go/services/secretsmanager v0.6.0
2121
github.com/stackitcloud/stackit-sdk-go/services/serviceaccount v0.3.6
22-
github.com/stackitcloud/stackit-sdk-go/services/ske v0.10.1
22+
github.com/stackitcloud/stackit-sdk-go/services/ske v0.11.0
2323
github.com/zalando/go-keyring v0.2.4
2424
golang.org/x/mod v0.16.0
2525
golang.org/x/oauth2 v0.18.0

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,8 @@ github.com/stackitcloud/stackit-sdk-go/services/secretsmanager v0.6.0 h1:VC7VWad
103103
github.com/stackitcloud/stackit-sdk-go/services/secretsmanager v0.6.0/go.mod h1:KRoLXZdH8yuO6FBu2Grl5VGqW9arH03qYAC0P6H8h9o=
104104
github.com/stackitcloud/stackit-sdk-go/services/serviceaccount v0.3.6 h1:3kkNh2kHi55w9dgh0MC1Zbn8fDpYxcXl3tvYjH8t9xo=
105105
github.com/stackitcloud/stackit-sdk-go/services/serviceaccount v0.3.6/go.mod h1:OOciROyQxPOYLo8OM/DE5ESH11+DvAyRt6wg7R+HVkg=
106-
github.com/stackitcloud/stackit-sdk-go/services/ske v0.10.1 h1:MZABtJ8HFOKG3KCCv5duibxBSAU1zTFAO0V9bso3N9M=
107-
github.com/stackitcloud/stackit-sdk-go/services/ske v0.10.1/go.mod h1:7M7bsVHN0REuwoZRYz5nK2yBwsMJcHTsVFHlG83QP2A=
106+
github.com/stackitcloud/stackit-sdk-go/services/ske v0.11.0 h1:BJ1Op7f3KJPNROkEXzqAREl55JCqyIAyQJ+Gfu4LYCM=
107+
github.com/stackitcloud/stackit-sdk-go/services/ske v0.11.0/go.mod h1:yFLjTx58pjHCp0KZTaqHlW9Qk60CY5HpnBWR/zztv8Y=
108108
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
109109
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
110110
github.com/stretchr/objx v0.5.0 h1:1zr/of2m5FGMsad5YfcqgdqdWrIhu+EBEJRhR1U7z/c=

internal/cmd/ske/credentials/credentials.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package credentials
33
import (
44
"github.com/stackitcloud/stackit-cli/internal/cmd/ske/credentials/describe"
55
"github.com/stackitcloud/stackit-cli/internal/cmd/ske/credentials/rotate"
6+
startrotation "github.com/stackitcloud/stackit-cli/internal/cmd/ske/credentials/start-rotation"
67
"github.com/stackitcloud/stackit-cli/internal/pkg/args"
78
"github.com/stackitcloud/stackit-cli/internal/pkg/utils"
89

@@ -24,4 +25,5 @@ func NewCmd() *cobra.Command {
2425
func addSubcommands(cmd *cobra.Command) {
2526
cmd.AddCommand(describe.NewCmd())
2627
cmd.AddCommand(rotate.NewCmd())
28+
cmd.AddCommand(startrotation.NewCmd())
2729
}
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
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

Comments
 (0)