From 6d7d47d6f58b3f48e254d3c4f81cbe212b122ae2 Mon Sep 17 00:00:00 2001 From: Simon Emms Date: Wed, 27 Nov 2024 16:58:50 +0000 Subject: [PATCH] fix(azure): make the dns resource group optional --- internal/azure/azure.go | 44 +++++++++++++++++++++++++++++++ internal/controller/domain.go | 10 ++++++- pkg/providerConfigs/detokenize.go | 4 +-- 3 files changed, 55 insertions(+), 3 deletions(-) diff --git a/internal/azure/azure.go b/internal/azure/azure.go index 29ee8a57..398277a6 100644 --- a/internal/azure/azure.go +++ b/internal/azure/azure.go @@ -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 { diff --git a/internal/controller/domain.go b/internal/controller/domain.go index adc221bd..2360c34b 100644 --- a/internal/controller/domain.go +++ b/internal/controller/domain.go @@ -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) } diff --git a/pkg/providerConfigs/detokenize.go b/pkg/providerConfigs/detokenize.go index 24585f19..86213192 100644 --- a/pkg/providerConfigs/detokenize.go +++ b/pkg/providerConfigs/detokenize.go @@ -94,8 +94,8 @@ func detokenizeGitops(tokens *GitopsDirectoryValues, gitProtocol string, useClou } newContents = strings.ReplaceAll(newContents, "", tokens.AzureStorageResourceGroup) newContents = strings.ReplaceAll(newContents, "", tokens.AzureStorageContainerName) - newContents = strings.ReplaceAll(newContents, "", tokens.AzureDNSZoneResourceGroup) - newContents = strings.ReplaceAll(newContents, "", azureDNSZoneName) // This is only set if using Azure for DNS + newContents = strings.ReplaceAll(newContents, "", tokens.AzureDNSZoneResourceGroup) // This may or may not be set if using Azure for DNS + newContents = strings.ReplaceAll(newContents, "", azureDNSZoneName) // This is only set if using Azure for DNS // google newContents = strings.ReplaceAll(newContents, "", tokens.GoogleProject)