aboutsummaryrefslogtreecommitdiff
path: root/plugin/kubernetes/parse.go
blob: da8392cbe6f5457e1a2afbd30cec89d25cf9f2db (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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package kubernetes

import (
	"github.com/coredns/coredns/plugin/pkg/dnsutil"
	"github.com/miekg/dns"
)

type recordRequest struct {
	// The named port from the kubernetes DNS spec, this is the service part (think _https) from a well formed
	// SRV record.
	port string
	// The protocol is usually _udp or _tcp (if set), and comes from the protocol part of a well formed
	// SRV record.
	protocol string
	endpoint string
	// The servicename used in Kubernetes.
	service string
	// The namespace used in Kubernetes.
	namespace string
	// A each name can be for a pod or a service, here we track what we've seen, either "pod" or "service".
	podOrSvc string
}

// parseRequest parses the qname to find all the elements we need for querying k8s. Anything
// that is not parsed will have the wildcard "*" value (except r.endpoint).
// Potential underscores are stripped from _port and _protocol.
func parseRequest(name, zone string) (r recordRequest, err error) {
	// 3 Possible cases:
	// 1. _port._protocol.service.namespace.pod|svc.zone
	// 2. (endpoint): endpoint.service.namespace.pod|svc.zone
	// 3. (service): service.namespace.pod|svc.zone

	base, _ := dnsutil.TrimZone(name, zone)
	// return NODATA for apex queries
	if base == "" || base == Svc || base == Pod {
		return r, nil
	}
	segs := dns.SplitDomainName(base)

	r.port = "*"
	r.protocol = "*"
	// for r.name, r.namespace and r.endpoint, we need to know if they have been set or not...
	// For endpoint: if empty we should skip the endpoint check in k.get(). Hence we cannot set if to "*".
	// For name: myns.svc.cluster.local != *.myns.svc.cluster.local
	// For namespace: svc.cluster.local != *.svc.cluster.local

	// start at the right and fill out recordRequest with the bits we find, so we look for
	// pod|svc.namespace.service and then either
	// * endpoint
	// *_protocol._port

	last := len(segs) - 1
	if last < 0 {
		return r, nil
	}
	r.podOrSvc = segs[last]
	if r.podOrSvc != Pod && r.podOrSvc != Svc {
		return r, errInvalidRequest
	}
	last--
	if last < 0 {
		return r, nil
	}

	r.namespace = segs[last]
	last--
	if last < 0 {
		return r, nil
	}

	r.service = segs[last]
	last--
	if last < 0 {
		return r, nil
	}

	// Because of ambiguity we check the labels left: 1: an endpoint. 2: port and protocol.
	// Anything else is a query that is too long to answer and can safely be delegated to return an nxdomain.
	switch last {

	case 0: // endpoint only
		r.endpoint = segs[last]
	case 1: // service and port
		r.protocol = stripUnderscore(segs[last])
		r.port = stripUnderscore(segs[last-1])

	default: // too long
		return r, errInvalidRequest
	}

	return r, nil
}

// stripUnderscore removes a prefixed underscore from s.
func stripUnderscore(s string) string {
	if s[0] != '_' {
		return s
	}
	return s[1:]
}

// String return a string representation of r, it just returns all fields concatenated with dots.
// This is mostly used in tests.
func (r recordRequest) String() string {
	s := r.port
	s += "." + r.protocol
	s += "." + r.endpoint
	s += "." + r.service
	s += "." + r.namespace
	s += "." + r.podOrSvc
	return s
}
d> 2022-07-12docs: remove double v of version (#601)Gravatar pnxdxt 1-1/+1 2022-07-12docs(various): `.md` readability improvements (#597)Gravatar Ryan Russell 3-3/+3 2022-07-12docs: builtin -> built-in (#585)Gravatar Evan Boehs 1-3/+3 2022-07-12docs: Added note of AVX2 requirement and mentioned Intel SDE workaround in RE...Gravatar deijjji303 1-0/+3 2022-07-12chore(docs): add comma (#580)Gravatar josesilveiraa 1-1/+1 2022-07-12docs: Fix typo (#599)Gravatar Rudraksh Karpe 1-1/+1 2022-07-11Broken links?origin/mainGravatar Jarred Sumner 2-194/+84 2022-07-11Fixed broken links on the Bun.serve benchmark (#590)Gravatar B E C H 1-3/+3 2022-07-11Update javascript.zigbun-v0.1.3Gravatar Jarred Sumner 1-2/+0 2022-07-11woopsGravatar Jarred Sumner 1-1/+1 2022-07-11[integration test scripts] `bun` -> `bun dev`Gravatar Jarred Sumner 3-4/+4 2022-07-11Revert changes to integration test scripts that break macOS testsGravatar Jarred Sumner 8-98/+58 2022-07-11Update build-idGravatar Jarred Sumner 1-1/+1 2022-07-11feat(core): implement web interaction APIs (#528)Gravatar Carter Snook 3-7/+358 2022-07-11[resolver] Stripe `file://` from import pathsGravatar Jarred Sumner 2-5/+17 2022-07-11Fixes https://github.com/oven-sh/bun/issues/195Gravatar Jarred Sumner 3-3/+70 2022-07-11Revert "Fix: NotSameFileSystem at clonefile (#546)" (#581)Gravatar Jarred Sumner 1-1/+1 2022-07-11feat(types): Add types for node modules and various fixing (#470)Gravatar Snazzah 24-60/+23551 2022-07-11fix: environment typo (#568)Gravatar Carlos Gabriel Vilas Novas Soares 1-3/+3 2022-07-11Cleanup discord-interactions readme (#451)Gravatar CharlieS1103 1-1/+1 2022-07-11Fix: NotSameFileSystem at clonefile (#546)Gravatar Aditya Gupta 1-1/+1 2022-07-11Updated typo in example (#573)Gravatar rml1997 1-2/+2 2022-07-11Fixes https://github.com/oven-sh/bun/issues/561Gravatar Jarred Sumner 3-14/+56 2022-07-11[js] Include TypedArray type name for empty arraysGravatar Jarred Sumner 1-16/+16 2022-07-11[js] When `console.log` typed arrays, include the type name and limit printed...Gravatar Jarred Sumner 1-12/+17 2022-07-11Fixes https://github.com/oven-sh/bun/issues/229Gravatar Jarred Sumner 4-6/+56 2022-07-11Bump zig buildGravatar Jarred Sumner 1-1/+1 2022-07-11Move symbolGravatar Jarred Sumner 1-3/+7 2022-07-11Run prettierGravatar Jarred Sumner 1-10/+25 2022-07-11Fix broken buildGravatar Jarred Sumner 1-1/+1 2022-07-11[bun wiptest] Fix copyGravatar Jarred Sumner 1-1/+1 2022-07-11add depd browser polyfill (#517)Gravatar evan 2-0/+89 2022-07-11Fix macOS build (#525)Gravatar thislooksfun 3-94/+103 2022-07-10Update GitHub URL to match new repo URL (#547)Gravatar Aurora Luna Takemi 22-47/+47 2022-07-10chore(feature-request): change feature request to enhancementGravatar Hyro 1-1/+1 2022-07-10chore(api-ref-docs): change doc to documentationGravatar Hyro 1-1/+1 2022-07-10chore(bug-report): add label bug, need reproGravatar Hyro 1-0/+1