From 170431a0af23efb8a8f9accfa0ba13c163cb2050 Mon Sep 17 00:00:00 2001 From: Juho Makinen Date: Fri, 17 Jan 2025 16:46:24 +1100 Subject: [PATCH] chore: use Mod* pattern to set scaling in runtime --- backend/controller/state/deployments.go | 16 +++------------- common/schema/moduleruntime.go | 7 +++++++ 2 files changed, 10 insertions(+), 13 deletions(-) diff --git a/backend/controller/state/deployments.go b/backend/controller/state/deployments.go index f07f9b4e88..9a553e7e3e 100644 --- a/backend/controller/state/deployments.go +++ b/backend/controller/state/deployments.go @@ -81,7 +81,7 @@ func (r *DeploymentReplicasUpdatedEvent) Handle(t SchemaState) (SchemaState, err if !ok { return t, fmt.Errorf("deployment %s not found", r.Key) } - setMinReplicas(existing, r.Replicas) + existing.ModRuntime().ModScaling().MinReplicas = int32(r.Replicas) return t, nil } @@ -98,7 +98,7 @@ func (r *DeploymentActivatedEvent) Handle(t SchemaState) (SchemaState, error) { } existing.ModRuntime().ModDeployment().ActivatedAt = r.ActivatedAt - setMinReplicas(existing, r.MinReplicas) + existing.ModRuntime().ModScaling().MinReplicas = int32(r.MinReplicas) t.activeDeployments[r.Key] = true return t, nil } @@ -114,17 +114,7 @@ func (r *DeploymentDeactivatedEvent) Handle(t SchemaState) (SchemaState, error) return t, fmt.Errorf("deployment %s not found", r.Key) } - setMinReplicas(existing, 0) + existing.ModRuntime().ModScaling().MinReplicas = 0 delete(t.activeDeployments, r.Key) return t, nil } - -func setMinReplicas(s *schema.Module, minReplicas int) { - if s.Runtime == nil { - s.Runtime = &schema.ModuleRuntime{} - } - if s.Runtime.Scaling == nil { - s.Runtime.Scaling = &schema.ModuleRuntimeScaling{} - } - s.Runtime.Scaling.MinReplicas = int32(minReplicas) -} diff --git a/common/schema/moduleruntime.go b/common/schema/moduleruntime.go index af2513cbb7..82125f1bb1 100644 --- a/common/schema/moduleruntime.go +++ b/common/schema/moduleruntime.go @@ -114,3 +114,10 @@ func (m *ModuleRuntimeDeployment) GetDeploymentKey() key.Deployment { } return m.DeploymentKey } + +func (m *ModuleRuntime) ModScaling() *ModuleRuntimeScaling { + if m.Scaling == nil { + m.Scaling = &ModuleRuntimeScaling{} + } + return m.Scaling +}