|
| 1 | +package ske |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "strconv" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/hashicorp/terraform-plugin-framework-validators/int64validator" |
| 10 | + "github.com/hashicorp/terraform-plugin-framework/ephemeral" |
| 11 | + "github.com/hashicorp/terraform-plugin-framework/ephemeral/schema" |
| 12 | + "github.com/hashicorp/terraform-plugin-framework/schema/validator" |
| 13 | + "github.com/hashicorp/terraform-plugin-framework/types" |
| 14 | + ske "github.com/stackitcloud/stackit-sdk-go/services/ske/v2api" |
| 15 | + "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/conversion" |
| 16 | + "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/core" |
| 17 | + "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/features" |
| 18 | + skeUtils "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/services/ske/utils" |
| 19 | + "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/validate" |
| 20 | +) |
| 21 | + |
| 22 | +const ( |
| 23 | + defaultKubeconfigExpiration = 1800 |
| 24 | +) |
| 25 | + |
| 26 | +// Ensure the implementation satisfies the expected interfaces. |
| 27 | +var ( |
| 28 | + _ ephemeral.EphemeralResource = &kubeconfigEphemeralResource{} |
| 29 | + _ ephemeral.EphemeralResourceWithConfigure = &kubeconfigEphemeralResource{} |
| 30 | +) |
| 31 | + |
| 32 | +// NewKubeconfigEphemeralResource is a helper function to simplify the provider implementation. |
| 33 | +func NewKubeconfigEphemeralResource() ephemeral.EphemeralResource { |
| 34 | + return &kubeconfigEphemeralResource{} |
| 35 | +} |
| 36 | + |
| 37 | +// kubeconfigEphemeralResource is the ephemeral resource implementation. |
| 38 | +type kubeconfigEphemeralResource struct { |
| 39 | + client *ske.APIClient |
| 40 | + providerData core.ProviderData |
| 41 | +} |
| 42 | + |
| 43 | +// Metadata returns the resource type name. |
| 44 | +func (e *kubeconfigEphemeralResource) Metadata(_ context.Context, req ephemeral.MetadataRequest, resp *ephemeral.MetadataResponse) { |
| 45 | + resp.TypeName = req.ProviderTypeName + "_ske_kubeconfig" |
| 46 | +} |
| 47 | + |
| 48 | +// Configure adds the provider configured client to the resource. |
| 49 | +func (e *kubeconfigEphemeralResource) Configure(ctx context.Context, req ephemeral.ConfigureRequest, resp *ephemeral.ConfigureResponse) { |
| 50 | + ephemeralProviderData, ok := conversion.ParseEphemeralProviderData(ctx, req.ProviderData, &resp.Diagnostics) |
| 51 | + if !ok { |
| 52 | + return |
| 53 | + } |
| 54 | + |
| 55 | + features.CheckBetaResourcesEnabled( |
| 56 | + ctx, |
| 57 | + &ephemeralProviderData.ProviderData, |
| 58 | + &resp.Diagnostics, |
| 59 | + "stackit_ske_kubeconfig", "ephemeral_resource", |
| 60 | + ) |
| 61 | + if resp.Diagnostics.HasError() { |
| 62 | + return |
| 63 | + } |
| 64 | + |
| 65 | + e.providerData = ephemeralProviderData.ProviderData |
| 66 | + e.client = skeUtils.ConfigureClient(ctx, &e.providerData, &resp.Diagnostics) |
| 67 | +} |
| 68 | + |
| 69 | +// ephemeralModel is the model for the ephemeral resource. |
| 70 | +type ephemeralModel struct { |
| 71 | + ClusterName types.String `tfsdk:"cluster_name"` |
| 72 | + ProjectId types.String `tfsdk:"project_id"` |
| 73 | + Expiration types.Int64 `tfsdk:"expiration"` |
| 74 | + Region types.String `tfsdk:"region"` |
| 75 | + Kubeconfig types.String `tfsdk:"kube_config"` |
| 76 | + ExpiresAt types.String `tfsdk:"expires_at"` |
| 77 | +} |
| 78 | + |
| 79 | +// Schema defines the schema for the ephemeral resource. |
| 80 | +func (e *kubeconfigEphemeralResource) Schema(_ context.Context, _ ephemeral.SchemaRequest, resp *ephemeral.SchemaResponse) { |
| 81 | + description := features.AddBetaDescription( |
| 82 | + "Ephemeral resource that generates a short-lived SKE kubeconfig. "+ |
| 83 | + "A new kubeconfig is generated each time the resource is evaluated, and it remains consistent for the duration of a Terraform operation.", |
| 84 | + core.EphemeralResource, |
| 85 | + ) |
| 86 | + |
| 87 | + resp.Schema = schema.Schema{ |
| 88 | + Description: description, |
| 89 | + Attributes: map[string]schema.Attribute{ |
| 90 | + "cluster_name": schema.StringAttribute{ |
| 91 | + Description: "Name of the SKE cluster.", |
| 92 | + Required: true, |
| 93 | + Validators: []validator.String{ |
| 94 | + validate.NoSeparator(), |
| 95 | + }, |
| 96 | + }, |
| 97 | + "project_id": schema.StringAttribute{ |
| 98 | + Description: "STACKIT project ID to which the cluster is associated.", |
| 99 | + Required: true, |
| 100 | + Validators: []validator.String{ |
| 101 | + validate.UUID(), |
| 102 | + validate.NoSeparator(), |
| 103 | + }, |
| 104 | + }, |
| 105 | + "expiration": schema.Int64Attribute{ |
| 106 | + Description: "Expiration time of the kubeconfig, in seconds. Defaults to `1800` (30m). Maximum is `14400` (4h).", |
| 107 | + Optional: true, |
| 108 | + Validators: []validator.Int64{ |
| 109 | + int64validator.AtLeast(60), |
| 110 | + int64validator.AtMost(14400), |
| 111 | + }, |
| 112 | + }, |
| 113 | + "region": schema.StringAttribute{ |
| 114 | + Description: "The resource region. If not defined, the provider region is used.", |
| 115 | + Optional: true, |
| 116 | + }, |
| 117 | + "kube_config": schema.StringAttribute{ |
| 118 | + Description: "Raw short-lived admin kubeconfig.", |
| 119 | + Computed: true, |
| 120 | + Sensitive: true, |
| 121 | + }, |
| 122 | + "expires_at": schema.StringAttribute{ |
| 123 | + Description: "Timestamp when the kubeconfig expires.", |
| 124 | + Computed: true, |
| 125 | + }, |
| 126 | + }, |
| 127 | + } |
| 128 | +} |
| 129 | + |
| 130 | +// Open creates the kubeconfig and sets the result. |
| 131 | +func (e *kubeconfigEphemeralResource) Open(ctx context.Context, req ephemeral.OpenRequest, resp *ephemeral.OpenResponse) { |
| 132 | + var model ephemeralModel |
| 133 | + |
| 134 | + resp.Diagnostics.Append(req.Config.Get(ctx, &model)...) |
| 135 | + if resp.Diagnostics.HasError() { |
| 136 | + return |
| 137 | + } |
| 138 | + |
| 139 | + ctx = core.InitProviderContext(ctx) |
| 140 | + |
| 141 | + projectId := model.ProjectId.ValueString() |
| 142 | + clusterName := model.ClusterName.ValueString() |
| 143 | + region := e.providerData.GetRegionWithOverride(model.Region) |
| 144 | + |
| 145 | + // Kubeconfig only needs to be valid for the duration of the Terraform operation. |
| 146 | + // Defaulted to 1800s (30m) for better security than the API default (3600s). |
| 147 | + expiration := conversion.Int64ValueToPointer(model.Expiration) |
| 148 | + if expiration == nil { |
| 149 | + expiration = new(int64) |
| 150 | + *expiration = defaultKubeconfigExpiration |
| 151 | + } |
| 152 | + |
| 153 | + kubeconfigResp, err := getKubeconfig(ctx, e.client, projectId, region, clusterName, expiration) |
| 154 | + |
| 155 | + ctx = core.LogResponse(ctx) |
| 156 | + |
| 157 | + if err != nil { |
| 158 | + core.LogAndAddError(ctx, &resp.Diagnostics, "Error creating kubeconfig", fmt.Sprintf("Calling SKE API: %v", err)) |
| 159 | + return |
| 160 | + } |
| 161 | + |
| 162 | + if kubeconfigResp == nil || kubeconfigResp.Kubeconfig == nil { |
| 163 | + core.LogAndAddError(ctx, &resp.Diagnostics, "Error creating kubeconfig", "API returned an empty response") |
| 164 | + return |
| 165 | + } |
| 166 | + |
| 167 | + model.Kubeconfig = types.StringPointerValue(kubeconfigResp.Kubeconfig) |
| 168 | + model.ExpiresAt = types.StringValue(kubeconfigResp.ExpirationTimestamp.Format(time.RFC3339)) |
| 169 | + model.Region = types.StringValue(region) |
| 170 | + |
| 171 | + resp.Diagnostics.Append(resp.Result.Set(ctx, model)...) |
| 172 | +} |
| 173 | + |
| 174 | +// getKubeconfig initializes the API call to generate a new kubeconfig |
| 175 | +func getKubeconfig(ctx context.Context, client *ske.APIClient, projectId, region, clusterName string, expiration *int64) (*ske.Kubeconfig, error) { |
| 176 | + var expirationStringPtr *string |
| 177 | + if expiration != nil { |
| 178 | + expirationStringPtr = new(string) |
| 179 | + *expirationStringPtr = strconv.FormatInt(*expiration, 10) |
| 180 | + } |
| 181 | + |
| 182 | + payload := ske.CreateKubeconfigPayload{ |
| 183 | + ExpirationSeconds: expirationStringPtr, |
| 184 | + } |
| 185 | + |
| 186 | + return client.DefaultAPI.CreateKubeconfig(ctx, projectId, region, clusterName).CreateKubeconfigPayload(payload).Execute() |
| 187 | +} |
0 commit comments