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

Fix VPC update, only send vpc attributes if set #595

Merged
merged 1 commit into from
Mar 12, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
10 changes: 8 additions & 2 deletions pkg/clients/ec2/vpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,12 +100,18 @@ func GenerateVpcObservation(vpc ec2.Vpc) v1beta1.VPCObservation {
}

// LateInitializeVPC fills the empty fields in *v1beta1.VPCParameters with
// the values seen in ec2.Vpc.
func LateInitializeVPC(in *v1beta1.VPCParameters, v *ec2.Vpc) { // nolint:gocyclo
// the values seen in ec2.Vpc and ec2.DescribeVpcAttributeOutput.
func LateInitializeVPC(in *v1beta1.VPCParameters, v *ec2.Vpc, attributes *ec2.DescribeVpcAttributeOutput) { // nolint:gocyclo
if v == nil {
return
}

in.CIDRBlock = awsclients.LateInitializeString(in.CIDRBlock, v.CidrBlock)
in.InstanceTenancy = awsclients.LateInitializeStringPtr(in.InstanceTenancy, aws.String(string(v.InstanceTenancy)))
if attributes.EnableDnsHostnames != nil {
in.EnableDNSHostNames = awsclients.LateInitializeBoolPtr(in.EnableDNSHostNames, attributes.EnableDnsHostnames.Value)
}
if attributes.EnableDnsHostnames != nil {
in.EnableDNSSupport = awsclients.LateInitializeBoolPtr(in.EnableDNSSupport, attributes.EnableDnsSupport.Value)
}
}
40 changes: 23 additions & 17 deletions pkg/controller/ec2/vpc/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,16 +124,6 @@ func (e *external) Observe(ctx context.Context, mgd resource.Managed) (managed.E

// update the CRD spec for any new values from provider
current := cr.Spec.ForProvider.DeepCopy()
ec2.LateInitializeVPC(&cr.Spec.ForProvider, &observed)

switch observed.State {
case awsec2.VpcStateAvailable:
cr.SetConditions(xpv1.Available())
case awsec2.VpcStatePending:
cr.SetConditions(xpv1.Creating())
}

cr.Status.AtProvider = ec2.GenerateVpcObservation(observed)

o := awsec2.DescribeVpcAttributeOutput{}

Expand All @@ -159,6 +149,17 @@ func (e *external) Observe(ctx context.Context, mgd resource.Managed) (managed.E
}
}

ec2.LateInitializeVPC(&cr.Spec.ForProvider, &observed, &o)

switch observed.State {
case awsec2.VpcStateAvailable:
cr.SetConditions(xpv1.Available())
case awsec2.VpcStatePending:
cr.SetConditions(xpv1.Creating())
}

cr.Status.AtProvider = ec2.GenerateVpcObservation(observed)

return managed.ExternalObservation{
ResourceExists: true,
ResourceUpToDate: ec2.IsVpcUpToDate(cr.Spec.ForProvider, observed, o),
Expand Down Expand Up @@ -191,17 +192,22 @@ func (e *external) Update(ctx context.Context, mgd resource.Managed) (managed.Ex
return managed.ExternalUpdate{}, errors.New(errUnexpectedObject)
}

for _, input := range []*awsec2.ModifyVpcAttributeInput{
{
if cr.Spec.ForProvider.EnableDNSSupport != nil {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't love that we will make all of these calls if the fields are set even if they don't need to be changed (i.e. the diff was elsewhere in the spec). That being said, this is the current existing pattern (as opposed to getting again, calculating diff, then only making the specific call), so I am fine with moving forward with this fix 👍

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@hasheddan I think I just ran into a bug here. So in my case I'm importing and existing vpc. I'm using the crossplane.io/external-name annotation in order to pull in an existing vpc. In doing so I'm getting an error of:

 Message:               update failed: failed to modify the VPC resource attributes: api error UnauthorizedOperation: You are not authorized to perform this operation. Encoded authorization failure message: 17IZjLqa5Zh3OjVaZ3t9FH0FftiP1SQ3_yRUhVUg42S9N-S5HC6P5UNgad1lXNzm9uEfvQyWMV-JzvjbCbdBoeCDdaAeadxZWWO5F94c-Qm0ba2sMINryty1b1F_wuzxG9Pi_1Mb9QEwuMSjR2zK7Bttkm20YGfP-NKxSIenBSHVCmy5yfW915cTLqakozjE8Dmt2Ml95_XT8nQ5QZj-bYHkCIu5WjTY5VQQNe7Ur7hPXNoj2Zqp3mUnwpJKmPp0XAHgRrQtnClrc_aFldtspgv2Ty97AhgIQtOWHIvX4iadQ1uafOakWfKoONODQaUw8zqHHsBNt6a1-Cy7nQ9Q1weM0tyeVykTioT1iRqq2arbvdvSjWvPq9P8qqpkovXFBqxRExx8wibFiZ6G5bV3lmAv4wMZOa11p0nuisNmnc2xJXDb8gyX2dwSQSXL336pRHgKt4S-VITej52_-o-qD7wqEzmN6rZibbfIA2xwGddtEBX95zwgqJcgWLIr-Rvz4q4ktgLuIJH5G5rOkmH1ZiCMTVswY98WjouXAl9bXUaNnp55LzvhSdNajsW77Uuu5kDlc0A4WAUrdaGD6He8foMacrySqBwyrlJLULa-NxkHcKC5p2tt5i3k-BYurVJswQnqXTIbmH4lwV5GIimXuPkcaGX9lKXhvGUl4zAiYCSmG_QweVQT0cglv8dg9R4oOwws8WNtDM_aHQbDjxO13SO1I17geD1ERFiHqdN7igqbPLs2aRUA7f5vVS7Z-DsF-p5Mf5uke7qv-4eyd7on2bV-e-rDrH5W6KfwoyBLvbq7xZGY7rIG3is9aQgDoBFwk2NVcIAKPlP0OQ2y0OKxx83q_mUcDcpsYdz5ZmZjghiaJr9UMxzEfnuEwTvBwVPqRJiw1CdWhoqmMoIPGg

In checking CloudTrail I see it's attempting to do a ModifyVpcResource on enableDnsSupport even though it's already set.

Obviously in our case we are using vpc as a ReadOnly resource for reference but it shouldn't try to Modify an attribute if it matches. I've tried setting the forProvider to not set these 2 attributes and it still tries to Modify... I've tried to set these forProvider parameters to match what they currently are on vpc and it still tries to modify.

modifyInput := &awsec2.ModifyVpcAttributeInput{
VpcId: aws.String(meta.GetExternalName(cr)),
EnableDnsSupport: &awsec2.AttributeBooleanValue{Value: cr.Spec.ForProvider.EnableDNSSupport},
},
{
}
if _, err := e.client.ModifyVpcAttributeRequest(modifyInput).Send(ctx); err != nil {
return managed.ExternalUpdate{}, awsclient.Wrap(err, errModifyVPCAttributes)
}
}

if cr.Spec.ForProvider.EnableDNSHostNames != nil {
modifyInput := &awsec2.ModifyVpcAttributeInput{
VpcId: aws.String(meta.GetExternalName(cr)),
EnableDnsHostnames: &awsec2.AttributeBooleanValue{Value: cr.Spec.ForProvider.EnableDNSHostNames},
},
} {
if _, err := e.client.ModifyVpcAttributeRequest(input).Send(ctx); err != nil {
}
if _, err := e.client.ModifyVpcAttributeRequest(modifyInput).Send(ctx); err != nil {
return managed.ExternalUpdate{}, awsclient.Wrap(err, errModifyVPCAttributes)
}
}
Expand Down
31 changes: 29 additions & 2 deletions pkg/controller/ec2/vpc/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ var (
vpcID = "some Id"
cidr = "192.168.0.0/32"
tenancyDefault = "default"
enableDNS = true

errBoom = errors.New("boom")
)
Expand Down Expand Up @@ -233,6 +234,27 @@ func TestCreate(t *testing.T) {
want
}{
"Successful": {
args: args{
vpc: &fake.MockVPCClient{
MockCreate: func(input *awsec2.CreateVpcInput) awsec2.CreateVpcRequest {
return awsec2.CreateVpcRequest{
Request: &aws.Request{HTTPRequest: &http.Request{}, Retryer: aws.NoOpRetryer{}, Data: &awsec2.CreateVpcOutput{
Vpc: &awsec2.Vpc{
VpcId: aws.String(vpcID),
CidrBlock: aws.String(cidr),
},
}},
}
},
},
cr: vpc(),
},
want: want{
cr: vpc(withExternalName(vpcID)),
result: managed.ExternalCreation{ExternalNameAssigned: true},
},
},
"SuccessfulWithAttributes": {
args: args{
vpc: &fake.MockVPCClient{
MockCreate: func(input *awsec2.CreateVpcInput) awsec2.CreateVpcRequest {
Expand All @@ -251,10 +273,15 @@ func TestCreate(t *testing.T) {
}
},
},
cr: vpc(),
cr: vpc(withSpec(v1beta1.VPCParameters{
EnableDNSSupport: &enableDNS,
})),
},
want: want{
cr: vpc(withExternalName(vpcID)),
cr: vpc(withExternalName(vpcID),
withSpec(v1beta1.VPCParameters{
EnableDNSSupport: &enableDNS,
})),
result: managed.ExternalCreation{ExternalNameAssigned: true},
},
},
Expand Down