Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Slight ExternalDNS Config Refactor #363

Open
wants to merge 18 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 7 additions & 8 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,15 +115,10 @@ func (c *Config) ParseAndValidateZoneIDs(zonesString string) error {
if err != nil {
return fmt.Errorf("while parsing dns zone resource ID %s: %s", zoneId, err)
}

if !strings.EqualFold(parsedZone.Provider, "Microsoft.Network") {
return fmt.Errorf("invalid resource provider %s from zone %s: resource ID must be a public or private DNS Zone resource ID from provider Microsoft.Network", parsedZone.Provider, zoneId)
}

switch strings.ToLower(parsedZone.ResourceType) {
case PrivateZoneType:
// it's a private zone
if err := validateSubAndRg(parsedZone, c.PrivateZoneConfig.Subscription, c.PrivateZoneConfig.ResourceGroup); err != nil {
if err := ValidateProviderSubAndRg(parsedZone, c.PrivateZoneConfig.Subscription, c.PrivateZoneConfig.ResourceGroup); err != nil {
return err
}

Expand All @@ -136,7 +131,7 @@ func (c *Config) ParseAndValidateZoneIDs(zonesString string) error {
c.PrivateZoneConfig.ZoneIds[strings.ToLower(zoneId)] = struct{}{} // azure resource names are case insensitive
case PublicZoneType:
// it's a public zone
if err := validateSubAndRg(parsedZone, c.PublicZoneConfig.Subscription, c.PublicZoneConfig.ResourceGroup); err != nil {
if err := ValidateProviderSubAndRg(parsedZone, c.PublicZoneConfig.Subscription, c.PublicZoneConfig.ResourceGroup); err != nil {
return err
}

Expand All @@ -155,7 +150,11 @@ func (c *Config) ParseAndValidateZoneIDs(zonesString string) error {
return nil
}

func validateSubAndRg(parsedZone azure.Resource, subscription, resourceGroup string) error {
func ValidateProviderSubAndRg(parsedZone azure.Resource, subscription, resourceGroup string) error {
if !strings.EqualFold(parsedZone.Provider, "Microsoft.Network") {
return fmt.Errorf("invalid resource provider %s from zone %s: resource ID must be a public or private DNS Zone resource ID from provider Microsoft.Network", parsedZone.Provider, parsedZone.String())
}

if subscription != "" && !strings.EqualFold(parsedZone.SubscriptionID, subscription) {
return fmt.Errorf("while parsing resource IDs for %s: detected multiple subscriptions %s and %s", parsedZone.ResourceType, parsedZone.SubscriptionID, subscription)
}
Expand Down
10 changes: 2 additions & 8 deletions pkg/controller/dns/external_dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,13 +108,10 @@ func publicConfigForIngress(conf *config.Config) (*manifests.ExternalDnsConfig,
conf,
manifests.InputExternalDNSConfig{
TenantId: conf.TenantID,
Subscription: conf.PublicZoneConfig.Subscription,
ResourceGroup: conf.PublicZoneConfig.ResourceGroup,
ClientId: conf.MSIClientID,
Namespace: conf.NS,
IdentityType: manifests.IdentityTypeMSI,
ResourceTypes: []manifests.ResourceType{manifests.ResourceTypeIngress},
Provider: manifests.PublicProvider,
ResourceTypes: manifests.ResourceTypes{Ingress: true},
DnsZoneresourceIDs: util.Keys(conf.PublicZoneConfig.ZoneIds),
})

Expand All @@ -129,13 +126,10 @@ func privateConfigForIngress(conf *config.Config) (*manifests.ExternalDnsConfig,
conf,
manifests.InputExternalDNSConfig{
TenantId: conf.TenantID,
Subscription: conf.PrivateZoneConfig.Subscription,
ResourceGroup: conf.PrivateZoneConfig.ResourceGroup,
ClientId: conf.MSIClientID,
Namespace: conf.NS,
IdentityType: manifests.IdentityTypeMSI,
ResourceTypes: []manifests.ResourceType{manifests.ResourceTypeIngress},
Provider: manifests.PrivateProvider,
ResourceTypes: manifests.ResourceTypes{Ingress: true},
DnsZoneresourceIDs: util.Keys(conf.PrivateZoneConfig.ZoneIds),
},
)
Expand Down
22 changes: 11 additions & 11 deletions pkg/controller/dns/external_dns_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,28 +151,28 @@ func TestPrivateConfig(t *testing.T) {
}

func TestInstances(t *testing.T) {
noZonesPublic, err := manifests.NewExternalDNSConfig(&noZones, manifests.InputExternalDNSConfig{TenantId: noZones.TenantID, Subscription: noZones.PublicZoneConfig.Subscription, ResourceGroup: noZones.PublicZoneConfig.ResourceGroup, ClientId: noZones.MSIClientID, Namespace: noZones.NS, IdentityType: manifests.IdentityTypeMSI, ResourceTypes: []manifests.ResourceType{manifests.ResourceTypeIngress}, Provider: manifests.PublicProvider, DnsZoneresourceIDs: util.Keys(noZones.PublicZoneConfig.ZoneIds)})
noZonesPublic, err := manifests.NewExternalDNSConfig(&noZones, manifests.InputExternalDNSConfig{TenantId: noZones.TenantID, ClientId: noZones.MSIClientID, Namespace: noZones.NS, IdentityType: manifests.IdentityTypeMSI, ResourceTypes: manifests.ResourceTypes{Ingress: true}, DnsZoneresourceIDs: util.Keys(noZones.PublicZoneConfig.ZoneIds)})
require.NoError(t, err)

noZonesPrivate, err := manifests.NewExternalDNSConfig(&noZones, manifests.InputExternalDNSConfig{TenantId: noZones.TenantID, Subscription: noZones.PublicZoneConfig.Subscription, ResourceGroup: noZones.PublicZoneConfig.ResourceGroup, ClientId: noZones.MSIClientID, Namespace: noZones.NS, IdentityType: manifests.IdentityTypeMSI, ResourceTypes: []manifests.ResourceType{manifests.ResourceTypeIngress}, Provider: manifests.PrivateProvider, DnsZoneresourceIDs: util.Keys(noZones.PrivateZoneConfig.ZoneIds)})
noZonesPrivate, err := manifests.NewExternalDNSConfig(&noZones, manifests.InputExternalDNSConfig{TenantId: noZones.TenantID, ClientId: noZones.MSIClientID, Namespace: noZones.NS, IdentityType: manifests.IdentityTypeMSI, ResourceTypes: manifests.ResourceTypes{Ingress: true}, DnsZoneresourceIDs: util.Keys(noZones.PrivateZoneConfig.ZoneIds)})
require.NoError(t, err)

onlyPrivZonesPublic, err := manifests.NewExternalDNSConfig(&onlyPrivZones, manifests.InputExternalDNSConfig{TenantId: onlyPrivZones.TenantID, Subscription: onlyPrivZones.PublicZoneConfig.Subscription, ResourceGroup: onlyPrivZones.PublicZoneConfig.ResourceGroup, ClientId: onlyPrivZones.MSIClientID, Namespace: onlyPrivZones.NS, IdentityType: manifests.IdentityTypeMSI, ResourceTypes: []manifests.ResourceType{manifests.ResourceTypeIngress}, Provider: manifests.PublicProvider, DnsZoneresourceIDs: util.Keys(onlyPrivZones.PublicZoneConfig.ZoneIds)})
onlyPrivZonesPublic, err := manifests.NewExternalDNSConfig(&onlyPrivZones, manifests.InputExternalDNSConfig{TenantId: onlyPrivZones.TenantID, ClientId: onlyPrivZones.MSIClientID, Namespace: onlyPrivZones.NS, IdentityType: manifests.IdentityTypeMSI, ResourceTypes: manifests.ResourceTypes{Ingress: true}, DnsZoneresourceIDs: util.Keys(onlyPrivZones.PublicZoneConfig.ZoneIds)})
require.NoError(t, err)

onlyPrivZonesPrivate, err := manifests.NewExternalDNSConfig(&onlyPrivZones, manifests.InputExternalDNSConfig{TenantId: onlyPrivZones.TenantID, Subscription: onlyPrivZones.PrivateZoneConfig.Subscription, ResourceGroup: onlyPrivZones.PrivateZoneConfig.ResourceGroup, ClientId: onlyPrivZones.MSIClientID, Namespace: onlyPrivZones.NS, IdentityType: manifests.IdentityTypeMSI, ResourceTypes: []manifests.ResourceType{manifests.ResourceTypeIngress}, Provider: manifests.PrivateProvider, DnsZoneresourceIDs: util.Keys(onlyPrivZones.PrivateZoneConfig.ZoneIds)})
onlyPrivZonesPrivate, err := manifests.NewExternalDNSConfig(&onlyPrivZones, manifests.InputExternalDNSConfig{TenantId: onlyPrivZones.TenantID, ClientId: onlyPrivZones.MSIClientID, Namespace: onlyPrivZones.NS, IdentityType: manifests.IdentityTypeMSI, ResourceTypes: manifests.ResourceTypes{Ingress: true}, DnsZoneresourceIDs: util.Keys(onlyPrivZones.PrivateZoneConfig.ZoneIds)})
require.NoError(t, err)

publicDeployPublic, err := manifests.NewExternalDNSConfig(&onlyPubZones, manifests.InputExternalDNSConfig{TenantId: onlyPubZones.TenantID, Subscription: onlyPubZones.PublicZoneConfig.Subscription, ResourceGroup: onlyPubZones.PublicZoneConfig.ResourceGroup, ClientId: onlyPubZones.MSIClientID, Namespace: onlyPubZones.NS, IdentityType: manifests.IdentityTypeMSI, ResourceTypes: []manifests.ResourceType{manifests.ResourceTypeIngress}, Provider: manifests.PublicProvider, DnsZoneresourceIDs: util.Keys(onlyPubZones.PublicZoneConfig.ZoneIds)})
publicDeployPublic, err := manifests.NewExternalDNSConfig(&onlyPubZones, manifests.InputExternalDNSConfig{TenantId: onlyPubZones.TenantID, ClientId: onlyPubZones.MSIClientID, Namespace: onlyPubZones.NS, IdentityType: manifests.IdentityTypeMSI, ResourceTypes: manifests.ResourceTypes{Ingress: true}, DnsZoneresourceIDs: util.Keys(onlyPubZones.PublicZoneConfig.ZoneIds)})
require.NoError(t, err)

publicDeployPrivate, err := manifests.NewExternalDNSConfig(&onlyPubZones, manifests.InputExternalDNSConfig{TenantId: onlyPubZones.TenantID, Subscription: onlyPubZones.PrivateZoneConfig.Subscription, ResourceGroup: onlyPubZones.PrivateZoneConfig.ResourceGroup, ClientId: onlyPubZones.MSIClientID, Namespace: onlyPubZones.NS, IdentityType: manifests.IdentityTypeMSI, ResourceTypes: []manifests.ResourceType{manifests.ResourceTypeIngress}, Provider: manifests.PrivateProvider, DnsZoneresourceIDs: util.Keys(onlyPubZones.PrivateZoneConfig.ZoneIds)})
publicDeployPrivate, err := manifests.NewExternalDNSConfig(&onlyPubZones, manifests.InputExternalDNSConfig{TenantId: onlyPubZones.TenantID, ClientId: onlyPubZones.MSIClientID, Namespace: onlyPubZones.NS, IdentityType: manifests.IdentityTypeMSI, ResourceTypes: manifests.ResourceTypes{Ingress: true}, DnsZoneresourceIDs: util.Keys(onlyPubZones.PrivateZoneConfig.ZoneIds)})
require.NoError(t, err)

allDeployPublic, err := manifests.NewExternalDNSConfig(&allZones, manifests.InputExternalDNSConfig{TenantId: allZones.TenantID, Subscription: allZones.PublicZoneConfig.Subscription, ResourceGroup: allZones.PublicZoneConfig.ResourceGroup, ClientId: allZones.MSIClientID, Namespace: allZones.NS, IdentityType: manifests.IdentityTypeMSI, ResourceTypes: []manifests.ResourceType{manifests.ResourceTypeIngress}, Provider: manifests.PublicProvider, DnsZoneresourceIDs: util.Keys(allZones.PublicZoneConfig.ZoneIds)})
allDeployPublic, err := manifests.NewExternalDNSConfig(&allZones, manifests.InputExternalDNSConfig{TenantId: allZones.TenantID, ClientId: allZones.MSIClientID, Namespace: allZones.NS, IdentityType: manifests.IdentityTypeMSI, ResourceTypes: manifests.ResourceTypes{Ingress: true}, DnsZoneresourceIDs: util.Keys(allZones.PublicZoneConfig.ZoneIds)})
require.NoError(t, err)

allDeployPrivate, err := manifests.NewExternalDNSConfig(&allZones, manifests.InputExternalDNSConfig{TenantId: allZones.TenantID, Subscription: allZones.PublicZoneConfig.Subscription, ResourceGroup: allZones.PublicZoneConfig.ResourceGroup, ClientId: allZones.MSIClientID, Namespace: allZones.NS, IdentityType: manifests.IdentityTypeMSI, ResourceTypes: []manifests.ResourceType{manifests.ResourceTypeIngress}, Provider: manifests.PrivateProvider, DnsZoneresourceIDs: util.Keys(allZones.PrivateZoneConfig.ZoneIds)})
allDeployPrivate, err := manifests.NewExternalDNSConfig(&allZones, manifests.InputExternalDNSConfig{TenantId: allZones.TenantID, ClientId: allZones.MSIClientID, Namespace: allZones.NS, IdentityType: manifests.IdentityTypeMSI, ResourceTypes: manifests.ResourceTypes{Ingress: true}, DnsZoneresourceIDs: util.Keys(allZones.PrivateZoneConfig.ZoneIds)})
require.NoError(t, err)

tests := []struct {
Expand Down Expand Up @@ -445,9 +445,9 @@ func TestCleanObjs(t *testing.T) {
}

func TestActionFromConfig(t *testing.T) {
emptyDns, _ := manifests.NewExternalDNSConfig(&config.Config{}, manifests.InputExternalDNSConfig{TenantId: "tenant", Subscription: "sub", ResourceGroup: "rg", ClientId: "client", Namespace: "ns", IdentityType: manifests.IdentityTypeMSI, ResourceTypes: []manifests.ResourceType{manifests.ResourceTypeIngress}, Provider: manifests.PublicProvider, DnsZoneresourceIDs: []string{}})
oneDns, _ := manifests.NewExternalDNSConfig(&config.Config{}, manifests.InputExternalDNSConfig{TenantId: "tenant", Subscription: "sub", ResourceGroup: "rg", ClientId: "client", Namespace: "ns", IdentityType: manifests.IdentityTypeMSI, ResourceTypes: []manifests.ResourceType{manifests.ResourceTypeIngress}, Provider: manifests.PublicProvider, DnsZoneresourceIDs: []string{"one"}})
multipleDns, _ := manifests.NewExternalDNSConfig(&config.Config{}, manifests.InputExternalDNSConfig{TenantId: "tenant", Subscription: "sub", ResourceGroup: "rg", ClientId: "client", Namespace: "ns", IdentityType: manifests.IdentityTypeMSI, ResourceTypes: []manifests.ResourceType{manifests.ResourceTypeIngress}, Provider: manifests.PublicProvider, DnsZoneresourceIDs: []string{"one", "two"}})
emptyDns, _ := manifests.NewExternalDNSConfig(&config.Config{}, manifests.InputExternalDNSConfig{TenantId: "tenant", ClientId: "client", Namespace: "ns", IdentityType: manifests.IdentityTypeMSI, ResourceTypes: manifests.ResourceTypes{Ingress: true}, DnsZoneresourceIDs: []string{}})
oneDns, _ := manifests.NewExternalDNSConfig(&config.Config{}, manifests.InputExternalDNSConfig{TenantId: "tenant", ClientId: "client", Namespace: "ns", IdentityType: manifests.IdentityTypeMSI, ResourceTypes: manifests.ResourceTypes{Ingress: true}, DnsZoneresourceIDs: []string{"one"}})
multipleDns, _ := manifests.NewExternalDNSConfig(&config.Config{}, manifests.InputExternalDNSConfig{TenantId: "tenant", ClientId: "client", Namespace: "ns", IdentityType: manifests.IdentityTypeMSI, ResourceTypes: manifests.ResourceTypes{Ingress: true}, DnsZoneresourceIDs: []string{"one", "two"}})

tests := []struct {
name string
Expand Down
Loading