Skip to content

Commit 2d87eda

Browse files
authored
Fix CoreDNS Detection
1 parent 1125b11 commit 2d87eda

6 files changed

Lines changed: 46 additions & 5 deletions

File tree

integration/coredns_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ func (s *CoreDNSSuite) SetUpSuite(c *check.C) {
3232
{Name: "coredns/coredns:1.5.2"},
3333
{Name: "coredns/coredns:1.6.3"},
3434
{Name: "coredns/coredns:1.7.0"},
35+
{Name: "coredns/coredns:1.8.0"},
36+
{Name: "coredns/coredns:1.9.0"},
3537
{Name: "giantswarm/tiny-tools:3.9"},
3638
}
3739

integration/testdata/traefik-mesh/controller-acl-disabled.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ rules:
5050
resources:
5151
- deployments
5252
verbs:
53+
- list
5354
- get
5455
- update
5556
- apiGroups:

integration/testdata/traefik-mesh/controller-acl-enabled.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ rules:
5050
resources:
5151
- deployments
5252
verbs:
53+
- list
5354
- get
5455
- update
5556
- apiGroups:

pkg/dns/dns.go

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -89,14 +89,30 @@ func (c *Client) CheckDNSProvider(ctx context.Context) (Provider, error) {
8989
func (c *Client) coreDNSMatch(ctx context.Context) (bool, error) {
9090
c.logger.Debugf("Checking if CoreDNS is installed in namespace %q...", metav1.NamespaceSystem)
9191

92-
deployment, err := c.kubeClient.AppsV1().Deployments(metav1.NamespaceSystem).Get(ctx, "coredns", metav1.GetOptions{})
93-
if kerrors.IsNotFound(err) {
94-
c.logger.Debug("CoreDNS deployment not found")
95-
return false, nil
92+
// Most Kubernetes distributions deploy CoreDNS with the following label, so look for it first.
93+
opts := metav1.ListOptions{
94+
LabelSelector: "kubernetes.io/name=CoreDNS",
9695
}
9796

97+
deployments, err := c.kubeClient.AppsV1().Deployments(metav1.NamespaceSystem).List(ctx, opts)
9898
if err != nil {
99-
return false, fmt.Errorf("unable to get CoreDNS deployment in namespace %q: %w", metav1.NamespaceSystem, err)
99+
return false, fmt.Errorf("unable to list CoreDNS deployments in namespace %q: %w", metav1.NamespaceSystem, err)
100+
}
101+
102+
var deployment *appsv1.Deployment
103+
if len(deployments.Items) == 1 {
104+
deployment = &deployments.Items[0]
105+
} else {
106+
// If we did not find CoreDNS using the annotation (e.g.: with kubeadm), fall back to matching the name of the deployment.
107+
deployment, err = c.kubeClient.AppsV1().Deployments(metav1.NamespaceSystem).Get(ctx, "coredns", metav1.GetOptions{})
108+
if kerrors.IsNotFound(err) {
109+
c.logger.Debug("CoreDNS deployment not found")
110+
return false, nil
111+
}
112+
113+
if err != nil {
114+
return false, fmt.Errorf("unable to get CoreDNS deployment in namespace %q: %w", metav1.NamespaceSystem, err)
115+
}
100116
}
101117

102118
version, err := c.getCoreDNSVersion(deployment)

pkg/dns/dns_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,12 @@ func TestCheckDNSProvider(t *testing.T) {
2626
expProvider: CoreDNS,
2727
expErr: false,
2828
},
29+
{
30+
desc: "CoreDNS supported version using label",
31+
mockFile: "checkdnsprovider_coredns_using_label.yaml",
32+
expProvider: CoreDNS,
33+
expErr: false,
34+
},
2935
{
3036
desc: "CoreDNS supported version with suffix",
3137
mockFile: "checkdnsprovider_supported_version_suffix.yaml",
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
apiVersion: apps/v1
2+
kind: Deployment
3+
metadata:
4+
name: rke2-coredns-rke2-coredns
5+
namespace: kube-system
6+
labels:
7+
kubernetes.io/name: CoreDNS
8+
spec:
9+
template:
10+
spec:
11+
containers:
12+
- name: coredns
13+
image: image-registry.dkr.ecr.eu-west-1.amazonaws.com/eks/coredns:v1.8.4
14+
- name: titi
15+
image: titi/toto:latest

0 commit comments

Comments
 (0)