diff --git a/plugins/scheduler-k3s/functions.go b/plugins/scheduler-k3s/functions.go index 4d37ac2ec..df3decc37 100644 --- a/plugins/scheduler-k3s/functions.go +++ b/plugins/scheduler-k3s/functions.go @@ -122,6 +122,118 @@ type WaitForPodToExistInput struct { LabelSelector string } +// applyKedaClusterTriggerAuthentications applies keda cluster trigger authentications chart to the cluster +func applyKedaClusterTriggerAuthentications(ctx context.Context, triggerType string, metadata map[string]string) error { + chartDir, err := os.MkdirTemp("", "keda-cluster-trigger-authentications-chart-") + if err != nil { + return fmt.Errorf("Error creating keda-cluster-trigger-authentications chart directory: %w", err) + } + defer os.RemoveAll(chartDir) + + // create the chart.yaml + chart := &Chart{ + ApiVersion: "v2", + AppVersion: "1.0.0", + Icon: "https://dokku.com/assets/dokku-logo.svg", + Name: fmt.Sprintf("keda-cluster-trigger-authentications-%s", triggerType), + Version: "0.0.1", + } + + err = writeYaml(WriteYamlInput{ + Object: chart, + Path: filepath.Join(chartDir, "Chart.yaml"), + }) + if err != nil { + return fmt.Errorf("Error writing keda-cluster-trigger-authentications chart: %w", err) + } + + // create the values.yaml + values := ClusterKedaValues{ + Secrets: map[string]string{}, + Type: triggerType, + } + + for key, value := range metadata { + values.Secrets[key] = base64.StdEncoding.EncodeToString([]byte(value)) + } + + if err := os.MkdirAll(filepath.Join(chartDir, "templates"), os.FileMode(0755)); err != nil { + return fmt.Errorf("Error creating keda-cluster-trigger-authentications chart templates directory: %w", err) + } + + err = writeYaml(WriteYamlInput{ + Object: values, + Path: filepath.Join(chartDir, "values.yaml"), + }) + if err != nil { + return fmt.Errorf("Error writing chart: %w", err) + } + + templateFiles := []string{"keda-cluster-trigger-authentication", "keda-cluster-secret"} + for _, template := range templateFiles { + b, err := templates.ReadFile(fmt.Sprintf("templates/chart/%s.yaml", template)) + if err != nil { + return fmt.Errorf("Error reading %s template: %w", template, err) + } + + filename := filepath.Join(chartDir, "templates", fmt.Sprintf("%s.yaml", template)) + err = os.WriteFile(filename, b, os.FileMode(0644)) + if err != nil { + return fmt.Errorf("Error writing %s template: %w", template, err) + } + + if os.Getenv("DOKKU_TRACE") == "1" { + common.CatFile(filename) + } + } + + b, err := templates.ReadFile("templates/chart/_helpers.tpl") + if err != nil { + return fmt.Errorf("Error reading _helpers template: %w", err) + } + + helpersFile := filepath.Join(chartDir, "templates", "_helpers.tpl") + err = os.WriteFile(helpersFile, b, os.FileMode(0644)) + if err != nil { + return fmt.Errorf("Error writing _helpers template: %w", err) + } + + if os.Getenv("DOKKU_TRACE") == "1" { + common.CatFile(helpersFile) + } + + // install the chart + helmAgent, err := NewHelmAgent("keda", DeployLogPrinter) + if err != nil { + return fmt.Errorf("Error creating helm agent: %w", err) + } + + chartPath, err := filepath.Abs(chartDir) + if err != nil { + return fmt.Errorf("Error getting chart path: %w", err) + } + + timeoutDuration, err := time.ParseDuration("300s") + if err != nil { + return fmt.Errorf("Error parsing deploy timeout duration: %w", err) + } + + err = helmAgent.InstallOrUpgradeChart(ctx, ChartInput{ + ChartPath: chartPath, + Namespace: "keda", + ReleaseName: fmt.Sprintf("keda-cluster-trigger-authentications-%s", triggerType), + RollbackOnFailure: true, + Timeout: timeoutDuration, + }) + if err != nil { + return fmt.Errorf("Error installing keda-cluster-trigger-authentications-%s chart: %w", triggerType, err) + } + + common.LogInfo1Quiet(fmt.Sprintf("Applied keda-cluster-trigger-authentications-%s chart", triggerType)) + + return nil +} + func applyClusterIssuers(ctx context.Context) error { chartDir, err := os.MkdirTemp("", "cluster-issuer-chart-") if err != nil { diff --git a/plugins/scheduler-k3s/subcommands.go b/plugins/scheduler-k3s/subcommands.go index 2fbd83624..abe02da91 100644 --- a/plugins/scheduler-k3s/subcommands.go +++ b/plugins/scheduler-k3s/subcommands.go @@ -75,6 +75,19 @@ func CommandAutoscalingAuthSet(appName string, trigger string, metadata map[stri return fmt.Errorf("Unable to delete property: %w", err) } } + + if appName == "--global" { + helmAgent, err := NewHelmAgent("keda", DeployLogPrinter) + if err != nil { + return fmt.Errorf("Unable to create helm agent: %w", err) + } + + releaseName := fmt.Sprintf("keda-cluster-trigger-authentications-%s", trigger) + if err := helmAgent.UninstallChart(releaseName); err != nil { + return fmt.Errorf("Unable to uninstall chart: %w", err) + } + } + return nil } @@ -83,6 +96,14 @@ func CommandAutoscalingAuthSet(appName string, trigger string, metadata map[stri return fmt.Errorf("Unable to set property: %w", err) } } + + if appName == "--global" { + err := applyKedaClusterTriggerAuthentications(context.Background(), trigger, metadata) + if err != nil { + return fmt.Errorf("Unable to install chart: %w", err) + } + } + return nil } diff --git a/plugins/scheduler-k3s/template.go b/plugins/scheduler-k3s/template.go index 9aebcc2f1..d5fb94ba3 100644 --- a/plugins/scheduler-k3s/template.go +++ b/plugins/scheduler-k3s/template.go @@ -27,6 +27,14 @@ type ClusterIssuerValues struct { ClusterIssuers map[string]ClusterIssuer `yaml:"cluster_issuers"` } +type ClusterKedaValues struct { + Global struct { + Annotations ProcessAnnotations `yaml:"annotations,omitempty"` + } `yaml:"global"` + Secrets map[string]string `yaml:"secrets"` + Type string `yaml:"type"` +} + type AppValues struct { Global GlobalValues `yaml:"global"` Processes map[string]ProcessValues `yaml:"processes"` diff --git a/plugins/scheduler-k3s/templates/chart/keda-cluster-secret.yaml b/plugins/scheduler-k3s/templates/chart/keda-cluster-secret.yaml index cd9d997eb..b60c4424d 100644 --- a/plugins/scheduler-k3s/templates/chart/keda-cluster-secret.yaml +++ b/plugins/scheduler-k3s/templates/chart/keda-cluster-secret.yaml @@ -4,17 +4,13 @@ apiVersion: v1 kind: Secret metadata: annotations: - app.kubernetes.io/version: {{ $.Values.global.deploment_id | quote }} dokku.com/managed: "true" - {{- if and $.Values.global.annotations $.Values.global.annotations.keda_secret }} - {{- range $k, $v := $.Values.global.annotations.keda_secret }} - {{ $k }}: {{ $v | quote }} - {{- end }} - {{- end }} + {{ include "print.annotations" (dict "config" $.Values.global "key" "keda_secret") | indent 4 }} labels: app.kubernetes.io/instance: {{ $.Values.type }} app.kubernetes.io/name: {{ $.Values.type }} app.kubernetes.io/part-of: cluster + {{ include "print.labels" (dict "config" $.Values.global "key" "keda_secret") | indent 4 }} name: ckta-{{ $.Values.type }} namespace: keda data: diff --git a/plugins/scheduler-k3s/templates/chart/keda-cluster-trigger-authentication.yaml b/plugins/scheduler-k3s/templates/chart/keda-cluster-trigger-authentication.yaml index b74b9d30b..581698fa9 100644 --- a/plugins/scheduler-k3s/templates/chart/keda-cluster-trigger-authentication.yaml +++ b/plugins/scheduler-k3s/templates/chart/keda-cluster-trigger-authentication.yaml @@ -4,17 +4,13 @@ apiVersion: keda.sh/v1alpha1 kind: ClusterTriggerAuthentication metadata: annotations: - app.kubernetes.io/version: {{ $.Values.global.deploment_id | quote }} dokku.com/managed: "true" - {{- if and $.Values.global.annotations $.Values.global.annotations.keda_trigger_authentication }} - {{- range $k, $v := $.Values.global.annotations.keda_trigger_authentication }} - {{ $k }}: {{ $v | quote }} - {{- end }} - {{- end }} + {{ include "print.annotations" (dict "config" $.Values.global "key" "keda_trigger_authentication") | indent 4 }} labels: app.kubernetes.io/instance: {{ $.Values.type }} app.kubernetes.io/name: {{ $.Values.type }} app.kubernetes.io/part-of: cluster + {{ include "print.labels" (dict "config" $.Values.global "key" "keda_trigger_authentication") | indent 4 }} name: {{ $.Values.type }} spec: secretTargetRef: diff --git a/plugins/scheduler-k3s/templates/chart/keda-scaled-object.yaml b/plugins/scheduler-k3s/templates/chart/keda-scaled-object.yaml index 7df2f250d..13e766797 100644 --- a/plugins/scheduler-k3s/templates/chart/keda-scaled-object.yaml +++ b/plugins/scheduler-k3s/templates/chart/keda-scaled-object.yaml @@ -8,20 +8,14 @@ metadata: annotations: app.kubernetes.io/version: {{ $.Values.global.deploment_id | quote }} dokku.com/managed: "true" - {{- 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 }} + {{ include "print.annotations" (dict "config" $.Values.global "key" "keda_scaled_object") | indent 4 }} + {{ include "print.annotations" (dict "config" $config "key" "keda_scaled_object") | indent 4 }} labels: app.kubernetes.io/instance: {{ $.Values.global.app_name }}-{{ $processName }} app.kubernetes.io/name: {{ $processName }} app.kubernetes.io/part-of: {{ $.Values.global.app_name }} + {{ include "print.labels" (dict "config" $.Values.global "key" "keda_scaled_object") | indent 4 }} + {{ include "print.labels" (dict "config" $config "key" "keda_scaled_object") | indent 4 }} name: {{ $.Values.global.app_name}}-{{ $processName }} namespace: {{ $.Values.global.namespace }} spec: diff --git a/plugins/scheduler-k3s/templates/chart/keda-secret.yaml b/plugins/scheduler-k3s/templates/chart/keda-secret.yaml index c906bdde4..d57815ffa 100644 --- a/plugins/scheduler-k3s/templates/chart/keda-secret.yaml +++ b/plugins/scheduler-k3s/templates/chart/keda-secret.yaml @@ -7,15 +7,12 @@ metadata: annotations: app.kubernetes.io/version: {{ $.Values.global.deploment_id | quote }} dokku.com/managed: "true" - {{- if and $.Values.global.annotations $.Values.global.annotations.keda_secret }} - {{- range $k, $v := $.Values.global.annotations.keda_secret }} - {{ $k }}: {{ $v | quote }} - {{- end }} - {{- end }} + {{ include "print.annotations" (dict "config" $.Values.global "key" "keda_secret") | indent 4 }} labels: app.kubernetes.io/instance: {{ $.Values.global.app_name }}-{{ $config.type }} app.kubernetes.io/name: {{ $config.type }} app.kubernetes.io/part-of: {{ $.Values.global.app_name }} + {{ include "print.labels" (dict "config" $.Values.global "key" "keda_secret") | indent 4 }} name: kta-{{ $.Values.global.app_name }}-{{ $config.type }} namespace: {{ $.Values.global.namespace }} data: