Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update flow of wait timeout; creates tfstate after TF timeout #2163

Merged
merged 18 commits into from
Jul 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .changelog/2163.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
`manifest/provider/apply.go`: update flow in `wait` block to fix timeout bug within tf apply where the resource is created and appears in Kubernetes but does not appear in TF state file after deadline. The fix would ensure that the resource has been created in the state file while also tainting the resource requiring the user to make the necessary changes in order for their to not be another timeout error.
```
10 changes: 4 additions & 6 deletions manifest/provider/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -425,12 +425,12 @@ func (s *RawProviderServer) ApplyResourceChange(ctx context.Context, req *tfprot
if !waitConfig.IsNull() {
err = s.waitForCompletion(ctxDeadline, waitConfig, rs, rname, wt, th)
if err != nil {
if err == context.DeadlineExceeded {
if reason, ok := err.(WaiterError); ok {
resp.Diagnostics = append(resp.Diagnostics,
&tfprotov5.Diagnostic{
Severity: tfprotov5.DiagnosticSeverityError,
Summary: "Operation timed out",
Detail: "Terraform timed out waiting on the operation to complete",
Detail: reason.Error(),
})
BBBmau marked this conversation as resolved.
Show resolved Hide resolved
} else {
resp.Diagnostics = append(resp.Diagnostics,
Expand All @@ -439,11 +439,10 @@ func (s *RawProviderServer) ApplyResourceChange(ctx context.Context, req *tfprot
Summary: "Error waiting for operation to complete",
Detail: err.Error(),
})
return resp, nil
}
return resp, nil
}

r, err := rs.Get(ctxDeadline, rname, metav1.GetOptions{})
r, err := rs.Get(ctx, rname, metav1.GetOptions{})
if err != nil {
s.logger.Error("[ApplyResourceChange][ReadAfterWait]", "API error", dump(err), "API response", dump(result))
resp.Diagnostics = append(resp.Diagnostics,
Expand All @@ -452,7 +451,6 @@ func (s *RawProviderServer) ApplyResourceChange(ctx context.Context, req *tfprot
Summary: fmt.Sprintf(`Failed to read resource %q after wait conditions`, rname),
Detail: err.Error(),
})

return resp, nil
}
result = r
Expand Down
14 changes: 11 additions & 3 deletions manifest/provider/waiter.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,14 @@ type Waiter interface {
Wait(context.Context) error
}

type WaiterError struct {
Reason string
}

func (e WaiterError) Error() string {
return fmt.Sprintf("timed out waiting on %v", e.Reason)
}

// NewResourceWaiter constructs an appropriate Waiter using the supplied waitForBlock configuration
func NewResourceWaiter(resource dynamic.ResourceInterface, resourceName string, resourceType tftypes.Type, th map[string]string, waitForBlock tftypes.Value, hl hclog.Logger) (Waiter, error) {
var waitForBlockVal map[string]tftypes.Value
Expand Down Expand Up @@ -145,7 +153,7 @@ func (w *FieldWaiter) Wait(ctx context.Context) error {
for {
if deadline, ok := ctx.Deadline(); ok {
if time.Now().After(deadline) {
return context.DeadlineExceeded
return WaiterError{Reason: "field matchers"}
}
}

Expand Down Expand Up @@ -283,7 +291,7 @@ func (w *RolloutWaiter) Wait(ctx context.Context) error {
for {
if deadline, ok := ctx.Deadline(); ok {
if time.Now().After(deadline) {
return context.DeadlineExceeded
return WaiterError{Reason: "rollout to complete"}
}
}

Expand Down Expand Up @@ -333,7 +341,7 @@ func (w *ConditionsWaiter) Wait(ctx context.Context) error {
for {
if deadline, ok := ctx.Deadline(); ok {
if time.Now().After(deadline) {
return context.DeadlineExceeded
return WaiterError{Reason: "conditions"}
}
}

Expand Down
11 changes: 10 additions & 1 deletion manifest/test/acceptance/wait_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,9 +219,18 @@ func TestKubernetesManifest_Wait_InvalidCondition(t *testing.T) {
tf.Init(ctx)

err = tf.Apply(ctx)
if err == nil || !strings.Contains(err.Error(), "Terraform timed out waiting on the operation to complete") {
if err == nil || !strings.Contains(err.Error(), "timed out waiting on") {
t.Fatalf("Waiter should have timed out")
}
BBBmau marked this conversation as resolved.
Show resolved Hide resolved

st, err := tf.State(ctx)
if err != nil {
t.Fatalf("Failed to get state: %q", err)
}
tfstate := tfstatehelper.NewHelper(st)
if !tfstate.ResourceExists(t, "kubernetes_manifest.test") {
t.Fatalf("Expected resource to exist in state")
}
}

func TestKubernetesManifest_WaitFields_Annotations(t *testing.T) {
Expand Down
5 changes: 5 additions & 0 deletions manifest/test/helper/state/state_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ type Helper struct {
func NewHelper(tfstate *tfjson.State) *Helper {
return &Helper{tfstate}
}
func (s *Helper) ResourceExists(t *testing.T, resourceAddress string) bool {
t.Helper()
_, err := getAttributesValuesFromResource(s, resourceAddress)
return err == nil
}

// getAttributesValuesFromResource pulls out the AttributeValues field from the resource at the given address
func getAttributesValuesFromResource(state *Helper, address string) (interface{}, error) {
Expand Down