@@ -10,6 +10,7 @@ import (
1010 "github.com/Azure/azure-sdk-for-go/sdk/azcore/to"
1111 "github.com/Azure/azure-sdk-for-go/sdk/azidentity"
1212 armcontainerservice "github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/containerservice/armcontainerservice/v4"
13+ armnetwork "github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/network/armnetwork/v5"
1314)
1415
1516var (
@@ -47,6 +48,40 @@ func (c *CreateNPMCluster) Stop() error {
4748
4849func (c * CreateNPMCluster ) Run () error {
4950 // Start with default cluster template
51+ // Deploy cluster
52+ cred , err := azidentity .NewAzureCLICredential (nil )
53+ if err != nil {
54+ return fmt .Errorf ("failed to obtain a credential: %w" , err )
55+ }
56+ ctx , cancel := context .WithTimeout (context .Background (), clusterTimeout )
57+ defer cancel ()
58+
59+ clientFactory , err := armcontainerservice .NewClientFactory (c .SubscriptionID , cred , nil )
60+ if err != nil {
61+ return fmt .Errorf ("failed to create az client: %w" , err )
62+ }
63+
64+ // create ip for public ip
65+ publicip := armnetwork.PublicIPAddress {
66+ Location : to .Ptr (c .Location ),
67+ Name : to .Ptr (c .ClusterName + "-pip" ),
68+ ID : to .Ptr (fmt .Sprintf ("/subscriptions/%s/resourceGroups/%s/providers/Microsoft.Network/publicIPAddresses/%s-pip" , c .SubscriptionID , c .ResourceGroupName , c .ClusterName )),
69+ Properties : & armnetwork.PublicIPAddressPropertiesFormat {
70+ IPTags : []* armnetwork.IPTag {
71+ {
72+ IPTagType : to .Ptr ("FirstPartyUsage" ),
73+ Tag : to .Ptr ("/NonProd" ),
74+ },
75+ },
76+ },
77+ }
78+
79+ err = c .createPublicIP (ctx , publicip )
80+ if err != nil {
81+ return fmt .Errorf ("failed to create public ip: %w" , err )
82+ }
83+
84+ // get cluster template to mutate
5085 npmCluster := GetStarterClusterTemplate (c .Location )
5186
5287 npmCluster .Properties .NetworkProfile .NetworkPolicy = to .Ptr (armcontainerservice .NetworkPolicyAzure )
@@ -99,17 +134,10 @@ func (c *CreateNPMCluster) Run() error {
99134 NodeOSUpgradeChannel : to .Ptr (armcontainerservice .NodeOSUpgradeChannelNodeImage ),
100135 }
101136
102- // Deploy cluster
103- cred , err := azidentity .NewAzureCLICredential (nil )
104- if err != nil {
105- return fmt .Errorf ("failed to obtain a credential: %w" , err )
106- }
107- ctx , cancel := context .WithTimeout (context .Background (), clusterTimeout )
108- defer cancel ()
109-
110- clientFactory , err := armcontainerservice .NewClientFactory (c .SubscriptionID , cred , nil )
111- if err != nil {
112- return fmt .Errorf ("failed to create az client: %w" , err )
137+ npmCluster .Properties .NetworkProfile .LoadBalancerProfile .OutboundIPs .PublicIPs = []* armcontainerservice.ResourceReference {
138+ {
139+ ID : to .Ptr (* publicip .ID ),
140+ },
113141 }
114142
115143 log .Printf ("when the cluster is ready, use the below command to access and debug" )
@@ -150,3 +178,27 @@ func (c *CreateNPMCluster) Run() error {
150178 }
151179 }
152180}
181+
182+ func (c * CreateNPMCluster ) createPublicIP (ctx context.Context , ip armnetwork.PublicIPAddress ) error {
183+ cred , err := azidentity .NewAzureCLICredential (nil )
184+ if err != nil {
185+ return fmt .Errorf ("failed to obtain a credential: %w" , err )
186+ }
187+ clientFactory , err := armnetwork .NewClientFactory (c .SubscriptionID , cred , nil )
188+ if err != nil {
189+ return fmt .Errorf ("failed to create client: %w" , err )
190+ }
191+
192+ log .Printf ("creating public ip \" %s\" in resource group \" %s\" ..." , * ip .Name , c .ResourceGroupName )
193+
194+ poller , err := clientFactory .NewPublicIPAddressesClient ().BeginCreateOrUpdate (ctx , c .ResourceGroupName , * ip .Name , ip , nil )
195+ if err != nil {
196+ return fmt .Errorf ("failed to finish the request for create public ip: %w" , err )
197+ }
198+
199+ _ , err = poller .PollUntilDone (ctx , nil )
200+ if err != nil {
201+ return fmt .Errorf ("failed to pull the result for create public ip: %w" , err )
202+ }
203+ return nil
204+ }
0 commit comments