aboutsummaryrefslogtreecommitdiff
path: root/plugin/kubernetes/external.go
blob: 91a8a2ed1597b57f4c7a97d6f7e4ca8ff9eaf620 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package kubernetes

import (
	"strings"

	"github.com/coredns/coredns/plugin/etcd/msg"
	"github.com/coredns/coredns/plugin/kubernetes/object"
	"github.com/coredns/coredns/plugin/pkg/dnsutil"
	"github.com/coredns/coredns/request"

	"github.com/miekg/dns"
)

// External implements the ExternalFunc call from the external plugin.
// It returns any services matching in the services' ExternalIPs.
func (k *Kubernetes) External(state request.Request) ([]msg.Service, int) {
	base, _ := dnsutil.TrimZone(state.Name(), state.Zone)

	segs := dns.SplitDomainName(base)
	last := len(segs) - 1
	if last < 0 {
		return nil, dns.RcodeServerFailure
	}
	// We dealing with a fairly normal domain name here, but; we still need to have the service
	// and the namespace:
	// service.namespace.<base>
	//
	// for service (and SRV) you can also say _tcp, and port (i.e. _http), we need those be picked
	// up, unless they are not specified, then we use an internal wildcard.
	port := "*"
	protocol := "*"
	namespace := segs[last]
	if !k.namespaceExposed(namespace) {
		return nil, dns.RcodeNameError
	}

	last--
	if last < 0 {
		return nil, dns.RcodeSuccess
	}

	service := segs[last]
	last--
	if last == 1 {
		protocol = stripUnderscore(segs[last])
		port = stripUnderscore(segs[last-1])
		last -= 2
	}

	if last != -1 {
		// too long
		return nil, dns.RcodeNameError
	}

	idx := object.ServiceKey(service, namespace)
	serviceList := k.APIConn.SvcIndex(idx)

	services := []msg.Service{}
	zonePath := msg.Path(state.Zone, coredns)
	rcode := dns.RcodeNameError

	for _, svc := range serviceList {
		if namespace != svc.Namespace {
			continue
		}
		if service != svc.Name {
			continue
		}

		for _, ip := range svc.ExternalIPs {
			for _, p := range svc.Ports {
				if !(match(port, p.Name) && match(protocol, string(p.Protocol))) {
					continue
				}
				rcode = dns.RcodeSuccess
				s := msg.Service{Host: ip, Port: int(p.Port), TTL: k.ttl}
				s.Key = strings.Join([]string{zonePath, svc.Namespace, svc.Name}, "/")

				services = append(services, s)
			}
		}
	}
	return services, rcode
}

// ExternalAddress returns the external service address(es) for the CoreDNS service.
func (k *Kubernetes) ExternalAddress(state request.Request) []dns.RR {
	// This is probably wrong, because of all the fallback behavior of k.nsAddr, i.e. can get
	// an address that isn't reacheable from outside the cluster.
	rrs := []dns.RR{k.nsAddr()}
	return rrs
}
14:01:07 -0800'>2023-02-16Fix #1516 (#2089)Gravatar Justin Whear 3-7/+14 2023-02-16Update globals.d.tsGravatar Jarred Sumner 1-0/+15 2023-02-16Add missing type definitionGravatar Jarred Sumner 1-0/+2 2023-02-16[napi] Fix crash in creating arrays > 8 elements longGravatar Jarred Sumner 1-10/+9 2023-02-16Clarify and clean up macOS build process (#2087)Gravatar Luke Deen Taylor 2-4/+4 2023-02-15Don't crash on null version stringGravatar Jarred Sumner 1-1/+1 2023-02-15Add disabled optimizationGravatar Jarred Sumner 1-0/+51 2023-02-15Add more logging to napiGravatar Jarred Sumner 1-30/+150 2023-02-15Incorrect implementation of `napi_create_threadsafe_function`Gravatar Jarred Sumner 1-21/+43 2023-02-15feat(fetch) AbortSignal (#2019)Gravatar Ciro Spaciari 17-58/+443 2023-02-15fix(webcrypto): fix ed25519 CryptoKey.algorithm (#2082)Gravatar Derrick Farris 2-9/+28 2023-02-15Fix 2063 (#2079)Gravatar Justin Whear 2-2/+11 2023-02-15Make sure we test * in tesconfigGravatar Jarred Sumner 2-1/+3 2023-02-15don't return an error thereGravatar Jarred Sumner 2-1/+3 2023-02-15Fix castGravatar Jarred Sumner 1-15/+17 2023-02-15ensure we allocate for > 6 argumentsGravatar Jarred Sumner 1-6/+13 2023-02-15Update async_hooks.exports.jsGravatar Jarred Sumner 1-2/+2 2023-02-15workaround prisma's usage of `eval("__dirname")`Gravatar Jarred Sumner 1-1/+23 2023-02-15some cleanupGravatar Jarred Sumner 2-15/+9 2023-02-15ED25519 WebCrypto (#1971)Gravatar Jarred Sumner 12-11/+1167 2023-02-14Fix up async_hooks polyfillGravatar Jarred Sumner 2-8/+63 2023-02-14Add temporary polyfill for async_hooksGravatar Jarred Sumner 5-108/+324 2023-02-14:mask: async_hooksGravatar Jarred Sumner 1-0/+4 2023-02-14[install] link network-delayed `.bin` scripts correctly (#2076)Gravatar Alex Lam S.L 3-16/+21 2023-02-14don't break esbuildGravatar Jarred Sumner 7-75/+50 2023-02-14Add workaround for `tls` and `worker_threads`Gravatar Jarred Sumner 3-1/+64 2023-02-14[install] improve `package.json` validation (#2074)Gravatar Alex Lam S.L 6-104/+342 2023-02-14[WIP] fix(node:fs): export `fs.ReadStream` and `fs.WriteStream` (#1798)Gravatar Derrick Farris 4-72/+326 2023-02-14Reject with error when invalid fetch() body (#2047)Gravatar Eric Zhang 2-12/+44 2023-02-13fix(FormData): make String explicit, thanks @dylan-conway (#2065)Gravatar Derrick Farris 1-1/+1 2023-02-13fix(FormData): add string literal operator (#2064)Gravatar Derrick Farris 1-2/+2 2023-02-13Add pretty printer for FormDataGravatar Jarred Sumner 5-1/+101 2023-02-13Add dynamic port assigning to Bun.serve (#2062)Gravatar Michał Warda 3-5/+40 2023-02-13feat(napi): add `napi_get_value_bigint_words` (#2061)Gravatar Derrick Farris 3-0/+44 2023-02-13Fixes https://github.com/oven-sh/bun/issues/1456Gravatar Jarred Sumner 8-1/+148