@@ -1479,18 +1479,70 @@ func (c *FlowContext) deletePrivateRoutingTable(zoneName string) flow.TaskFn {
14791479 }
14801480}
14811481
1482+ func (c * FlowContext ) routingAssociationSpecs () []routeTableAssociationSpec {
1483+ return []routeTableAssociationSpec {
1484+ {IdentifierZoneSubnetPublic , IdentifierZoneSubnetPublicRouteTableAssoc , false },
1485+ {IdentifierZoneSubnetPrivate , IdentifierZoneSubnetPrivateRouteTableAssoc , true },
1486+ {IdentifierZoneSubnetWorkers , IdentifierZoneSubnetWorkersRouteTableAssoc , true },
1487+ }
1488+ }
1489+
1490+ // validateAndPruneRoutingTableAssocState checks whether the routing table associations stored in the state
1491+ // still exist in AWS. If not, it removes them from the state.
1492+ func (c * FlowContext ) validateAndPruneRoutingTableAssocState (ctx context.Context , zoneName string , specs []routeTableAssociationSpec ) error {
1493+ child := c .getSubnetZoneChild (zoneName )
1494+ log := LogFromContext (ctx )
1495+
1496+ // should validate only if at least one association ID is present in state
1497+ if ! hasRouteTableAssociationInState (child .Get , specs ) {
1498+ return nil
1499+ }
1500+
1501+ subnetIDs := make ([]string , 0 , len (specs ))
1502+ for _ , spec := range specs {
1503+ id := child .Get (spec .subnetKey )
1504+ if id == nil {
1505+ return fmt .Errorf ("missing subnet id for key %s" , spec .subnetKey )
1506+ }
1507+ subnetIDs = append (subnetIDs , * id )
1508+ }
1509+
1510+ vpc := c .state .Get (IdentifierVPC )
1511+ if vpc == nil {
1512+ return fmt .Errorf ("VPC ID not found in state" )
1513+ }
1514+
1515+ routeTableAssociations , err := c .client .GetRouteTableAssociationIDs (ctx , * vpc , subnetIDs )
1516+ if err != nil {
1517+ return err
1518+ }
1519+
1520+ for _ , spec := range specs {
1521+ if assocID := child .Get (spec .assocKey ); assocID != nil && ! slices .Contains (routeTableAssociations , * assocID ) {
1522+ log .Info ("route table association not found in AWS, removing from state" ,
1523+ "AssociationID" , * assocID )
1524+ child .Delete (spec .assocKey )
1525+ }
1526+ }
1527+ return nil
1528+ }
1529+
14821530func (c * FlowContext ) ensureRoutingTableAssociations (zoneName string ) flow.TaskFn {
14831531 return func (ctx context.Context ) error {
1484- if err := c .ensureZoneRoutingTableAssociation (ctx , zoneName , false ,
1485- IdentifierZoneSubnetPublic , IdentifierZoneSubnetPublicRouteTableAssoc ); err != nil {
1532+ specs := c .routingAssociationSpecs ()
1533+
1534+ err := c .validateAndPruneRoutingTableAssocState (ctx , zoneName , specs )
1535+ if err != nil {
14861536 return err
14871537 }
1488- if err := c .ensureZoneRoutingTableAssociation (ctx , zoneName , true ,
1489- IdentifierZoneSubnetPrivate , IdentifierZoneSubnetPrivateRouteTableAssoc ); err != nil {
1490- return err
1538+
1539+ for _ , spec := range specs {
1540+ err := c .ensureZoneRoutingTableAssociation (ctx , zoneName , spec .zoneRouteTable , spec .subnetKey , spec .assocKey )
1541+ if err != nil {
1542+ return err
1543+ }
14911544 }
1492- return c .ensureZoneRoutingTableAssociation (ctx , zoneName , true ,
1493- IdentifierZoneSubnetWorkers , IdentifierZoneSubnetWorkersRouteTableAssoc )
1545+ return nil
14941546 }
14951547}
14961548
@@ -1570,16 +1622,16 @@ func (c *FlowContext) ensureVPCEndpointZoneRoutingTableAssociation(ctx context.C
15701622
15711623func (c * FlowContext ) deleteRoutingTableAssociations (zoneName string ) flow.TaskFn {
15721624 return func (ctx context.Context ) error {
1573- if err := c .deleteZoneRoutingTableAssociation ( ctx , zoneName , false ,
1574- IdentifierZoneSubnetPublic , IdentifierZoneSubnetPublicRouteTableAssoc ); err != nil {
1575- return err
1576- }
1577- if err := c . deleteZoneRoutingTableAssociation ( ctx , zoneName , true ,
1578- IdentifierZoneSubnetPrivate , IdentifierZoneSubnetPrivateRouteTableAssoc ); err != nil {
1579- return err
1625+ specs := c .routingAssociationSpecs ()
1626+
1627+ for _ , spec := range specs {
1628+ err := c . deleteZoneRoutingTableAssociation ( ctx , zoneName , spec . zoneRouteTable , spec . subnetKey , spec . assocKey )
1629+ if err != nil {
1630+ return err
1631+ }
15801632 }
1581- return c . deleteZoneRoutingTableAssociation ( ctx , zoneName , true ,
1582- IdentifierZoneSubnetWorkers , IdentifierZoneSubnetWorkersRouteTableAssoc )
1633+
1634+ return nil
15831635 }
15841636}
15851637
0 commit comments