|
| 1 | +package azure |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "log" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/Azure/azure-sdk-for-go/sdk/azcore/runtime" |
| 10 | + "github.com/Azure/azure-sdk-for-go/sdk/azcore/to" |
| 11 | + "github.com/Azure/azure-sdk-for-go/sdk/azidentity" |
| 12 | + "github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/network/armnetwork" |
| 13 | +) |
| 14 | + |
| 15 | +type CreatePublicIP struct { |
| 16 | + SubscriptionID string |
| 17 | + ResourceGroupName string |
| 18 | + Location string |
| 19 | + ClusterName string |
| 20 | + IPVersion string |
| 21 | + IPPrefix string |
| 22 | +} |
| 23 | + |
| 24 | +func (c *CreatePublicIP) Prevalidate() error { |
| 25 | + return nil |
| 26 | +} |
| 27 | + |
| 28 | +func (c *CreatePublicIP) Stop() error { |
| 29 | + return nil |
| 30 | +} |
| 31 | + |
| 32 | +func (c *CreatePublicIP) Run() error { |
| 33 | + cred, err := azidentity.NewDefaultAzureCredential(nil) |
| 34 | + if err != nil { |
| 35 | + log.Fatal(err) |
| 36 | + } |
| 37 | + |
| 38 | + ctx, cancel := context.WithTimeout(context.Background(), clusterTimeout) |
| 39 | + defer cancel() |
| 40 | + |
| 41 | + publicIPClient, err := armnetwork.NewPublicIPAddressesClient(c.SubscriptionID, cred, nil) |
| 42 | + if err != nil { |
| 43 | + return fmt.Errorf("%w: failed to create public IP client", err) |
| 44 | + } |
| 45 | + |
| 46 | + publicIPParams := armnetwork.PublicIPAddress{ |
| 47 | + Location: to.Ptr(c.Location), |
| 48 | + SKU: &armnetwork.PublicIPAddressSKU{ |
| 49 | + Name: to.Ptr(armnetwork.PublicIPAddressSKUNameStandard), |
| 50 | + Tier: to.Ptr(armnetwork.PublicIPAddressSKUTierRegional), |
| 51 | + }, |
| 52 | + Properties: &armnetwork.PublicIPAddressPropertiesFormat{ |
| 53 | + PublicIPAllocationMethod: to.Ptr(armnetwork.IPAllocationMethodStatic), |
| 54 | + PublicIPAddressVersion: to.Ptr(armnetwork.IPVersion(c.IPVersion)), |
| 55 | + IPTags: []*armnetwork.IPTag{ |
| 56 | + { |
| 57 | + IPTagType: to.Ptr("FirstPartyUsage"), |
| 58 | + Tag: to.Ptr("/NonProd"), |
| 59 | + }, |
| 60 | + }, |
| 61 | + }, |
| 62 | + } |
| 63 | + |
| 64 | + var version string |
| 65 | + switch c.IPVersion { |
| 66 | + case string(armnetwork.IPVersionIPv4): |
| 67 | + version = "v4" |
| 68 | + case string(armnetwork.IPVersionIPv6): |
| 69 | + version = "v6" |
| 70 | + default: |
| 71 | + return fmt.Errorf("%w: invalid IP version: %s", err, c.IPVersion) |
| 72 | + } |
| 73 | + |
| 74 | + ipName := fmt.Sprintf("%s-%s-%s", c.IPPrefix, c.ClusterName, version) |
| 75 | + |
| 76 | + poller, err := publicIPClient.BeginCreateOrUpdate(ctx, c.ResourceGroupName, ipName, publicIPParams, nil) |
| 77 | + if err != nil { |
| 78 | + return fmt.Errorf("%w: failed to create public IP address", err) |
| 79 | + } |
| 80 | + |
| 81 | + notifychan := make(chan struct{}) |
| 82 | + go func() { |
| 83 | + _, err = poller.PollUntilDone(ctx, &runtime.PollUntilDoneOptions{ |
| 84 | + Frequency: 5 * time.Second, |
| 85 | + }) |
| 86 | + if err != nil { |
| 87 | + log.Printf("failed to create Public IP - %s : %v\n", ipName, err) |
| 88 | + } else { |
| 89 | + log.Printf("Public IP %s created\n", ipName) |
| 90 | + } |
| 91 | + close(notifychan) |
| 92 | + }() |
| 93 | + |
| 94 | + ticker := time.NewTicker(30 * time.Second) |
| 95 | + defer ticker.Stop() |
| 96 | + for { |
| 97 | + select { |
| 98 | + case <-ctx.Done(): |
| 99 | + return fmt.Errorf("failed to create Public IP: %w", ctx.Err()) |
| 100 | + case <-ticker.C: |
| 101 | + log.Printf("waiting for Public IP %s to be ready...\n", ipName) |
| 102 | + case <-notifychan: |
| 103 | + if err != nil { |
| 104 | + return fmt.Errorf("received notification, failed to create public IP address: %w", err) |
| 105 | + } |
| 106 | + return nil |
| 107 | + } |
| 108 | + } |
| 109 | +} |
0 commit comments