From 85fff0a4013e66ac8aa208dd9d646a91d655c4ce Mon Sep 17 00:00:00 2001 From: Hidde Beydals Date: Thu, 19 Sep 2024 23:30:50 +0200 Subject: [PATCH] feat(controller): configure work dir for promotion (#2547) Signed-off-by: Hidde Beydals --- internal/controller/promotions/promotions.go | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/internal/controller/promotions/promotions.go b/internal/controller/promotions/promotions.go index 113c94cdc..07c237bdf 100644 --- a/internal/controller/promotions/promotions.go +++ b/internal/controller/promotions/promotions.go @@ -3,6 +3,8 @@ package promotions import ( "context" "fmt" + "os" + "path/filepath" "slices" "strconv" "sync" @@ -522,20 +524,35 @@ func (r *reconciler) promote( }) } + workDir := filepath.Join(os.TempDir(), "promotion-"+string(workingPromo.UID)) + if err := os.MkdirAll(workDir, 0o700); err != nil && !os.IsExist(err) { + return nil, fmt.Errorf("error creating working directory: %w", err) + } + defer func() { + if workingPromo.Status.Phase.IsTerminal() { + if err := os.RemoveAll(workDir); err != nil { + logger.Error(err, "could not remove working directory") + } + } + }() + status, err := r.directivesEngine.Execute(ctx, directives.PromotionContext{ + WorkDir: workDir, Project: stageNamespace, Stage: stageName, FreightRequests: stage.Spec.RequestedFreight, Freight: *workingPromo.Status.FreightCollection.DeepCopy(), }, steps) switch status { + case directives.StatusPending: + workingPromo.Status.Phase = kargoapi.PromotionPhaseRunning case directives.StatusSuccess: workingPromo.Status.Phase = kargoapi.PromotionPhaseSucceeded case directives.StatusFailure: workingPromo.Status.Phase = kargoapi.PromotionPhaseFailed } if err != nil { - return nil, err + return &workingPromo.Status, err } }