diff --git a/plugins/scheduler-k3s/Makefile b/plugins/scheduler-k3s/Makefile index 449103c2c..c0c9165bd 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/install triggers/post-app-clone-setup triggers/post-app-rename-setup 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-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 BUILD = commands subcommands triggers PLUGIN_NAME = scheduler-k3s diff --git a/plugins/scheduler-k3s/functions.go b/plugins/scheduler-k3s/functions.go index a7ec92f92..472c06025 100644 --- a/plugins/scheduler-k3s/functions.go +++ b/plugins/scheduler-k3s/functions.go @@ -1494,6 +1494,19 @@ func getStartCommand(input StartCommandInput) (StartCommandOutput, error) { }, nil } +func hasKustomizeRootPath(appName string) bool { + kustomizeRootPath := getComputedKustomizeRootPath(appName) + if common.DirectoryExists(fmt.Sprintf("%s.%s.missing", kustomizeRootPath, os.Getenv("DOKKU_PID"))) { + return false + } + + if common.DirectoryExists(fmt.Sprintf("%s.%s", kustomizeRootPath, os.Getenv("DOKKU_PID"))) { + return true + } + + return common.DirectoryExists(kustomizeRootPath) +} + func installHelmCharts(ctx context.Context, clientset KubernetesClient, shouldInstall func(HelmChart) bool) error { for _, repo := range HelmRepositories { helmAgent, err := NewHelmAgent("default", DeployLogPrinter) diff --git a/plugins/scheduler-k3s/src/triggers/triggers.go b/plugins/scheduler-k3s/src/triggers/triggers.go index 4092da096..27b6bdc98 100644 --- a/plugins/scheduler-k3s/src/triggers/triggers.go +++ b/plugins/scheduler-k3s/src/triggers/triggers.go @@ -22,6 +22,13 @@ func main() { var err error switch trigger { + case "core-post-deploy": + appName := flag.Arg(0) + err = scheduler_k3s.TriggerCorePostDeploy(appName) + case "core-post-extract": + appName := flag.Arg(0) + sourceWorkDir := flag.Arg(1) + err = scheduler_k3s.TriggerCorePostExtract(appName, sourceWorkDir) case "install": err = scheduler_k3s.TriggerInstall() case "post-app-clone-setup": diff --git a/plugins/scheduler-k3s/triggers.go b/plugins/scheduler-k3s/triggers.go index a228ac048..5fb53b6f1 100644 --- a/plugins/scheduler-k3s/triggers.go +++ b/plugins/scheduler-k3s/triggers.go @@ -11,6 +11,7 @@ import ( "io" "os" "os/signal" + "path" "path/filepath" "sort" "strconv" @@ -32,6 +33,85 @@ import ( "k8s.io/kubernetes/pkg/client/conditions" ) +// TriggerCorePostDeploy moves a configured kustomize root path to be in the app root dir +func TriggerCorePostDeploy(appName string) error { + existingKustomizeRootPath := getComputedKustomizeRootPath(appName) + processSpecificKustomizeRootPath := fmt.Sprintf("%s.%s", existingKustomizeRootPath, os.Getenv("DOKKU_PID")) + if common.DirectoryExists(processSpecificKustomizeRootPath) { + if err := os.Rename(processSpecificKustomizeRootPath, existingKustomizeRootPath); err != nil { + return err + } + } else if common.FileExists(fmt.Sprintf("%s.missing", processSpecificKustomizeRootPath)) { + if err := os.Remove(fmt.Sprintf("%s.missing", processSpecificKustomizeRootPath)); err != nil { + return err + } + + if common.DirectoryExists(existingKustomizeRootPath) { + if err := os.Remove(existingKustomizeRootPath); err != nil { + return err + } + } + } + + return nil +} + +// TriggerCorePostExtract moves a configured kustomize root path to be in the app root dir +func TriggerCorePostExtract(appName string, sourceWorkDir string) error { + kustomizeRootPath := getComputedKustomizeRootPath(appName) + if kustomizeRootPath == "" { + return nil + } + + directory := filepath.Join(common.MustGetEnv("DOKKU_LIB_ROOT"), "data", "scheduler-k3s", appName) + existingKustomizeDirectory := filepath.Join(directory, "kustomization") + files, err := filepath.Glob(fmt.Sprintf("%s.*", existingKustomizeDirectory)) + if err != nil { + return err + } + for _, f := range files { + if err := os.Remove(f); err != nil { + return err + } + } + + processSpecificKustomizeRootPath := fmt.Sprintf("%s.%s", kustomizeRootPath, os.Getenv("DOKKU_PID")) + results, _ := common.CallPlugnTrigger(common.PlugnTriggerInput{ + Trigger: "git-get-property", + Args: []string{appName, "source-image"}, + }) + appSourceImage := results.StdoutContents() + + if appSourceImage == "" { + repoDefaultKustomizeRootPath := path.Join(sourceWorkDir, "kustomization") + repoKustomizeRootPath := path.Join(sourceWorkDir, kustomizeRootPath) + if !common.DirectoryExists(repoKustomizeRootPath) { + if kustomizeRootPath != "kustomization" && common.DirectoryExists(repoDefaultKustomizeRootPath) { + if err := os.RemoveAll(repoDefaultKustomizeRootPath); err != nil { + return fmt.Errorf("Unable to remove existing kustomize directory: %s", err.Error()) + } + } + return common.TouchDir(fmt.Sprintf("%s.missing", processSpecificKustomizeRootPath)) + } + + if err := common.Copy(repoKustomizeRootPath, processSpecificKustomizeRootPath); err != nil { + return fmt.Errorf("Unable to extract kustomize root path: %s", err.Error()) + } + + if kustomizeRootPath != "kustomization" { + if err := common.Copy(repoKustomizeRootPath, repoDefaultKustomizeRootPath); err != nil { + return fmt.Errorf("Unable to move kustomize root path into place: %s", err.Error()) + } + } + } else { + if err := common.CopyDirFromImage(appName, appSourceImage, kustomizeRootPath, processSpecificKustomizeRootPath); err != nil { + return common.TouchDir(fmt.Sprintf("%s.missing", processSpecificKustomizeRootPath)) + } + } + + return nil +} + // TriggerInstall runs the install step for the scheduler-k3s plugin func TriggerInstall() error { if err := common.PropertySetup("scheduler-k3s"); err != nil {