@@ -182,6 +182,99 @@ func getBaseClusterModel(clusterName, location, k8sSystemPoolSKU string) *armcon
182182 }
183183}
184184
185+ func addFirewallRules (ctx context.Context , resourceGroupName , location , firewallName , publicIPId , firewallSubnetID , ipConfigName string ) * armnetwork.AzureFirewall {
186+ var (
187+ natRuleCollections []* armnetwork.AzureFirewallNatRuleCollection
188+ netRuleCollections []* armnetwork.AzureFirewallNetworkRuleCollection
189+ )
190+
191+ appRule := armnetwork.AzureFirewallApplicationRule {
192+ Name : to .Ptr ("fqdn" ),
193+ SourceAddresses : []* string {to .Ptr ("*" )},
194+ Protocols : []* armnetwork.AzureFirewallApplicationRuleProtocol {
195+ {
196+ ProtocolType : to .Ptr (armnetwork .AzureFirewallApplicationRuleProtocolTypeHTTP ),
197+ Port : to.Ptr [int32 ](80 ),
198+ },
199+ {
200+ ProtocolType : to .Ptr (armnetwork .AzureFirewallApplicationRuleProtocolTypeHTTPS ),
201+ Port : to.Ptr [int32 ](443 ),
202+ },
203+ },
204+ FqdnTags : []* string {to .Ptr ("AzureKubernetesService" )},
205+ }
206+
207+ // Add to a collection
208+ appRuleCollection := armnetwork.AzureFirewallApplicationRuleCollection {
209+ Name : to .Ptr ("aksfwar" ),
210+ Properties : & armnetwork.AzureFirewallApplicationRuleCollectionPropertiesFormat {
211+ Priority : to.Ptr [int32 ](100 ),
212+ Action : & armnetwork.AzureFirewallRCAction {
213+ Type : to .Ptr (armnetwork .AzureFirewallRCActionTypeAllow ),
214+ },
215+ Rules : []* armnetwork.AzureFirewallApplicationRule {& appRule },
216+ },
217+ }
218+
219+ ipConfigurations := []* armnetwork.AzureFirewallIPConfiguration {
220+ {
221+ Name : to .Ptr (ipConfigName ),
222+ Properties : & armnetwork.AzureFirewallIPConfigurationPropertiesFormat {
223+ Subnet : & armnetwork.SubResource {
224+ ID : to .Ptr (firewallSubnetID ),
225+ },
226+ PublicIPAddress : & armnetwork.SubResource {
227+ ID : to .Ptr (publicIPId ),
228+ },
229+ },
230+ },
231+ }
232+
233+ logf (ctx , "Firewall rules added successfully" )
234+ return & armnetwork.AzureFirewall {
235+ Location : to .Ptr (location ),
236+ Properties : & armnetwork.AzureFirewallPropertiesFormat {
237+ ApplicationRuleCollections : []* armnetwork.AzureFirewallApplicationRuleCollection {& appRuleCollection },
238+ NetworkRuleCollections : netRuleCollections ,
239+ NatRuleCollections : natRuleCollections ,
240+ IPConfigurations : ipConfigurations ,
241+ },
242+ }
243+ }
244+
245+ func getFirewall (ctx context.Context , resourceGroupName , location string , vnet * VNet ) * armnetwork.AzureFirewall {
246+ firewallClient := config .Azure .AzureFirewall
247+ firewallName := fmt .Sprintf ("%s-fw" , resourceGroupName )
248+ ipConfigName := fmt .Sprintf ("%s-fwconfig" , resourceGroupName )
249+ firewallSubnetId := * mustGetSubnetBySubnetName (* vnet .Subnets , handlerazure .DefaultAzureVNetFirewallSubnetName ).ID
250+ firewall := firewallWithDNSProxy (* publicIP .ID , firewallSubnetId , ipConfigName , location )
251+
252+ gomegahelper .Retry (
253+ "createFirewall" ,
254+ logger ,
255+ func (n uint , retrym gomegahelper.RetryAssertionBuilder ) {
256+ firewallCreateFuture , err := firewallClient .CreateOrUpdate (ctx , resourceGroupName , firewallName , * firewall )
257+ m .Expect (err ).NotTo (handlerazure .ResponseFailed (firewallCreateFuture .FutureAPI .Response ()), "create firewall" )
258+
259+ logger .LogKV ("step" , "create firewall" , "state" , "waiting" , "firewall" , firewallName )
260+ err = firewallCreateFuture .WaitForCompletionRef (ctx , firewallClient .Client )
261+ retrym .Expect (err ).
262+ NotTo (handlerazure .ResponseFailed (firewallCreateFuture .FutureAPI .Response ()), "wait for firewall creation" )
263+ logger .LogKV ("step" , "create firewall" , "state" , "created" , "firewall" , firewallName )
264+ },
265+ retry .DelayType (retry .FixedDelay ),
266+ retry .Delay (time .Duration (1 )* time .Minute ),
267+ retry .Attempts (5 ),
268+ )
269+
270+ firewallCreated , err := firewallClient .Get (ctx , resourceGroupName , firewallName )
271+ m .Expect (err ).NotTo (handlerazure .ResponseFailed (firewallCreated .Response ), "get firewall" )
272+ firewallPrivateIP := * (* firewallCreated .IPConfigurations )[0 ].PrivateIPAddress
273+ logger .Logf ("firewall private ip: %s" , firewallPrivateIP )
274+
275+ return & firewallCreated
276+ }
277+
185278func addAirgapNetworkSettings (ctx context.Context , clusterModel * armcontainerservice.ManagedCluster , privateACRName , location string ) error {
186279 logf (ctx , "Adding network settings for airgap cluster %s in rg %s" , * clusterModel .Name , * clusterModel .Properties .NodeResourceGroup )
187280
0 commit comments