@@ -13,9 +13,9 @@ import (
1313 "github.com/Azure/agentbaker/pkg/agent"
1414 "github.com/Azure/azure-sdk-for-go/sdk/azcore"
1515 "github.com/Azure/azure-sdk-for-go/sdk/azcore/to"
16- "github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/containerregistry/armcontainerregistry"
17- "github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/containerservice/armcontainerservice/v6 "
18- "github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/network/armnetwork/v6 "
16+ "github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/containerregistry/armcontainerregistry/v2 "
17+ "github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/containerservice/armcontainerservice/v8 "
18+ "github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/network/armnetwork/v7 "
1919 "github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/privatedns/armprivatedns"
2020)
2121
@@ -175,6 +175,16 @@ func getBaseClusterModel(clusterName, location, k8sSystemPoolSKU string) *armcon
175175 Enabled : to .Ptr (false ),
176176 },
177177 },
178+ LinuxProfile : & armcontainerservice.LinuxProfile {
179+ AdminUsername : to .Ptr ("azureuser" ),
180+ SSH : & armcontainerservice.SSHConfiguration {
181+ PublicKeys : []* armcontainerservice.SSHPublicKey {
182+ {
183+ KeyData : to .Ptr (string (config .SysSSHPublicKey )),
184+ },
185+ },
186+ },
187+ },
178188 },
179189 Identity : & armcontainerservice.ManagedClusterIdentity {
180190 Type : to .Ptr (armcontainerservice .ResourceIdentityTypeSystemAssigned ),
@@ -275,6 +285,17 @@ func addFirewallRules(
275285 ctx context.Context , clusterModel * armcontainerservice.ManagedCluster ,
276286 location string ,
277287) error {
288+ routeTableName := "abe2e-fw-rt"
289+ rtGetResp , err := config .Azure .RouteTables .Get (
290+ ctx ,
291+ * clusterModel .Properties .NodeResourceGroup ,
292+ routeTableName ,
293+ nil ,
294+ )
295+ if err == nil && len (rtGetResp .Properties .Subnets ) != 0 {
296+ // already associated with aks subnet
297+ return nil
298+ }
278299
279300 vnet , err := getClusterVNet (ctx , * clusterModel .Properties .NodeResourceGroup )
280301 if err != nil {
@@ -366,7 +387,6 @@ func addFirewallRules(
366387 return fmt .Errorf ("failed to get firewall private IP address" )
367388 }
368389
369- routeTableName := "abe2e-fw-rt"
370390 routeTableParams := armnetwork.RouteTable {
371391 Location : to .Ptr (location ),
372392 Properties : & armnetwork.RouteTablePropertiesFormat {
@@ -535,55 +555,33 @@ func airGapSecurityGroup(location, clusterFQDN string) (armnetwork.SecurityGroup
535555
536556func addPrivateEndpointForACR (ctx context.Context , nodeResourceGroup , privateACRName string , vnet VNet , location string ) error {
537557 logf (ctx , "Checking if private endpoint for private container registry is in rg %s" , nodeResourceGroup )
538-
539558 var err error
540- var exists bool
559+ var privateEndpoint * armnetwork. PrivateEndpoint
541560 privateEndpointName := "PE-for-ABE2ETests"
542- if exists , err = privateEndpointExists (ctx , nodeResourceGroup , privateEndpointName ); err != nil {
543- return err
544- }
545- if exists {
546- logf (ctx , "Private Endpoint already exists, skipping creation" )
547- return nil
548- }
549-
550- var peResp armnetwork.PrivateEndpointsClientCreateOrUpdateResponse
551- if peResp , err = createPrivateEndpoint (ctx , nodeResourceGroup , privateEndpointName , privateACRName , vnet , location ); err != nil {
561+ if privateEndpoint , err = createPrivateEndpoint (ctx , nodeResourceGroup , privateEndpointName , privateACRName , vnet , location ); err != nil {
552562 return err
553563 }
554564
555565 privateZoneName := "privatelink.azurecr.io"
556- var pzResp armprivatedns.PrivateZonesClientCreateOrUpdateResponse
557- if pzResp , err = createPrivateZone (ctx , nodeResourceGroup , privateZoneName ); err != nil {
566+ var privateZone * armprivatedns.PrivateZone
567+ if privateZone , err = createPrivateZone (ctx , nodeResourceGroup , privateZoneName ); err != nil {
558568 return err
559569 }
560570
561571 if err = createPrivateDNSLink (ctx , vnet , nodeResourceGroup , privateZoneName ); err != nil {
562572 return err
563573 }
564574
565- if err = addRecordSetToPrivateDNSZone (ctx , peResp , nodeResourceGroup , privateZoneName ); err != nil {
575+ if err = addRecordSetToPrivateDNSZone (ctx , privateEndpoint , nodeResourceGroup , privateZoneName ); err != nil {
566576 return err
567577 }
568578
569- if err = addDNSZoneGroup (ctx , pzResp , nodeResourceGroup , privateZoneName , * peResp .Name ); err != nil {
579+ if err = addDNSZoneGroup (ctx , privateZone , nodeResourceGroup , privateZoneName , * privateEndpoint .Name ); err != nil {
570580 return err
571581 }
572582 return nil
573583}
574584
575- func privateEndpointExists (ctx context.Context , nodeResourceGroup , privateEndpointName string ) (bool , error ) {
576- existingPE , err := config .Azure .PrivateEndpointClient .Get (ctx , nodeResourceGroup , privateEndpointName , nil )
577- if err == nil && existingPE .ID != nil {
578- logf (ctx , "Private Endpoint already exists with ID: %s" , * existingPE .ID )
579- return true , nil
580- }
581- if err != nil && ! strings .Contains (err .Error (), "ResourceNotFound" ) {
582- return false , fmt .Errorf ("failed to get private endpoint: %w" , err )
583- }
584- return false , nil
585- }
586-
587585func createPrivateAzureContainerRegistryPullSecret (ctx context.Context , cluster * armcontainerservice.ManagedCluster , kubeconfig * Kubeclient , resourceGroup string , isNonAnonymousPull bool ) error {
588586 privateACRName := config .GetPrivateACRName (isNonAnonymousPull , * cluster .Location )
589587 if isNonAnonymousPull {
@@ -768,7 +766,15 @@ func addCacheRulesToPrivateAzureContainerRegistry(ctx context.Context, resourceG
768766 return nil
769767}
770768
771- func createPrivateEndpoint (ctx context.Context , nodeResourceGroup , privateEndpointName , privateACRName string , vnet VNet , location string ) (armnetwork.PrivateEndpointsClientCreateOrUpdateResponse , error ) {
769+ func createPrivateEndpoint (ctx context.Context , nodeResourceGroup , privateEndpointName , privateACRName string , vnet VNet , location string ) (* armnetwork.PrivateEndpoint , error ) {
770+ existingPE , err := config .Azure .PrivateEndpointClient .Get (ctx , nodeResourceGroup , privateEndpointName , nil )
771+ if err == nil && existingPE .ID != nil {
772+ logf (ctx , "Private Endpoint already exists with ID: %s" , * existingPE .ID )
773+ return & existingPE .PrivateEndpoint , nil
774+ }
775+ if err != nil && ! strings .Contains (err .Error (), "ResourceNotFound" ) {
776+ return nil , fmt .Errorf ("failed to get private endpoint: %w" , err )
777+ }
772778 logf (ctx , "Creating Private Endpoint in rg %s" , nodeResourceGroup )
773779 acrID := fmt .Sprintf ("/subscriptions/%s/resourceGroups/%s/providers/Microsoft.ContainerRegistry/registries/%s" , config .Config .SubscriptionID , config .ResourceGroupName (location ), privateACRName )
774780
@@ -798,18 +804,27 @@ func createPrivateEndpoint(ctx context.Context, nodeResourceGroup, privateEndpoi
798804 nil ,
799805 )
800806 if err != nil {
801- return armnetwork. PrivateEndpointsClientCreateOrUpdateResponse {} , fmt .Errorf ("failed to create private endpoint in BeginCreateOrUpdate: %w" , err )
807+ return nil , fmt .Errorf ("failed to create private endpoint in BeginCreateOrUpdate: %w" , err )
802808 }
803809 resp , err := poller .PollUntilDone (ctx , nil )
804810 if err != nil {
805- return armnetwork. PrivateEndpointsClientCreateOrUpdateResponse {} , fmt .Errorf ("failed to create private endpoint in polling: %w" , err )
811+ return nil , fmt .Errorf ("failed to create private endpoint in polling: %w" , err )
806812 }
807813
808814 logf (ctx , "Private Endpoint created or updated with ID: %s" , * resp .ID )
809- return resp , nil
815+ return & resp . PrivateEndpoint , nil
810816}
811817
812- func createPrivateZone (ctx context.Context , nodeResourceGroup , privateZoneName string ) (armprivatedns.PrivateZonesClientCreateOrUpdateResponse , error ) {
818+ func createPrivateZone (ctx context.Context , nodeResourceGroup , privateZoneName string ) (* armprivatedns.PrivateZone , error ) {
819+ pzResp , err := config .Azure .PrivateZonesClient .Get (
820+ ctx ,
821+ nodeResourceGroup ,
822+ privateZoneName ,
823+ nil ,
824+ )
825+ if err == nil {
826+ return & pzResp .PrivateZone , nil
827+ }
813828 dnsZoneParams := armprivatedns.PrivateZone {
814829 Location : to .Ptr ("global" ),
815830 }
@@ -821,23 +836,36 @@ func createPrivateZone(ctx context.Context, nodeResourceGroup, privateZoneName s
821836 nil ,
822837 )
823838 if err != nil {
824- return armprivatedns. PrivateZonesClientCreateOrUpdateResponse {} , fmt .Errorf ("failed to create private dns zone in BeginCreateOrUpdate: %w" , err )
839+ return nil , fmt .Errorf ("failed to create private dns zone in BeginCreateOrUpdate: %w" , err )
825840 }
826841 resp , err := poller .PollUntilDone (ctx , nil )
827842 if err != nil {
828- return armprivatedns. PrivateZonesClientCreateOrUpdateResponse {} , fmt .Errorf ("failed to create private dns zone in polling: %w" , err )
843+ return nil , fmt .Errorf ("failed to create private dns zone in polling: %w" , err )
829844 }
830845
831846 logf (ctx , "Private DNS Zone created or updated with ID: %s" , * resp .ID )
832- return resp , nil
847+ return & resp . PrivateZone , nil
833848}
834849
835850func createPrivateDNSLink (ctx context.Context , vnet VNet , nodeResourceGroup , privateZoneName string ) error {
851+ networkLinkName := "link-ABE2ETests"
852+ _ , err := config .Azure .VirutalNetworkLinksClient .Get (
853+ ctx ,
854+ nodeResourceGroup ,
855+ privateZoneName ,
856+ networkLinkName ,
857+ nil ,
858+ )
859+
860+ if err == nil {
861+ // private dns link already created
862+ return nil
863+ }
864+
836865 vnetForId , err := config .Azure .VNet .Get (ctx , nodeResourceGroup , vnet .name , nil )
837866 if err != nil {
838867 return fmt .Errorf ("failed to get vnet: %w" , err )
839868 }
840- networkLinkName := "link-ABE2ETests"
841869 linkParams := armprivatedns.VirtualNetworkLink {
842870 Location : to .Ptr ("global" ),
843871 Properties : & armprivatedns.VirtualNetworkLinkProperties {
@@ -867,16 +895,16 @@ func createPrivateDNSLink(ctx context.Context, vnet VNet, nodeResourceGroup, pri
867895 return nil
868896}
869897
870- func addRecordSetToPrivateDNSZone (ctx context.Context , peResp armnetwork.PrivateEndpointsClientCreateOrUpdateResponse , nodeResourceGroup , privateZoneName string ) error {
871- for i , dnsConfigPtr := range peResp .Properties .CustomDNSConfigs {
898+ func addRecordSetToPrivateDNSZone (ctx context.Context , privateEndpoint * armnetwork.PrivateEndpoint , nodeResourceGroup , privateZoneName string ) error {
899+ for i , dnsConfigPtr := range privateEndpoint .Properties .CustomDNSConfigs {
872900 var ipAddresses []string
873901 if dnsConfigPtr == nil {
874902 return fmt .Errorf ("CustomDNSConfigs[%d] is nil" , i )
875903 }
876904
877905 // get the ip addresses
878906 dnsConfig := * dnsConfigPtr
879- if dnsConfig . IPAddresses == nil || len (dnsConfig .IPAddresses ) == 0 {
907+ if len (dnsConfig .IPAddresses ) == 0 {
880908 return fmt .Errorf ("CustomDNSConfigs[%d].IPAddresses is nil or empty" , i )
881909 }
882910 for _ , ipPtr := range dnsConfig .IPAddresses {
@@ -907,15 +935,19 @@ func addRecordSetToPrivateDNSZone(ctx context.Context, peResp armnetwork.Private
907935 return nil
908936}
909937
910- func addDNSZoneGroup (ctx context.Context , pzResp armprivatedns.PrivateZonesClientCreateOrUpdateResponse , nodeResourceGroup , privateZoneName , endpointName string ) error {
938+ func addDNSZoneGroup (ctx context.Context , privateZone * armprivatedns.PrivateZone , nodeResourceGroup , privateZoneName , endpointName string ) error {
911939 groupName := strings .Replace (privateZoneName , "." , "-" , - 1 ) // replace . with -
940+ _ , err := config .Azure .PrivateDNSZoneGroup .Get (ctx , nodeResourceGroup , endpointName , groupName , nil )
941+ if err == nil {
942+ return nil
943+ }
912944 dnsZonegroup := armnetwork.PrivateDNSZoneGroup {
913945 Name : to .Ptr (fmt .Sprintf ("%s/default" , privateZoneName )),
914946 Properties : & armnetwork.PrivateDNSZoneGroupPropertiesFormat {
915947 PrivateDNSZoneConfigs : []* armnetwork.PrivateDNSZoneConfig {{
916948 Name : to .Ptr (groupName ),
917949 Properties : & armnetwork.PrivateDNSZonePropertiesFormat {
918- PrivateDNSZoneID : pzResp .ID ,
950+ PrivateDNSZoneID : privateZone .ID ,
919951 },
920952 }},
921953 },
0 commit comments