Files
floci/src/main/java/io/github/hectorvent
Michal Schreiber 74e43a4632 feat(ec2): support transit gateway VPC attachments (#2347)
* feat(ec2): support transit gateway VPC attachments

CreateTransitGatewayVpcAttachment, DescribeTransitGatewayVpcAttachments,
DescribeTransitGatewayAttachments, ModifyTransitGatewayVpcAttachment and
DeleteTransitGatewayVpcAttachment. Part two of #2308, on top of the
gateway itself.

The two describes are separate shapes rather than one being a superset:
the VPC-specific form carries the subnets and options, while the
resource-agnostic form drops both, types the VPC as a resource, and is
the only place the route table association appears.

Behaviour taken from a live account rather than the reference. The
attachment's option defaults are its own: dnsSupport and
securityGroupReferencingSupport enabled, ipv6Support and
applianceModeSupport disabled. The second of those is enabled here while
the gateway that owns the attachment defaults it to disabled, which is
not something the documentation calls out.

An attachment associates with the gateway's default route table only when
the gateway asks for it, and an association already made survives the
gateway later disabling that default: the association belongs to the
attachment from creation, and only later attachments are affected.

Rejections, all as observed: a VPC attached twice to one gateway is
DuplicateTransitGatewayAttachment, two subnets in one zone are
DuplicateSubnetsInSameZone, a subnet from another VPC is reported missing
rather than mismatched, removing a subnet that is not attached is
InvalidSubnetID.NotFound with its own wording, and removing the last one
is InsufficientSubnetsException. A malformed attachment id is rejected
before the lookup, with the terse message the real API uses.

Two changes reach outside the new surface, both deliberate:

DeleteTransitGateway now refuses while attachments exist, reporting
IncorrectState and naming them. Part one could not have hit this, since
nothing could attach then.

parseTagsForResource reads TagSpecifications as well as TagSpecification.
CreateTransitGatewayVpcAttachment declares no locationName on that member
and so serializes the plural, unlike its neighbours; every tag sent by
the AWS CLI was being dropped. The change only adds a spelling, so
actions using the singular are untouched.

State is reported settled rather than transitional, as for the gateway.
Ipv6Support is accepted without checking that the subnets carry IPv6
CIDRs, which real AWS rejects; Floci does not model subnet IPv6
allocation, and inventing that state to enforce it seemed worse than
documenting the gap.

* fix(ec2): make attachment uniqueness atomic and guard attached resources

Two defects in the VPC attachment work, both found in review.

The one-attachment-per-VPC rule scanned for an existing attachment and
then stored the new one without holding a lock between the two, so
concurrent creates for the same VPC could all find nothing and all
persist. Reproduced by disabling the fix: eight threads, eight
attachments, where the API promises one. The check and the write now
share the gateway's lock, and the test asserts one winner, the right
error for every loser, and one stored attachment, over forty trials.

Deleting a VPC or a subnet that an attachment uses left the attachment
naming resources that no longer existed, which surfaced later as a modify
failing on a subnet the caller never touched. Checked on a live account:
both refuse, with the same code and shape of message.

  DependencyViolation: The subnet 'subnet-…' has dependencies and cannot
  be deleted.
  DependencyViolation: The vpc 'vpc-…' has dependencies and cannot be
  deleted.

Both deletes now refuse the same way. A subnet of the attached VPC that
the attachment does not use still deletes, so the guard is no wider than
the dependency it protects.

* test(ec2): lighten the attachment concurrency harness

Forty trials of eight threads was heavier than the race needs — without
the lock it shows on the first trial — and the extra load disturbed two
thread-timing-sensitive tests elsewhere in the suite,
PostgresProtocolHandlerTest and ElbV2HealthCheckerTest, neither of which
this branch touches. Ten trials of six still reproduces it and leaves
those alone.

* fix(ec2): guard everything an attachment depends on under one lock

The previous fix locked the uniqueness check alone, which was too narrow
twice over. The gateway was resolved before the locked section, so a
concurrent DeleteTransitGateway could see no attachments, delete the
gateway, and let the create persist against the object it had already
read. The VPC and subnet checks were outside any shared lock at all, and
lockFor is striped, so a gateway's monitor never guarded a VPC or subnet
delete regardless.

Everything an attachment depends on now resolves and writes under one
region-scoped monitor: the gateway, the VPC, the subnets, and the
uniqueness rule. Deleting a gateway, a VPC or a subnet takes the same
monitor across its dependency check and its delete. Where a gateway lock
is also taken it is acquired inside this one, so the two cannot
interleave in opposite orders.

The test races a create against each delete and asserts the invariant
rather than a winner: whoever wins, the store must never hold an
attachment naming a gateway, VPC or subnet that is gone. It reproduces
both holes — with the create-side lock removed, "attachment outlived its
gateway" fails on the first trial.

* fix(ec2): stop a tag write restoring a deleted transit gateway resource

Tagging is a read-modify-write, so a tag call overlapping a delete could
read the record, lose the race, and put it back afterwards. A resurrected
attachment goes on blocking the VPC and subnets it names; a resurrected
gateway goes on refusing its own deletion. The gateway and route table
branches were as exposed as the attachment one, so all three moved
together rather than only the case reported.

Ordering matters as much as the lock here. The deletes take the topology
lock and then a striped resource lock, so the tag paths have to take them
the same way round; taking the topology lock further down the stack, with
the resource lock already held, deadlocks against a concurrent delete.
That is what the first attempt did, and the race test caught it as a pool
that never drained. createTags and deleteTags now take the topology lock
outermost for tgw- resources and updateResourceTags relies on its callers
for it.

The test races tagging against deletion for an attachment and for a
gateway, asserting neither comes back.
2026-08-18 07:05:24 -04:00
..