Skip to content

Commit 6ee1d50

Browse files
committed
review changes
Signed-off-by: Mauritz Uphoff <mauritz.uphoff@stackit.cloud>
1 parent ac521c3 commit 6ee1d50

3 files changed

Lines changed: 27 additions & 61 deletions

File tree

docs/ephemeral-resources/ske_kubeconfig.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ provider "kubernetes" {
4343

4444
### Optional
4545

46-
- `expiration` (Number) Expiration time of the kubeconfig in seconds. Must be between `600` (10m) and `14400` (4h). Defaults to `1800` (30m) for optimal security during Terraform operations, which is more restrictive than the API default of `3600` (1h).
46+
- `expiration` (Number) Expiration time of the kubeconfig in seconds. Must be between `600` (10m) and `14400` (4h). API defaults to `3600` (1h).
4747
- `region` (String) The resource region. If not defined, the provider region is used.
4848

4949
### Read-Only

stackit/internal/services/ske/kubeconfig/ephemeral_resource.go

Lines changed: 7 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,6 @@ import (
2020
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/validate"
2121
)
2222

23-
const (
24-
defaultKubeconfigExpiration = 1800
25-
)
26-
2723
// Ensure the implementation satisfies the expected interfaces.
2824
var (
2925
_ ephemeral.EphemeralResource = &kubeconfigEphemeralResource{}
@@ -94,7 +90,7 @@ func (e *kubeconfigEphemeralResource) Schema(_ context.Context, _ ephemeral.Sche
9490
},
9591
"expiration": schema.Int64Attribute{
9692
Description: "Expiration time of the kubeconfig in seconds. Must be between `600` (10m) and `14400` (4h). " +
97-
"Defaults to `1800` (30m) for optimal security during Terraform operations, which is more restrictive than the API default of `3600` (1h).",
93+
"API defaults to `3600` (1h).",
9894
Optional: true,
9995
Validators: []validator.Int64{
10096
int64validator.AtLeast(600),
@@ -135,15 +131,7 @@ func (e *kubeconfigEphemeralResource) Open(ctx context.Context, req ephemeral.Op
135131
clusterName := model.ClusterName.ValueString()
136132
region := e.providerData.GetRegionWithOverride(model.Region)
137133

138-
// Kubeconfig only needs to be valid for the duration of the Terraform operation.
139-
// Defaulted to 1800s (30m) for better security than the API default (3600s).
140-
expiration := conversion.Int64ValueToPointer(model.Expiration)
141-
if expiration == nil {
142-
expiration = new(int64)
143-
*expiration = defaultKubeconfigExpiration
144-
}
145-
146-
kubeconfigResp, err := getKubeconfig(ctx, e.client, projectId, region, clusterName, expiration)
134+
kubeconfigResp, err := getKubeconfig(ctx, e.client.DefaultAPI, projectId, region, clusterName, conversion.Int64ValueToPointer(model.Expiration))
147135

148136
ctx = core.LogResponse(ctx)
149137

@@ -166,16 +154,12 @@ func (e *kubeconfigEphemeralResource) Open(ctx context.Context, req ephemeral.Op
166154
}
167155

168156
// getKubeconfig initializes the API call to generate a new kubeconfig
169-
func getKubeconfig(ctx context.Context, client *ske.APIClient, projectId, region, clusterName string, expiration *int64) (*ske.Kubeconfig, error) {
170-
var expirationStringPtr *string
157+
func getKubeconfig(ctx context.Context, client ske.DefaultAPI, projectId, region, clusterName string, expiration *int64) (*ske.Kubeconfig, error) {
158+
payload := ske.CreateKubeconfigPayload{}
171159
if expiration != nil {
172-
expirationStringPtr = new(string)
173-
*expirationStringPtr = strconv.FormatInt(*expiration, 10)
174-
}
175-
176-
payload := ske.CreateKubeconfigPayload{
177-
ExpirationSeconds: expirationStringPtr,
160+
expirationStr := strconv.FormatInt(*expiration, 10)
161+
payload.ExpirationSeconds = &expirationStr
178162
}
179163

180-
return client.DefaultAPI.CreateKubeconfig(ctx, projectId, region, clusterName).CreateKubeconfigPayload(payload).Execute()
164+
return client.CreateKubeconfig(ctx, projectId, region, clusterName).CreateKubeconfigPayload(payload).Execute()
181165
}

stackit/internal/services/ske/kubeconfig/ephemeral_resource_test.go

Lines changed: 19 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,11 @@ package ske
22

33
import (
44
"context"
5-
"encoding/json"
65
"fmt"
7-
"net/http"
8-
"net/http/httptest"
96
"testing"
107
"time"
118

129
"github.com/google/go-cmp/cmp"
13-
"github.com/stackitcloud/stackit-sdk-go/core/config"
1410
ske "github.com/stackitcloud/stackit-sdk-go/services/ske/v2api"
1511
)
1612

@@ -24,22 +20,21 @@ func TestGetKubeconfig(t *testing.T) {
2420
expirationTime := time.Now().Add(time.Hour).Truncate(time.Second)
2521

2622
tests := []struct {
27-
description string
28-
expiration *int64
29-
mockResponse *ske.Kubeconfig
30-
mockStatusCode int
31-
expectError bool
23+
description string
24+
expiration *int64
25+
mockResponse *ske.Kubeconfig
26+
mockError error
27+
expectError bool
3228
}{
3329
{
34-
description: "success",
30+
description: "success without expiration",
3531
expiration: nil,
3632
mockResponse: &ske.Kubeconfig{
3733
Kubeconfig: &[]string{kubeconfig}[0],
3834
ExpirationTimestamp: &expirationTime,
3935
AdditionalProperties: make(map[string]any),
4036
},
41-
mockStatusCode: http.StatusOK,
42-
expectError: false,
37+
expectError: false,
4338
},
4439
{
4540
description: "success with expiration",
@@ -49,40 +44,27 @@ func TestGetKubeconfig(t *testing.T) {
4944
ExpirationTimestamp: &expirationTime,
5045
AdditionalProperties: make(map[string]any),
5146
},
52-
mockStatusCode: http.StatusOK,
53-
expectError: false,
47+
expectError: false,
5448
},
5549
{
56-
description: "api error",
57-
mockStatusCode: http.StatusInternalServerError,
58-
expectError: true,
50+
description: "api error",
51+
mockError: fmt.Errorf("api error"),
52+
expectError: true,
5953
},
6054
}
6155

6256
for _, tt := range tests {
6357
t.Run(tt.description, func(t *testing.T) {
64-
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
65-
expectedPath := fmt.Sprintf("/v2/projects/%s/regions/%s/clusters/%s/kubeconfig", projectId, region, clusterName)
66-
if r.URL.Path != expectedPath {
67-
t.Errorf("Expected path %s, got %s", expectedPath, r.URL.Path)
68-
}
69-
w.Header().Set("Content-Type", "application/json")
70-
w.WriteHeader(tt.mockStatusCode)
71-
if tt.mockResponse != nil {
72-
_ = json.NewEncoder(w).Encode(tt.mockResponse)
73-
}
74-
}))
75-
defer server.Close()
76-
77-
cfg, err := ske.NewAPIClient(
78-
config.WithEndpoint(server.URL),
79-
config.WithoutAuthentication(),
80-
)
81-
if err != nil {
82-
t.Fatalf("Failed to create SKE client: %v", err)
58+
mockResp := tt.mockResponse
59+
mockErr := tt.mockError
60+
createKubeconfigFn := func(_ ske.ApiCreateKubeconfigRequest) (*ske.Kubeconfig, error) {
61+
return mockResp, mockErr
62+
}
63+
client := &ske.DefaultAPIServiceMock{
64+
CreateKubeconfigExecuteMock: &createKubeconfigFn,
8365
}
8466

85-
resp, err := getKubeconfig(context.Background(), cfg, projectId, region, clusterName, tt.expiration)
67+
resp, err := getKubeconfig(context.Background(), client, projectId, region, clusterName, tt.expiration)
8668

8769
if (err != nil) != tt.expectError {
8870
t.Fatalf("getKubeconfig() error = %v, expectError %v", err, tt.expectError)

0 commit comments

Comments
 (0)