|
| 1 | +package azure |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + "github.com/Azure/azure-sdk-for-go/sdk/azidentity" |
| 8 | + "github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/dns/armdns" |
| 9 | + "github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/privatedns/armprivatedns" |
| 10 | + "k8s.io/utils/ptr" |
| 11 | + capz "sigs.k8s.io/cluster-api-provider-azure/api/v1beta1" |
| 12 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 13 | + |
| 14 | + "github.com/openshift/installer/pkg/asset/manifests/capiutils" |
| 15 | + "github.com/openshift/installer/pkg/infrastructure/clusterapi" |
| 16 | + "github.com/openshift/installer/pkg/types" |
| 17 | +) |
| 18 | + |
| 19 | +type recordListType string |
| 20 | + |
| 21 | +const ( |
| 22 | + cname recordListType = "Cname" |
| 23 | + arecord recordListType = "ARecord" |
| 24 | + aaaarecord recordListType = "AaaaRecord" |
| 25 | +) |
| 26 | + |
| 27 | +type recordList struct { |
| 28 | + Name string |
| 29 | + RecordType armdns.RecordType |
| 30 | + RecordSet armdns.RecordSet |
| 31 | +} |
| 32 | + |
| 33 | +type recordPrivateList struct { |
| 34 | + Name string |
| 35 | + RecordType armprivatedns.RecordType |
| 36 | + RecordSet armprivatedns.RecordSet |
| 37 | +} |
| 38 | + |
| 39 | +// Create DNS entries for azure. |
| 40 | +func createDNSEntries(ctx context.Context, in clusterapi.InfraReadyInput) error { |
| 41 | + private := in.InstallConfig.Config.Publish == types.InternalPublishingStrategy |
| 42 | + baseDomainResourceGroup := in.InstallConfig.Config.Azure.BaseDomainResourceGroupName |
| 43 | + zone := in.InstallConfig.Config.BaseDomain |
| 44 | + privatezone := in.InstallConfig.Config.ClusterDomain() |
| 45 | + apiExternalName := fmt.Sprintf("api.%s", in.InstallConfig.Config.ObjectMeta.Name) |
| 46 | + |
| 47 | + resourceGroup := fmt.Sprintf("%s-rg", in.InfraID) |
| 48 | + if in.InstallConfig.Config.Azure.ResourceGroupName != "" { |
| 49 | + resourceGroup = in.InstallConfig.Config.Azure.ResourceGroupName |
| 50 | + } |
| 51 | + azureTags := make(map[string]*string) |
| 52 | + for k, v := range in.InstallConfig.Config.Azure.UserTags { |
| 53 | + azureTags[k] = ptr.To(v) |
| 54 | + } |
| 55 | + azureCluster := &capz.AzureCluster{} |
| 56 | + key := client.ObjectKey{ |
| 57 | + Name: in.InfraID, |
| 58 | + Namespace: capiutils.Namespace, |
| 59 | + } |
| 60 | + if err := in.Client.Get(ctx, key, azureCluster); err != nil && azureCluster != nil { |
| 61 | + return fmt.Errorf("failed to get Azure cluster: %w", err) |
| 62 | + } |
| 63 | + |
| 64 | + if len(azureCluster.Spec.NetworkSpec.APIServerLB.FrontendIPs) == 0 { |
| 65 | + return fmt.Errorf("failed to get Azure cluster LB frontend IPs") |
| 66 | + } |
| 67 | + ipIlb := azureCluster.Spec.NetworkSpec.APIServerLB.FrontendIPs[0].PrivateIPAddress |
| 68 | + // useIPv6 := false |
| 69 | + // for _, network := range in.InstallConfig.Config.Networking.ServiceNetwork { |
| 70 | + // if network.IP.To4() == nil { |
| 71 | + // useIPv6 = true |
| 72 | + // } |
| 73 | + // } |
| 74 | + |
| 75 | + privateRecords := []recordPrivateList{} |
| 76 | + ttl := int64(300) |
| 77 | + recordType := arecord |
| 78 | + // if useIPv6 { |
| 79 | + // recordType = aaaarecord |
| 80 | + // } |
| 81 | + privateRecords = append(privateRecords, createPrivateRecordSet("api-int", azureTags, ttl, recordType, ipIlb, "")) |
| 82 | + privateRecords = append(privateRecords, createPrivateRecordSet("api", azureTags, ttl, recordType, ipIlb, "")) |
| 83 | + |
| 84 | + session, err := in.InstallConfig.Azure.Session() |
| 85 | + if err != nil { |
| 86 | + return fmt.Errorf("failed to create session: %w", err) |
| 87 | + } |
| 88 | + subscriptionID := session.Credentials.SubscriptionID |
| 89 | + tokenCreds, err := azidentity.NewClientSecretCredential(session.Credentials.TenantID, session.Credentials.ClientID, session.Credentials.ClientSecret, nil) |
| 90 | + if err != nil { |
| 91 | + return fmt.Errorf("failed to create identity: %w", err) |
| 92 | + } |
| 93 | + recordSetClient, err := armdns.NewRecordSetsClient(subscriptionID, tokenCreds, nil) |
| 94 | + if err != nil { |
| 95 | + return fmt.Errorf("failed to create public record client: %w", err) |
| 96 | + } |
| 97 | + privateRecordSetClient, err := armprivatedns.NewRecordSetsClient(subscriptionID, tokenCreds, nil) |
| 98 | + if err != nil { |
| 99 | + return fmt.Errorf("failed to create private record client: %w", err) |
| 100 | + } |
| 101 | + |
| 102 | + // Create the records for api and api-int in the private zone and api.<clustername> for public zone. |
| 103 | + // CAPI currently creates a record called "apiserver" instead of "api" so creating "api" for the installer in the private zone. |
| 104 | + if !private { |
| 105 | + cnameRecordName := apiExternalName |
| 106 | + // apiExternalNameV6 := fmt.Sprintf("v6-api.%s", infraID) |
| 107 | + // if useIPv6 { |
| 108 | + // cnameRecordName = apiExternalNameV6 |
| 109 | + // } |
| 110 | + // TODO: Populate with public LB FQDN. Placeholder text as value. |
| 111 | + publicRecords := createRecordSet(cnameRecordName, azureTags, ttl, cname, "", in.InstallConfig.Config.ClusterDomain()) |
| 112 | + _, err = recordSetClient.CreateOrUpdate(ctx, baseDomainResourceGroup, zone, publicRecords.Name, publicRecords.RecordType, publicRecords.RecordSet, nil) |
| 113 | + if err != nil { |
| 114 | + return fmt.Errorf("failed to create public record set: %w", err) |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + for _, record := range privateRecords { |
| 119 | + _, err = privateRecordSetClient.CreateOrUpdate(ctx, resourceGroup, privatezone, record.RecordType, record.Name, record.RecordSet, nil) |
| 120 | + if err != nil { |
| 121 | + return fmt.Errorf("failed to create private record set: %w", err) |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + return nil |
| 126 | +} |
| 127 | + |
| 128 | +func createPrivateRecordSet(lbType string, azureTags map[string]*string, ttl int64, rType recordListType, ipAddress string, recordName string) (record recordPrivateList) { |
| 129 | + record = recordPrivateList{ |
| 130 | + Name: lbType, |
| 131 | + RecordSet: armprivatedns.RecordSet{ |
| 132 | + Properties: &armprivatedns.RecordSetProperties{ |
| 133 | + TTL: &ttl, |
| 134 | + Metadata: azureTags, |
| 135 | + }, |
| 136 | + }, |
| 137 | + } |
| 138 | + |
| 139 | + switch rType { |
| 140 | + case cname: |
| 141 | + record.RecordType = armprivatedns.RecordTypeCNAME |
| 142 | + record.RecordSet.Properties.CnameRecord = &armprivatedns.CnameRecord{ |
| 143 | + Cname: &recordName, |
| 144 | + } |
| 145 | + case arecord: |
| 146 | + record.RecordType = armprivatedns.RecordTypeA |
| 147 | + record.RecordSet.Properties.ARecords = []*armprivatedns.ARecord{ |
| 148 | + { |
| 149 | + IPv4Address: &ipAddress, |
| 150 | + }, |
| 151 | + } |
| 152 | + case aaaarecord: |
| 153 | + record.RecordType = armprivatedns.RecordTypeAAAA |
| 154 | + record.RecordSet.Properties.AaaaRecords = []*armprivatedns.AaaaRecord{ |
| 155 | + { |
| 156 | + IPv6Address: &ipAddress, |
| 157 | + }, |
| 158 | + } |
| 159 | + } |
| 160 | + return record |
| 161 | +} |
| 162 | + |
| 163 | +func createRecordSet(lbType string, azureTags map[string]*string, ttl int64, rType recordListType, ipAddress string, recordName string) (record recordList) { |
| 164 | + record = recordList{ |
| 165 | + Name: lbType, |
| 166 | + RecordSet: armdns.RecordSet{ |
| 167 | + Properties: &armdns.RecordSetProperties{ |
| 168 | + TTL: &ttl, |
| 169 | + Metadata: azureTags, |
| 170 | + }, |
| 171 | + }, |
| 172 | + } |
| 173 | + |
| 174 | + switch rType { |
| 175 | + case cname: |
| 176 | + record.RecordType = armdns.RecordTypeCNAME |
| 177 | + record.RecordSet.Properties.CnameRecord = &armdns.CnameRecord{ |
| 178 | + Cname: &recordName, |
| 179 | + } |
| 180 | + case arecord: |
| 181 | + record.RecordType = armdns.RecordTypeA |
| 182 | + record.RecordSet.Properties.ARecords = []*armdns.ARecord{ |
| 183 | + { |
| 184 | + IPv4Address: &ipAddress, |
| 185 | + }, |
| 186 | + } |
| 187 | + case aaaarecord: |
| 188 | + record.RecordType = armdns.RecordTypeAAAA |
| 189 | + record.RecordSet.Properties.AaaaRecords = []*armdns.AaaaRecord{ |
| 190 | + { |
| 191 | + IPv6Address: &ipAddress, |
| 192 | + }, |
| 193 | + } |
| 194 | + } |
| 195 | + return record |
| 196 | +} |
0 commit comments