Skip to content

Commit

Permalink
Merge pull request #1625 from mergenci/fix-ec2-instance-panic
Browse files Browse the repository at this point in the history
Consume EC2 Instance panic fix and address perpetual diff
  • Loading branch information
mergenci authored Jan 3, 2025
2 parents 7cbc096 + 972f8ce commit 1f3a24c
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 3 deletions.
29 changes: 29 additions & 0 deletions config/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
xpresource "github.com/crossplane/crossplane-runtime/pkg/resource"
"github.com/crossplane/upjet/pkg/config"
"github.com/crossplane/upjet/pkg/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
jsoniter "github.com/json-iterator/go"
"github.com/pkg/errors"
corev1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -142,3 +143,31 @@ func RemovePolicyVersion(p string) (string, error) {
r, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(m)
return string(r), errors.Wrap(err, "failed to marshal the policy map as JSON")
}

// RemoveDiffIfEmpty removes supplied keys from Terraform diffs, if old and new
// values for the key are empty (""). It is probably safe to delete all such
// keys in the diff. Until we decide to do so, we'll specify the keys, to be on
// the safe side.
func RemoveDiffIfEmpty(keys []string) config.CustomDiff { //nolint:gocyclo // The implementation is pretty straightforward as of this writing.
return func(diff *terraform.InstanceDiff, state *terraform.InstanceState, config *terraform.ResourceConfig) (*terraform.InstanceDiff, error) {
// Skip diff customization on create
if state == nil || state.Empty() {
return diff, nil
}
if config == nil {
return nil, errors.New("resource config cannot be nil")
}
// Skip no diff or destroy diffs
if diff == nil || diff.Empty() || diff.Destroy || diff.Attributes == nil {
return diff, nil
}

for _, key := range keys {
if diff.Attributes[key] != nil && diff.Attributes[key].Old == "" && diff.Attributes[key].New == "" {
delete(diff.Attributes, key)
}
}

return diff, nil
}
}
3 changes: 3 additions & 0 deletions config/ec2/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ func Configure(p *config.Provider) { //nolint:gocyclo
"cpu_threads_per_core",
},
}
r.TerraformCustomDiff = common.RemoveDiffIfEmpty([]string{
"volume_tags.%",
})
config.MoveToStatus(r.TerraformResource, "security_groups")
})
p.AddResourceConfigurator("aws_eip", func(r *config.Resource) {
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -439,4 +439,4 @@ require (
// copied from the Terraform provider
replace github.com/hashicorp/terraform-plugin-log => github.com/gdavison/terraform-plugin-log v0.0.0-20230928191232-6c653d8ef8fb

replace github.com/hashicorp/terraform-provider-aws => github.com/upbound/terraform-provider-aws v0.0.0-20241203141151-997a60ec5360
replace github.com/hashicorp/terraform-provider-aws => github.com/upbound/terraform-provider-aws v0.0.0-20250102143213-c0eb96154778
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -865,8 +865,8 @@ github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsT
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/tmccombs/hcl2json v0.3.3 h1:+DLNYqpWE0CsOQiEZu+OZm5ZBImake3wtITYxQ8uLFQ=
github.com/tmccombs/hcl2json v0.3.3/go.mod h1:Y2chtz2x9bAeRTvSibVRVgbLJhLJXKlUeIvjeVdnm4w=
github.com/upbound/terraform-provider-aws v0.0.0-20241203141151-997a60ec5360 h1:HLrTYw0kB74C7JMNM5dUe0h7V2nzvdSMzciWmsJCUvQ=
github.com/upbound/terraform-provider-aws v0.0.0-20241203141151-997a60ec5360/go.mod h1:7l6VnUvAF/LY5jcEABkfHuK8gAkOODwTT1TcGpy+tl0=
github.com/upbound/terraform-provider-aws v0.0.0-20250102143213-c0eb96154778 h1:RxW17Zn9BdCvSSeB7LGOZCoJJYO1d4VDvrdPmSRP5G8=
github.com/upbound/terraform-provider-aws v0.0.0-20250102143213-c0eb96154778/go.mod h1:7l6VnUvAF/LY5jcEABkfHuK8gAkOODwTT1TcGpy+tl0=
github.com/vmihailenco/msgpack v3.3.3+incompatible/go.mod h1:fy3FlTQTDXWkZ7Bh6AcGMlsjHatGryHQYUTf1ShIgkk=
github.com/vmihailenco/msgpack v4.0.4+incompatible h1:dSLoQfGFAo3F6OoNhwUmLwVgaUXK79GlxNBwueZn0xI=
github.com/vmihailenco/msgpack v4.0.4+incompatible/go.mod h1:fy3FlTQTDXWkZ7Bh6AcGMlsjHatGryHQYUTf1ShIgkk=
Expand Down

0 comments on commit 1f3a24c

Please sign in to comment.