diff --git a/plugins/scheduler-k3s/Makefile b/plugins/scheduler-k3s/Makefile index 870e3ba05..e894570d5 100644 --- a/plugins/scheduler-k3s/Makefile +++ b/plugins/scheduler-k3s/Makefile @@ -1,5 +1,5 @@ SUBCOMMANDS = subcommands/annotations:set subcommands/autoscaling-auth:set subcommands/autoscaling-auth:report subcommands/cluster-add subcommands/cluster-list subcommands/cluster-remove subcommands/ensure-charts subcommands/initialize subcommands/labels:set subcommands/report subcommands/set subcommands/show-kubeconfig subcommands/uninstall -TRIGGERS = triggers/core-post-deploy triggers/core-post-extract triggers/install triggers/post-app-clone-setup triggers/post-app-rename-setup triggers/post-create triggers/post-delete triggers/report triggers/scheduler-app-status triggers/scheduler-deploy triggers/scheduler-enter triggers/scheduler-logs triggers/scheduler-proxy-config triggers/scheduler-proxy-logs triggers/scheduler-post-delete triggers/scheduler-run triggers/scheduler-run-list triggers/scheduler-stop +TRIGGERS = triggers/core-post-deploy triggers/core-post-extract triggers/install triggers/post-app-clone-setup triggers/post-app-rename-setup triggers/post-create triggers/post-delete triggers/report triggers/scheduler-app-status triggers/scheduler-deploy triggers/scheduler-enter triggers/scheduler-is-deployed triggers/scheduler-logs triggers/scheduler-proxy-config triggers/scheduler-proxy-logs triggers/scheduler-post-delete triggers/scheduler-run triggers/scheduler-run-list triggers/scheduler-stop BUILD = commands subcommands triggers PLUGIN_NAME = scheduler-k3s diff --git a/plugins/scheduler-k3s/functions.go b/plugins/scheduler-k3s/functions.go index 16b9a1ab6..a2084c305 100644 --- a/plugins/scheduler-k3s/functions.go +++ b/plugins/scheduler-k3s/functions.go @@ -576,7 +576,6 @@ func getAutoscaling(input GetAutoscalingInput) (ProcessAutoscaling, error) { } if !ok { - common.LogWarn(fmt.Sprintf("No autoscaling config found for %s:%s", input.AppName, input.ProcessType)) return ProcessAutoscaling{}, nil } diff --git a/plugins/scheduler-k3s/helm.go b/plugins/scheduler-k3s/helm.go index e9dd9f97e..3f5e49df4 100644 --- a/plugins/scheduler-k3s/helm.go +++ b/plugins/scheduler-k3s/helm.go @@ -264,6 +264,7 @@ func (h *HelmAgent) InstallChart(ctx context.Context, input ChartInput) error { } kustomizeRenderer := KustomizeRenderer{ + ReleaseName: input.ReleaseName, KustomizeRootPath: input.KustomizeRootPath, } @@ -387,6 +388,7 @@ func (h *HelmAgent) UpgradeChart(ctx context.Context, input ChartInput) error { } kustomizeRenderer := KustomizeRenderer{ + ReleaseName: input.ReleaseName, KustomizeRootPath: input.KustomizeRootPath, } @@ -464,10 +466,16 @@ func (p *DebugRenderer) Run(renderedManifests *bytes.Buffer) (*bytes.Buffer, err return renderedManifests, nil } +// KustomizeRenderer is a post renderer that kustomizes the rendered manifests type KustomizeRenderer struct { + // KustomizeRootPath is the path to the kustomize root path to use KustomizeRootPath string + + // ReleaseName is the name of the release to kustomize + ReleaseName string } +// Run kustomizes the rendered manifests func (p *KustomizeRenderer) Run(renderedManifests *bytes.Buffer) (*bytes.Buffer, error) { if p.KustomizeRootPath == "" { return renderedManifests, nil @@ -477,6 +485,7 @@ func (p *KustomizeRenderer) Run(renderedManifests *bytes.Buffer) (*bytes.Buffer, return renderedManifests, nil } + common.LogVerboseQuiet(fmt.Sprintf("Applying kustomization to %s", p.ReleaseName)) fs, err := filesys.MakeFsOnDiskSecureBuild(p.KustomizeRootPath) if err != nil { return nil, fmt.Errorf("Error creating filesystem: %w", err) diff --git a/plugins/scheduler-k3s/src/triggers/triggers.go b/plugins/scheduler-k3s/src/triggers/triggers.go index 692c62e1f..dd48a6b71 100644 --- a/plugins/scheduler-k3s/src/triggers/triggers.go +++ b/plugins/scheduler-k3s/src/triggers/triggers.go @@ -72,6 +72,10 @@ func main() { } err = scheduler_k3s.TriggerSchedulerEnter(scheduler, appName, containerType, ptr.Deref(podIdentifier, ""), args) + case "scheduler-is-deployed": + scheduler := flag.Arg(0) + appName := flag.Arg(1) + err = scheduler_k3s.TriggerSchedulerIsDeployed(scheduler, appName) case "scheduler-logs": var tail bool var quiet bool diff --git a/plugins/scheduler-k3s/templates/chart/cron-job.yaml b/plugins/scheduler-k3s/templates/chart/cron-job.yaml index cad22ce7c..7aedc8be2 100644 --- a/plugins/scheduler-k3s/templates/chart/cron-job.yaml +++ b/plugins/scheduler-k3s/templates/chart/cron-job.yaml @@ -1,6 +1,6 @@ {{- range $processName, $config := .Values.processes }} -# Only process cron jobs {{- if not (and (hasKey $config "cron") (hasKey $config.cron "id")) }} +# Skip {{ $processName }} as it is not a cron job {{- continue }} {{- end }} --- diff --git a/plugins/scheduler-k3s/templates/chart/deployment.yaml b/plugins/scheduler-k3s/templates/chart/deployment.yaml index a741804d1..30a38ec12 100644 --- a/plugins/scheduler-k3s/templates/chart/deployment.yaml +++ b/plugins/scheduler-k3s/templates/chart/deployment.yaml @@ -1,6 +1,6 @@ {{- range $processName, $config := .Values.processes }} -{{- if not (hasKey $config "web") }} -# Skip {{ $processName }} as it doesn't have a web section +{{- if hasKey $config "cron" }} +# Skip {{ $processName }} as it is a cron job {{- continue }} {{- end }} diff --git a/plugins/scheduler-k3s/templates/chart/keda-scaled-object.yaml b/plugins/scheduler-k3s/templates/chart/keda-scaled-object.yaml index 3f37603ac..ac0f61f23 100644 --- a/plugins/scheduler-k3s/templates/chart/keda-scaled-object.yaml +++ b/plugins/scheduler-k3s/templates/chart/keda-scaled-object.yaml @@ -1,11 +1,11 @@ {{- if .Capabilities.APIVersions.Has "keda.sh/v1alpha1" -}} {{- range $processName, $config := .Values.processes }} -{{- if not (hasKey $config "web") }} -# Skip {{ $processName }} as it doesn't have a web section +{{- if hasKey $config "cron" }} +# Skip {{ $processName }} as it is a cron job {{- continue }} {{- end }} -{{- if and $config.autoscaling (and $config.autoscaling.enabled (eq $config.autoscaling.type "keda")) }} +{{- if and (hasKey $config "autoscaling") (and $config.autoscaling.enabled (eq $config.autoscaling.type "keda")) }} --- apiVersion: keda.sh/v1alpha1 kind: ScaledObject diff --git a/plugins/scheduler-k3s/triggers.go b/plugins/scheduler-k3s/triggers.go index 7c0a9f8c4..0207d42f3 100644 --- a/plugins/scheduler-k3s/triggers.go +++ b/plugins/scheduler-k3s/triggers.go @@ -789,6 +789,32 @@ func TriggerSchedulerDeploy(scheduler string, appName string, imageTag string) e return nil } +// TriggerSchedulerIsDeployed returns true if given app has a running container +func TriggerSchedulerIsDeployed(scheduler string, appName string) error { + if scheduler != "k3s" { + return nil + } + + // check if there are any helm revisions for the specified appName + helmAgent, err := NewHelmAgent(getComputedNamespace(appName), DeployLogPrinter) + if err != nil { + return fmt.Errorf("Error creating helm agent: %w", err) + } + + revisions, err := helmAgent.ListRevisions(ListRevisionsInput{ + ReleaseName: appName, + }) + if err != nil { + return fmt.Errorf("Error listing helm revisions: %w", err) + } + + if len(revisions) > 0 { + return nil + } + + return fmt.Errorf("App %s is not deployed", appName) +} + // TriggerSchedulerEnter enters a container for a given application func TriggerSchedulerEnter(scheduler string, appName string, processType string, podName string, args []string) error { if scheduler != "k3s" { diff --git a/tests/unit/scheduler-k3s-3.bats b/tests/unit/scheduler-k3s-3.bats index c6dba70c6..29d0f2f27 100644 --- a/tests/unit/scheduler-k3s-3.bats +++ b/tests/unit/scheduler-k3s-3.bats @@ -29,6 +29,11 @@ teardown_() { echo "status: $status" assert_success + run /bin/bash -c "dokku ps:scale $TEST_APP web=1 worker=2" + echo "output: $output" + echo "status: $status" + assert_success + run deploy_app python dokku@$DOKKU_DOMAIN:$TEST_APP inject_kustomization_yaml echo "output: $output" echo "status: $status" @@ -39,6 +44,12 @@ teardown_() { echo "status: $status" assert_success assert_output "3" + + run /bin/bash -c "kubectl get deployment $TEST_APP-worker -o json | jq -r '.spec.replicas'" + echo "output: $output" + echo "status: $status" + assert_success + assert_output "2" } inject_kustomization_yaml() { @@ -58,8 +69,9 @@ patches: path: /spec/replicas value: 3 target: - group: apps - version: v1 - kind: Deployment + group: apps + version: v1 + kind: Deployment + name: $APP-web EOF }