Skip to content

Commit

Permalink
Patch our CoreDNS implementation in kube-dns mode
Browse files Browse the repository at this point in the history
  • Loading branch information
dtomcej authored May 27, 2020
1 parent d04c2ef commit 736427d
Show file tree
Hide file tree
Showing 7 changed files with 111 additions and 46 deletions.
18 changes: 0 additions & 18 deletions helm/chart/maesh/templates/dns/coredns/coredns-configmap.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,4 @@ data:
reload
loadbalance
}
maesh:53 {
errors
rewrite continue {
name regex ([a-zA-Z0-9-_]*)\.([a-zv0-9-_]*)\.maesh {{ .Release.Namespace }}-{1}-6d61657368-{2}.{{ .Release.Namespace }}.svc.{{ default "cluster.local" .Values.clusterDomain }}
answer name {{ .Release.Namespace }}-([a-zA-Z0-9-_]*)-6d61657368-([a-zA-Z0-9-_]*)\.{{ .Release.Namespace }}\.svc\.{{ default "cluster.local" .Values.clusterDomain | replace "." "\\." }} {1}.{2}.maesh
}
kubernetes {{ default "cluster.local" .Values.clusterDomain }} in-addr.arpa ip6.arpa {
pods insecure
upstream
fallthrough in-addr.arpa ip6.arpa
}
forward . /etc/resolv.conf
cache 30
loop
reload
loadbalance
}
{{- end }}
17 changes: 0 additions & 17 deletions integration/testdata/kubedns/coredns.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -68,23 +68,6 @@ data:
loadbalance
}
maesh:53 {
errors
rewrite continue {
name regex ([a-zA-Z0-9-_]*)\.([a-zv0-9-_]*)\.maesh maesh-{1}-6d61657368-{2}.maesh.svc.cluster.local
answer name maesh-([a-zA-Z0-9-_]*)-6d61657368-([a-zA-Z0-9-_]*)\.maesh\.svc\.cluster\.local {1}.{2}.maesh
}
kubernetes cluster.local in-addr.arpa ip6.arpa {
pods insecure
upstream
fallthrough in-addr.arpa ip6.arpa
}
forward . /etc/resolv.conf
cache 30
loop
reload
loadbalance
}
---
apiVersion: apps/v1
kind: Deployment
Expand Down
12 changes: 9 additions & 3 deletions pkg/dns/dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,10 +142,10 @@ func (c *Client) kubeDNSMatch() (bool, error) {
}

// ConfigureCoreDNS patches the CoreDNS configuration for Maesh.
func (c *Client) ConfigureCoreDNS(clusterDomain, maeshNamespace string) error {
func (c *Client) ConfigureCoreDNS(coreDNSNamespace, clusterDomain, maeshNamespace string) error {
c.log.Debug("Patching CoreDNS")

deployment, err := c.client.KubernetesClient().AppsV1().Deployments(metav1.NamespaceSystem).Get("coredns", metav1.GetOptions{})
deployment, err := c.client.KubernetesClient().AppsV1().Deployments(coreDNSNamespace).Get("coredns", metav1.GetOptions{})
if err != nil {
return err
}
Expand Down Expand Up @@ -242,7 +242,7 @@ func (c *Client) getCorefileConfigMap(coreDeployment *appsv1.Deployment) (*corev
}

// ConfigureKubeDNS patches the KubeDNS configuration for Maesh.
func (c *Client) ConfigureKubeDNS(maeshNamespace string) error {
func (c *Client) ConfigureKubeDNS(clusterDomain, maeshNamespace string) error {
c.log.Debug("Patching KubeDNS")

deployment, err := c.client.KubernetesClient().AppsV1().Deployments(metav1.NamespaceSystem).Get("kube-dns", metav1.GetOptions{})
Expand Down Expand Up @@ -283,6 +283,12 @@ func (c *Client) ConfigureKubeDNS(maeshNamespace string) error {
return err
}

c.log.Debug("Patching CoreDNS configmap")

if err := c.ConfigureCoreDNS(maeshNamespace, clusterDomain, maeshNamespace); err != nil {
return err
}

if err := c.restartPods(deployment); err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/dns/dns_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ func TestConfigureCoreDNS(t *testing.T) {
log.SetOutput(os.Stdout)
log.SetLevel(logrus.DebugLevel)
client := NewClient(log, clt)
err := client.ConfigureCoreDNS("titi", "toto")
err := client.ConfigureCoreDNS("kube-system", "titi", "toto")
if test.expectedErr {
assert.Error(t, err)
return
Expand Down Expand Up @@ -172,7 +172,7 @@ func TestConfigureKubeDNS(t *testing.T) {
log.SetOutput(os.Stdout)
log.SetLevel(logrus.DebugLevel)
client := NewClient(log, clt)
err := client.ConfigureKubeDNS("maesh")
err := client.ConfigureKubeDNS("cluster.local", "maesh")
if test.expectedErr {
assert.Error(t, err)
return
Expand Down
47 changes: 47 additions & 0 deletions pkg/dns/testdata/configurekubedns_already_patched.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,50 @@ metadata:
namespace: maesh
spec:
clusterIP: "1.2.3.4"
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: coredns
namespace: maesh
spec:
template:
spec:
volumes:
- configMap:
name: "other-cfgmap"
- configMap:
name: "coredns-cfgmap"
---
apiVersion: v1
kind: ConfigMap
metadata:
name: other-cfgmap
namespace: maesh
---
apiVersion: v1
kind: ConfigMap
metadata:
name: coredns-cfgmap
namespace: maesh
data:
Corefile: |
.:53 {
errors
health {
lameduck 5s
}
ready
kubernetes {{ pillar['dns_domain'] }} in-addr.arpa ip6.arpa {
pods insecure
fallthrough in-addr.arpa ip6.arpa
ttl 30
}
prometheus :9153
forward . /etc/resolv.conf
cache 30
loop
reload
loadbalance
}
46 changes: 46 additions & 0 deletions pkg/dns/testdata/configurekubedns_not_patched.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,49 @@ metadata:
namespace: maesh
spec:
clusterIP: "1.2.3.4"
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: coredns
namespace: maesh
spec:
template:
spec:
volumes:
- configMap:
name: "other-cfgmap"
- configMap:
name: "coredns-cfgmap"
---
apiVersion: v1
kind: ConfigMap
metadata:
name: other-cfgmap
namespace: maesh
---
apiVersion: v1
kind: ConfigMap
metadata:
name: coredns-cfgmap
namespace: maesh
data:
Corefile: |
.:53 {
errors
health {
lameduck 5s
}
ready
kubernetes {{ pillar['dns_domain'] }} in-addr.arpa ip6.arpa {
pods insecure
fallthrough in-addr.arpa ip6.arpa
ttl 30
}
prometheus :9153
forward . /etc/resolv.conf
cache 30
loop
reload
loadbalance
}
13 changes: 7 additions & 6 deletions pkg/prepare/prepare.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
specsinformer "github.com/servicemeshinterface/smi-sdk-go/pkg/gen/client/specs/informers/externalversions"
splitinformer "github.com/servicemeshinterface/smi-sdk-go/pkg/gen/client/split/informers/externalversions"
"github.com/sirupsen/logrus"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/informers"
"k8s.io/client-go/tools/cache"
)
Expand Down Expand Up @@ -124,13 +125,13 @@ func (p *Prepare) CheckDNSProvider() (dns.Provider, error) {
}

// ConfigureCoreDNS patches the CoreDNS configuration for Maesh.
func (p *Prepare) ConfigureCoreDNS(clusterDomain, maeshNamespace string) error {
return p.dns.ConfigureCoreDNS(clusterDomain, maeshNamespace)
func (p *Prepare) ConfigureCoreDNS(coreDNSNamespace, clusterDomain, maeshNamespace string) error {
return p.dns.ConfigureCoreDNS(coreDNSNamespace, clusterDomain, maeshNamespace)
}

// ConfigureKubeDNS patches the KubeDNS configuration for Maesh.
func (p *Prepare) ConfigureKubeDNS(maeshNamespace string) error {
return p.dns.ConfigureKubeDNS(maeshNamespace)
func (p *Prepare) ConfigureKubeDNS(clusterDomain, maeshNamespace string) error {
return p.dns.ConfigureKubeDNS(clusterDomain, maeshNamespace)
}

// ConfigureDNS configures and patches the DNS system.
Expand All @@ -142,11 +143,11 @@ func (p *Prepare) ConfigureDNS(clusterDomain, maeshNamespace string) error {

switch provider {
case dns.CoreDNS:
if err := p.ConfigureCoreDNS(clusterDomain, maeshNamespace); err != nil {
if err := p.ConfigureCoreDNS(metav1.NamespaceSystem, clusterDomain, maeshNamespace); err != nil {
return fmt.Errorf("unable to configure CoreDNS: %v", err)
}
case dns.KubeDNS:
if err := p.ConfigureKubeDNS(maeshNamespace); err != nil {
if err := p.ConfigureKubeDNS(clusterDomain, maeshNamespace); err != nil {
return fmt.Errorf("unable to configure KubeDNS: %v", err)
}
}
Expand Down

0 comments on commit 736427d

Please sign in to comment.