Skip to content

Commit

Permalink
Merge 892ba65 into 1125b11
Browse files Browse the repository at this point in the history
  • Loading branch information
skwair committed Aug 2, 2022
2 parents 1125b11 + 892ba65 commit 242cd01
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 5 deletions.
26 changes: 21 additions & 5 deletions pkg/dns/dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,14 +89,30 @@ func (c *Client) CheckDNSProvider(ctx context.Context) (Provider, error) {
func (c *Client) coreDNSMatch(ctx context.Context) (bool, error) {
c.logger.Debugf("Checking if CoreDNS is installed in namespace %q...", metav1.NamespaceSystem)

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

deployments, err := c.kubeClient.AppsV1().Deployments(metav1.NamespaceSystem).List(ctx, opts)
if err != nil {
return false, fmt.Errorf("unable to get CoreDNS deployment in namespace %q: %w", metav1.NamespaceSystem, err)
return false, fmt.Errorf("unable to list CoreDNS deployments in namespace %q: %w", metav1.NamespaceSystem, err)
}

var deployment *appsv1.Deployment
if len(deployments.Items) == 1 {
deployment = &deployments.Items[0]
} else {
// If we did not find CoreDNS using the annotation (e.g.: with kubeadm), fall back to matching the name of the deployment.
deployment, err = c.kubeClient.AppsV1().Deployments(metav1.NamespaceSystem).Get(ctx, "coredns", metav1.GetOptions{})
if kerrors.IsNotFound(err) {
c.logger.Debug("CoreDNS deployment not found")
return false, nil
}

if err != nil {
return false, fmt.Errorf("unable to get CoreDNS deployment in namespace %q: %w", metav1.NamespaceSystem, err)
}
}

version, err := c.getCoreDNSVersion(deployment)
Expand Down
6 changes: 6 additions & 0 deletions pkg/dns/dns_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ func TestCheckDNSProvider(t *testing.T) {
expProvider: CoreDNS,
expErr: false,
},
{
desc: "CoreDNS supported version using label",
mockFile: "checkdnsprovider_coredns_using_label.yaml",
expProvider: CoreDNS,
expErr: false,
},
{
desc: "CoreDNS supported version with suffix",
mockFile: "checkdnsprovider_supported_version_suffix.yaml",
Expand Down
15 changes: 15 additions & 0 deletions pkg/dns/testdata/checkdnsprovider_coredns_using_label.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: rke2-coredns-rke2-coredns
namespace: kube-system
labels:
kubernetes.io/name: CoreDNS
spec:
template:
spec:
containers:
- name: coredns
image: image-registry.dkr.ecr.eu-west-1.amazonaws.com/eks/coredns:v1.8.4
- name: titi
image: titi/toto:latest

0 comments on commit 242cd01

Please sign in to comment.