Skip to content

Commit

Permalink
feat: Prometheus Autoscale (#57)
Browse files Browse the repository at this point in the history
* feat: Prometheus Autoscale

* Creating prometheus autoscale tests

* Prometheus address is now a computed attribut

* Creating a new testing case
  • Loading branch information
gvicentin authored Apr 11, 2024
1 parent f6ac385 commit 4b64c23
Show file tree
Hide file tree
Showing 5 changed files with 290 additions and 26 deletions.
2 changes: 1 addition & 1 deletion GNUmakefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ HOSTNAME=registry.terraform.io
NAMESPACE=tsuru
NAME=tsuru
BINARY=terraform-provider-${NAME}
VERSION=2.9.2
VERSION=2.12.0

UNAME_S := $(shell uname -s)
UNAME_P := $(shell uname -p)
Expand Down
14 changes: 7 additions & 7 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ require (
github.com/hashicorp/terraform-plugin-sdk/v2 v2.26.1
github.com/labstack/echo/v4 v4.9.1
github.com/pkg/errors v0.9.1
github.com/stretchr/testify v1.8.4
github.com/tsuru/go-tsuruclient v0.0.0-20240403182619-fe8da980483b
github.com/stretchr/testify v1.9.0
github.com/tsuru/go-tsuruclient v0.0.0-20240409125509-22a1e08326f4
github.com/tsuru/tsuru-client v0.0.0-20240325204824-8c0dc602a5be
k8s.io/apimachinery v0.26.2
)
Expand Down Expand Up @@ -63,12 +63,12 @@ require (
github.com/vmihailenco/msgpack/v4 v4.3.12 // indirect
github.com/vmihailenco/tagparser v0.1.1 // indirect
github.com/zclconf/go-cty v1.13.1 // indirect
golang.org/x/crypto v0.18.0 // indirect
golang.org/x/crypto v0.22.0 // indirect
golang.org/x/mod v0.8.0 // indirect
golang.org/x/net v0.20.0 // indirect
golang.org/x/oauth2 v0.16.0 // indirect
golang.org/x/sys v0.16.0 // indirect
golang.org/x/term v0.16.0 // indirect
golang.org/x/net v0.21.0 // indirect
golang.org/x/oauth2 v0.19.0 // indirect
golang.org/x/sys v0.19.0 // indirect
golang.org/x/term v0.19.0 // indirect
golang.org/x/text v0.14.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/genproto v0.0.0-20230227214838-9b19f0bdc514 // indirect
Expand Down
28 changes: 14 additions & 14 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -202,12 +202,12 @@ github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.2/go.mod h1:R6va5+xMeoiuVRoj+gSkQ7d3FALtqAAGI1FQKckRals=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/tsuru/gnuflag v0.0.0-20151217162021-86b8c1b864aa h1:JlLQP1xa13a994p/Aau2e3K9xXYaHNoNvTDVIMHSUa4=
github.com/tsuru/gnuflag v0.0.0-20151217162021-86b8c1b864aa/go.mod h1:UibOSvkMFKRe/eiwktAPAvQG8L+p8nYsECJvu3Dgw7I=
github.com/tsuru/go-tsuruclient v0.0.0-20240403182619-fe8da980483b h1:CWioMVdmtkdipWKCWVnuT+6ZqzVzB/R6hdX0SW4KJOI=
github.com/tsuru/go-tsuruclient v0.0.0-20240403182619-fe8da980483b/go.mod h1:qwh/KJ6ypa2GISRI79XFOHhnSjGOe1cZVPHF3nfrf18=
github.com/tsuru/go-tsuruclient v0.0.0-20240409125509-22a1e08326f4 h1:MGmG6AxKP8XRe7nQqIQR+Tsb5tCzHnYpYk0tiuXVgxY=
github.com/tsuru/go-tsuruclient v0.0.0-20240409125509-22a1e08326f4/go.mod h1:qwh/KJ6ypa2GISRI79XFOHhnSjGOe1cZVPHF3nfrf18=
github.com/tsuru/tablecli v0.0.0-20190131152944-7ded8a3383c6 h1:1XDdWFAjIbCSG1OjN9v9KdWhuM8UtYlFcfHe/Ldkchk=
github.com/tsuru/tablecli v0.0.0-20190131152944-7ded8a3383c6/go.mod h1:ztYpOhW+u1k21FEqp7nZNgpWbr0dUKok5lgGCZi+1AQ=
github.com/tsuru/tsuru v0.0.0-20240325190920-410c71393b77 h1:cuWFjNLaemdQZhojqJbb/rOXO97tlcPeLAHg/x+EQGk=
Expand Down Expand Up @@ -244,8 +244,8 @@ golang.org/x/crypto v0.0.0-20210322153248-0c34fe9e7dc2/go.mod h1:T9bdIzuCu7OtxOm
golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b/go.mod h1:T9bdIzuCu7OtxOm1hfPfRQxPLYneinmdGuTeoZ9dtd4=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.5.0/go.mod h1:NK/OQwhpMQP3MwtdjgLlYHnH9ebylxKWv3e0fK+mkQU=
golang.org/x/crypto v0.18.0 h1:PGVlW0xEltQnzFZ55hkuX5+KLyrMYhHld1YHO4AKcdc=
golang.org/x/crypto v0.18.0/go.mod h1:R0j02AL6hcrfOiy9T4ZYp/rcWeMxM3L6QYxlOuEG1mg=
golang.org/x/crypto v0.22.0 h1:g1v0xeRhjcugydODzvb3mEM9SQ0HGp9s/nh3COQ/C30=
golang.org/x/crypto v0.22.0/go.mod h1:vr6Su+7cTlO45qkww3VDJlzDn0ctJvRgYbC2NvXHt+M=
golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
Expand All @@ -267,11 +267,11 @@ golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v
golang.org/x/net v0.0.0-20210326060303-6b1517762897/go.mod h1:uSPa2vr4CLtc/ILN5odXGNXS6mhrKVzTaCXzk9m6W3k=
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
golang.org/x/net v0.5.0/go.mod h1:DivGGAXEgPSlEBzxGzZI+ZLohi+xUj054jfeKui00ws=
golang.org/x/net v0.20.0 h1:aCL9BSgETF1k+blQaYUBx9hJ9LOGP3gAVemcZlf1Kpo=
golang.org/x/net v0.20.0/go.mod h1:z8BVo6PvndSri0LbOE3hAn0apkU+1YvI6E70E9jsnvY=
golang.org/x/net v0.21.0 h1:AQyQV4dYCvJ7vGmJyKki9+PBdyvhkSd8EIx/qb0AYv4=
golang.org/x/net v0.21.0/go.mod h1:bIjVDfnllIU7BJ2DNgfnXvpSvtn8VRwhlsaeUTyUS44=
golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
golang.org/x/oauth2 v0.16.0 h1:aDkGMBSYxElaoP81NpoUoz2oo2R2wHdZpGToUxfyQrQ=
golang.org/x/oauth2 v0.16.0/go.mod h1:hqZ+0LWXsiVoZpeld6jVt06P3adbS2Uu911W1SsJv2o=
golang.org/x/oauth2 v0.19.0 h1:9+E/EZBCbTLNrbN35fHv/a/d/mOBatymz1zbtQrXpIg=
golang.org/x/oauth2 v0.19.0/go.mod h1:vYi7skDa1x015PmRRYZ7+s1cWyPgrPiSYRe4rnsexc8=
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
Expand Down Expand Up @@ -300,13 +300,13 @@ golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.4.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.16.0 h1:xWw16ngr6ZMtmxDyKyIgsE93KNKz5HKmMa3b8ALHidU=
golang.org/x/sys v0.16.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.19.0 h1:q5f1RH2jigJ1MoAWp2KTp3gm5zAGFUTarQZ5U386+4o=
golang.org/x/sys v0.19.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.4.0/go.mod h1:9P2UbLfCdcvo3p/nzKvsmas4TnlujnuoV9hGgYzW1lQ=
golang.org/x/term v0.16.0 h1:m+B6fahuftsE9qjo0VWp2FW0mB3MTJvR0BaMQrq0pmE=
golang.org/x/term v0.16.0/go.mod h1:yn7UURbUtPyrVJPGPq404EukNFxcm/foM+bV/bfcDsY=
golang.org/x/term v0.19.0 h1:+ThwsDv+tYfnJFhF4L8jITxu1tdTWRTZpdsWgEgjL6Q=
golang.org/x/term v0.19.0/go.mod h1:2CuTdWZ7KHSQwUzKva0cbMg6q2DMI3Mmxp+gKJbskEk=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
Expand Down
100 changes: 98 additions & 2 deletions internal/provider/resource_tsuru_app_autoscale.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func resourceTsuruApplicationAutoscale() *schema.Resource {
Type: schema.TypeString,
Description: "CPU average, for example: 20%, mean that we trigger autoscale when the average of CPU Usage of units is 20%.",
Optional: true,
AtLeastOneOf: []string{"cpu_average", "schedule"},
AtLeastOneOf: []string{"cpu_average", "schedule", "prometheus"},
},
"schedule": {
Type: schema.TypeList,
Expand All @@ -91,7 +91,42 @@ func resourceTsuruApplicationAutoscale() *schema.Resource {
},
},
Optional: true,
AtLeastOneOf: []string{"cpu_average", "schedule"},
AtLeastOneOf: []string{"cpu_average", "schedule", "prometheus"},
},
"prometheus": {
Type: schema.TypeList,
Description: "List of Prometheus autoscale rules",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
Description: "Name of the Prometheus autoscale rule",
},
"threshold": {
Type: schema.TypeFloat,
Required: true,
Description: "Threshold value to trigger the autoscaler",
},
"query": {
Type: schema.TypeString,
Required: true,
Description: "Prometheus query to be used in the autoscale rule",
},
"custom_address": {
Type: schema.TypeString,
Optional: true,
Description: "Custom Prometheus URL. If not specified, it will use the default Prometheus from the app's pool",
},
"prometheus_address": {
Type: schema.TypeString,
Computed: true,
Description: "Custom Prometheus URL. If not specified, it will use the default Prometheus from the app's pool",
},
},
},
Optional: true,
AtLeastOneOf: []string{"cpu_average", "schedule", "prometheus"},
},
},
}
Expand Down Expand Up @@ -145,6 +180,13 @@ func resourceTsuruApplicationAutoscaleSet(ctx context.Context, d *schema.Resourc
}
}

if p, ok := d.GetOk("prometheus"); ok {
prometheus := prometheusFromResourceData(p)
if prometheus != nil {
autoscale.Prometheus = prometheus
}
}

err = resource.RetryContext(ctx, d.Timeout(schema.TimeoutCreate), func() *resource.RetryError {
_, err = provider.TsuruClient.AppApi.AutoScaleAdd(ctx, app, autoscale)
if err != nil {
Expand Down Expand Up @@ -211,6 +253,7 @@ func resourceTsuruApplicationAutoscaleRead(ctx context.Context, d *schema.Resour
}

d.Set("schedule", flattenSchedules(autoscale.Schedules))
d.Set("prometheus", flattenPrometheus(autoscale.Prometheus, d))

return nil
}
Expand Down Expand Up @@ -316,6 +359,41 @@ func schedulesFromResourceData(meta interface{}) []tsuru_client.AutoScaleSchedul
return schedules
}

func prometheusFromResourceData(meta interface{}) []tsuru_client.AutoScalePrometheus {
prometheusMeta := meta.([]interface{})

if len(prometheusMeta) == 0 {
return nil
}

prometheus := []tsuru_client.AutoScalePrometheus{}

for _, iface := range prometheusMeta {
prom := tsuru_client.AutoScalePrometheus{}
pm := iface.(map[string]interface{})

if v, ok := pm["name"]; ok {
prom.Name = v.(string)
}

if v, ok := pm["threshold"]; ok {
prom.Threshold = v.(float64)
}

if v, ok := pm["query"]; ok {
prom.Query = v.(string)
}

if v, ok := pm["custom_address"]; ok {
prom.PrometheusAddress = v.(string)
}

prometheus = append(prometheus, prom)
}

return prometheus
}

func flattenSchedules(schedules []tsuru_client.AutoScaleSchedule) []interface{} {
result := []interface{}{}

Expand All @@ -330,3 +408,21 @@ func flattenSchedules(schedules []tsuru_client.AutoScaleSchedule) []interface{}

return result
}

func flattenPrometheus(prometheus []tsuru_client.AutoScalePrometheus, d *schema.ResourceData) []interface{} {
result := []interface{}{}

for i, prom := range prometheus {
customAddressStr := fmt.Sprintf("prometheus.%d.custom_address", i)
customAddress := d.Get(customAddressStr).(string)
result = append(result, map[string]interface{}{
"name": prom.Name,
"threshold": prom.Threshold,
"query": prom.Query,
"custom_address": customAddress,
"prometheus_address": prom.PrometheusAddress,
})
}

return result
}
Loading

0 comments on commit 4b64c23

Please sign in to comment.