Skip to content

Commit

Permalink
Improve workdir cleanup logic (#195)
Browse files Browse the repository at this point in the history
* Improve workdir cleanup logic

* Changelog

* Revert unnecessary change
  • Loading branch information
Vivek Lakshmanan authored Sep 15, 2021
1 parent d7f1737 commit 55213bb
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ CHANGELOG
=========

## HEAD (Unreleased)
(None)
- Improve workdir cleanup logic [#195](https://github.com/pulumi/pulumi-kubernetes-operator/pull/195)

## 0.0.19 (2021-09-02)
- Add support for safe upgrades and graceful shutdowns [#189](https://github.com/pulumi/pulumi-kubernetes-operator/pull/189)
Expand Down
26 changes: 21 additions & 5 deletions pkg/controller/stack/stack_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,14 +174,14 @@ func (r *ReconcileStack) Reconcile(ctx context.Context, request reconcile.Reques
return reconcile.Result{}, err
}

// Delete the working directory after the reconciliation is completed (regardless of success or failure).
defer sess.CleanupPulumiWorkdir()

currentCommit, err := revisionAtWorkingDir(sess.workdir)
if err != nil {
return reconcile.Result{}, err
}

// Delete the working directory after the reconciliation is completed (regardless of success or failure).
defer sess.CleanupPulumiWorkdir()

// Step 2. If there are extra environment variables, read them in now and use them for subsequent commands.
if err = sess.SetEnvs(stack.Envs, request.Namespace); err != nil {
reqLogger.Error(err, "Could not find ConfigMap for Envs")
Expand Down Expand Up @@ -599,7 +599,22 @@ func (sess *reconcileStackSession) SetupPulumiWorkdir(gitAuth *auto.GitAuth) err
sess.logger.Debug("Setting up pulumi workdir for stack", "stack", sess.stack)
// Create a new workspace.
secretsProvider := auto.SecretsProvider(sess.stack.SecretsProvider)
w, err := auto.NewLocalWorkspace(context.Background(), auto.Repo(repo), secretsProvider)

// Create the temporary workdir
dir, err := os.MkdirTemp("", "pulumi_auto")
if err != nil {
return errors.Wrap(err, "unable to create tmp directory for workspace")
}

// Cleanup the workdir on failure setting up the workspace.
defer func() {
if err != nil {
_ = os.RemoveAll(dir)
}
}()

var w auto.Workspace
w, err = auto.NewLocalWorkspace(context.Background(), auto.WorkDir(dir), auto.Repo(repo), secretsProvider)
if err != nil {
return errors.Wrap(err, "failed to create local workspace")
}
Expand Down Expand Up @@ -633,7 +648,8 @@ func (sess *reconcileStackSession) SetupPulumiWorkdir(gitAuth *auto.GitAuth) err
sess.autoStack = &a
sess.logger.Debug("Setting autostack", "autostack", sess.autoStack)

c, err := sess.autoStack.GetAllConfig(ctx)
var c auto.ConfigMap
c, err = sess.autoStack.GetAllConfig(ctx)
if err != nil {
return err
}
Expand Down

0 comments on commit 55213bb

Please sign in to comment.