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

add custom diff for LBListener forward options #1528

Merged
Merged
Show file tree
Hide file tree
Changes from 4 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
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ PROJECT_REPO := github.com/upbound/$(PROJECT_NAME)

export TERRAFORM_VERSION := 1.5.5
export TERRAFORM_PROVIDER_VERSION := 5.68.0
export TERRAFORM_PROVIDER_RELEASE := v$(TERRAFORM_PROVIDER_VERSION)-upjet.1
export TERRAFORM_PROVIDER_RELEASE := v$(TERRAFORM_PROVIDER_VERSION)-upjet.2
export TERRAFORM_PROVIDER_SOURCE := hashicorp/aws
export TERRAFORM_PROVIDER_REPO ?= https://github.com/hashicorp/terraform-provider-aws
export TERRAFORM_DOCS_PATH ?= website/docs/r
Expand Down
82 changes: 80 additions & 2 deletions config/elbv2/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,17 @@

package elbv2

import "github.com/crossplane/upjet/pkg/config"
import (
"regexp"
"strings"

"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"

"github.com/crossplane/upjet/pkg/config"
)

// Configure adds configurations for the elbv2 group.
func Configure(p *config.Provider) {
func Configure(p *config.Provider) { //nolint:gocyclo
p.AddResourceConfigurator("aws_lb", func(r *config.Resource) {
r.ExternalName.OmittedFields = append(r.ExternalName.OmittedFields, "name_prefix")
r.References = config.References{
Expand Down Expand Up @@ -44,6 +51,64 @@ func Configure(p *config.Provider) {
TerraformName: "aws_lb_target_group",
},
}

// lb_listener schema allows to configure "default_action" with type
// "forward", in 2 different ways.
// 1. you can specify, default_action.0.forward, which allows configuring
// multiple target groups.
// 2. you can specify default_action.0.target_group_arn if you want
// to configure only one target group.
// Former is a more general way, and latter is more of a shortcut for
// a specific case, which can already be expressed with #1.
// TF implementation instructs to specify either #1 or #2, not both.
// However, they both end up in the state redundantly and cause
// unnecessary diff.
r.TerraformCustomDiff = func(diff *terraform.InstanceDiff, _ *terraform.InstanceState, _ *terraform.ResourceConfig) (*terraform.InstanceDiff, error) { //nolint:gocyclo
// skip no diff or destroy diffs
if diff == nil || diff.Empty() || diff.Destroy || diff.Attributes == nil {
return diff, nil
}
duplicatedAction := ""
for k, attrDiff := range diff.Attributes {
// user specified the "default_action.0.target_group_arn" and
// "default_action.0.forward" is not specified in config.
// In that case, default_action.0.forward is populated
// by the AWS API, which can cause an unnecessary diff,
// trying to remove that "auto-populated" element.
if regexp.MustCompile(`^default_action\.\d+\.forward\.#$`).MatchString(k) &&
attrDiff.New == "0" && attrDiff.Old == "1" {
delete(diff.Attributes, k)
// save this attribute path to remove remaining diffs
// of its nested fields if any in a second pass
duplicatedAction = strings.TrimSuffix(k, ".#")
}
// this is the same case as above
if regexp.MustCompile(`^default_action\.\d+\.forward\.\d+\.target_group\.#$`).MatchString(k) &&
attrDiff.New == "0" && attrDiff.Old == "1" {
delete(diff.Attributes, k)
}
// this is the same case but vice versa. user specified
// forward target via default_action.0.forward, and
// default_action.0.target_group_arn is omitted.
// In that case, default_action.0.target_group_arn is populated
// by the AWS API and ends up in the state, causing
// an unnecessary diff, which we omit here.
if regexp.MustCompile(`^default_action\.\d+\.target_group_arn$`).MatchString(k) &&
attrDiff.New == "" && attrDiff.Old != "" && attrDiff.NewRemoved {
delete(diff.Attributes, k)
}
}
// if we have caught an unnecessary diff for default_action.0.forward, remove
// any sub-element diffs of it.
if duplicatedAction != "" {
for k := range diff.Attributes {
if strings.HasPrefix(k, duplicatedAction) {
delete(diff.Attributes, k)
}
}
}
return diff, nil
}
})

p.AddResourceConfigurator("aws_lb_target_group", func(r *config.Resource) {
Expand All @@ -54,6 +119,19 @@ func Configure(p *config.Provider) {
s.Computed = false
}
r.LateInitializer.IgnoredFields = []string{"target_failover"}
r.TerraformCustomDiff = func(diff *terraform.InstanceDiff, _ *terraform.InstanceState, _ *terraform.ResourceConfig) (*terraform.InstanceDiff, error) {
// skip no diff or destroy diffs
if diff == nil || diff.Empty() || diff.Destroy || diff.Attributes == nil {
return diff, nil
}

// ignore diff due to defaulting in the TF schema
udiDiff, ok := diff.Attributes["target_health_state.0.unhealthy_draining_interval"]
if ok && udiDiff.Old == "" && udiDiff.New == "0" {
delete(diff.Attributes, "target_health_state.0.unhealthy_draining_interval")
}
return diff, nil
}
})

p.AddResourceConfigurator("aws_lb_target_group_attachment", 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 @@ -435,4 +435,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-20241008123507-c58f3dd551f5
replace github.com/hashicorp/terraform-provider-aws => github.com/upbound/terraform-provider-aws v0.0.0-20241019052313-8fed8454b0e3
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -857,8 +857,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-20241008123507-c58f3dd551f5 h1:2Ccu89BL+ZGzdUzpfFBDJ5MYnxdXSmyaDaHwQnZ5Dhc=
github.com/upbound/terraform-provider-aws v0.0.0-20241008123507-c58f3dd551f5/go.mod h1:gMeW31HPSyiaHsXeaesXhZXtFIDWP8x1Lr1YVeC8qQ4=
github.com/upbound/terraform-provider-aws v0.0.0-20241019052313-8fed8454b0e3 h1:f4VOuQgP3QN1+B41l9t3wz4eNWc4Nx2zm/1TVf6aX14=
github.com/upbound/terraform-provider-aws v0.0.0-20241019052313-8fed8454b0e3/go.mod h1:gMeW31HPSyiaHsXeaesXhZXtFIDWP8x1Lr1YVeC8qQ4=
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
Loading