Skip to content

Commit

Permalink
Merge pull request #39 from janeczku/refactor-for-testing
Browse files Browse the repository at this point in the history
Refactoring
  • Loading branch information
janeczku authored Jul 27, 2016
2 parents 018825d + 4821418 commit 7003e99
Show file tree
Hide file tree
Showing 17 changed files with 654 additions and 617 deletions.
4 changes: 2 additions & 2 deletions cattle.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package main

import (
"github.com/rancher/external-dns/dns"
"github.com/rancher/external-dns/utils"
"github.com/rancher/go-rancher/client"
)

Expand All @@ -25,7 +25,7 @@ func NewCattleClient(cattleUrl string, cattleAccessKey string, cattleSecretKey s
}, nil
}

func (c *CattleClient) UpdateServiceDomainName(serviceDnsRecord dns.ServiceDnsRecord) error {
func (c *CattleClient) UpdateServiceDomainName(serviceDnsRecord utils.ServiceDnsRecord) error {

event := &client.ExternalDnsEvent{
EventType: "dns.update",
Expand Down
39 changes: 39 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package config

import (
"os"
"strconv"

"github.com/Sirupsen/logrus"
"github.com/rancher/external-dns/utils"
)

var (
RootDomainName string
TTL int
CattleURL string
CattleAccessKey string
CattleSecretKey string
)

func SetFromEnvironment() {
CattleURL = getEnv("CATTLE_URL")
CattleAccessKey = getEnv("CATTLE_ACCESS_KEY")
CattleSecretKey = getEnv("CATTLE_SECRET_KEY")
RootDomainName = utils.Fqdn(getEnv("ROOT_DOMAIN"))
TTLEnv := os.Getenv("TTL")
i, err := strconv.Atoi(TTLEnv)
if err != nil {
TTL = 300
} else {
TTL = i
}
}

func getEnv(name string) string {
envVar := os.Getenv(name)
if len(envVar) == 0 {
logrus.Fatalf("Environment variable '%s' is not set", name)
}
return envVar
}
58 changes: 0 additions & 58 deletions dns/dns.go

This file was deleted.

31 changes: 16 additions & 15 deletions external-dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ package main
import (
"fmt"
"github.com/Sirupsen/logrus"
"github.com/rancher/external-dns/dns"
"github.com/rancher/external-dns/config"
"github.com/rancher/external-dns/utils"
"strings"
)

func UpdateProviderDnsRecords(metadataRecs map[string]dns.DnsRecord) ([]dns.DnsRecord, error) {
var updated []dns.DnsRecord
func UpdateProviderDnsRecords(metadataRecs map[string]utils.DnsRecord) ([]utils.DnsRecord, error) {
var updated []utils.DnsRecord
providerRecs, err := getProviderDnsRecords()
if err != nil {
return nil, fmt.Errorf("Provider error reading dns entries: %v", err)
Expand All @@ -24,8 +25,8 @@ func UpdateProviderDnsRecords(metadataRecs map[string]dns.DnsRecord) ([]dns.DnsR
return updated, nil
}

func addMissingRecords(metadataRecs map[string]dns.DnsRecord, providerRecs map[string]dns.DnsRecord) []dns.DnsRecord {
var toAdd []dns.DnsRecord
func addMissingRecords(metadataRecs map[string]utils.DnsRecord, providerRecs map[string]utils.DnsRecord) []utils.DnsRecord {
var toAdd []utils.DnsRecord
for key := range metadataRecs {
if _, ok := providerRecs[key]; !ok {
toAdd = append(toAdd, metadataRecs[key])
Expand All @@ -39,8 +40,8 @@ func addMissingRecords(metadataRecs map[string]dns.DnsRecord, providerRecs map[s
return updateRecords(toAdd, &Add)
}

func updateRecords(toChange []dns.DnsRecord, op *Op) []dns.DnsRecord {
var changed []dns.DnsRecord
func updateRecords(toChange []utils.DnsRecord, op *Op) []utils.DnsRecord {
var changed []utils.DnsRecord
for _, value := range toChange {
switch *op {
case Add:
Expand All @@ -67,8 +68,8 @@ func updateRecords(toChange []dns.DnsRecord, op *Op) []dns.DnsRecord {
return changed
}

func updateExistingRecords(metadataRecs map[string]dns.DnsRecord, providerRecs map[string]dns.DnsRecord) []dns.DnsRecord {
var toUpdate []dns.DnsRecord
func updateExistingRecords(metadataRecs map[string]utils.DnsRecord, providerRecs map[string]utils.DnsRecord) []utils.DnsRecord {
var toUpdate []utils.DnsRecord
for key := range metadataRecs {
if _, ok := providerRecs[key]; ok {
metadataR := make(map[string]struct{}, len(metadataRecs[key].Records))
Expand Down Expand Up @@ -110,8 +111,8 @@ func updateExistingRecords(metadataRecs map[string]dns.DnsRecord, providerRecs m
return updateRecords(toUpdate, &Update)
}

func removeExtraRecords(metadataRecs map[string]dns.DnsRecord, providerRecs map[string]dns.DnsRecord) []dns.DnsRecord {
var toRemove []dns.DnsRecord
func removeExtraRecords(metadataRecs map[string]utils.DnsRecord, providerRecs map[string]utils.DnsRecord) []utils.DnsRecord {
var toRemove []utils.DnsRecord
for key := range providerRecs {
if _, ok := metadataRecs[key]; !ok {
toRemove = append(toRemove, providerRecs[key])
Expand All @@ -126,16 +127,16 @@ func removeExtraRecords(metadataRecs map[string]dns.DnsRecord, providerRecs map[
return updateRecords(toRemove, &Remove)
}

func getProviderDnsRecords() (map[string]dns.DnsRecord, error) {
func getProviderDnsRecords() (map[string]utils.DnsRecord, error) {
allRecords, err := provider.GetRecords()
if err != nil {
return nil, err
}
ourRecords := make(map[string]dns.DnsRecord, len(allRecords))
joins := []string{m.EnvironmentName, dns.RootDomainName}
ourRecords := make(map[string]utils.DnsRecord, len(allRecords))
joins := []string{m.EnvironmentName, config.RootDomainName}
suffix := "." + strings.ToLower(strings.Join(joins, "."))
for _, value := range allRecords {
if value.Type == "A" && strings.HasSuffix(value.Fqdn, suffix) && value.TTL == dns.TTL {
if value.Type == "A" && strings.HasSuffix(value.Fqdn, suffix) && value.TTL == config.TTL {
ourRecords[value.Fqdn] = value
}
}
Expand Down
4 changes: 2 additions & 2 deletions healthcheck.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ func healtcheck(w http.ResponseWriter, req *http.Request) {
http.Error(w, "Failed to reach metadata server", http.StatusInternalServerError)
} else {
// 2) test provider
_, err := provider.GetRecords()
err := provider.HealthCheck()
if err != nil {
logrus.Error("Healtcheck failed: unable to reach a provider")
logrus.Error("Healtcheck failed: Error from provider: %v", err)
http.Error(w, "Failed to reach an external provider ", http.StatusInternalServerError)
} else {
err := c.TestConnect()
Expand Down
51 changes: 26 additions & 25 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,21 @@ package main

import (
"flag"
"os"
"time"

"github.com/Sirupsen/logrus"
"github.com/rancher/external-dns/dns"
"github.com/rancher/external-dns/config"
"github.com/rancher/external-dns/metadata"
"github.com/rancher/external-dns/providers"
"os"
"time"
_ "github.com/rancher/external-dns/providers/cloudflare"
_ "github.com/rancher/external-dns/providers/dnsimple"
_ "github.com/rancher/external-dns/providers/gandi"
_ "github.com/rancher/external-dns/providers/pointhq"
_ "github.com/rancher/external-dns/providers/powerdns"
_ "github.com/rancher/external-dns/providers/rfc2136"
_ "github.com/rancher/external-dns/providers/route53"
"github.com/rancher/external-dns/utils"
)

const (
Expand All @@ -27,7 +36,7 @@ var (
)

var (
providerName = flag.String("provider", "", "External provider name")
providerName = flag.String("provider", "route53", "External provider name")
debug = flag.Bool("debug", false, "Debug")
logFile = flag.String("log", "", "Log file")

Expand All @@ -38,7 +47,6 @@ var (

func setEnv() {
flag.Parse()
provider = providers.GetProvider(*providerName)
if *debug {
logrus.SetLevel(logrus.DebugLevel)
}
Expand All @@ -54,39 +62,32 @@ func setEnv() {
}
}

// get config from environment variables
config.SetFromEnvironment()

var err error
// configure metadata client
mClient, err := metadata.NewMetadataClient()
m, err = metadata.NewMetadataClient()
if err != nil {
logrus.Fatalf("Failed to configure rancher-metadata client: %v", err)
}
m = mClient

cattleUrl := os.Getenv("CATTLE_URL")
if len(cattleUrl) == 0 {
logrus.Fatalf("CATTLE_URL is not set")
}

cattleApiKey := os.Getenv("CATTLE_ACCESS_KEY")
if len(cattleApiKey) == 0 {
logrus.Fatalf("CATTLE_ACCESS_KEY is not set")
}

cattleSecretKey := os.Getenv("CATTLE_SECRET_KEY")
if len(cattleSecretKey) == 0 {
logrus.Fatalf("CATTLE_SECRET_KEY is not set")
}

//configure cattle client
c, err = NewCattleClient(cattleUrl, cattleApiKey, cattleSecretKey)
c, err = NewCattleClient(config.CattleURL, config.CattleAccessKey, config.CattleSecretKey)
if err != nil {
logrus.Fatalf("Failed to configure cattle client: %v", err)
}

// get provider
provider, err = providers.GetProvider(*providerName, config.RootDomainName)
if err != nil {
logrus.Fatalf("Failed to get provider '%s': %v", *providerName, err)
}
}

func main() {
logrus.Infof("Starting Rancher External DNS service")
setEnv()
logrus.Infof("Powered by %s", provider.GetName())

go startHealthcheck()

Expand Down Expand Up @@ -125,7 +126,7 @@ func main() {
}

for _, toUpdate := range updated {
serviceDnsRecord := dns.ConvertToServiceDnsRecord(toUpdate)
serviceDnsRecord := utils.ConvertToServiceDnsRecord(toUpdate)
c.UpdateServiceDomainName(serviceDnsRecord)
}

Expand Down
17 changes: 9 additions & 8 deletions metadata/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ package metadata
import (
"fmt"
"github.com/Sirupsen/logrus"
"github.com/rancher/external-dns/dns"
"github.com/rancher/external-dns/config"
"github.com/rancher/external-dns/utils"
"github.com/rancher/go-rancher-metadata/metadata"
"time"
)
Expand Down Expand Up @@ -54,16 +55,16 @@ func (m *MetadataClient) GetVersion() (string, error) {
return m.MetadataClient.GetVersion()
}

func (m *MetadataClient) GetMetadataDnsRecords() (map[string]dns.DnsRecord, error) {
dnsEntries := make(map[string]dns.DnsRecord)
func (m *MetadataClient) GetMetadataDnsRecords() (map[string]utils.DnsRecord, error) {
dnsEntries := make(map[string]utils.DnsRecord)
err := m.getContainersDnsRecords(dnsEntries, "", "")
if err != nil {
return dnsEntries, err
}
return dnsEntries, nil
}

func (m *MetadataClient) getContainersDnsRecords(dnsEntries map[string]dns.DnsRecord, serviceName string, stackName string) error {
func (m *MetadataClient) getContainersDnsRecords(dnsEntries map[string]utils.DnsRecord, serviceName string, stackName string) error {
containers, err := m.MetadataClient.GetContainers()
if err != nil {
return err
Expand Down Expand Up @@ -104,24 +105,24 @@ func (m *MetadataClient) getContainersDnsRecords(dnsEntries map[string]dns.DnsRe
ip = host.AgentIP
}

fqdn := dns.ConvertToFqdn(container.ServiceName, container.StackName, m.EnvironmentName)
fqdn := utils.ConvertToFqdn(container.ServiceName, container.StackName, m.EnvironmentName, config.RootDomainName)
records := []string{ip}
dnsEntry := dns.DnsRecord{fqdn, records, "A", dns.TTL}
dnsEntry := utils.DnsRecord{fqdn, records, "A", config.TTL}

addToDnsEntries(dnsEntry, dnsEntries)
}

return nil
}

func addToDnsEntries(dnsEntry dns.DnsRecord, dnsEntries map[string]dns.DnsRecord) {
func addToDnsEntries(dnsEntry utils.DnsRecord, dnsEntries map[string]utils.DnsRecord) {
var records []string
if _, ok := dnsEntries[dnsEntry.Fqdn]; !ok {
records = dnsEntry.Records
} else {
records = dnsEntries[dnsEntry.Fqdn].Records
records = append(records, dnsEntry.Records...)
}
dnsEntry = dns.DnsRecord{dnsEntry.Fqdn, records, "A", dns.TTL}
dnsEntry = utils.DnsRecord{dnsEntry.Fqdn, records, "A", config.TTL}
dnsEntries[dnsEntry.Fqdn] = dnsEntry
}
Loading

0 comments on commit 7003e99

Please sign in to comment.