From c1c98924c3733b853115c62adc1a2b5978ae2df3 Mon Sep 17 00:00:00 2001 From: Miek Gieben Date: Fri, 14 Dec 2018 09:41:51 +0000 Subject: Add new plugin: external - resolve k8s ingress and LB address with external names (#2379) * Add new plugin: external This plugin works in conjunction with the kubernetes plugin and exports ingress and LB addresses as DNS records. It bypasses backend.go and backend_lookup.go flow because it is not needed. README, tests are implemented. The tests only exercise the unit tests, this has not been tested in any ci. Signed-off-by: Miek Gieben * Rename to k8s_external Signed-off-by: Miek Gieben * go gen Signed-off-by: Miek Gieben --- plugin/k8s_external/msg_to_dns.go | 148 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 148 insertions(+) create mode 100644 plugin/k8s_external/msg_to_dns.go (limited to 'plugin/k8s_external/msg_to_dns.go') diff --git a/plugin/k8s_external/msg_to_dns.go b/plugin/k8s_external/msg_to_dns.go new file mode 100644 index 000000000..d09229d48 --- /dev/null +++ b/plugin/k8s_external/msg_to_dns.go @@ -0,0 +1,148 @@ +package external + +import ( + "math" + + "github.com/coredns/coredns/plugin/etcd/msg" + "github.com/coredns/coredns/request" + + "github.com/miekg/dns" +) + +func (e *External) a(services []msg.Service, state request.Request) (records []dns.RR) { + dup := make(map[string]struct{}) + + for _, s := range services { + + what, ip := s.HostType() + + switch what { + case dns.TypeCNAME: + // can't happen + + case dns.TypeA: + if _, ok := dup[s.Host]; !ok { + dup[s.Host] = struct{}{} + rr := s.NewA(state.QName(), ip) + rr.Hdr.Ttl = e.ttl + records = append(records, rr) + } + + case dns.TypeAAAA: + // nada + } + } + return records +} + +func (e *External) aaaa(services []msg.Service, state request.Request) (records []dns.RR) { + dup := make(map[string]struct{}) + + for _, s := range services { + + what, ip := s.HostType() + + switch what { + case dns.TypeCNAME: + // can't happen + + case dns.TypeA: + // nada + + case dns.TypeAAAA: + if _, ok := dup[s.Host]; !ok { + dup[s.Host] = struct{}{} + rr := s.NewAAAA(state.QName(), ip) + rr.Hdr.Ttl = e.ttl + records = append(records, rr) + } + } + } + return records +} + +func (e *External) srv(services []msg.Service, state request.Request) (records, extra []dns.RR) { + dup := make(map[item]struct{}) + + // Looping twice to get the right weight vs priority. This might break because we may drop duplicate SRV records latter on. + w := make(map[int]int) + for _, s := range services { + weight := 100 + if s.Weight != 0 { + weight = s.Weight + } + if _, ok := w[s.Priority]; !ok { + w[s.Priority] = weight + continue + } + w[s.Priority] += weight + } + for _, s := range services { + // Don't add the entry if the port is -1 (invalid). The kubernetes plugin uses port -1 when a service/endpoint + // does not have any declared ports. + if s.Port == -1 { + continue + } + w1 := 100.0 / float64(w[s.Priority]) + if s.Weight == 0 { + w1 *= 100 + } else { + w1 *= float64(s.Weight) + } + weight := uint16(math.Floor(w1)) + + what, ip := s.HostType() + + switch what { + case dns.TypeCNAME: + // can't happen + + case dns.TypeA, dns.TypeAAAA: + addr := s.Host + s.Host = msg.Domain(s.Key) + srv := s.NewSRV(state.QName(), weight) + + if ok := isDuplicate(dup, srv.Target, "", srv.Port); !ok { + records = append(records, srv) + } + + if ok := isDuplicate(dup, srv.Target, addr, 0); !ok { + hdr := dns.RR_Header{Name: srv.Target, Rrtype: what, Class: dns.ClassINET, Ttl: e.ttl} + + switch what { + case dns.TypeA: + extra = append(extra, &dns.A{Hdr: hdr, A: ip}) + case dns.TypeAAAA: + extra = append(extra, &dns.AAAA{Hdr: hdr, AAAA: ip}) + } + } + } + } + return records, extra +} + +// not sure if this is even needed. + +// item holds records. +type item struct { + name string // name of the record (either owner or something else unique). + port uint16 // port of the record (used for address records, A and AAAA). + addr string // address of the record (A and AAAA). +} + +// isDuplicate uses m to see if the combo (name, addr, port) already exists. If it does +// not exist already IsDuplicate will also add the record to the map. +func isDuplicate(m map[item]struct{}, name, addr string, port uint16) bool { + if addr != "" { + _, ok := m[item{name, 0, addr}] + if !ok { + m[item{name, 0, addr}] = struct{}{} + } + return ok + } + _, ok := m[item{name, port, ""}] + if !ok { + m[item{name, port, ""}] = struct{}{} + } + return ok +} -- cgit v1.2.3 -string'>dylan/resolve-rope-in-string Unnamed repository; edit this file 'description' to name the repository.
aboutsummaryrefslogtreecommitdiff
AgeCommit message (Expand)AuthorFilesLines
2023-07-30Fixes #3884Gravatar Jarred Sumner 2-42/+7
2023-07-30BumpGravatar Jarred Sumner 1-1/+1
2023-07-30remove unnecessary moveGravatar Jarred Sumner 1-1/+1
2023-07-30Fix memory leak (#3887)Gravatar Jarred Sumner 10-87/+191
2023-07-30Fix coredump when reading an empty file(`node:stream:createReadStream`) (#3882)Gravatar Ai Hoshino 2-0/+30
2023-07-30Improv. (#3885)Gravatar Tiramify (A.K. Daniel) 3-37/+38
2023-07-29Make `bun:jsc` memoryUsage more accurate (#3876)Gravatar Jarred Sumner 6-68/+61
2023-07-29docs: fixed small errors (#3879)Gravatar 0xflotus 4-4/+4
2023-07-29fix: small error (#3878)Gravatar 0xflotus 1-1/+1
2023-07-29typo spawn.md (#3875)Gravatar Jhorman Tito 1-1/+1
2023-07-28Update nodejs-apis.mdGravatar Jarred Sumner 1-4/+4
2023-07-28Update nodejs-apis.mdGravatar Jarred Sumner 1-5/+5
2023-07-28Defer task destructionbun-v0.7.1Gravatar Jarred Sumner 1-1/+17
2023-07-28Stat largefile test (#3870)Gravatar Jarred Sumner 1-0/+22
2023-07-28Ignore when printing to stdout errors (#3869)Gravatar Jarred Sumner 1-2/+4
2023-07-28Fix bug with `/path/to/absolute/bun.lockb`Gravatar Jarred Sumner 1-10/+13
2023-07-28markBindingGravatar Dylan Conway 1-0/+1
2023-07-28optional parameterGravatar Dylan Conway 1-3/+3
2023-07-28Fixes #3868Gravatar Jarred Sumner 1-2/+3
2023-07-28Fix assertion failure and possible infinite loop when printing as yarn lock f...Gravatar Jarred Sumner 2-3/+16
2023-07-28Mark broken test as todoGravatar Jarred Sumner 2-1/+5
2023-07-28fix types and add message channel/port gc testGravatar Dylan Conway 2-62/+18
2023-07-28`MessageChannel` and `MessagePort` (#3860)Gravatar Dylan Conway 57-247/+3752
2023-07-28mark tests as todoGravatar Jarred Sumner 2-81/+75
2023-07-28add fork to child_process (#3851)Gravatar Vlad Sirenko 7-19/+446
2023-07-28feat(bun/test): Impl. expect().pass() & expect().fail() (#3843)Gravatar Tiramify (A.K. Daniel) 6-5/+212
2023-07-28fix the chunk boundary (`node:stream:createReadStream`) (#3853)Gravatar Ai Hoshino 3-8/+90
2023-07-28Support file: URLs in `fetch` (#3858)Gravatar Jarred Sumner 6-183/+281
2023-07-28fix(tls) exposes native canonicalizeIP and fix rootCertificates (#3866)Gravatar Ciro Spaciari 7-29/+125
2023-07-28Fixes #3795 (#3856)Gravatar Jarred Sumner 11-6/+140
2023-07-28Add todoGravatar Jarred Sumner 2-0/+8
2023-07-28Update pull_request_template.mdGravatar Jarred Sumner 1-1/+1
2023-07-28Update pull_request_template.mdGravatar Jarred Sumner 1-11/+16
2023-07-27Fix bug with // @bun annotation in main thread (#3855)Gravatar Jarred Sumner 4-2/+53
2023-07-27Add `Bun.isMainThread`Gravatar Jarred Sumner 7-3/+48
2023-07-27Uncomment testGravatar Jarred Sumner 1-1/+1
2023-07-27Resolve watch directories outside main thread + async iterator and symlink fi...Gravatar Ciro Spaciari 8-197/+523
2023-07-27Update pull_request_template.mdGravatar Jarred Sumner 1-1/+1
2023-07-27Update pull_request_template.mdGravatar Jarred Sumner 1-1/+1
2023-07-27Update pull_request_template.mdGravatar Jarred Sumner 1-1/+1
2023-07-27Update pull_request_template.mdGravatar Jarred Sumner 1-2/+2
2023-07-27Update pull_request_template.mdGravatar Jarred Sumner 1-0/+4
2023-07-27Update pull_request_template.mdGravatar Jarred Sumner 1-5/+11