feat: add initial support for injecting keda addons

This commit is contained in:
Jose Diaz-Gonzalez
2024-02-27 14:26:16 -05:00
parent 3242073e84
commit b7ff696507
11 changed files with 223 additions and 4 deletions

View File

@@ -3,10 +3,12 @@ package appjson
import (
"encoding/json"
"fmt"
"math"
"os"
"strings"
"github.com/dokku/dokku/plugins/common"
"k8s.io/utils/ptr"
)
var (
@@ -59,6 +61,9 @@ type CronCommand struct {
// Formation is a struct that represents the scale for a process from an app.json file
type Formation struct {
// Autoscaling is whether or not to enable autoscaling
Autoscaling *FormationAutoscaling `json:"autoscaling"`
// Quantity is the number of processes to run
Quantity *int `json:"quantity"`
@@ -66,6 +71,36 @@ type Formation struct {
MaxParallel *int `json:"max_parallel"`
}
// FormationAutoscaling is a struct that represents the autoscaling configuration for a process from an app.json file
type FormationAutoscaling struct {
// CoolDownSeconds is the number of seconds to wait before scaling again
CooldownPeriodSeconds *int `json:"cooldown_period_seconds,omitempty"`
// MaxQuantity is the maximum number of processes to run
MaxQuantity *int `json:"max_quantity,omitempty"`
// MinQuantity is the minimum number of processes to run
MinQuantity *int `json:"min_quantity,omitempty"`
// PollingIntervalSeconds is the number of seconds to wait between autoscaling checks
PollingIntervalSeconds *int `json:"polling_interval_seconds,omitempty"`
// Triggers is a list of triggers to use for autoscaling
Triggers []FormationAutoscalingTrigger `json:"triggers,omitempty"`
}
// FormationAutoscalingTrigger is a struct that represents a single autoscaling trigger from an app.json file
type FormationAutoscalingTrigger struct {
// Name is the name of the trigger
Name string `json:"name,omitempty"`
// Type is the type of the trigger
Type string `json:"type,omitempty"`
// Metadata is a map of metadata to use for the trigger
Metadata map[string]interface{} `json:"metadata,omitempty"`
}
// Healthcheck is a struct that represents a single healthcheck from an app.json file
type Healthcheck struct {
// Attempts is the number of attempts to make before considering a healthcheck failed
@@ -181,3 +216,45 @@ func GetAppJSON(appName string) (AppJSON, error) {
return appJSON, nil
}
func GetAutoscalingConfig(appName string, processType string, replicas int) (FormationAutoscaling, bool, error) {
appJSON, err := GetAppJSON(appName)
if err != nil {
return FormationAutoscaling{}, false, err
}
common.LogWarn(fmt.Sprintf("appJSON: %v", appJSON))
formation, ok := appJSON.Formation[processType]
if !ok {
return FormationAutoscaling{}, false, nil
}
if formation.Autoscaling == nil {
return FormationAutoscaling{}, false, nil
}
autoscaling := *formation.Autoscaling
if autoscaling.CooldownPeriodSeconds == nil {
autoscaling.CooldownPeriodSeconds = ptr.To(300)
}
if autoscaling.MinQuantity == nil {
autoscaling.MinQuantity = ptr.To(replicas)
}
if autoscaling.MaxQuantity == nil {
defaultValue := math.Max(float64(replicas), float64(*autoscaling.MinQuantity))
autoscaling.MaxQuantity = ptr.To(int(defaultValue))
}
if autoscaling.PollingIntervalSeconds == nil {
autoscaling.PollingIntervalSeconds = ptr.To(30)
}
if len(autoscaling.Triggers) == 0 {
return FormationAutoscaling{}, false, nil
}
return autoscaling, true, nil
}

View File

@@ -7,6 +7,7 @@ require (
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51
github.com/spf13/pflag v1.0.5
golang.org/x/sync v0.6.0
k8s.io/utils v0.0.0-20240102154912-e7106e64919e
)
require (

View File

@@ -4,8 +4,9 @@ github.com/codegangsta/inject v0.0.0-20150114235600-33e0aa1cb7c0 h1:sDMmm+q/3+Bu
github.com/codegangsta/inject v0.0.0-20150114235600-33e0aa1cb7c0/go.mod h1:4Zcjuz89kmFXt9morQgcfYZAYZ5n8WHjt81YYWIwtTM=
github.com/codeskyblue/go-sh v0.0.0-20190412065543-76bd3d59ff27 h1:HHUr4P/aKh4quafGxDT9LDasjGdlGkzLbfmmrlng3kA=
github.com/codeskyblue/go-sh v0.0.0-20190412065543-76bd3d59ff27/go.mod h1:VQx0hjo2oUeQkQUET7wRwradO6f+fN5jzXgB/zROxxE=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/fatih/color v1.16.0 h1:zmkK9Ngbjj+K0yRhTVONQh1p/HknKYSlNT+vZCzyokM=
github.com/fatih/color v1.16.0/go.mod h1:fL2Sau1YI5c0pdGEVCbKQbLXB6edEj1ZgiY4NijnWvE=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
@@ -95,3 +96,5 @@ gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
k8s.io/utils v0.0.0-20240102154912-e7106e64919e h1:eQ/4ljkx21sObifjzXwlPKpdGLrCfRziVtos3ofG/sQ=
k8s.io/utils v0.0.0-20240102154912-e7106e64919e/go.mod h1:OLgZIPagt7ERELqWJFomSt595RzquPNLL48iOWgYOg0=