@@ -398,13 +398,25 @@ func (s *PowerVSClusterScope) GetServiceInstanceID() string {
398
398
return ""
399
399
}
400
400
401
+ // SetTransitGatewayConnectionStatus sets the connection status of Transit gateway.
402
+ func (s * PowerVSClusterScope ) SetTransitGatewayConnectionStatus (networkType networkConnectionType , resource * infrav1beta2.ResourceReference ) {
403
+ if s .IBMPowerVSCluster .Status .TransitGateway == nil || resource == nil {
404
+ return
405
+ }
406
+
407
+ switch networkType {
408
+ case powervsNetworkConnectionType :
409
+ s .IBMPowerVSCluster .Status .TransitGateway .PowerVSConnection = resource
410
+ case vpcNetworkConnectionType :
411
+ s .IBMPowerVSCluster .Status .TransitGateway .VPCConnection = resource
412
+ }
413
+ }
414
+
401
415
// SetTransitGatewayStatus sets the status of Transit gateway.
402
- func (s * PowerVSClusterScope ) SetTransitGatewayStatus (id * string , controllerCreated * bool , powerVSConnResource , vpcConnResource * infrav1beta2. ResourceReference ) {
416
+ func (s * PowerVSClusterScope ) SetTransitGatewayStatus (id * string , controllerCreated * bool ) {
403
417
s .IBMPowerVSCluster .Status .TransitGateway = & infrav1beta2.TransitGatewayStatus {
404
418
ID : id ,
405
419
ControllerCreated : controllerCreated ,
406
- PowerVSConnection : powerVSConnResource ,
407
- VPCConnection : vpcConnResource ,
408
420
}
409
421
}
410
422
@@ -1632,7 +1644,7 @@ func (s *PowerVSClusterScope) ReconcileTransitGateway() (bool, error) {
1632
1644
if err != nil {
1633
1645
return false , err
1634
1646
}
1635
- requeue , _ , _ , err := s .checkAndUpdateTransitGateway (tg , false )
1647
+ requeue , err := s .checkAndUpdateTransitGateway (tg )
1636
1648
if err != nil {
1637
1649
return false , err
1638
1650
}
@@ -1647,24 +1659,19 @@ func (s *PowerVSClusterScope) ReconcileTransitGateway() (bool, error) {
1647
1659
1648
1660
// check the status and update the transit gateway's connections if they are not proper
1649
1661
if tg != nil {
1650
- requeue , powerVSConn , vpcConn , err := s .checkAndUpdateTransitGateway (tg , true )
1662
+ requeue , err := s .checkAndUpdateTransitGateway (tg )
1651
1663
if err != nil {
1652
1664
return false , err
1653
1665
}
1654
- s .SetTransitGatewayStatus (tg .ID , ptr .To (false ), powerVSConn , vpcConn )
1655
1666
return requeue , nil
1656
1667
}
1657
1668
1658
1669
// create transit gateway
1659
1670
s .V (3 ).Info ("Creating transit gateway" )
1660
- transitGatewayID , powerVSConnID , vpcConnID , err := s .createTransitGateway ()
1661
- if err != nil {
1671
+ if err := s .createTransitGateway (); err != nil {
1662
1672
return false , fmt .Errorf ("failed to create transit gateway: %v" , err )
1663
1673
}
1664
- if transitGatewayID != nil {
1665
- s .Info ("Created transit gateway" , "id" , transitGatewayID )
1666
- s .SetTransitGatewayStatus (transitGatewayID , ptr .To (true ), & infrav1beta2.ResourceReference {ID : powerVSConnID , ControllerCreated : ptr .To (true )}, & infrav1beta2.ResourceReference {ID : vpcConnID , ControllerCreated : ptr .To (true )})
1667
- }
1674
+
1668
1675
return true , nil
1669
1676
}
1670
1677
@@ -1691,21 +1698,23 @@ func (s *PowerVSClusterScope) isTransitGatewayExists() (*tgapiv1.TransitGateway,
1691
1698
return nil , nil
1692
1699
}
1693
1700
1701
+ s .SetTransitGatewayStatus (transitGateway .ID , ptr .To (false ))
1702
+
1694
1703
return transitGateway , nil
1695
1704
}
1696
1705
1697
1706
// checkAndUpdateTransitGateway checks given transit gateway's status and its connections.
1698
1707
// if update is set to true, it updates the transit gateway connections too if it is not exist already.
1699
- func (s * PowerVSClusterScope ) checkAndUpdateTransitGateway (transitGateway * tgapiv1.TransitGateway , update bool ) (bool , * infrav1beta2. ResourceReference , * infrav1beta2. ResourceReference , error ) {
1708
+ func (s * PowerVSClusterScope ) checkAndUpdateTransitGateway (transitGateway * tgapiv1.TransitGateway ) (bool , error ) {
1700
1709
requeue , err := s .checkTransitGatewayStatus (transitGateway )
1701
1710
if err != nil {
1702
- return false , nil , nil , err
1711
+ return false , err
1703
1712
}
1704
1713
if requeue {
1705
- return requeue , nil , nil , nil
1714
+ return requeue , nil
1706
1715
}
1707
1716
1708
- return s .checkAndUpdateTransitGatewayConnections (transitGateway , update )
1717
+ return s .checkAndUpdateTransitGatewayConnections (transitGateway )
1709
1718
}
1710
1719
1711
1720
// checkTransitGatewayStatus checks the state of a transit gateway.
@@ -1727,144 +1736,96 @@ func (s *PowerVSClusterScope) checkTransitGatewayStatus(tg *tgapiv1.TransitGatew
1727
1736
}
1728
1737
1729
1738
// checkAndUpdateTransitGatewayConnections checks given transit gateway's connections status.
1730
- // if update is set to true, it updates the transit gateway connections too if it is not exist already.
1731
- func (s * PowerVSClusterScope ) checkAndUpdateTransitGatewayConnections (transitGateway * tgapiv1.TransitGateway , update bool ) (bool , * infrav1beta2. ResourceReference , * infrav1beta2. ResourceReference , error ) {
1739
+ // it also creates the transit gateway connections if it is not exist already.
1740
+ func (s * PowerVSClusterScope ) checkAndUpdateTransitGatewayConnections (transitGateway * tgapiv1.TransitGateway ) (bool , error ) {
1732
1741
tgConnections , _ , err := s .TransitGatewayClient .ListTransitGatewayConnections (& tgapiv1.ListTransitGatewayConnectionsOptions {
1733
1742
TransitGatewayID : transitGateway .ID ,
1734
1743
})
1735
1744
if err != nil {
1736
- return false , nil , nil , fmt .Errorf ("failed to list transit gateway connections: %w" , err )
1745
+ return false , fmt .Errorf ("failed to list transit gateway connections: %w" , err )
1737
1746
}
1738
1747
1739
1748
vpcCRN , err := s .fetchVPCCRN ()
1740
1749
if err != nil {
1741
- return false , nil , nil , fmt .Errorf ("failed to fetch VPC CRN: %w" , err )
1750
+ return false , fmt .Errorf ("failed to fetch VPC CRN: %w" , err )
1742
1751
}
1743
1752
1744
1753
pvsServiceInstanceCRN , err := s .fetchPowerVSServiceInstanceCRN ()
1745
1754
if err != nil {
1746
- return false , nil , nil , fmt .Errorf ("failed to fetch PowerVS service instance CRN: %w" , err )
1755
+ return false , fmt .Errorf ("failed to fetch PowerVS service instance CRN: %w" , err )
1747
1756
}
1748
1757
1749
- var powerVSConnResource , vpcConnResource * infrav1beta2.ResourceReference
1750
-
1751
1758
if len (tgConnections .Connections ) == 0 {
1752
- if update {
1753
- s .V (3 ).Info ("Connections not exist on existing transit gateway, creating them" )
1754
- powerVSConnID , vpcConnID , err := s .createTransitGatewayConnections (transitGateway , pvsServiceInstanceCRN , vpcCRN )
1755
- if err != nil {
1756
- return false , nil , nil , err
1757
- }
1758
-
1759
- powerVSConnResource = & infrav1beta2.ResourceReference {
1760
- ID : powerVSConnID ,
1761
- ControllerCreated : ptr .To (true ),
1762
- }
1763
-
1764
- vpcConnResource = & infrav1beta2.ResourceReference {
1765
- ID : vpcConnID ,
1766
- ControllerCreated : ptr .To (true ),
1767
- }
1768
-
1769
- return true , powerVSConnResource , vpcConnResource , nil
1759
+ s .V (3 ).Info ("Connections not exist on transit gateway, creating them" )
1760
+ if err := s .createTransitGatewayConnections (transitGateway , pvsServiceInstanceCRN , vpcCRN ); err != nil {
1761
+ return false , err
1770
1762
}
1771
1763
1772
- return false , nil , nil , fmt . Errorf ( "no connections are attached to transit gateway" )
1764
+ return true , nil
1773
1765
}
1774
1766
1775
- requeue , powerVSConnID , vpcConnID , err := s .validateTransitGatewayConnections (tgConnections .Connections , vpcCRN , pvsServiceInstanceCRN )
1767
+ requeue , powerVSConnStatus , vpcConnStatus , err := s .validateTransitGatewayConnections (tgConnections .Connections , vpcCRN , pvsServiceInstanceCRN )
1776
1768
if err != nil {
1777
- return false , nil , nil , err
1769
+ return false , err
1778
1770
} else if requeue {
1779
- return requeue , nil , nil , nil
1771
+ return requeue , nil
1780
1772
}
1781
1773
1782
1774
// return when connections are in attached state.
1783
- if powerVSConnID != nil && vpcConnID != nil {
1784
- powerVSConnResource = & infrav1beta2.ResourceReference {
1785
- ID : powerVSConnID ,
1786
- ControllerCreated : ptr .To (false ),
1787
- }
1788
-
1789
- vpcConnResource = & infrav1beta2.ResourceReference {
1790
- ID : vpcConnID ,
1791
- ControllerCreated : ptr .To (false ),
1792
- }
1793
-
1794
- return false , powerVSConnResource , vpcConnResource , nil
1795
- }
1796
-
1797
- // return error when any of the connections are not in attached state.
1798
- if (powerVSConnID == nil || vpcConnID == nil ) && ! update {
1799
- return false , nil , nil , fmt .Errorf ("either one of PowerVS or VPC transit gateway connections is not attached, PowerVS: %t VPC: %t" , powerVSConnID != nil , vpcConnID != nil )
1775
+ if powerVSConnStatus && vpcConnStatus {
1776
+ return false , nil
1800
1777
}
1801
1778
1802
- // update the connections when update is set to true
1803
- if powerVSConnID == nil {
1779
+ // update the connections when connection not exist
1780
+ if ! powerVSConnStatus {
1804
1781
s .V (3 ).Info ("Only PowerVS connection not exist in transit gateway, creating it" )
1805
- conn , err := s .createTransitGatewayConnection (transitGateway .ID , ptr .To (getTGPowerVSConnectionName (* transitGateway .Name )), ptr .To (string (powervsNetworkConnectionType )), pvsServiceInstanceCRN )
1806
- if err != nil {
1807
- return false , nil , nil , err
1808
- }
1809
- powerVSConnResource = & infrav1beta2.ResourceReference {
1810
- ID : conn .ID ,
1811
- ControllerCreated : ptr .To (true ),
1812
- }
1813
- } else {
1814
- s .V (3 ).Info ("Using existing PowerVS connection in transit gateway" , "id" , powerVSConnID )
1815
- powerVSConnResource = & infrav1beta2.ResourceReference {
1816
- ID : powerVSConnID ,
1817
- ControllerCreated : ptr .To (false ),
1782
+ if err := s .createTransitGatewayConnection (transitGateway .ID , ptr .To (getTGPowerVSConnectionName (* transitGateway .Name )), pvsServiceInstanceCRN , powervsNetworkConnectionType ); err != nil {
1783
+ return false , err
1818
1784
}
1819
1785
}
1820
1786
1821
- if vpcConnID == nil {
1787
+ if ! vpcConnStatus {
1822
1788
s .V (3 ).Info ("Only VPC connection not exist in transit gateway, creating it" )
1823
- conn , err := s .createTransitGatewayConnection (transitGateway .ID , ptr .To (getTGVPCConnectionName (* transitGateway .Name )), ptr .To (string (vpcNetworkConnectionType )), vpcCRN )
1824
- if err != nil {
1825
- return false , nil , nil , err
1826
- }
1827
- vpcConnResource = & infrav1beta2.ResourceReference {
1828
- ID : conn .ID ,
1829
- ControllerCreated : ptr .To (true ),
1830
- }
1831
- } else {
1832
- s .V (3 ).Info ("Using existing VPC connection in transit gateway" , "id" , vpcConnID )
1833
- vpcConnResource = & infrav1beta2.ResourceReference {
1834
- ID : vpcConnID ,
1835
- ControllerCreated : ptr .To (false ),
1789
+ if err := s .createTransitGatewayConnection (transitGateway .ID , ptr .To (getTGVPCConnectionName (* transitGateway .Name )), vpcCRN , vpcNetworkConnectionType ); err != nil {
1790
+ return false , err
1836
1791
}
1837
1792
}
1838
1793
1839
- return true , powerVSConnResource , vpcConnResource , nil
1794
+ return true , nil
1840
1795
}
1841
1796
1842
1797
// validateTransitGatewayConnections validates the existing transit gateway connections.
1843
1798
// to avoid returning many return values, connection id will be returned and considered that connection is in attached state.
1844
- func (s * PowerVSClusterScope ) validateTransitGatewayConnections (connections []tgapiv1.TransitGatewayConnectionCust , vpcCRN , pvsServiceInstanceCRN * string ) (bool , * string , * string , error ) {
1845
- var powerVSConnID , vpcConnID * string
1799
+ func (s * PowerVSClusterScope ) validateTransitGatewayConnections (connections []tgapiv1.TransitGatewayConnectionCust , vpcCRN , pvsServiceInstanceCRN * string ) (bool , bool , bool , error ) {
1800
+ var powerVSConnStatus , vpcConnStatus bool
1846
1801
for _ , conn := range connections {
1847
1802
if * conn .NetworkType == string (vpcNetworkConnectionType ) && * conn .NetworkID == * vpcCRN {
1848
1803
if requeue , err := s .checkTransitGatewayConnectionStatus (conn ); err != nil {
1849
- return requeue , nil , nil , err
1804
+ return requeue , false , false , err
1850
1805
} else if requeue {
1851
- return requeue , nil , nil , nil
1806
+ return requeue , false , false , nil
1807
+ }
1808
+
1809
+ if s .IBMPowerVSCluster .Status .TransitGateway != nil && s .IBMPowerVSCluster .Status .TransitGateway .VPCConnection == nil {
1810
+ s .SetTransitGatewayConnectionStatus (vpcNetworkConnectionType , & infrav1beta2.ResourceReference {ID : conn .ID , ControllerCreated : ptr .To (false )})
1852
1811
}
1853
- s .V (3 ).Info ("VPC connection in Transit gateway is in attached state" , "name" , * conn .Name )
1854
- vpcConnID = conn .ID
1812
+ vpcConnStatus = true
1855
1813
}
1856
1814
if * conn .NetworkType == string (powervsNetworkConnectionType ) && * conn .NetworkID == * pvsServiceInstanceCRN {
1857
1815
if requeue , err := s .checkTransitGatewayConnectionStatus (conn ); err != nil {
1858
- return requeue , nil , nil , err
1816
+ return requeue , false , false , err
1859
1817
} else if requeue {
1860
- return requeue , nil , nil , nil
1818
+ return requeue , false , false , nil
1861
1819
}
1862
- s .V (3 ).Info ("PowerVS connection in Transit gateway is in attached state" , "name" , * conn .Name )
1863
- powerVSConnID = conn .ID
1820
+
1821
+ if s .IBMPowerVSCluster .Status .TransitGateway != nil && s .IBMPowerVSCluster .Status .TransitGateway .PowerVSConnection == nil {
1822
+ s .SetTransitGatewayConnectionStatus (powervsNetworkConnectionType , & infrav1beta2.ResourceReference {ID : conn .ID , ControllerCreated : ptr .To (false )})
1823
+ }
1824
+ powerVSConnStatus = true
1864
1825
}
1865
1826
}
1866
1827
1867
- return false , powerVSConnID , vpcConnID , nil
1828
+ return false , powerVSConnStatus , vpcConnStatus , nil
1868
1829
}
1869
1830
1870
1831
// checkTransitGatewayConnectionStatus checks the state of a transit gateway connection.
@@ -1885,54 +1846,56 @@ func (s *PowerVSClusterScope) checkTransitGatewayConnectionStatus(con tgapiv1.Tr
1885
1846
return false , nil
1886
1847
}
1887
1848
1888
- // createTransitGatewayConnection creates transit gateway connection.
1889
- func (s * PowerVSClusterScope ) createTransitGatewayConnection (transitGatewayID , connName , networkType , networkID * string ) ( * tgapiv1. TransitGatewayConnectionCust , error ) {
1849
+ // createTransitGatewayConnection creates transit gateway connection and sets the connection status .
1850
+ func (s * PowerVSClusterScope ) createTransitGatewayConnection (transitGatewayID , connName , networkID * string , networkType networkConnectionType ) error {
1890
1851
s .V (3 ).Info ("Creating transit gateway connection" , "tgID" , transitGatewayID , "type" , networkType , "name" , connName )
1891
1852
conn , _ , err := s .TransitGatewayClient .CreateTransitGatewayConnection (& tgapiv1.CreateTransitGatewayConnectionOptions {
1892
1853
TransitGatewayID : transitGatewayID ,
1893
- NetworkType : networkType ,
1854
+ NetworkType : ptr . To ( string ( networkType )) ,
1894
1855
NetworkID : networkID ,
1895
1856
Name : connName ,
1896
1857
})
1858
+ if err != nil {
1859
+ return err
1860
+ }
1861
+ s .SetTransitGatewayConnectionStatus (networkType , & infrav1beta2.ResourceReference {ID : conn .ID , ControllerCreated : ptr .To (true )})
1897
1862
1898
- return conn , err
1863
+ return nil
1899
1864
}
1900
1865
1901
1866
// createTransitGatewayConnections creates PowerVS and VPC connections in the transit gateway.
1902
- func (s * PowerVSClusterScope ) createTransitGatewayConnections (tg * tgapiv1.TransitGateway , pvsServiceInstanceCRN , vpcCRN * string ) (* string , * string , error ) {
1903
- powerVSConn , err := s .createTransitGatewayConnection (tg .ID , ptr .To (getTGPowerVSConnectionName (* tg .Name )), ptr .To (string (powervsNetworkConnectionType )), pvsServiceInstanceCRN )
1904
- if err != nil {
1905
- return nil , nil , fmt .Errorf ("failed to create PowerVS connection in transit gateway: %w" , err )
1867
+ func (s * PowerVSClusterScope ) createTransitGatewayConnections (tg * tgapiv1.TransitGateway , pvsServiceInstanceCRN , vpcCRN * string ) error {
1868
+ if err := s .createTransitGatewayConnection (tg .ID , ptr .To (getTGPowerVSConnectionName (* tg .Name )), pvsServiceInstanceCRN , powervsNetworkConnectionType ); err != nil {
1869
+ return fmt .Errorf ("failed to create PowerVS connection in transit gateway: %w" , err )
1906
1870
}
1907
1871
1908
- vpcConn , err := s .createTransitGatewayConnection (tg .ID , ptr .To (getTGVPCConnectionName (* tg .Name )), ptr .To (string (vpcNetworkConnectionType )), vpcCRN )
1909
- if err != nil {
1910
- return nil , nil , fmt .Errorf ("failed to create VPC connection in transit gateway: %w" , err )
1872
+ if err := s .createTransitGatewayConnection (tg .ID , ptr .To (getTGVPCConnectionName (* tg .Name )), vpcCRN , vpcNetworkConnectionType ); err != nil {
1873
+ return fmt .Errorf ("failed to create VPC connection in transit gateway: %w" , err )
1911
1874
}
1912
1875
1913
- return powerVSConn . ID , vpcConn . ID , nil
1876
+ return nil
1914
1877
}
1915
1878
1916
- // createTransitGateway create transit gateway.
1917
- func (s * PowerVSClusterScope ) createTransitGateway () ( * string , * string , * string , error ) {
1879
+ // createTransitGateway creates transit gateway and sets the transit gateway status .
1880
+ func (s * PowerVSClusterScope ) createTransitGateway () error {
1918
1881
// TODO(karthik-k-n): Verify that the supplied zone supports PER
1919
1882
// TODO(karthik-k-n): consider moving to clusterscope
1920
1883
1921
1884
// fetch resource group id
1922
1885
resourceGroupID := s .GetResourceGroupID ()
1923
1886
if resourceGroupID == "" {
1924
- return nil , nil , nil , fmt .Errorf ("failed to fetch resource group ID for resource group %v, ID is empty" , s .ResourceGroup ())
1887
+ return fmt .Errorf ("failed to fetch resource group ID for resource group %v, ID is empty" , s .ResourceGroup ())
1925
1888
}
1926
1889
1927
1890
location , globalRouting , err := genUtil .GetTransitGatewayLocationAndRouting (s .Zone (), s .VPC ().Region )
1928
1891
if err != nil {
1929
- return nil , nil , nil , fmt .Errorf ("failed to get transit gateway location and routing: %w" , err )
1892
+ return fmt .Errorf ("failed to get transit gateway location and routing: %w" , err )
1930
1893
}
1931
1894
1932
1895
// throw error when user tries to use local routing where global routing is required.
1933
1896
// TODO: Add a webhook validation for below condition.
1934
1897
if s .IBMPowerVSCluster .Spec .TransitGateway .GlobalRouting != nil && ! * s .IBMPowerVSCluster .Spec .TransitGateway .GlobalRouting && * globalRouting {
1935
- return nil , nil , nil , fmt .Errorf ("failed to use local routing for transit gateway since powervs and vpc are in different region and requires global routing" )
1898
+ return fmt .Errorf ("failed to use local routing for transit gateway since powervs and vpc are in different region and requires global routing" )
1936
1899
}
1937
1900
// setting global routing to true when it is set by user.
1938
1901
if s .IBMPowerVSCluster .Spec .TransitGateway .GlobalRouting != nil && * s .IBMPowerVSCluster .Spec .TransitGateway .GlobalRouting {
@@ -1947,23 +1910,25 @@ func (s *PowerVSClusterScope) createTransitGateway() (*string, *string, *string,
1947
1910
ResourceGroup : & tgapiv1.ResourceGroupIdentity {ID : ptr .To (resourceGroupID )},
1948
1911
})
1949
1912
if err != nil {
1950
- return nil , nil , nil , err
1913
+ return err
1951
1914
}
1952
1915
1916
+ s .SetTransitGatewayStatus (tg .ID , ptr .To (true ))
1917
+
1953
1918
vpcCRN , err := s .fetchVPCCRN ()
1954
1919
if err != nil {
1955
- return nil , nil , nil , fmt .Errorf ("failed to fetch VPC CRN: %w" , err )
1920
+ return fmt .Errorf ("failed to fetch VPC CRN: %w" , err )
1956
1921
}
1957
1922
pvsServiceInstanceCRN , err := s .fetchPowerVSServiceInstanceCRN ()
1958
1923
if err != nil {
1959
- return nil , nil , nil , fmt .Errorf ("failed to fetch PowerVS service instance CRN: %w" , err )
1924
+ return fmt .Errorf ("failed to fetch PowerVS service instance CRN: %w" , err )
1960
1925
}
1961
1926
1962
- powerVSConnID , vpcConnID , err := s .createTransitGatewayConnections (tg , pvsServiceInstanceCRN , vpcCRN )
1963
- if err != nil {
1964
- return nil , nil , nil , err
1927
+ if err := s .createTransitGatewayConnections (tg , pvsServiceInstanceCRN , vpcCRN ); err != nil {
1928
+ return err
1965
1929
}
1966
- return tg .ID , powerVSConnID , vpcConnID , nil
1930
+
1931
+ return nil
1967
1932
}
1968
1933
1969
1934
// ReconcileLoadBalancers reconcile loadBalancer.
0 commit comments