Skip to content

Commit

Permalink
fix(azure): make the dns resource group optional (#495)
Browse files Browse the repository at this point in the history
  • Loading branch information
mrsimonemms authored Dec 2, 2024
1 parent 8f17e0f commit ff284a9
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 3 deletions.
44 changes: 44 additions & 0 deletions internal/azure/azure.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,50 @@ func (c *Client) GetStorageAccessKeys(ctx context.Context, resourceGroup, storag
}, nil
}

func (c *Client) ListResourceGroups(ctx context.Context) ([]*armresources.ResourceGroup, error) {
client, err := c.newResourceClientFactory()
if err != nil {
return nil, err
}

pager := client.NewResourceGroupsClient().NewListPager(nil)

var groups []*armresources.ResourceGroup

for pager.More() {
page, err := pager.NextPage(ctx)
if err != nil {
return nil, fmt.Errorf("failed to list resource groups: %w", err)
}

groups = append(groups, page.Value...)
}

return groups, nil
}

func (c *Client) TestHostedZoneLivenessWildcard(ctx context.Context, domainName string) (bool, *string, error) {
groups, err := c.ListResourceGroups(ctx)
if err != nil {
return false, nil, err
}

// Search through resource groups and return true for first match
for _, resourceGroup := range groups {
name := resourceGroup.Name
hasDomain, err := c.TestHostedZoneLiveness(ctx, domainName, *name)
if err != nil {
return false, nil, err
}

if hasDomain {
return true, name, nil
}
}

return false, nil, nil
}

func (c *Client) TestHostedZoneLiveness(ctx context.Context, domainName, resourceGroup string) (bool, error) {
client, err := c.newDNSClientFactory()
if err != nil {
Expand Down
10 changes: 9 additions & 1 deletion internal/controller/domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,15 @@ func (clctrl *ClusterController) DomainLivenessTest() error {
return fmt.Errorf("domain liveness check failed for AWS: %w", err)
}
case "azure":
domainLiveness, err := clctrl.AzureClient.TestHostedZoneLiveness(context.Background(), clctrl.DomainName, clctrl.AzureDNSZoneResourceGroup)
var domainLiveness bool
ctx := context.Background()

if clctrl.AzureDNSZoneResourceGroup == "" {
domainLiveness, _, err = clctrl.AzureClient.TestHostedZoneLivenessWildcard(ctx, clctrl.DomainName)
} else {
domainLiveness, err = clctrl.AzureClient.TestHostedZoneLiveness(ctx, clctrl.DomainName, clctrl.AzureDNSZoneResourceGroup)
}

if err != nil {
return fmt.Errorf("domain liveness command failed for Azure: %w", err)
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/providerConfigs/detokenize.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,8 @@ func detokenizeGitops(tokens *GitopsDirectoryValues, gitProtocol string, useClou
}
newContents = strings.ReplaceAll(newContents, "<KUBEFIRST_STATE_STORE_RESOURCE_GROUP>", tokens.AzureStorageResourceGroup)
newContents = strings.ReplaceAll(newContents, "<KUBEFIRST_STATE_STORE_CONTAINER_NAME>", tokens.AzureStorageContainerName)
newContents = strings.ReplaceAll(newContents, "<AZURE_DNS_ZONE_RESOURCE_GROUP>", tokens.AzureDNSZoneResourceGroup)
newContents = strings.ReplaceAll(newContents, "<AZURE_DNS_ZONE_NAME>", azureDNSZoneName) // This is only set if using Azure for DNS
newContents = strings.ReplaceAll(newContents, "<AZURE_DNS_ZONE_RESOURCE_GROUP>", tokens.AzureDNSZoneResourceGroup) // This may or may not be set if using Azure for DNS
newContents = strings.ReplaceAll(newContents, "<AZURE_DNS_ZONE_NAME>", azureDNSZoneName) // This is only set if using Azure for DNS

// google
newContents = strings.ReplaceAll(newContents, "<GOOGLE_PROJECT>", tokens.GoogleProject)
Expand Down

0 comments on commit ff284a9

Please sign in to comment.