|
| 1 | +package utils |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "github.com/pkg/errors" |
| 6 | + "strings" |
| 7 | + |
| 8 | + corev1 "k8s.io/api/core/v1" |
| 9 | + apierrors "k8s.io/apimachinery/pkg/api/errors" |
| 10 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 11 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 12 | + "k8s.io/klog" |
| 13 | + "net" |
| 14 | +) |
| 15 | + |
| 16 | +const ( |
| 17 | + coreDNSDomain = "cluster.local" |
| 18 | + coreDNSIP = "10.96.0.10" |
| 19 | +) |
| 20 | + |
| 21 | +// getCoreDNSService fetches the CoreDNS Service |
| 22 | +func getCoreDNSService(ctx context.Context, c client.Client) (*corev1.Service, error) { |
| 23 | + kubernetesService := &corev1.Service{} |
| 24 | + id := client.ObjectKey{Namespace: metav1.NamespaceDefault, Name: "kubernetes"} |
| 25 | + |
| 26 | + // Get the CoreDNS Service |
| 27 | + err := c.Get(ctx, id, kubernetesService) |
| 28 | + |
| 29 | + return kubernetesService, err |
| 30 | +} |
| 31 | + |
| 32 | + |
| 33 | +// FindDNSClusterIP tries to find the Cluster IP to be used by the DNS service |
| 34 | +// It is usually the 10th address to the Kubernetes Service Cluster IP |
| 35 | +// If the Kubernetes Service Cluster IP is not found, we default it to be "10.96.0.10" |
| 36 | +func FindDNSClusterIP(ctx context.Context, c client.Client) (string, error) { |
| 37 | + kubernetesService, err := getCoreDNSService(ctx, c) |
| 38 | + if err != nil && !apierrors.IsNotFound(err) { |
| 39 | + return "", err |
| 40 | + } |
| 41 | + |
| 42 | + if apierrors.IsNotFound(err) { |
| 43 | + // If it cannot determine the Cluster IP, we default it to "10.96.0.10" |
| 44 | + return coreDNSIP, nil |
| 45 | + } |
| 46 | + |
| 47 | + ip := net.ParseIP(kubernetesService.Spec.ClusterIP) |
| 48 | + if ip == nil { |
| 49 | + return "", errors.Errorf("cannot parse kubernetes ClusterIP %q", kubernetesService.Spec.ClusterIP) |
| 50 | + } |
| 51 | + |
| 52 | + // The kubernetes Service ClusterIP is the 1st IP in the Service Subnet. |
| 53 | + // Increment the right-most byte by 9 to get to the 10th address, canonically used for CoreDNS. |
| 54 | + // This works for both IPV4, IPV6, and 16-byte IPV4 addresses. |
| 55 | + ip[len(ip)-1] += 9 |
| 56 | + |
| 57 | + result := ip.String() |
| 58 | + klog.Infof("determined ClusterIP for cluster should be %q", result) |
| 59 | + return result, nil |
| 60 | +} |
| 61 | + |
| 62 | +// GetDNSDomain returns Kubernetes DNS cluster domain |
| 63 | +// If it cannot determine the domain, we default it to "cluster.local" |
| 64 | +// TODO (rajansandeep): find a better way to implement this? |
| 65 | +func GetDNSDomain() string { |
| 66 | + svc := "kubernetes.default.svc" |
| 67 | + |
| 68 | + cname, err := net.LookupCNAME(svc) |
| 69 | + if err != nil { |
| 70 | + // If it cannot determine the domain, we default it to "cluster.local" |
| 71 | + klog.Infof("determined DNS Domain for cluster should be %q", coreDNSDomain) |
| 72 | + return coreDNSDomain |
| 73 | + } |
| 74 | + |
| 75 | + domain := strings.TrimPrefix(cname, svc) |
| 76 | + domain = strings.TrimSuffix(coreDNSDomain, ".") |
| 77 | + |
| 78 | + klog.Infof("determined DNS Domain for CoreDNS should be %q", domain) |
| 79 | + |
| 80 | + return domain |
| 81 | +} |
0 commit comments