@@ -89,14 +89,30 @@ func (c *Client) CheckDNSProvider(ctx context.Context) (Provider, error) {
8989func (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 )
0 commit comments