feat: trigger.enabled + job.run_id lifecycle control (operator v0.1.6, provider v0.1.4)

- TriggerSpec.Enabled bool (default=true): enabled=false масштабирует Deployment до 0
- FunctionJobSpec.RunID int64 (default=0): run_id=0 = skip, >0 = run
- API: PATCH /v1/namespaces/{ns}/triggers/{name} (UpdateTrigger)
- Provider: enabled attribute (Optional, Computed, in-place update)
- Provider: run_id attribute (Optional, Computed, default=0, RequiresReplace)
- operator image: naeel/sless-operator:v0.1.6
- provider: terra.k8c.ru/naeel/sless v0.1.4
This commit is contained in:
“Naeel”
2026-03-08 10:10:32 +04:00
parent 8fb0ef5ea1
commit d67b9745a8
20 changed files with 509 additions and 143 deletions
@@ -1,8 +1,8 @@
// 2026-03-07
// 2026-03-08
// trigger_resource.go — Terraform ресурс sless_trigger.
// Поддерживает type=http (создаёт Service+Ingress) и type=cron (запускает по schedule).
// Все ключевые поля имеют RequiresReplace — API не поддерживает обновление триггеров.
// URL функции (для http-триггера) — computed, пишется оператором в status.url.
// enabled=false: масштабирует Deployment функции до 0 (не принимает запросы, не потребляет ресурсы).
// enabled не требует RequiresReplace — поддерживает in-place обновление через PATCH.
package resources
import (
@@ -13,6 +13,7 @@ import (
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/booldefault"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/boolplanmodifier"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
@@ -36,8 +37,11 @@ type TriggerModel struct {
Type types.String `tfsdk:"type"`
FunctionRef types.String `tfsdk:"function"`
Schedule types.String `tfsdk:"schedule"`
Active types.Bool `tfsdk:"active"`
URL types.String `tfsdk:"url"`
// Enabled — false = Deployment масштабируется до 0, функция не потребляет ресурсы.
// Не требует пересоздания - изменяется in-place через PATCH.
Enabled types.Bool `tfsdk:"enabled"`
Active types.Bool `tfsdk:"active"`
URL types.String `tfsdk:"url"`
}
func (r *TriggerResource) Metadata(_ context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) {
@@ -80,6 +84,13 @@ func (r *TriggerResource) Schema(_ context.Context, _ resource.SchemaRequest, re
stringplanmodifier.RequiresReplace(),
},
},
// enabled: false → Deployment масштабируется до 0 (не удаляет ресурс).
// Не имеет RequiresReplace: применяется in-place через PATCH.
"enabled": schema.BoolAttribute{
Optional: true,
Computed: true,
Default: booldefault.StaticBool(true),
},
// active, url — только для чтения, вычисляются оператором
"active": schema.BoolAttribute{
Computed: true,
@@ -124,6 +135,7 @@ func (r *TriggerResource) Create(ctx context.Context, req resource.CreateRequest
Type: plan.Type.ValueString(),
FunctionRef: plan.FunctionRef.ValueString(),
Schedule: plan.Schedule.ValueString(),
Enabled: boolPtr(plan.Enabled.ValueBool()),
})
if err != nil {
resp.Diagnostics.AddError("create trigger", err.Error())
@@ -153,10 +165,26 @@ func (r *TriggerResource) Read(ctx context.Context, req resource.ReadRequest, re
resp.Diagnostics.Append(resp.State.Set(ctx, trToModel(tr))...)
}
// Update — никогда не вызывается: все поля имеют RequiresReplace.
// Метод обязателен интерфейсом resource.Resource.
func (r *TriggerResource) Update(_ context.Context, _ resource.UpdateRequest, resp *resource.UpdateResponse) {
resp.Diagnostics.AddError("update not supported", "all trigger fields require replacement")
// Update — обновляет enabled через PATCH без пересоздания.
// единственное поле без RequiresReplace, поэтому Update срабатывает только если enabled изменился.
func (r *TriggerResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
var plan, state TriggerModel
resp.Diagnostics.Append(req.Plan.Get(ctx, &plan)...)
resp.Diagnostics.Append(req.State.Get(ctx, &state)...)
if resp.Diagnostics.HasError() {
return
}
enabled := plan.Enabled.ValueBool()
tr, err := r.client.UpdateTrigger(ctx, plan.Namespace.ValueString(), plan.Name.ValueString(), client.TriggerUpdateRequest{
Enabled: &enabled,
})
if err != nil {
resp.Diagnostics.AddError("update trigger", err.Error())
return
}
resp.Diagnostics.Append(resp.State.Set(ctx, trToModel(tr))...)
}
func (r *TriggerResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {
@@ -185,7 +213,13 @@ func trToModel(tr *client.TriggerResponse) TriggerModel {
Type: types.StringValue(tr.Type),
FunctionRef: types.StringValue(tr.FunctionRef),
Schedule: schedule,
Enabled: types.BoolValue(tr.Enabled),
Active: types.BoolValue(tr.Active),
URL: types.StringValue(tr.URL),
}
}
// boolPtr — хелпер для получения указателя на bool (API требует *bool).
func boolPtr(v bool) *bool {
return &v
}