@@ -10,6 +10,7 @@ import (
1010 skeUtils "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/services/ske/utils"
1111
1212 "github.com/google/uuid"
13+ "github.com/hashicorp/terraform-plugin-framework-validators/int64validator"
1314 "github.com/hashicorp/terraform-plugin-framework/path"
1415 "github.com/hashicorp/terraform-plugin-framework/schema/validator"
1516 "github.com/hashicorp/terraform-plugin-log/tflog"
@@ -38,16 +39,17 @@ var (
3839)
3940
4041type Model struct {
41- Id types.String `tfsdk:"id"` // needed by TF
42- ClusterName types.String `tfsdk:"cluster_name"`
43- ProjectId types.String `tfsdk:"project_id"`
44- KubeconfigId types.String `tfsdk:"kube_config_id"` // uuid generated internally because kubeconfig has no identifier
45- Kubeconfig types.String `tfsdk:"kube_config"`
46- Expiration types.Int64 `tfsdk:"expiration"`
47- Refresh types.Bool `tfsdk:"refresh"`
48- ExpiresAt types.String `tfsdk:"expires_at"`
49- CreationTime types.String `tfsdk:"creation_time"`
50- Region types.String `tfsdk:"region"`
42+ Id types.String `tfsdk:"id"` // needed by TF
43+ ClusterName types.String `tfsdk:"cluster_name"`
44+ ProjectId types.String `tfsdk:"project_id"`
45+ KubeconfigId types.String `tfsdk:"kube_config_id"` // uuid generated internally because kubeconfig has no identifier
46+ Kubeconfig types.String `tfsdk:"kube_config"`
47+ Expiration types.Int64 `tfsdk:"expiration"`
48+ Refresh types.Bool `tfsdk:"refresh"`
49+ RefreshBefore types.Int64 `tfsdk:"refresh_before"`
50+ ExpiresAt types.String `tfsdk:"expires_at"`
51+ CreationTime types.String `tfsdk:"creation_time"`
52+ Region types.String `tfsdk:"region"`
5153}
5254
5355// NewKubeconfigResource is a helper function to simplify the provider implementation.
@@ -94,6 +96,7 @@ func (r *kubeconfigResource) Schema(_ context.Context, _ resource.SchemaRequest,
9496 "expiration" : "Expiration time of the kubeconfig, in seconds. Defaults to `3600`" ,
9597 "expires_at" : "Timestamp when the kubeconfig expires" ,
9698 "refresh" : "If set to true, the provider will check if the kubeconfig has expired and will generated a new valid one in-place" ,
99+ "refresh_before" : "Number of seconds before expiration to trigger refresh of the kubeconfig at. Only used if refresh is set to true." ,
97100 "creation_time" : "Date-time when the kubeconfig was created" ,
98101 "region" : "The resource region. If not defined, the provider region is used." ,
99102 }
@@ -155,6 +158,16 @@ func (r *kubeconfigResource) Schema(_ context.Context, _ resource.SchemaRequest,
155158 boolplanmodifier .RequiresReplace (),
156159 },
157160 },
161+ "refresh_before" : schema.Int64Attribute {
162+ Description : descriptions ["refresh_before" ],
163+ Optional : true ,
164+ PlanModifiers : []planmodifier.Int64 {
165+ int64planmodifier .UseStateForUnknown (),
166+ },
167+ Validators : []validator.Int64 {
168+ int64validator .AtLeast (1 ),
169+ },
170+ },
158171 "kube_config" : schema.StringAttribute {
159172 Description : descriptions ["kube_config" ],
160173 Computed : true ,
@@ -442,6 +455,9 @@ func checkHasExpired(model *Model, currentTime time.Time) (bool, error) {
442455 if err != nil {
443456 return false , fmt .Errorf ("converting expiresAt field to timestamp: %w" , err )
444457 }
458+ if ! model .RefreshBefore .IsNull () {
459+ expiresAt = expiresAt .Add (- time .Duration (model .RefreshBefore .ValueInt64 ()) * time .Second )
460+ }
445461 if expiresAt .Before (currentTime ) {
446462 return true , nil
447463 }
0 commit comments