feat: add initial support for injecting keda addons

This commit is contained in:
Jose Diaz-Gonzalez committed 2024-03-06 03:39:11 -05:00
1 parent 3242073e84
commit b7ff696507
11 files changed
+223 -4

No files matched your search

+42
View File
@@ -27,6 +27,7 @@ import (
"k8s.io/client-go/tools/remotecommand"
"k8s.io/kubectl/pkg/util/term"
"k8s.io/kubernetes/pkg/client/conditions"
"k8s.io/utils/ptr"
"mvdan.cc/sh/v3/shell"
)
@@ -395,6 +396,12 @@ func getAnnotations(appName string, processType string) (ProcessAnnotations, err
}
annotations.JobAnnotations = jobAnnotations
kedaScalingObjectAnnotations, err := getAnnotation(appName, processType, "keda_scaled_object")
if err != nil {
return annotations, err
}
annotations.KedaScalingObjectAnnotations = kedaScalingObjectAnnotations
podAnnotations, err := getAnnotation(appName, processType, "pod")
if err != nil {
return annotations, err
@@ -434,6 +441,41 @@ func getAnnotations(appName string, processType string) (ProcessAnnotations, err
return annotations, nil
}
// getAutoscaling retrieves autoscaling config for a given app and process type
func getAutoscaling(appName string, processName string, replicas int) (ProcessAutoscaling, error) {
config, ok, err := appjson.GetAutoscalingConfig(appName, processName, replicas)
if err != nil {
common.LogWarn(fmt.Sprintf("Error getting autoscaling config for %s: %v", appName, err))
return ProcessAutoscaling{}, err
}
if !ok {
common.LogWarn(fmt.Sprintf("No autoscaling config found for %s", appName))
return ProcessAutoscaling{}, nil
}
triggers := []ProcessAutoscalingTrigger{}
for _, trigger := range config.Triggers {
triggers = append(triggers, ProcessAutoscalingTrigger{
Name: trigger.Name,
Type: trigger.Type,
Metadata: trigger.Metadata,
})
}
autoscaling := ProcessAutoscaling{
CooldownPeriodSeconds: ptr.Deref(config.CooldownPeriodSeconds, 300),
Enabled: len(triggers) > 0,
MaxReplicas: ptr.Deref(config.MaxQuantity, 0),
MinReplicas: ptr.Deref(config.MinQuantity, 0),
PollingIntervalSeconds: ptr.Deref(config.PollingIntervalSeconds, 30),
Triggers: triggers,
Type: "keda",
}
return autoscaling, nil
}
// getGlobalAnnotations retrieves global annotations for a given app
func getGlobalAnnotations(appName string) (ProcessAnnotations, error) {
return getAnnotations(appName, GlobalProcessType)
+38
View File
@@ -59,6 +59,7 @@ type GlobalNetwork struct {
type ProcessValues struct {
Annotations ProcessAnnotations `yaml:"annotations,omitempty"`
Args []string `yaml:"args,omitempty"`
Autoscaling ProcessAutoscaling `yaml:"autoscaling,omitempty"`
Cron ProcessCron `yaml:"cron,omitempty"`
Healthchecks ProcessHealthchecks `yaml:"healthchecks,omitempty"`
Labels ProcessLabels `yaml:"labels,omitempty"`
@@ -74,6 +75,7 @@ type ProcessAnnotations struct {
DeploymentAnnotations map[string]string `yaml:"deployment,omitempty"`
IngressAnnotations map[string]string `yaml:"ingress,omitempty"`
JobAnnotations map[string]string `yaml:"job,omitempty"`
KedaScalingObjectAnnotations map[string]string `yaml:"keda_scaled_object,omitempty"`
PodAnnotations map[string]string `yaml:"pod,omitempty"`
SecretAnnotations map[string]string `yaml:"secret,omitempty"`
ServiceAccountAnnotations map[string]string `yaml:"serviceaccount,omitempty"`
@@ -82,6 +84,42 @@ type ProcessAnnotations struct {
TraefikMiddlewareAnnotations map[string]string `yaml:"traefik_middleware,omitempty"`
}
// ProcessAutoscaling contains the autoscaling configuration for a process
type ProcessAutoscaling struct {
// CooldownPeriodSeconds is the number of seconds after a scaling event before another can be triggered
CooldownPeriodSeconds int `yaml:"cooldown_period_seconds,omitempty"`
// Enabled is a flag to enable autoscaling
Enabled bool `yaml:"enabled"`
// MaxReplicas is the maximum number of replicas to scale to
MaxReplicas int `yaml:"max_replicas,omitempty"`
// MinReplicas is the minimum number of replicas to scale to
MinReplicas int `yaml:"min_replicas,omitempty"`
// PollingIntervalSeconds is the number of seconds between polling for new metrics
PollingIntervalSeconds int `yaml:"polling_interval_seconds,omitempty"`
// Triggers is a list of triggers to use for autoscaling
Triggers []ProcessAutoscalingTrigger `yaml:"triggers,omitempty"`
// Type is the type of autoscaling to use
Type string `yaml:"type"`
}
// ProcessAutoscalingTrigger is a trigger to use for autoscaling
type ProcessAutoscalingTrigger struct {
// Name is the name of the trigger
Name string `yaml:"name"`
// Type is the type of trigger to use
Type string `yaml:"type"`
// Metadata is a map of key-value pairs that can be used to store arbitrary trigger data
Metadata map[string]interface{} `yaml:"metadata,omitempty"`
}
type ProcessHealthchecks struct {
Liveness ProcessHealthcheck `yaml:"liveness,omitempty"`
Readiness ProcessHealthcheck `yaml:"readiness,omitempty"`
@@ -0,0 +1,43 @@
{{- $processName := "PROCESS_NAME" }}
{{- $config := index .Values.processes "PROCESS_NAME" }}
{{- if and $config.autoscaling (and $config.autoscaling.enabled (eq $config.autoscaling.type "keda")) }}
apiVersion: keda.sh/v1alpha1
kind: ScaledObject
metadata:
annotations:
app.kubernetes.io/version: {{ $.Values.global.deploment_id | quote }}
dokku.com/builder-type: {{ $.Values.global.image.type }}
dokku.com/managed: "true"
kubectl.kubernetes.io/default-container: {{ $.Values.global.app_name }}-{{ $processName }}
{{- if and $.Values.global.annotations $.Values.global.annotations.keda_scaled_object }}
{{- range $k, $v := $.Values.global.annotations.keda_scaled_object }}
{{ $k }}: {{ $v | quote }}
{{- end }}
{{- end }}
{{- if and $config.annotations $config.annotations.keda_scaled_object }}
{{- range $k, $v := $config.annotations.keda_scaled_object }}
{{ $k }}: {{ $v | quote }}
{{- end }}
{{- end }}
labels:
app.kubernetes.io/instance: {{ $.Values.global.app_name }}-{{ $processName }}-scaled-object
app.kubernetes.io/name: {{ $processName }}
app.kubernetes.io/part-of: {{ $.Values.global.app_name }}
name: {{ $.Values.global.app_name}}-{{ $processName }}
namespace: {{ $.Values.global.namespace }}
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: {{ $.Values.global.app_name }}-{{ $processName }}
envSourceContainerName: {{ $.Values.global.app_name }}-{{ $processName }}
cooldownPeriod: {{ $config.autoscaling.cooldown_period_seconds }}
pollingInterval: {{ $config.autoscaling.polling_interval_seconds }}
minReplicaCount: {{ $config.autoscaling.min_replicas }}
maxReplicaCount: {{ $config.autoscaling.max_replicas }}
fallback:
failureThreshold: 3
replicas: {{ $config.replicas }}
triggers:
{{- toYaml $config.autoscaling.triggers | nindent 4 }}
{{- end }}
+8 -1
View File
@@ -327,8 +327,14 @@ func TriggerSchedulerDeploy(scheduler string, appName string, imageTag string) e
return fmt.Errorf("Error getting process labels: %w", err)
}
autoscaling, err := getAutoscaling(appName, processType, int(processCount))
if err != nil {
return fmt.Errorf("Error getting autoscaling: %w", err)
}
processValues := ProcessValues{
Annotations: annotations,
Autoscaling: autoscaling,
Args: args,
Healthchecks: processHealthchecks,
Labels: labels,
@@ -336,6 +342,7 @@ func TriggerSchedulerDeploy(scheduler string, appName string, imageTag string) e
Replicas: int32(processCount),
Resources: processResources,
}
if processType == "web" {
processValues.Web = ProcessWeb{
Domains: domains,
@@ -392,7 +399,7 @@ func TriggerSchedulerDeploy(scheduler string, appName string, imageTag string) e
values.Processes[processType] = processValues
templateFiles := []string{"deployment"}
templateFiles := []string{"deployment", "scaled-object"}
if processType == "web" {
templateFiles = append(templateFiles, "service", "certificate", "ingress", "ingress-route", "https-redirect-middleware")
}