Skip to content

Commit 7af44af

Browse files
committed
add firewall to AB e2e cluster
1 parent ef5e03c commit 7af44af

File tree

2 files changed

+99
-0
lines changed

2 files changed

+99
-0
lines changed

e2e/aks_model.go

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
185278
func 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

e2e/config/azure.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ import (
3939

4040
type AzureClient struct {
4141
AKS *armcontainerservice.ManagedClustersClient
42+
AzureFirewall *armnetwork.AzureFirewallsClient
4243
Blob *azblob.Client
4344
StorageContainers *armstorage.BlobContainersClient
4445
CacheRulesClient *armcontainerregistry.CacheRulesClient
@@ -258,6 +259,11 @@ func NewAzureClient() (*AzureClient, error) {
258259
return nil, fmt.Errorf("create vnet client: %w", err)
259260
}
260261

262+
cloud.AzureFirewall, err = armnetwork.NewAzureFirewallsClient(Config.SubscriptionID, credential, opts)
263+
if err != nil {
264+
return nil, fmt.Errorf("create firewall client: %w", err)
265+
}
266+
261267
cloud.Blob, err = azblob.NewClient(Config.BlobStorageAccountURL(), credential, nil)
262268
if err != nil {
263269
return nil, fmt.Errorf("create blob container client: %w", err)

0 commit comments

Comments
 (0)