/
dns.go
276 lines (230 loc) · 7.67 KB
/
dns.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
package clients
import (
"context"
"fmt"
"github.com/Azure/azure-provider-external-dns-e2e/logger"
"github.com/Azure/azure-sdk-for-go/sdk/azcore/to"
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/dns/armdns"
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/privatedns/armprivatedns"
"github.com/Azure/go-autorest/autorest/azure"
)
type zone struct {
name string
subscriptionId string
resourceGroup string
id string
nameservers []string
}
type privateZone struct {
name string
subscriptionId string
resourceGroup string
id string
}
// ZoneOpt specifies what kind of zone to create
type ZoneOpt func(z *armdns.Zone) error
// PrivateZoneOpt specifies what kind of private zone to create
type PrivateZoneOpt func(z *armprivatedns.PrivateZone) error
func LoadZone(id azure.Resource, nameservers []string) *zone {
return &zone{
id: id.String(),
name: id.ResourceName,
subscriptionId: id.SubscriptionID,
resourceGroup: id.ResourceGroup,
nameservers: nameservers,
}
}
func NewZone(ctx context.Context, subscriptionId, resourceGroup, name string, zoneOpts ...ZoneOpt) (*zone, error) {
name = nonAlphanumericRegex.ReplaceAllString(name, "")
name = name + ".com"
lgr := logger.FromContext(ctx).With("name", name, "subscriptionId", subscriptionId, "resourceGroup", resourceGroup)
ctx = logger.WithContext(ctx, lgr)
lgr.Info("starting to create zone")
defer lgr.Info("finished creating zone")
cred, err := getAzCred()
if err != nil {
return nil, fmt.Errorf("getting az credentials: %w", err)
}
factory, err := armdns.NewClientFactory(subscriptionId, cred, nil)
if err != nil {
return nil, fmt.Errorf("creating client factory: %w", err)
}
z := &armdns.Zone{
Location: to.Ptr("global"), // https://github.com/Azure/azure-cli/issues/6052 this must be global because DNS zones are global resources
Name: to.Ptr(name),
}
for _, opt := range zoneOpts {
if err := opt(z); err != nil {
return nil, fmt.Errorf("applying zone option: %w", err)
}
}
resp, err := factory.NewZonesClient().CreateOrUpdate(ctx, resourceGroup, name, *z, nil)
if err != nil {
return nil, fmt.Errorf("creating zone: %w", err)
}
// guard against two things that should be impossible
if resp.Properties == nil {
return nil, fmt.Errorf("zone properties are nil")
}
if resp.Properties.NameServers == nil {
return nil, fmt.Errorf("zone nameservers are nil")
}
if resp.Name == nil {
return nil, fmt.Errorf("zone name is nil")
}
if resp.ID == nil {
return nil, fmt.Errorf("zone id is nil")
}
nameservers := make([]string, len(resp.Properties.NameServers))
for i, ns := range resp.Properties.NameServers {
if ns == nil {
return nil, fmt.Errorf("zone nameserver %d is nil", i)
}
nameservers[i] = *ns
}
return &zone{
name: *resp.Name,
resourceGroup: resourceGroup,
subscriptionId: subscriptionId,
id: *resp.ID,
nameservers: nameservers,
}, nil
}
func (z *zone) GetDnsZone(ctx context.Context) (*armdns.Zone, error) {
lgr := logger.FromContext(ctx).With("name", z.name, "subscriptionId", z.subscriptionId, "resourceGroup", z.resourceGroup)
ctx = logger.WithContext(ctx, lgr)
lgr.Info("starting to get dns")
defer lgr.Info("finished getting dns")
cred, err := getAzCred()
if err != nil {
return nil, fmt.Errorf("getting az credentials: %w", err)
}
client, err := armdns.NewZonesClient(z.subscriptionId, cred, nil)
if err != nil {
return nil, fmt.Errorf("creating client: %w", err)
}
resp, err := client.Get(ctx, z.resourceGroup, z.name, nil)
if err != nil {
return nil, fmt.Errorf("getting dns: %w", err)
}
return &resp.Zone, nil
}
func (z *zone) GetName() string {
return z.name
}
func (z *zone) GetNameservers() []string {
return z.nameservers
}
func (z *zone) GetId() string {
return z.id
}
func LoadPrivateZone(id azure.Resource) *privateZone {
return &privateZone{
id: id.String(),
name: id.ResourceName,
subscriptionId: id.SubscriptionID,
resourceGroup: id.ResourceGroup,
}
}
func NewPrivateZone(ctx context.Context, subscriptionId, resourceGroup, name string, opts ...PrivateZoneOpt) (*privateZone, error) {
name = nonAlphanumericRegex.ReplaceAllString(name, "")
name = name + ".com"
lgr := logger.FromContext(ctx).With("name", name, "subscriptionId", subscriptionId, "resourceGroup", resourceGroup)
ctx = logger.WithContext(ctx, lgr)
lgr.Info("starting to create private zone")
defer lgr.Info("finished creating private zone")
cred, err := getAzCred()
if err != nil {
return nil, fmt.Errorf("getting az credentials: %w", err)
}
client, err := armprivatedns.NewPrivateZonesClient(subscriptionId, cred, nil)
if err != nil {
return nil, fmt.Errorf("creating client: %w", err)
}
pz := armprivatedns.PrivateZone{
Location: to.Ptr("global"),
Name: to.Ptr(name),
}
for _, opt := range opts {
if err := opt(&pz); err != nil {
return nil, fmt.Errorf("applying private zone option: %w", err)
}
}
poller, err := client.BeginCreateOrUpdate(ctx, resourceGroup, name, pz, nil)
if err != nil {
return nil, fmt.Errorf("creating private zone: %w", err)
}
result, err := poller.PollUntilDone(ctx, nil)
if err != nil {
return nil, fmt.Errorf("creating private zone: %w", err)
}
// guard against things that should be impossible
if result.PrivateZone.Name == nil {
return nil, fmt.Errorf("private zone name is nil")
}
if result.ID == nil {
return nil, fmt.Errorf("private zone id is nil")
}
return &privateZone{
name: *result.PrivateZone.Name,
subscriptionId: subscriptionId,
resourceGroup: resourceGroup,
id: *result.ID,
}, nil
}
func (p *privateZone) GetName() string {
return p.name
}
func (p *privateZone) GetDnsZone(ctx context.Context) (*armprivatedns.PrivateZone, error) {
lgr := logger.FromContext(ctx).With("name", p.name, "subscriptionId", p.subscriptionId, "resourceGroup", p.resourceGroup)
ctx = logger.WithContext(ctx, lgr)
lgr.Info("starting to get private dns")
defer lgr.Info("finished getting private dns")
cred, err := getAzCred()
if err != nil {
return nil, fmt.Errorf("getting az credentials: %w", err)
}
client, err := armprivatedns.NewPrivateZonesClient(p.subscriptionId, cred, nil)
if err != nil {
return nil, fmt.Errorf("creating client: %w", err)
}
resp, err := client.Get(ctx, p.resourceGroup, p.name, nil)
if err != nil {
return nil, fmt.Errorf("getting dns: %w", err)
}
return &resp.PrivateZone, nil
}
func (p *privateZone) LinkVnet(ctx context.Context, linkName, vnetId string) error {
linkName = nonAlphanumericRegex.ReplaceAllString(linkName, "")
linkName = truncate(linkName, 80)
lgr := logger.FromContext(ctx).With("name", p.name, "subscriptionId", p.subscriptionId, "resourceGroup", p.resourceGroup, "linkName", linkName, "vnetId", vnetId)
ctx = logger.WithContext(ctx, lgr)
lgr.Info("starting to link vnet")
defer lgr.Info("finished linking vnet")
cred, err := getAzCred()
if err != nil {
return fmt.Errorf("getting az credentials: %w", err)
}
factory, err := armprivatedns.NewClientFactory(p.subscriptionId, cred, nil)
if err != nil {
return fmt.Errorf("creating client factory: %w", err)
}
l := armprivatedns.VirtualNetworkLink{
Location: to.Ptr("global"),
Properties: &armprivatedns.VirtualNetworkLinkProperties{
RegistrationEnabled: to.Ptr(false),
VirtualNetwork: &armprivatedns.SubResource{
ID: to.Ptr(vnetId),
},
},
Name: to.Ptr(linkName),
}
_, err = factory.NewVirtualNetworkLinksClient().BeginCreateOrUpdate(ctx, p.resourceGroup, p.name, linkName, l, nil)
if err != nil {
return fmt.Errorf("creating virtual network link: %w", err)
}
return nil
}
func (p *privateZone) GetId() string {
return p.id
}