Skip to content

Commit

Permalink
Ensure zone/root domain exists in DNS provider at apply time
Browse files Browse the repository at this point in the history
  • Loading branch information
luxas committed Oct 28, 2020
1 parent 0e9b466 commit e165cd0
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 3 deletions.
16 changes: 13 additions & 3 deletions pkg/apply/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,25 @@ import (

func Apply(ctx context.Context, cfg *config.Config) error {
// TODO: Enforce that gen is up-to-date
// TODO: Verify that the NS records are properly pointing to the DNS provider's nameservers

cp, err := providers.CloudProviders().NewCloudProvider(ctx, &cfg.CloudProvider)
cloudP, err := providers.CloudProviders().NewCloudProvider(ctx, &cfg.CloudProvider)
if err != nil {
return err
}

dnsP, err := providers.DNSProviders().NewDNSProvider(ctx, cfg.DNSProvider, cfg.RootDomain)
if err != nil {
return err
}

// Make sure the domain zone is created before starting to reconcile the clusters
// Otherwise external-dns nor Traefik will work.
if err := dnsP.EnsureZone(ctx); err != nil {
return err
}

return config.ForCluster(ctx, cfg.Clusters, cfg, func(clusterCtx context.Context, clusterInfo *config.ClusterInfo) error {
return ApplyCluster(clusterCtx, clusterInfo, cp)
return ApplyCluster(clusterCtx, clusterInfo, cloudP)
})
}

Expand Down
33 changes: 33 additions & 0 deletions pkg/provider/digitalocean/dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"io"
"net/http"
"strings"

"github.com/cloud-native-nordics/workshopctl/pkg/config"
Expand Down Expand Up @@ -37,6 +38,38 @@ func (do *DigitalOceanDNSProvider) ValuesProcessors() []gen.Processor {
return nil
}

func (do *DigitalOceanDNSProvider) EnsureZone(ctx context.Context) error {
logger := util.Logger(ctx)

// First, check if it exists
logger.Debugf("Ensuring domain %s is managed by DigitalOcean DNS", do.rootDomain)
domain, resp, err := do.c.Domains.Get(ctx, do.rootDomain)
if err == nil {
util.DebugObject(ctx, "Domain already exists", domain)
return nil
} else if resp.StatusCode != http.StatusNotFound { // err != nil and status code is not 404
return err
} // else resp.StatusCode == http.StatusNotFound
return do.createDomain(ctx, do.rootDomain, logger)
}

func (do *DigitalOceanDNSProvider) createDomain(ctx context.Context, rootDomain string, logger *logrus.Entry) error {
if do.dryRun {
logger.Infof("Would create domain %s in DigitalOcean DNS", rootDomain)
return nil
}
// Create the domain
logger.Infof("Creating domain %s in DigitalOcean DNS", rootDomain)
domain, _, err := do.c.Domains.Create(ctx, &godo.DomainCreateRequest{
Name: do.rootDomain,
})
if err != nil {
return err
}
util.DebugObject(ctx, "Created domain", domain)
return err
}

func (do *DigitalOceanDNSProvider) CleanupRecords(ctx context.Context, m provider.ClusterMeta) error {
logger := util.Logger(ctx)

Expand Down
3 changes: 3 additions & 0 deletions pkg/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ type DNSProviderFactory interface {
type DNSProvider interface {
ChartProcessors() []gen.Processor
ValuesProcessors() []gen.Processor
// EnsureZone ensures that the root domain zone is registered with the DNS provider
// This is run at apply-time before the individual cluster processors
EnsureZone(ctx context.Context) error
// CleanupRecords deletes records associated with a cluster
CleanupRecords(ctx context.Context, m ClusterMeta) error
}

0 comments on commit e165cd0

Please sign in to comment.