feat: implement run:list
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
SUBCOMMANDS = subcommands/initialize subcommands/report subcommands/set subcommands/show-kubeconfig
|
SUBCOMMANDS = subcommands/initialize subcommands/report subcommands/set subcommands/show-kubeconfig
|
||||||
TRIGGERS = triggers/install triggers/post-app-clone-setup triggers/post-app-rename-setup triggers/post-delete triggers/post-registry-login triggers/report triggers/scheduler-deploy triggers/scheduler-enter triggers/scheduler-logs triggers/scheduler-post-delete triggers/scheduler-run triggers/scheduler-stop
|
TRIGGERS = triggers/install triggers/post-app-clone-setup triggers/post-app-rename-setup triggers/post-delete triggers/post-registry-login triggers/report triggers/scheduler-deploy triggers/scheduler-enter triggers/scheduler-logs triggers/scheduler-post-delete triggers/scheduler-run triggers/scheduler-run-list triggers/scheduler-stop
|
||||||
BUILD = commands subcommands triggers
|
BUILD = commands subcommands triggers
|
||||||
PLUGIN_NAME = scheduler-k3s
|
PLUGIN_NAME = scheduler-k3s
|
||||||
|
|
||||||
|
|||||||
@@ -109,6 +109,11 @@ func main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
err = scheduler_k3s.TriggerSchedulerRun(scheduler, appName, envCount, args)
|
err = scheduler_k3s.TriggerSchedulerRun(scheduler, appName, envCount, args)
|
||||||
|
case "scheduler-run-list":
|
||||||
|
scheduler := flag.Arg(0)
|
||||||
|
appName := flag.Arg(1)
|
||||||
|
format := flag.Arg(2)
|
||||||
|
err = scheduler_k3s.TriggerSchedulerRunList(scheduler, appName, format)
|
||||||
default:
|
default:
|
||||||
err = fmt.Errorf("Invalid plugin trigger call: %s", trigger)
|
err = fmt.Errorf("Invalid plugin trigger call: %s", trigger)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import (
|
|||||||
"bufio"
|
"bufio"
|
||||||
"context"
|
"context"
|
||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
@@ -21,6 +22,7 @@ import (
|
|||||||
"github.com/fatih/color"
|
"github.com/fatih/color"
|
||||||
"github.com/kballard/go-shellquote"
|
"github.com/kballard/go-shellquote"
|
||||||
"github.com/rancher/wharfie/pkg/registries"
|
"github.com/rancher/wharfie/pkg/registries"
|
||||||
|
"github.com/ryanuber/columnize"
|
||||||
orderedmap "github.com/wk8/go-ordered-map/v2"
|
orderedmap "github.com/wk8/go-ordered-map/v2"
|
||||||
"gopkg.in/yaml.v3"
|
"gopkg.in/yaml.v3"
|
||||||
appsv1 "k8s.io/api/apps/v1"
|
appsv1 "k8s.io/api/apps/v1"
|
||||||
@@ -951,6 +953,74 @@ func TriggerSchedulerRun(scheduler string, appName string, envCount int, args []
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TriggerSchedulerRunList lists one-off run pods for a given application
|
||||||
|
func TriggerSchedulerRunList(scheduler string, appName string, format string) error {
|
||||||
|
if scheduler != "k3s" {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
clientset, err := NewKubernetesClient()
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("Error creating kubernetes client: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
namespace := common.PropertyGetDefault("scheduler-k3s", appName, "namespace", "default")
|
||||||
|
|
||||||
|
ctx := context.Background()
|
||||||
|
cronJobs, err := clientset.ListCronJobs(ctx, ListCronJobsInput{
|
||||||
|
LabelSelector: fmt.Sprintf("dokku.com/app-name=%s", appName),
|
||||||
|
Namespace: namespace,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("Error getting cron jobs: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
type CronJobEntry struct {
|
||||||
|
ID string `json:"id"`
|
||||||
|
AppName string `json:"app"`
|
||||||
|
Command string `json:"command"`
|
||||||
|
Schedule string `json:"schedule"`
|
||||||
|
}
|
||||||
|
|
||||||
|
data := []CronJobEntry{}
|
||||||
|
lines := []string{"ID | Schedule | Command"}
|
||||||
|
for _, cronJob := range cronJobs {
|
||||||
|
command := ""
|
||||||
|
for _, container := range cronJob.Spec.JobTemplate.Spec.Template.Spec.Containers {
|
||||||
|
if container.Name == fmt.Sprintf("%s-cron", appName) {
|
||||||
|
command = strings.Join(container.Args, " ")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
cronID, ok := cronJob.Labels["dokku.com/cron-id"]
|
||||||
|
if !ok {
|
||||||
|
common.LogWarn(fmt.Sprintf("Cron job %s does not have a cron ID label", cronJob.Name))
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
lines = append(lines, fmt.Sprintf("%s | %s | %s", cronID, cronJob.Spec.Schedule, command))
|
||||||
|
data = append(data, CronJobEntry{
|
||||||
|
ID: cronID,
|
||||||
|
AppName: appName,
|
||||||
|
Command: command,
|
||||||
|
Schedule: cronJob.Spec.Schedule,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
if format == "stdout" {
|
||||||
|
result := columnize.SimpleFormat(lines)
|
||||||
|
fmt.Println(result)
|
||||||
|
} else {
|
||||||
|
b, err := json.Marshal(data)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("Error marshalling cron jobs: %w", err)
|
||||||
|
}
|
||||||
|
fmt.Println(string(b))
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
// TriggerSchedulerPostDelete destroys the scheduler-k3s data for a given app container
|
// TriggerSchedulerPostDelete destroys the scheduler-k3s data for a given app container
|
||||||
func TriggerSchedulerPostDelete(scheduler string, appName string) error {
|
func TriggerSchedulerPostDelete(scheduler string, appName string) error {
|
||||||
if scheduler != "k3s" {
|
if scheduler != "k3s" {
|
||||||
|
|||||||
Reference in New Issue
Block a user