Skip to content

Commit

Permalink
Address feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
metral committed Aug 1, 2020
1 parent 36004f7 commit 34b8bc1
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 22 deletions.
7 changes: 5 additions & 2 deletions pkg/apis/pulumi/v1alpha1/stack_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ type StackUpdateState struct {
// State is the state of the stack update - one of `succeeded` or `failed`
State string `json:"state,omitempty"`
// Permalink is the Pulumi Console URL of the stack operation.
Permalink string `json:"url,omitempty"`
Permalink Permalink `json:"permalink,omitempty"`
}

// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
Expand Down Expand Up @@ -137,6 +137,9 @@ const (
StackNotFound StackUpdateStatus = 4
)

// FailedStackStateMessage is a const to indicate stack failure in the status.
const FailedStackStateMessage = "failed"

// ProjectSourceOptions is the settings to work with the project source repo.
type ProjectSourceOptions struct {
// The access token to access project source repo. This is required for
Expand All @@ -155,7 +158,7 @@ type ProjectSourceOptions struct {
}

// Permalink is the Pulumi Service URL of the stack operation.
type Permalink *string
type Permalink string

// StackController contains methods to operate a Pulumi Project and Stack in an update.
//
Expand Down
55 changes: 35 additions & 20 deletions pkg/controller/stack/stack_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"os/exec"
"path"
"path/filepath"
"regexp"
"strings"
"time"

Expand Down Expand Up @@ -45,6 +46,9 @@ var log = logf.Log.WithName("controller_stack")
const pulumiFinalizer = "finalizer.stack.pulumi.com"
const maxConcurrentReconciles = 10 // arbitrary value greater than default of 1
const consoleURL = "https://app.pulumi.com"
const permalinkSearchStr = "Permalink: "

var regex = regexp.MustCompile(permalinkSearchStr)

// Add creates a new Stack Controller and adds it to the Manager. The Manager will set fields on the Controller
// and Start it when the Manager is Started.
Expand Down Expand Up @@ -198,6 +202,7 @@ func (r *ReconcileStack) Reconcile(request reconcile.Request) (reconcile.Result,
if err != nil {
return reconcile.Result{}, err
}
time.Sleep(2 * time.Second) // arbitrary sleep after finalizer add to avoid stale obj for permalink
// Add default permalink for the stack in the Pulumi Service.
sess.addDefaultPermalink(instance)
}
Expand Down Expand Up @@ -231,10 +236,10 @@ func (r *ReconcileStack) Reconcile(request reconcile.Request) (reconcile.Result,
}
if instance.Status.LastUpdate == nil {
instance.Status.LastUpdate = &pulumiv1alpha1.StackUpdateState{
Permalink: *permalink,
Permalink: permalink,
}
} else {
instance.Status.LastUpdate.Permalink = *permalink
instance.Status.LastUpdate.Permalink = permalink
}
err = sess.updateResourceStatus(instance)
if err != nil {
Expand Down Expand Up @@ -263,8 +268,8 @@ func (r *ReconcileStack) Reconcile(request reconcile.Request) (reconcile.Result,
reqLogger.Error(err, "Failed to update Stack", "Stack.Name", stack.Stack)
// Update Stack status with failed state
instance.Status.LastUpdate = &pulumiv1alpha1.StackUpdateState{
State: "failed",
Permalink: *permalink,
State: pulumiv1alpha1.FailedStackStateMessage,
Permalink: permalink,
}
if err2 := sess.updateResourceStatus(instance); err2 != nil {
msg := "Failed to update status for a failed Stack update"
Expand Down Expand Up @@ -295,11 +300,11 @@ func (r *ReconcileStack) Reconcile(request reconcile.Request) (reconcile.Result,
if instance.Status.LastUpdate == nil {
instance.Status.LastUpdate = &pulumiv1alpha1.StackUpdateState{
State: instance.Spec.Commit,
Permalink: *permalink,
Permalink: permalink,
}
} else {
instance.Status.LastUpdate.State = instance.Spec.Commit
instance.Status.LastUpdate.Permalink = *permalink
instance.Status.LastUpdate.Permalink = permalink
}
err = sess.updateResourceStatus(instance)
if err != nil {
Expand Down Expand Up @@ -647,9 +652,13 @@ func (sess *reconcileStackSession) RefreshStack(expectNoChanges bool) (pulumiv1a
}
stdout, _, err := sess.pulumi(cmdArgs...)
if err != nil {
return nil, errors.Wrapf(err, "refreshing stack '%s'", sess.stack.Stack)
return pulumiv1alpha1.Permalink(""), errors.Wrapf(err, "refreshing stack '%s'", sess.stack.Stack)
}
permalink, err := sess.getPermalink(stdout)
if err != nil {
return pulumiv1alpha1.Permalink(""), err
}
return sess.getPermalink(stdout), nil
return permalink, nil
}

// UpdateStack runs the update on the stack and returns an update status code
Expand All @@ -660,15 +669,19 @@ func (sess *reconcileStackSession) UpdateStack() (pulumiv1alpha1.StackUpdateStat
if err != nil {
// If this is the "conflict" error message, we will want to gracefully quit and retry.
if strings.Contains(stderr, "error: [409] Conflict: Another update is currently in progress") {
return pulumiv1alpha1.StackUpdateConflict, nil, err
return pulumiv1alpha1.StackUpdateConflict, pulumiv1alpha1.Permalink(""), err
}
// If this is the "not found" error message, we will want to gracefully quit and retry.
if strings.Contains(stderr, "error: [404] Not found") {
return pulumiv1alpha1.StackNotFound, nil, err
return pulumiv1alpha1.StackNotFound, pulumiv1alpha1.Permalink(""), err
}
return pulumiv1alpha1.StackUpdateFailed, nil, err
return pulumiv1alpha1.StackUpdateFailed, pulumiv1alpha1.Permalink(""), err
}
return pulumiv1alpha1.StackUpdateSucceeded, sess.getPermalink(stdout), nil
permalink, err := sess.getPermalink(stdout)
if err != nil {
return pulumiv1alpha1.StackUpdateFailed, pulumiv1alpha1.Permalink(""), err
}
return pulumiv1alpha1.StackUpdateSucceeded, permalink, nil
}

// GetPulumiOutputs gets the stack outputs and parses them into a map.
Expand Down Expand Up @@ -700,12 +713,14 @@ func (sess *reconcileStackSession) DestroyStack() error {
}

// Hack to parse the permalink until we have a programmatic way of doing so.
func (sess *reconcileStackSession) getPermalink(stdout string) pulumiv1alpha1.Permalink {
substr := "Permalink: "
idx := strings.Index(stdout, substr)
p := stdout[idx+len(substr):]
p = strings.TrimSuffix(p, "\n")
return pulumiv1alpha1.Permalink(&p)
func (sess *reconcileStackSession) getPermalink(stdout string) (pulumiv1alpha1.Permalink, error) {
found := regex.FindStringIndex(stdout)
if found == nil {
return pulumiv1alpha1.Permalink(""), errors.New(fmt.Sprintf("getting permalink for stack '%s'", sess.stack.Stack))
}
substr := stdout[found[1]:]
p := strings.TrimSuffix(substr, "\n")
return pulumiv1alpha1.Permalink(p), nil
}

// Add default permalink for the stack in the Pulumi Service.
Expand All @@ -718,10 +733,10 @@ func (sess *reconcileStackSession) addDefaultPermalink(stack *pulumiv1alpha1.Sta
}
if stack.Status.LastUpdate == nil {
stack.Status.LastUpdate = &pulumiv1alpha1.StackUpdateState{
Permalink: fmt.Sprintf("%s/%s", consoleURL, stack.Spec.Stack),
Permalink: pulumiv1alpha1.Permalink(fmt.Sprintf("%s/%s", consoleURL, stack.Spec.Stack)),
}
} else {
stack.Status.LastUpdate.Permalink = fmt.Sprintf("%s/%s", consoleURL, stack.Name)
stack.Status.LastUpdate.Permalink = pulumiv1alpha1.Permalink(fmt.Sprintf("%s/%s", consoleURL, stack.Name))
}
err = sess.updateResourceStatus(stack)
if err != nil {
Expand Down

0 comments on commit 34b8bc1

Please sign in to comment.