func corefileMigration(ctx context.Context, c client.Client, coreDNSVersion, corefile string) (string, error) {
	var err error
	coreDNSDeploy, err := getCoreDNSDeployment(ctx, c)
	if err != nil && !apierrors.IsNotFound(err) {
		return "", err
	}
	var coreDNSImage string
	coreDNSDeploySpec := coreDNSDeploy.Spec.Template.Spec
	for _, container := range coreDNSDeploySpec.Containers {
		if container.Name == coreDNSName {
			coreDNSImage = container.Image
		}
	}
	coreDNSImageParts := strings.Split(coreDNSImage, ":")
	currentCoreDNSVersion := coreDNSImageParts[len(coreDNSImageParts)-1]
	if currentCoreDNSVersion != coreDNSVersion {
		klog.Infof("from: %q  to: %q", currentCoreDNSVersion, coreDNSVersion)
		isDefault := migration.Default("", corefile)
		switch isDefault {
		case true:
			corefile = ""
			klog.Infof("the default Corefile will be applied")
		case false:
			corefile, err = performMigrationOfCorefile(ctx, c, corefile, currentCoreDNSVersion, coreDNSVersion)
			if err != nil {
				return "", errors.Errorf("unable to migrate the CoreDNS Corefile data: %v", err)
			}
			klog.Infof("determined Corefile for CoreDNS should be %q", corefile)
		}
	}
	return corefile, nil
}
corefileMigration if necessary/possible, migrates the Corefile to reflect the latest changes, when the CoreDNS version is being upgraded.
package controllers
import (
	"context"
	"net"
	"regexp"
	"strings"
	appsv1 "k8s.io/api/apps/v1"
	corev1 "k8s.io/api/core/v1"
	apierrors "k8s.io/apimachinery/pkg/api/errors"
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	"k8s.io/klog/v2"
	"sigs.k8s.io/controller-runtime/pkg/client"
	"github.com/coredns/corefile-migration/migration"
	"github.com/pkg/errors"
)
func getCoreDNSService(ctx context.Context, c client.Client) (*corev1.Service, error) {
	kubernetesService := &corev1.Service{}
	id := client.ObjectKey{Namespace: metav1.NamespaceDefault, Name: "kubernetes"}
	err := c.Get(ctx, id, kubernetesService)
	return kubernetesService, err
}
func getCoreDNSConfigMap(ctx context.Context, c client.Client, configMapName string) (*corev1.ConfigMap, error) {
	coreDNSConfigMap := &corev1.ConfigMap{}
	id := client.ObjectKey{Namespace: metav1.NamespaceSystem, Name: configMapName}
	err := c.Get(ctx, id, coreDNSConfigMap)
	return coreDNSConfigMap, err
}
func getCoreDNSDeployment(ctx context.Context, c client.Client) (*appsv1.Deployment, error) {
	coreDNSDeploy := &appsv1.Deployment{}
	deployId := client.ObjectKey{Namespace: metav1.NamespaceSystem, Name: coreDNSName}
	err := c.Get(ctx, deployId, coreDNSDeploy)
	return coreDNSDeploy, err
}
func findDNSClusterIP(ctx context.Context, c client.Client) (string, error) {
	kubernetesService, err := getCoreDNSService(ctx, c)
	if err != nil && !apierrors.IsNotFound(err) {
		return "", err
	}
	if apierrors.IsNotFound(err) {
		return coreDNSIP, nil
	}
	ip := net.ParseIP(kubernetesService.Spec.ClusterIP)
	if ip == nil {
		return "", errors.Errorf("cannot parse kubernetes ClusterIP %q", kubernetesService.Spec.ClusterIP)
	}
	ip[len(ip)-1] += 9
	result := ip.String()
	klog.Infof("determined ClusterIP for CoreDNS should be %q", result)
	return result, nil
}
func getDNSDomain() string {
	svc := "kubernetes.default.svc"
	cname, err := net.LookupCNAME(svc)
	if err != nil {
		klog.Infof("determined DNS Domain for CoreDNS should be %q", coreDNSDomain)
		return coreDNSDomain
	}
	domain := strings.TrimPrefix(cname, svc)
	domain = strings.TrimSuffix(coreDNSDomain, ".")
	klog.Infof("determined DNS Domain for CoreDNS should be %q", domain)
	return domain
}
func getCorefile(ctx context.Context, c client.Client) (string, error) {
	var err error
	coreDNSDeploy, err := getCoreDNSDeployment(ctx, c)
	if err != nil && !apierrors.IsNotFound(err) {
		return "", err
	}
	if apierrors.IsNotFound(err) {
		return "", nil
	}
	var configMapName string
	coreDNSDeploySpec := coreDNSDeploy.Spec.Template.Spec
	for _, volume := range coreDNSDeploySpec.Volumes {
		if volume.Name == "config-volume" && volume.ConfigMap != nil {
			configMapName = volume.ConfigMap.Name
		}
	}
	coreDNSConfigMap, err := getCoreDNSConfigMap(ctx, c, configMapName)
	if err != nil && !apierrors.IsNotFound(err) {
		return "", err
	}
	if apierrors.IsNotFound(err) {
		return "", nil
	}
	corefile, ok := coreDNSConfigMap.Data["Corefile"]
	if !ok {
		return "", errors.New("unable to find the CoreDNS Corefile data")
	}
	return corefile, nil
}