Skip to content

Commit

Permalink
Added replicas to resource configurations (#19)
Browse files Browse the repository at this point in the history
* Added replicas to resource configurations

* Added replicas to Validate step
  • Loading branch information
marhode authored Apr 8, 2024
1 parent a18f516 commit ee605ef
Show file tree
Hide file tree
Showing 5 changed files with 135 additions and 2 deletions.
5 changes: 5 additions & 0 deletions charts/infra-autoscaler/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,11 @@ autoscalerConfig:
min_usage: 0.3
max_bytes: 32768
max_usage: 0.7
replicas:
min_replicas: 1
max_replicas: 3
min_usage: 0.3
max_usage: 0.7
cycle_time_seconds: 60
api_token: $BBB_API_TOKEN
prometheus_config:
Expand Down
37 changes: 37 additions & 0 deletions config/bbb_scaler_config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
app_name: bbb-scaler
stage: prod
scaling_mode: direct
service_type: BBB
provider_type: Ionos
metrics_source_type: Prometheus
ionos_config:
username: $IONOS_USERNAME
password: $IONOS_PASSWORD
contract_id: $IONOS_CONTRACT_ID
server_source:
dynamic:
datacenter_ids: []
server_name_regex: "bbb-.*"
bbb_config:
resources:
cpu:
min_cores: 2
min_usage: 0.3
max_cores: 6
max_usage: 0.7
memory:
min_bytes: 12288
min_usage: 0.3
max_bytes: 32768
max_usage: 0.7
replicas:
min_replicas: 1
min_usage: 0.3
max_replicas: 3
max_usage: 0.7
cycle_time_seconds: 60
api_token: $BBB_API_TOKEN
prometheus_config:
url: https://grafana.example.com/api/datasources/proxy/uid/<uid>/
token: $GRAFANA_TOKEN
metrics_exporter_port: 9100
5 changes: 5 additions & 0 deletions services/test_files/bbb_config_ok.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ bbb_config:
min_bytes: 12288
max_bytes: 32768
max_usage: 0.7
replicas:
min_replicas: 2
max_replicas: 4
min_usage: 0.3
max_usage: 0.7
cycle_time_seconds: 60
api_token: $BBB_API_TOKEN

40 changes: 38 additions & 2 deletions shared/Resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import "fmt"
type Resources struct {
Cpu *CpuResources `yaml:"cpu"`
Memory *MemoryResources `yaml:"memory"`
Replica *ReplicaResources `yaml:"replicas"`
}

// TODO: replace this with a generic resource interface
Expand All @@ -23,9 +24,17 @@ type MemoryResources struct {
MaxUsage float32 `yaml:"max_usage"`
}

type ReplicaResources struct {
MinReplicas int `yaml:"min_replicas"`
MaxReplicas int `yaml:"max_replicas"`
MinUsage float32 `yaml:"min_usage"`
MaxUsage float32 `yaml:"max_usage"`
}

type ResourceState struct {
Cpu *CpuResourceState
Memory *MemoryResourceState
Replica *ReplicaResourceState
}

type CpuResourceState struct {
Expand All @@ -38,9 +47,15 @@ type MemoryResourceState struct {
CurrentUsage float32
}

type ReplicaResourceState struct {
CurrentReplicas int
CurrentUsage float32
}

type ResourceScalingProposal struct {
Cpu ScaleOp
Mem ScaleOp
Replica ScaleOp
}

type ScaleOp struct {
Expand Down Expand Up @@ -68,8 +83,13 @@ func (r Resources) Validate() error {
return err
}
}
if r.Cpu == nil && r.Memory == nil {
return fmt.Errorf("resources.cpu and resources.memory are nil, at least one must be set")
if replica := r.Replica; replica != nil {
if err := replica.Validate(); err != nil {
return err
}
}
if r.Cpu == nil && r.Memory == nil && r.Replica == nil{
return fmt.Errorf("resources.cpu and resources.memory and resources.replica are nil, at least one must be set")
}
return nil
}
Expand Down Expand Up @@ -105,3 +125,19 @@ func (m MemoryResources) Validate() error {
}
return nil
}

func (r ReplicaResources) Validate() error {
if r.MinReplicas < 1 {
return fmt.Errorf("replicas.min_replicas must be greater than or equal to 1 but got %d", r.MinReplicas)
}
if r.MaxReplicas < r.MinReplicas {
return fmt.Errorf("r.MaxReplicas must be greater than or equal to min_replicas (%d) but got %d", r.MinReplicas, r.MaxReplicas)
}
if r.MinUsage < 0 || r.MinUsage > 1 {
return fmt.Errorf("replicas.min_usage must be greater than 0 and less than or equal to 1 but got %f", r.MinUsage)
}
if r.MaxUsage <= r.MinUsage || r.MaxUsage > 1 {
return fmt.Errorf("replicas.max_usage must be greater than min_usage (%f) and less than or equal to 1 but got %f", r.MinUsage, r.MaxUsage)
}
return nil
}
50 changes: 50 additions & 0 deletions shared/Resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 +103,53 @@ func TestValidateMemoryResourcesMaxUsageGreaterThanOne(t *testing.T) {
}
ValidateFail(t, memoryResources)
}

func TestValidateReplicaResourcesOK(t *testing.T) {
ReplicaResources := &ReplicaResources{
MinReplicas: 2,
MaxReplicas: 4,
MinUsage: 0.3,
MaxUsage: 0.7,
}
ValidatePass(t, ReplicaResources)
}

func TestValidateReplicaResourcesMinReplicasZero(t *testing.T) {
ReplicaResources := &ReplicaResources{
MinReplicas: 0,
MaxReplicas: 4,
MinUsage: 0.3,
MaxUsage: 0.7,
}
ValidateFail(t, ReplicaResources)
}

func TestValidateReplicaResourcesMaxLessThanMin(t *testing.T) {
ReplicaResources := &ReplicaResources{
MinReplicas: 2,
MaxReplicas: 1,
MinUsage: 0.3,
MaxUsage: 0.7,
}
ValidateFail(t, ReplicaResources)
}

func TestValidateReplicaResourcesMaxUsageGreaterThanOne(t *testing.T) {
ReplicaResources := &ReplicaResources{
MinReplicas: 2,
MaxReplicas: 4,
MinUsage: 0.3,
MaxUsage: 1.1,
}
ValidateFail(t, ReplicaResources)
}

func TestValidateReplicaResourcesMaxUsageZero(t *testing.T) {
ReplicaResources := &ReplicaResources{
MinReplicas: 2,
MaxReplicas: 4,
MinUsage: 0.3,
MaxUsage: 0,
}
ValidateFail(t, ReplicaResources)
}

0 comments on commit ee605ef

Please sign in to comment.