-
Notifications
You must be signed in to change notification settings - Fork 170
/
dns.go
208 lines (170 loc) · 5.74 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
package dns
// Copyright (c) Microsoft Corporation.
// Licensed under the Apache License 2.0.
import (
"context"
"fmt"
"net/http"
"strings"
mgmtdns "github.com/Azure/azure-sdk-for-go/services/dns/mgmt/2018-05-01/dns"
"github.com/Azure/go-autorest/autorest"
"github.com/Azure/go-autorest/autorest/to"
"github.com/Azure/ARO-RP/pkg/api"
"github.com/Azure/ARO-RP/pkg/env"
"github.com/Azure/ARO-RP/pkg/util/azureclient/mgmt/dns"
)
const (
resourceID = "resourceId"
)
type Manager interface {
Create(context.Context, *api.OpenShiftCluster) error
Update(context.Context, *api.OpenShiftCluster, string) error
CreateOrUpdateRouter(context.Context, *api.OpenShiftCluster, string) error
Delete(context.Context, *api.OpenShiftCluster) error
}
type manager struct {
env env.Interface
recordsets dns.RecordSetsClient
}
func NewManager(env env.Interface, localFPAuthorizer autorest.Authorizer) Manager {
return &manager{
env: env,
recordsets: dns.NewRecordSetsClient(env.Environment(), env.SubscriptionID(), localFPAuthorizer),
}
}
func (m *manager) Create(ctx context.Context, oc *api.OpenShiftCluster) error {
prefix, err := m.managedDomainPrefix(oc.Properties.ClusterProfile.Domain)
if err != nil || prefix == "" {
return err
}
rs, err := m.recordsets.Get(ctx, m.env.ResourceGroup(), m.env.Domain(), "api."+prefix, mgmtdns.A)
if err == nil {
if rs.Metadata[resourceID] == nil || *rs.Metadata[resourceID] != oc.ID {
return api.NewCloudError(http.StatusBadRequest, api.CloudErrorCodeDuplicateDomain, "", "The provided domain '%s' is already in use by a cluster.", oc.Properties.ClusterProfile.Domain)
}
return nil
}
if detailedErr, ok := err.(autorest.DetailedError); ok &&
detailedErr.StatusCode == http.StatusNotFound {
err = nil
}
if err != nil {
return err
}
return m.createOrUpdate(ctx, oc, "", "", "*")
}
func (m *manager) Update(ctx context.Context, oc *api.OpenShiftCluster, ip string) error {
prefix, err := m.managedDomainPrefix(oc.Properties.ClusterProfile.Domain)
if err != nil || prefix == "" {
return err
}
rs, err := m.recordsets.Get(ctx, m.env.ResourceGroup(), m.env.Domain(), "api."+prefix, mgmtdns.A)
if err != nil {
return err
}
if rs.Metadata[resourceID] == nil || *rs.Metadata[resourceID] != oc.ID {
return fmt.Errorf("recordset %q already registered", "api."+prefix)
}
return m.createOrUpdate(ctx, oc, ip, *rs.Etag, "")
}
func (m *manager) CreateOrUpdateRouter(ctx context.Context, oc *api.OpenShiftCluster, routerIP string) error {
prefix, err := m.managedDomainPrefix(oc.Properties.ClusterProfile.Domain)
if err != nil || prefix == "" {
return err
}
var isCreate bool
rs, err := m.recordsets.Get(ctx, m.env.ResourceGroup(), m.env.Domain(), "*.apps."+prefix, mgmtdns.A)
if detailedErr, ok := err.(autorest.DetailedError); ok &&
detailedErr.StatusCode == http.StatusNotFound {
isCreate = true
}
// If record exists and routerIP already match - skip CreateOrUpdate
if err == nil && !isCreate {
for _, a := range *rs.ARecords {
if *a.Ipv4Address == routerIP {
return nil
}
}
}
_, err = m.recordsets.CreateOrUpdate(ctx, m.env.ResourceGroup(), m.env.Domain(), "*.apps."+prefix, mgmtdns.A, mgmtdns.RecordSet{
RecordSetProperties: &mgmtdns.RecordSetProperties{
TTL: to.Int64Ptr(300),
ARecords: &[]mgmtdns.ARecord{
{
Ipv4Address: &routerIP,
},
},
},
}, "", "")
return err
}
func (m *manager) Delete(ctx context.Context, oc *api.OpenShiftCluster) error {
prefix, err := m.managedDomainPrefix(oc.Properties.ClusterProfile.Domain)
if err != nil || prefix == "" {
return err
}
rs, err := m.recordsets.Get(ctx, m.env.ResourceGroup(), m.env.Domain(), "api."+prefix, mgmtdns.A)
if detailedErr, ok := err.(autorest.DetailedError); ok &&
detailedErr.StatusCode == http.StatusNotFound {
return nil
}
if err != nil {
return err
}
if rs.Metadata[resourceID] == nil || *rs.Metadata[resourceID] != oc.ID {
return nil
}
_, err = m.recordsets.Delete(ctx, m.env.ResourceGroup(), m.env.Domain(), "*.apps."+prefix, mgmtdns.A, "")
if err != nil {
return err
}
_, err = m.recordsets.Delete(ctx, m.env.ResourceGroup(), m.env.Domain(), "api."+prefix, mgmtdns.A, *rs.Etag)
return err
}
func (m *manager) createOrUpdate(ctx context.Context, oc *api.OpenShiftCluster, ip, ifMatch, ifNoneMatch string) error {
prefix, err := m.managedDomainPrefix(oc.Properties.ClusterProfile.Domain)
if err != nil || prefix == "" {
return err
}
rs := mgmtdns.RecordSet{
RecordSetProperties: &mgmtdns.RecordSetProperties{
Metadata: map[string]*string{
resourceID: &oc.ID,
},
TTL: to.Int64Ptr(300),
},
}
if ip != "" {
rs.ARecords = &[]mgmtdns.ARecord{
{
Ipv4Address: &ip,
},
}
}
_, err = m.recordsets.CreateOrUpdate(ctx, m.env.ResourceGroup(), m.env.Domain(), "api."+prefix, mgmtdns.A, rs, ifMatch, ifNoneMatch)
return err
}
func (m *manager) managedDomainPrefix(clusterDomain string) (string, error) {
managedDomain, err := ManagedDomain(m.env, clusterDomain)
if err != nil || managedDomain == "" {
return "", err
}
return managedDomain[:strings.IndexByte(managedDomain, '.')], nil
}
// ManagedDomain returns the fully qualified domain of a cluster if we manage
// it. If we don't, it returns the empty string. We manage only domains of the
// form "foo.$LOCATION.aroapp.io" and "foo" (we consider this a short form of
// the former).
func ManagedDomain(env env.Interface, domain string) (string, error) {
if domain == "" ||
strings.HasPrefix(domain, ".") ||
strings.HasSuffix(domain, ".") {
// belt and braces: validation should already prevent this
return "", fmt.Errorf("invalid domain %q", domain)
}
domain = strings.TrimSuffix(domain, "."+env.Domain())
if strings.ContainsRune(domain, '.') {
return "", nil
}
return domain + "." + env.Domain(), nil
}