Skip to content

Commit

Permalink
PR 8045: azure: Create DNS entries for azure CAPI
Browse files Browse the repository at this point in the history
Creating the DNS entries for CAPI implementation of Azure.

(cherry picked from commit eecd95a)
  • Loading branch information
rna-afk authored and jhixson74 committed Mar 14, 2024
1 parent cd8a7e5 commit 3de39d7
Show file tree
Hide file tree
Showing 3 changed files with 156 additions and 0 deletions.
3 changes: 3 additions & 0 deletions pkg/asset/manifests/azure/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ func GenerateClusterAssets(installConfig *installconfig.InstallConfig, clusterID
},
},
NetworkSpec: capz.NetworkSpec{
NetworkClassSpec: capz.NetworkClassSpec{
PrivateDNSZoneName: fmt.Sprintf("api.%s", clusterID.InfraID),
},
Vnet: capz.VnetSpec{
ID: installConfig.Config.Azure.VirtualNetwork,
VnetClassSpec: capz.VnetClassSpec{
Expand Down
6 changes: 6 additions & 0 deletions pkg/infrastructure/azure/azure.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,12 @@ func (p *Provider) InfraReady(ctx context.Context, in clusterapi.InfraReadyInput
return fmt.Errorf("failed to get session: %w", err)
}

// Create DNS entries
err = createAzureDNSEntries(ctx, in)
if err != nil {
return err
}

installConfig := in.InstallConfig.Config
platform := installConfig.Platform.Azure
subscriptionID := session.Credentials.SubscriptionID
Expand Down
147 changes: 147 additions & 0 deletions pkg/infrastructure/azure/dns.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
package azure

import (
"context"
"fmt"

"github.com/Azure/azure-sdk-for-go/sdk/azidentity"
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/dns/armdns"
"github.com/openshift/installer/pkg/infrastructure/clusterapi"
"github.com/openshift/installer/pkg/types"
)

// Create DNS entries for azure.
func createAzureDNSEntries(ctx context.Context, in clusterapi.InfraReadyInput) error {
clusterName := in.InfraID
private := in.InstallConfig.Config.Publish == types.InternalPublishingStrategy
resourceGroup := in.InstallConfig.Config.Azure.ResourceGroupName
zoneName := in.InstallConfig.Config.Azure.DefaultMachinePlatform.Zones
subscriptionId := in.InstallConfig.Azure.Credentials.SubscriptionID
apiExternalName := fmt.Sprintf("api.%s", clusterName)
apiExternalNameV6 := fmt.Sprintf("v6-api.%s", clusterName)
// TODO: set value from either manifests or getting from client.
var azureTags map[string]*string
var ipv4Elb string
var ipv4Ilb string
var ipv6Elb string
var ipv6Ilb string

useIPv6 := false
for _, network := range in.InstallConfig.Config.Networking.ServiceNetwork {
if network.IP.To4() == nil {
useIPv6 = true
}
}

type recordList struct {
Name string
RecordType armdns.RecordType
RecordSet armdns.RecordSet
}
records := []recordList{}
ttl := int64(300)
if !useIPv6 {
records = append(records, recordList{
Name: "api-int",
RecordType: armdns.RecordTypeA,
RecordSet: armdns.RecordSet{
Properties: &armdns.RecordSetProperties{
ARecords: []*armdns.ARecord{
{
IPv4Address: &ipv4Ilb,
},
},
TTL: &ttl,
Metadata: azureTags,
},
},
}, recordList{
Name: "api",
RecordType: armdns.RecordTypeA,
RecordSet: armdns.RecordSet{
Properties: &armdns.RecordSetProperties{
ARecords: []*armdns.ARecord{
{
IPv4Address: &ipv4Elb,
},
},
TTL: &ttl,
Metadata: azureTags,
},
},
})
} else {
records = append(records, recordList{
Name: "api-int",
RecordType: armdns.RecordTypeAAAA,
RecordSet: armdns.RecordSet{
Properties: &armdns.RecordSetProperties{
AaaaRecords: []*armdns.AaaaRecord{
{
IPv6Address: &ipv6Ilb,
},
},
TTL: &ttl,
Metadata: azureTags,
},
},
}, recordList{
Name: "api",
RecordType: armdns.RecordTypeAAAA,
RecordSet: armdns.RecordSet{
Properties: &armdns.RecordSetProperties{
AaaaRecords: []*armdns.AaaaRecord{
{
IPv6Address: &ipv6Elb,
},
},
TTL: &ttl,
Metadata: azureTags,
},
},
})
}

if !private {
cnameRecordName := apiExternalName
if useIPv6 {
cnameRecordName = apiExternalNameV6
}
records = append(records, recordList{
Name: "api-int",
RecordType: armdns.RecordTypeCNAME,
RecordSet: armdns.RecordSet{
Properties: &armdns.RecordSetProperties{
CnameRecord: &armdns.CnameRecord{
Cname: &cnameRecordName,
},
TTL: &ttl,
Metadata: azureTags,
},
},
})
}

session, err := in.InstallConfig.Azure.Session()
if err != nil {
return err
}
tokenCreds, err := azidentity.NewClientSecretCredential(session.Credentials.TenantID, session.Credentials.ClientID, session.Credentials.ClientSecret, nil)
if err != nil {
return err
}
recordSetClient, err := armdns.NewRecordSetsClient(subscriptionId, tokenCreds, nil)
if err != nil {
return err
}

for _, zone := range zoneName {
for _, record := range records {
_, err = recordSetClient.CreateOrUpdate(ctx, resourceGroup, zone, record.Name, record.RecordType, record.RecordSet, nil)
if err != nil {
return err
}
}
}
return err
}

0 comments on commit 3de39d7

Please sign in to comment.