Skip to content
This repository has been archived by the owner on Dec 18, 2020. It is now read-only.

Enable CoreDNS in nodeup/protokube #6

Merged
merged 2 commits into from
Mar 23, 2017
Merged
Show file tree
Hide file tree
Changes from all 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
5 changes: 3 additions & 2 deletions dns-controller/cmd/dns-controller/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
kubectl_util "k8s.io/kubernetes/pkg/kubectl/cmd/util"

_ "k8s.io/kubernetes/federation/pkg/dnsprovider/providers/aws/route53"
_ "k8s.io/kubernetes/federation/pkg/dnsprovider/providers/coredns"
_ "k8s.io/kubernetes/federation/pkg/dnsprovider/providers/google/clouddns"
)

Expand All @@ -45,7 +46,7 @@ func main() {
fmt.Printf("dns-controller version %s\n", BuildVersion)

dnsProviderId := "aws-route53"
flags.StringVar(&dnsProviderId, "dns", dnsProviderId, "DNS provider we should use (aws-route53, google-clouddns)")
flags.StringVar(&dnsProviderId, "dns", dnsProviderId, "DNS provider we should use (aws-route53, google-clouddns, coredns)")

var zones []string
flags.StringSliceVarP(&zones, "zone", "z", []string{}, "Configure permitted zones and their mappings")
Expand Down Expand Up @@ -95,7 +96,7 @@ func main() {
os.Exit(1)
}

dnsController, err := dns.NewDNSController(dnsProvider, zoneRules)
dnsController, err := dns.NewDNSController(dnsProvider, zoneRules, dnsProviderId)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not able to put a comment on line#49, can you please add coredns in the string message.

Also, can you please share what value will this dnsProviderId will carry?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is decided by the --dns flags inside DAEMON_ARGS. So it's setting to coredns at here: https://github.com/vmware/kops/pull/6/files/6326f161619435794c37157b7ef7c79db636ea1b#diff-b25ed23c3990586a8ed6e64c73bb178cR276

if err != nil {
glog.Errorf("Error building DNS controller: %v", err)
os.Exit(1)
Expand Down
95 changes: 69 additions & 26 deletions dns-controller/pkg/dns/dnscontroller.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (

"k8s.io/kops/dns-controller/pkg/util"
"k8s.io/kubernetes/federation/pkg/dnsprovider"
k8scoredns "k8s.io/kubernetes/federation/pkg/dnsprovider/providers/coredns"
"k8s.io/kubernetes/federation/pkg/dnsprovider/rrstype"
)

Expand All @@ -54,6 +55,9 @@ type DNSController struct {

// changeCount is a change-counter, which helps us avoid computation when nothing has changed
changeCount uint64

//DNS Provider ID, one of aws-route53, google-clouddns, and coredns
dnsProviderId string
}

// DNSController is a Context
Expand All @@ -80,16 +84,17 @@ type DNSControllerScope struct {
var _ Scope = &DNSControllerScope{}

// NewDnsController creates a DnsController
func NewDNSController(dnsProvider dnsprovider.Interface, zoneRules *ZoneRules) (*DNSController, error) {
func NewDNSController(dnsProvider dnsprovider.Interface, zoneRules *ZoneRules, dnsProviderId string) (*DNSController, error) {
dnsCache, err := newDNSCache(dnsProvider)
if err != nil {
return nil, fmt.Errorf("error initializing DNS cache: %v", err)
}

c := &DNSController{
scopes: make(map[string]*DNSControllerScope),
zoneRules: zoneRules,
dnsCache: dnsCache,
scopes: make(map[string]*DNSControllerScope),
zoneRules: zoneRules,
dnsCache: dnsCache,
dnsProviderId: dnsProviderId,
}

return c, nil
Expand Down Expand Up @@ -256,7 +261,7 @@ func (c *DNSController) runOnce() error {

glog.V(4).Infof("updating records for %s: %v -> %v", k, oldValues, newValues)

err := op.updateRecords(k, newValues, int64(ttl.Seconds()))
err := op.updateRecords(k, newValues, int64(ttl.Seconds()), c.dnsProviderId)
if err != nil {
glog.Infof("error updating records for %s: %v", k, err)
errors = append(errors, err)
Expand All @@ -271,7 +276,7 @@ func (c *DNSController) runOnce() error {

newValues := newValueMap[k]
if newValues == nil {
err := op.deleteRecords(k)
err := op.deleteRecords(k, c.dnsProviderId)
if err != nil {
glog.Infof("error deleting records for %s: %v", k, err)
errors = append(errors, err)
Expand Down Expand Up @@ -412,7 +417,7 @@ func (o *dnsOp) listRecords(zone dnsprovider.Zone) ([]dnsprovider.ResourceRecord
return rrs, nil
}

func (o *dnsOp) deleteRecords(k recordKey) error {
func (o *dnsOp) deleteRecords(k recordKey, dnsProviderId string) error {
glog.V(2).Infof("Deleting all records for %s", k)

fqdn := EnsureDotSuffix(k.FQDN)
Expand All @@ -423,6 +428,31 @@ func (o *dnsOp) deleteRecords(k recordKey) error {
return fmt.Errorf("no suitable zone found for %q", fqdn)
}

// TODO: work-around before ResourceRecordSets.List() is implemented for CoreDNS
if dnsProviderId == k8scoredns.ProviderName {
rrsProvider, ok := zone.ResourceRecordSets()
if !ok {
return fmt.Errorf("zone does not support resource records %q", zone.Name())
}

dnsRecord, err := rrsProvider.Get(fqdn)
if err != nil {
return fmt.Errorf("Failed to get DNS record %s with error: %v", fqdn, err)
}

if dnsRecord != nil && string(dnsRecord.Type()) == string(k.RecordType) {
glog.V(8).Infof("Found matching record: %s %s", k.RecordType, fqdn)
cs, err := o.getChangeset(zone)
if err != nil {
return err
}
cs.Remove(dnsRecord)
}

return nil
}

// when DNS provider is aws-route53 or google-clouddns
rrs, err := o.listRecords(zone)
if err != nil {
return fmt.Errorf("error querying resource records for zone %q: %v", zone.Name(), err)
Expand Down Expand Up @@ -451,7 +481,7 @@ func (o *dnsOp) deleteRecords(k recordKey) error {
return nil
}

func (o *dnsOp) updateRecords(k recordKey, newRecords []string, ttl int64) error {
func (o *dnsOp) updateRecords(k recordKey, newRecords []string, ttl int64, dnsProviderId string) error {
fqdn := EnsureDotSuffix(k.FQDN)

zone := o.findZone(fqdn)
Expand All @@ -465,29 +495,42 @@ func (o *dnsOp) updateRecords(k recordKey, newRecords []string, ttl int64) error
return fmt.Errorf("zone does not support resource records %q", zone.Name())
}

rrs, err := o.listRecords(zone)
if err != nil {
return fmt.Errorf("error querying resource records for zone %q: %v", zone.Name(), err)
}

var existing dnsprovider.ResourceRecordSet
for _, rr := range rrs {
rrName := EnsureDotSuffix(rr.Name())
if rrName != fqdn {
glog.V(8).Infof("Skipping record %q (name != %s)", rrName, fqdn)
continue
// TODO: work-around before ResourceRecordSets.List() is implemented for CoreDNS
if dnsProviderId == k8scoredns.ProviderName {
dnsRecord, err := rrsProvider.Get(fqdn)
if err != nil {
return fmt.Errorf("Failed to get DNS record %s with error: %v", fqdn, err)
}
if string(rr.Type()) != string(k.RecordType) {
glog.V(8).Infof("Skipping record %q (type %s != %s)", rrName, rr.Type(), k.RecordType)
continue
if dnsRecord != nil && string(dnsRecord.Type()) == string(k.RecordType) {
glog.V(8).Infof("Found matching record: %s %s", k.RecordType, fqdn)
existing = dnsRecord
}
} else {
// when DNS provider is aws-route53 or google-clouddns
rrs, err := o.listRecords(zone)
if err != nil {
return fmt.Errorf("error querying resource records for zone %q: %v", zone.Name(), err)
}

if existing != nil {
glog.Warningf("Found multiple matching records: %v and %v", existing, rr)
} else {
glog.V(8).Infof("Found matching record: %s %s", k.RecordType, rrName)
for _, rr := range rrs {
rrName := EnsureDotSuffix(rr.Name())
if rrName != fqdn {
glog.V(8).Infof("Skipping record %q (name != %s)", rrName, fqdn)
continue
}
if string(rr.Type()) != string(k.RecordType) {
glog.V(8).Infof("Skipping record %q (type %s != %s)", rrName, rr.Type(), k.RecordType)
continue
}

if existing != nil {
glog.Warningf("Found multiple matching records: %v and %v", existing, rr)
} else {
glog.V(8).Infof("Found matching record: %s %s", k.RecordType, rrName)
}
existing = rr
}
existing = rr
}

cs, err := o.getChangeset(zone)
Expand Down
21 changes: 18 additions & 3 deletions protokube/cmd/protokube/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@ limitations under the License.
package main

import (
"bytes"
"flag"
"fmt"
"github.com/golang/glog"
"github.com/spf13/pflag"
"io"
"k8s.io/kops/dns-controller/pkg/dns"
"k8s.io/kops/protokube/pkg/protokube"
"k8s.io/kubernetes/federation/pkg/dnsprovider"
Expand All @@ -30,6 +32,7 @@ import (

// Load DNS plugins
_ "k8s.io/kubernetes/federation/pkg/dnsprovider/providers/aws/route53"
k8scoredns "k8s.io/kubernetes/federation/pkg/dnsprovider/providers/coredns"
_ "k8s.io/kubernetes/federation/pkg/dnsprovider/providers/google/clouddns"
)

Expand All @@ -53,7 +56,7 @@ func main() {

func run() error {
dnsProviderId := "aws-route53"
flags.StringVar(&dnsProviderId, "dns", dnsProviderId, "DNS provider we should use (aws-route53, google-clouddns)")
flags.StringVar(&dnsProviderId, "dns", dnsProviderId, "DNS provider we should use (aws-route53, google-clouddns, coredns)")

var zones []string
flags.StringSliceVarP(&zones, "zone", "z", []string{}, "Configure permitted zones and their mappings")
Expand All @@ -73,6 +76,9 @@ func run() error {
clusterID := ""
flag.StringVar(&clusterID, "cluster-id", clusterID, "Cluster ID")

dnsServer := ""
flag.StringVar(&dnsServer, "dns-server", dnsServer, "DNS Server")

flagChannels := ""
flag.StringVar(&flagChannels, "channels", flagChannels, "channels to install")

Expand Down Expand Up @@ -172,7 +178,16 @@ func run() error {
var dnsScope dns.Scope
var dnsController *dns.DNSController
{
dnsProvider, err := dnsprovider.GetDnsProvider(dnsProviderId, nil)
var file io.Reader
if dnsProviderId == k8scoredns.ProviderName {
var lines []string
lines = append(lines, "etcd-endpoints = "+dnsServer)
lines = append(lines, "zones = "+zones[0])
config := "[global]\n" + strings.Join(lines, "\n") + "\n"
file = bytes.NewReader([]byte(config))

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please explain the role of this config file?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AWS or GCE's DNS server doesn't need any specific information because they have public server address to access. But to use CoreDNS, we need to pass information about the IP address of the CoreDNS's etcd and zones to query. These are required to start a CoreDNS client. And this config file (it's not a real file though) is used to pass in these information. Without this config file, the DNS provider won't know which CoreDNS server to talk to.

}

dnsProvider, err := dnsprovider.GetDnsProvider(dnsProviderId, file)
if err != nil {
return fmt.Errorf("Error initializing DNS provider %q: %v", dnsProviderId, err)
}
Expand All @@ -185,7 +200,7 @@ func run() error {
return fmt.Errorf("unexpected zone flags: %q", err)
}

dnsController, err = dns.NewDNSController(dnsProvider, zoneRules)
dnsController, err = dns.NewDNSController(dnsProvider, zoneRules, dnsProviderId)
if err != nil {
return err
}
Expand Down
2 changes: 2 additions & 0 deletions upup/pkg/fi/cloudup/apply_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,8 @@ func (c *ApplyClusterCmd) Run() error {
//&model.SSHKeyModelBuilder{KopsModelContext: modelContext},
)
case fi.CloudProviderVSphere:
l.Builders = append(l.Builders,
&model.PKIModelBuilder{KopsModelContext: modelContext})

default:
return fmt.Errorf("unknown cloudprovider %q", cluster.Spec.CloudProvider)
Expand Down
2 changes: 1 addition & 1 deletion upup/pkg/fi/cloudup/template_functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ func (tf *TemplateFunctions) DnsControllerArgv() ([]string, error) {
case fi.CloudProviderGCE:
argv = append(argv, "--dns=google-clouddns")
case fi.CloudProviderVSphere:
argv = append(argv, "--dns=aws-route53")
argv = append(argv, "--dns=coredns")

default:
return nil, fmt.Errorf("unhandled cloudprovider %q", tf.cluster.Spec.CloudProvider)
Expand Down
1 change: 1 addition & 0 deletions upup/pkg/fi/nodeup/protokube_flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,5 @@ type ProtokubeFlags struct {
Cloud *string `json:"cloud,omitempty" flag:"cloud"`
// ClusterId flag is required only for vSphere cloud type, to pass cluster id information to protokube. AWS and GCE workflows ignore this flag.
ClusterId *string `json:"cluster-id,omitempty" flag:"cluster-id"`
DNSServer *string `json:"dns-server,omitempty" flag:"dns-server"`
}
3 changes: 2 additions & 1 deletion upup/pkg/fi/nodeup/template_functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -273,8 +273,9 @@ func (t *templateFunctions) ProtokubeFlags() *ProtokubeFlags {
case fi.CloudProviderGCE:
f.DNSProvider = fi.String("google-clouddns")
case fi.CloudProviderVSphere:
f.DNSProvider = fi.String("aws-route53")
f.DNSProvider = fi.String("coredns")
f.ClusterId = fi.String(t.cluster.ObjectMeta.Name)
f.DNSServer = fi.String(*t.cluster.Spec.CloudConfig.VSphereCoreDNSServer)
default:
glog.Warningf("Unknown cloudprovider %q; won't set DNS provider")
}
Expand Down