-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(harness): support approval flow and more (#194)
* wait for approval, upload human-readable tf plan after plan stage and remove global timeout for harness run * omit arm64 for harness * initialize the map * set stack status on plan upload * memoize approval state (won't change once approved) --------- Co-authored-by: michaeljguarino <[email protected]>
- Loading branch information
1 parent
fdd464a
commit 45b3942
Showing
16 changed files
with
247 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
package controller | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"time" | ||
|
||
gqlclient "github.com/pluralsh/console-client-go" | ||
"k8s.io/apimachinery/pkg/util/wait" | ||
"k8s.io/klog/v2" | ||
|
||
"github.com/pluralsh/deployment-operator/pkg/harness/environment" | ||
internalerrors "github.com/pluralsh/deployment-operator/pkg/harness/errors" | ||
"github.com/pluralsh/deployment-operator/pkg/harness/stackrun" | ||
"github.com/pluralsh/deployment-operator/pkg/log" | ||
) | ||
|
||
var ( | ||
runApproved = false | ||
) | ||
|
||
func (in *stackRunController) preStart() { | ||
if in.stackRun.Status != gqlclient.StackStatusPending && !environment.IsDev() { | ||
klog.Fatalf("could not start stack run: invalid status: %s", in.stackRun.Status) | ||
} | ||
|
||
if err := in.markStackRun(gqlclient.StackStatusRunning); err != nil { | ||
klog.ErrorS(err, "could not update stack run status") | ||
} | ||
} | ||
|
||
func (in *stackRunController) postStart(err error) error { | ||
var status gqlclient.StackStatus | ||
|
||
switch { | ||
case err == nil: | ||
status = gqlclient.StackStatusSuccessful | ||
case errors.Is(err, internalerrors.ErrRemoteCancel): | ||
status = gqlclient.StackStatusCancelled | ||
default: | ||
status = gqlclient.StackStatusFailed | ||
} | ||
|
||
if err := in.completeStackRun(status, err); err != nil { | ||
klog.ErrorS(err, "could not complete stack run") | ||
} | ||
|
||
klog.V(log.LogLevelInfo).InfoS("stack run completed") | ||
return err | ||
} | ||
|
||
func (in *stackRunController) postStepRun(id string, err error) { | ||
var status gqlclient.StepStatus | ||
|
||
switch { | ||
case err == nil: | ||
status = gqlclient.StepStatusSuccessful | ||
default: | ||
status = gqlclient.StepStatusFailed | ||
} | ||
|
||
if err := in.markStackRunStep(id, status); err != nil { | ||
klog.ErrorS(err, "could not update stack run step status") | ||
} | ||
} | ||
|
||
func (in *stackRunController) preStepRun(id string) { | ||
if err := in.markStackRunStep(id, gqlclient.StepStatusRunning); err != nil { | ||
klog.ErrorS(err, "could not update stack run status") | ||
} | ||
} | ||
|
||
func (in *stackRunController) postExecHook(stage gqlclient.StepStage, id string) stackrun.HookFunction { | ||
return func() error { | ||
if stage != gqlclient.StepStagePlan { | ||
return nil | ||
} | ||
|
||
return in.uploadPlan() | ||
} | ||
} | ||
|
||
func (in *stackRunController) preExecHook(stage gqlclient.StepStage, id string) stackrun.HookFunction { | ||
return func() error { | ||
if stage == gqlclient.StepStageApply { | ||
if err := in.approvalCheck(); err != nil { | ||
return err | ||
} | ||
} | ||
|
||
if err := in.markStackRunStep(id, gqlclient.StepStatusRunning); err != nil { | ||
klog.ErrorS(err, "could not update stack run status") | ||
} | ||
return nil | ||
} | ||
} | ||
|
||
func (in *stackRunController) approvalCheck() error { | ||
if !in.stackRun.Approval || runApproved { | ||
return nil | ||
} | ||
|
||
return wait.PollUntilContextCancel(context.Background(), 5*time.Second, true, func(_ context.Context) (done bool, err error) { | ||
if runApproved { | ||
return true, nil | ||
} | ||
|
||
stack, err := in.consoleClient.GetStackRun(in.stackRunID) | ||
if err != nil { | ||
klog.ErrorS(err, "could not check stack run approval") | ||
return false, nil | ||
} | ||
|
||
runApproved = stack.ApprovedAt != nil | ||
return runApproved, nil | ||
}) | ||
} | ||
|
||
func (in *stackRunController) uploadPlan() error { | ||
state, err := in.tool.Plan() | ||
if err != nil { | ||
klog.ErrorS(err, "could not prepare plan") | ||
} | ||
|
||
return in.consoleClient.UpdateStackRun(in.stackRunID, gqlclient.StackRunAttributes{ | ||
State: state, | ||
Status: gqlclient.StackStatusRunning, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.