Skip to content

Commit

Permalink
Add support for configuring Terraform resource timeouts both from the…
Browse files Browse the repository at this point in the history
… schema and config.Resource.OperationTimeouts

- The timeouts specified in upjet resource configuration (config.Resource.OperationTimeouts)
  prevails any defaults configured in the Terraform schema.

Signed-off-by: Alper Rifat Ulucinar <[email protected]>
  • Loading branch information
ulucinar committed Nov 3, 2023
1 parent 229c273 commit 82a4ad9
Showing 1 changed file with 44 additions and 1 deletion.
45 changes: 44 additions & 1 deletion pkg/controller/external_nofork.go
Original file line number Diff line number Diff line change
Expand Up @@ -339,8 +339,44 @@ func filterInitExclusiveDiffs(tr resource.Terraformed, instanceDiff *tf.Instance
return nil
}

// resource timeouts configuration
func getTimeoutParameters(config *config.Resource) map[string]any {
timeouts := make(map[string]any)
// first use the timeout overrides specified in
// the Terraform resource schema
if config.TerraformResource.Timeouts != nil {
if config.TerraformResource.Timeouts.Create != nil && *config.TerraformResource.Timeouts.Create != 0 {
timeouts[schema.TimeoutCreate] = config.TerraformResource.Timeouts.Create.Nanoseconds()
}
if config.TerraformResource.Timeouts.Update != nil && *config.TerraformResource.Timeouts.Update != 0 {
timeouts[schema.TimeoutUpdate] = config.TerraformResource.Timeouts.Update.Nanoseconds()
}
if config.TerraformResource.Timeouts.Delete != nil && *config.TerraformResource.Timeouts.Delete != 0 {
timeouts[schema.TimeoutDelete] = config.TerraformResource.Timeouts.Delete.Nanoseconds()
}
if config.TerraformResource.Timeouts.Read != nil && *config.TerraformResource.Timeouts.Read != 0 {
timeouts[schema.TimeoutRead] = config.TerraformResource.Timeouts.Read.Nanoseconds()
}
}
// then, override any Terraform defaults using any upjet
// resource configuration overrides
if config.OperationTimeouts.Create != 0 {
timeouts[schema.TimeoutCreate] = config.OperationTimeouts.Create.Nanoseconds()
}
if config.OperationTimeouts.Update != 0 {
timeouts[schema.TimeoutUpdate] = config.OperationTimeouts.Update.Nanoseconds()
}
if config.OperationTimeouts.Delete != 0 {
timeouts[schema.TimeoutDelete] = config.OperationTimeouts.Delete.Nanoseconds()
}
if config.OperationTimeouts.Read != 0 {
timeouts[schema.TimeoutRead] = config.OperationTimeouts.Read.Nanoseconds()
}
return timeouts
}

func (n *noForkExternal) getResourceDataDiff(tr resource.Terraformed, ctx context.Context, s *tf.InstanceState, resourceExists bool) (*tf.InstanceDiff, error) {
resourceConfig := tf.NewResourceConfigRaw(n.params)
resourceConfig := tf.NewResourceConfigRaw(n.params)
instanceDiff, err := schema.InternalMap(n.resourceSchema.Schema).Diff(ctx, s, resourceConfig, nil, n.ts.Meta, false)
if err != nil {
return nil, errors.Wrap(err, "failed to get *terraform.InstanceDiff")
Expand Down Expand Up @@ -371,6 +407,13 @@ func (n *noForkExternal) getResourceDataDiff(tr resource.Terraformed, ctx contex
// Setting instanceDiff.RawConfig has no effect on diff application.
instanceDiff.RawConfig = n.rawConfig
}
timeouts := getTimeoutParameters(n.config)
if instanceDiff.Meta == nil && len(timeouts) > 0 {
instanceDiff.Meta = make(map[string]interface{})
}
if len(timeouts) > 0 {
instanceDiff.Meta[schema.TimeoutKey] = timeouts
}
return instanceDiff, nil
}

Expand Down

0 comments on commit 82a4ad9

Please sign in to comment.