Skip to content

Commit

Permalink
Merge pull request #466 from sergenyalcin/update-loop-prevention
Browse files Browse the repository at this point in the history
Add a new configuration option for preventing the possible update loops
  • Loading branch information
sergenyalcin authored Feb 11, 2025
2 parents 1a6d69b + 3d9beef commit 7267575
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
37 changes: 37 additions & 0 deletions pkg/config/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -575,6 +575,43 @@ type Resource struct {
// requiredFields are the fields that will be marked as required in the
// generated CRD schema, although they are not required in the TF schema.
requiredFields []string

// UpdateLoopPrevention is a mechanism to prevent infinite reconciliation
// loops. This is especially useful in cases where external services,
// silently modify resource data without notifying the management layer
// (e.g., sanitized XML fields).
UpdateLoopPrevention UpdateLoopPrevention
}

// UpdateLoopPrevention is an interface that defines the behavior to prevent
// update loops. Implementations of this interface are responsible for analyzing
// diffs and determining whether an update should be blocked or allowed.
type UpdateLoopPrevention interface {
// UpdateLoopPreventionFunc analyzes a diff and decides whether the update
// should be blocked. It returns a result containing a reason for blocking
// the update if a loop is detected, or nil if the update can proceed.
//
// Parameters:
// - diff: The diff object representing changes between the desired and
// current state.
// - mg: The managed resource that is being reconciled.
//
// Returns:
// - *UpdateLoopPreventResult: Contains the reason for blocking the update
// if a loop is detected.
// - error: An error if there are issues analyzing the diff
// (e.g., invalid data).
UpdateLoopPreventionFunc(diff *terraform.InstanceDiff, mg xpresource.Managed) (*UpdateLoopPreventResult, error)
}

// UpdateLoopPreventResult provides the result of an update loop prevention
// check. If a loop is detected, it includes a reason explaining why the update
// was blocked.
type UpdateLoopPreventResult struct {
// Reason provides a human-readable explanation of why the update was
// blocked. This message can be displayed to the user or logged for
// debugging purposes.
Reason string
}

// RequiredFields returns slice of the marked as required fieldpaths.
Expand Down
10 changes: 10 additions & 0 deletions pkg/controller/external_tfpluginsdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -686,6 +686,16 @@ func (n *terraformPluginSDKExternal) assertNoForceNew() error {
}

func (n *terraformPluginSDKExternal) Update(ctx context.Context, mg xpresource.Managed) (managed.ExternalUpdate, error) {
if n.config.UpdateLoopPrevention != nil {
preventResult, err := n.config.UpdateLoopPrevention.UpdateLoopPreventionFunc(n.instanceDiff, mg)
if err != nil {
return managed.ExternalUpdate{}, errors.Wrapf(err, "failed to apply the update loop prevention function for %s", n.config.Name)
}
if preventResult != nil {
return managed.ExternalUpdate{}, errors.Errorf("update operation was blocked because of a possible update loop: %s", preventResult.Reason)
}
}

n.logger.Debug("Updating the external resource")

if err := n.assertNoForceNew(); err != nil {
Expand Down

0 comments on commit 7267575

Please sign in to comment.