-
Notifications
You must be signed in to change notification settings - Fork 66
/
inmemory.go
177 lines (143 loc) · 3.7 KB
/
inmemory.go
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
// SPDX-FileCopyrightText: SAP SE or an SAP affiliate company and Gardener contributors
//
// SPDX-License-Identifier: Apache-2.0
package provider
import (
"fmt"
"sync"
"github.com/gardener/external-dns-management/pkg/dns"
)
type zonedata struct {
zone DNSHostedZone
dnssets dns.DNSSets
}
type InMemory struct {
lock sync.Mutex
zones map[dns.ZoneID]zonedata
}
func NewInMemory() *InMemory {
return &InMemory{zones: map[dns.ZoneID]zonedata{}}
}
func (m *InMemory) GetZones() DNSHostedZones {
m.lock.Lock()
defer m.lock.Unlock()
zones := DNSHostedZones{}
for _, z := range m.zones {
zones = append(zones, z.zone)
}
return zones
}
func (m *InMemory) FindHostedZone(zoneid dns.ZoneID) DNSHostedZone {
m.lock.Lock()
defer m.lock.Unlock()
data, ok := m.zones[zoneid]
if !ok {
return nil
}
return data.zone
}
func (m *InMemory) CloneZoneState(zone DNSHostedZone) (DNSZoneState, error) {
m.lock.Lock()
defer m.lock.Unlock()
data, ok := m.zones[zone.Id()]
if !ok {
return nil, fmt.Errorf("DNSZone %s not hosted", zone.Id())
}
dnssets := data.dnssets.Clone()
return NewDNSZoneState(dnssets), nil
}
func (m *InMemory) SetZone(zone DNSHostedZone, zoneState DNSZoneState) {
clone := zoneState.GetDNSSets().Clone()
m.lock.Lock()
defer m.lock.Unlock()
m.zones[zone.Id()] = zonedata{zone: zone, dnssets: clone}
}
func (m *InMemory) DeleteZone(zoneID dns.ZoneID) {
m.lock.Lock()
defer m.lock.Unlock()
delete(m.zones, zoneID)
}
func (m *InMemory) AddZone(zone DNSHostedZone) bool {
m.lock.Lock()
defer m.lock.Unlock()
_, ok := m.zones[zone.Id()]
if ok {
return false
}
m.zones[zone.Id()] = zonedata{zone: zone, dnssets: dns.DNSSets{}}
return true
}
func (m *InMemory) Apply(zoneID dns.ZoneID, request *ChangeRequest, metrics Metrics) error {
m.lock.Lock()
defer m.lock.Unlock()
data, ok := m.zones[zoneID]
if !ok {
return fmt.Errorf("DNSZone %s not hosted", zoneID)
}
name, rset := buildRecordSet(request)
switch request.Action {
case R_CREATE, R_UPDATE:
data.dnssets.AddRecordSet(name, request.Addition.RoutingPolicy, rset)
metrics.AddZoneRequests(zoneID.ID, M_UPDATERECORDS, 1)
case R_DELETE:
data.dnssets.RemoveRecordSet(name, rset.Type)
metrics.AddZoneRequests(zoneID.ID, M_DELETERECORDS, 1)
}
return nil
}
func buildRecordSet(req *ChangeRequest) (dns.DNSSetName, *dns.RecordSet) {
var dnsset *dns.DNSSet
switch req.Action {
case R_CREATE, R_UPDATE:
dnsset = req.Addition
case R_DELETE:
dnsset = req.Deletion
}
return dnsset.Name, dnsset.Sets[req.Type]
}
type DumpDNSHostedZone struct {
ProviderType string
Key string
Id string
Domain string
ForwardedDomains []string
}
type ZoneDump struct {
HostedZone DumpDNSHostedZone
DNSSets dns.DNSSets
}
type FullDump struct {
InMemory map[dns.ZoneID]*ZoneDump
}
func (m *InMemory) BuildFullDump() *FullDump {
m.lock.Lock()
defer m.lock.Unlock()
all := FullDump{InMemory: map[dns.ZoneID]*ZoneDump{}}
for zoneId := range m.zones {
all.InMemory[zoneId] = m.buildZoneDump(zoneId)
}
return &all
}
func (m *InMemory) buildZoneDump(zoneId dns.ZoneID) *ZoneDump {
data, ok := m.zones[zoneId]
if !ok {
return nil
}
hostedZone := DumpDNSHostedZone{
ProviderType: data.zone.Id().ProviderType, Id: data.zone.Id().ID, Domain: data.zone.Domain(),
Key: data.zone.Key(), ForwardedDomains: data.zone.ForwardedDomains(),
}
return &ZoneDump{HostedZone: hostedZone, DNSSets: data.dnssets}
}
/*
func (m *InMemory) ServeHTTP(w http.ResponseWriter, r *http.Request) {
fullDump := m.BuildFullDump()
js, err := json.Marshal(*fullDump)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
w.Write(js)
}
*/