Skip to content

Commit d83ef90

Browse files
authored
Merge pull request #1687 from crossplane-contrib/backport-1672-to-release-1.20
[Backport release-1.20] Add Update Prevent function to `mq_configuraiton` resource for preventing the update loop
2 parents a6c7438 + aa5ea31 commit d83ef90

4 files changed

Lines changed: 71 additions & 4 deletions

File tree

config/mq/config.go

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,15 @@
55
package mq
66

77
import (
8+
"encoding/base64"
89
"fmt"
910

11+
"github.com/crossplane/crossplane-runtime/pkg/errors"
12+
"github.com/crossplane/crossplane-runtime/pkg/fieldpath"
13+
xpresource "github.com/crossplane/crossplane-runtime/pkg/resource"
1014
"github.com/crossplane/upjet/pkg/config"
1115
"github.com/crossplane/upjet/pkg/registry"
16+
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
1217
)
1318

1419
// Configure adds configurations for the mq group.
@@ -74,4 +79,66 @@ func Configure(p *config.Provider) { //nolint:gocyclo
7479
}
7580
r.MetaResource.ArgumentDocs["console_access"] = `- (Optional) Setting consoleAccess will result in an update loop till the MQ Broker to which this user belongs is restarted.`
7681
})
82+
83+
p.AddResourceConfigurator("aws_mq_configuration", func(r *config.Resource) {
84+
c := &repeatedDiffCheckerForData{}
85+
r.UpdateLoopPrevention = c
86+
})
87+
}
88+
89+
// repeatedDiffCheckerForData implements the UpdateLoopPrevention interface.
90+
// It is responsible for checking if the same diff related to the "data"
91+
// argument (in this case, XML content) appears repeatedly, which may indicate
92+
// a schema violation or invalid data scenario. If a repeated diff is detected,
93+
// it blocks the update process.
94+
type repeatedDiffCheckerForData struct {
95+
// previousDiff stores the base64-encoded string of the last diff.
96+
// This is used to compare against the current diff to detect repeated
97+
// updates.
98+
previousDiff *string
99+
}
100+
101+
// UpdateLoopPreventionFunc checks for repeated diffs in the resource's "data"
102+
// attribute. If the diff has not changed since the previous reconciliation
103+
// loop, it blocks the update by returning an appropriate result.
104+
func (c *repeatedDiffCheckerForData) UpdateLoopPreventionFunc(diff *terraform.InstanceDiff, mg xpresource.Managed) (*config.UpdateLoopPreventResult, error) { //nolint:gocyclo // easier to follow as a unit
105+
// Skip processing if there is no diff, the diff is empty, or it is a destroy operation.
106+
if diff == nil || diff.Empty() || diff.Destroy || diff.Attributes == nil {
107+
return nil, nil
108+
}
109+
paved, err := fieldpath.PaveObject(mg)
110+
if err != nil {
111+
return nil, errors.Wrap(err, "cannot pave object")
112+
}
113+
// Retrieve the value of the "spec.forProvider.engineType" field from the paved object.
114+
// This field is used to determine the engine type of the MQ configuration.
115+
engineType, err := paved.GetString("spec.forProvider.engineType")
116+
if err != nil {
117+
return nil, errors.Wrap(err, "cannot get value of spec.forProvider.engineType")
118+
}
119+
// Check if the engine type is "ActiveMQ". If it is not, skip further checks and return nil.
120+
// This block ensures that the diff check logic only applies to resources with ActiveMQ engine type,
121+
// avoiding unnecessary diff processing for other engine types.
122+
if engineType != "ActiveMQ" {
123+
return nil, nil
124+
}
125+
// Encode the "data" attribute of the diff into a base64 string for comparison.
126+
var encodedDiff string
127+
if dataDiff, ok := diff.Attributes["data"]; ok {
128+
// Use GoString to get a string representation of the attribute.
129+
encodedDiff = base64.StdEncoding.EncodeToString([]byte(dataDiff.GoString()))
130+
}
131+
// If there is no previous diff recorded, store the current diff and allow the update to proceed.
132+
if c.previousDiff == nil {
133+
c.previousDiff = &encodedDiff
134+
return nil, nil
135+
}
136+
// If the current diff matches the previous diff, block the update and return a reason.
137+
if encodedDiff == *c.previousDiff {
138+
return &config.UpdateLoopPreventResult{Reason: "Repeated diff for the provided XML data, please check the XML content you have provided. " +
139+
"It may contain invalid or schema violating content."}, nil
140+
}
141+
// Update the previous diff with the current diff for the next reconciliation loop.
142+
c.previousDiff = &encodedDiff
143+
return nil, nil
77144
}

examples/mq/v1beta1/configuration.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,4 @@ spec:
2323
</broker>
2424
description: Example Configuration
2525
engineType: ActiveMQ
26-
engineVersion: 5.15.0
26+
engineVersion: "5.18"

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ require (
1818
github.com/aws/smithy-go v1.22.1
1919
github.com/crossplane/crossplane-runtime v1.17.0
2020
github.com/crossplane/crossplane-tools v0.0.0-20230925130601-628280f8bf79
21-
github.com/crossplane/upjet v1.4.1-0.20250206162150-1a6d69bbd22a
21+
github.com/crossplane/upjet v1.5.0
2222
github.com/go-ini/ini v1.46.0
2323
github.com/google/go-cmp v0.6.0
2424
github.com/hashicorp/aws-sdk-go-base/v2 v2.0.0-beta.59

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -580,8 +580,8 @@ github.com/crossplane/crossplane-runtime v1.17.0 h1:y+GvxPT1M9s8BKt2AeZJdd2d6pg2
580580
github.com/crossplane/crossplane-runtime v1.17.0/go.mod h1:vtglCrnnbq2HurAk9yLHa4qS0bbnCxaKL7C21cQcB/0=
581581
github.com/crossplane/crossplane-tools v0.0.0-20230925130601-628280f8bf79 h1:HigXs5tEQxWz0fcj8hzbU2UAZgEM7wPe0XRFOsrtF8Y=
582582
github.com/crossplane/crossplane-tools v0.0.0-20230925130601-628280f8bf79/go.mod h1:+e4OaFlOcmr0JvINHl/yvEYBrZawzTgj6pQumOH1SS0=
583-
github.com/crossplane/upjet v1.4.1-0.20250206162150-1a6d69bbd22a h1:BX1cOu+WEPMWG9/qI9B4gjOMYFC82L+40PJ+7wuW94s=
584-
github.com/crossplane/upjet v1.4.1-0.20250206162150-1a6d69bbd22a/go.mod h1:F2u9XwKNzxM+myfS1Opjnc6a5E1N2PC7Mytbkavawvs=
583+
github.com/crossplane/upjet v1.5.0 h1:SJc6lUKjcuIGrJmu4dXgnS6OlP1/xBasHhGk4uAW1yI=
584+
github.com/crossplane/upjet v1.5.0/go.mod h1:F2u9XwKNzxM+myfS1Opjnc6a5E1N2PC7Mytbkavawvs=
585585
github.com/cyphar/filepath-securejoin v0.2.4 h1:Ugdm7cg7i6ZK6x3xDF1oEu1nfkyfH53EtKeQYTC3kyg=
586586
github.com/cyphar/filepath-securejoin v0.2.4/go.mod h1:aPGpWjXOXUn2NCNjFvBE6aRxGGx79pTxQpKOJNYHHl4=
587587
github.com/dave/jennifer v1.4.1 h1:XyqG6cn5RQsTj3qlWQTKlRGAyrTcsk1kUmWdZBzRjDw=

0 commit comments

Comments
 (0)