generated from dogmatiq/template-go
/
advertiser_ptr.go
193 lines (165 loc) 路 4.34 KB
/
advertiser_ptr.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
package route53provider
import (
"context"
"errors"
"fmt"
"strconv"
"strings"
"time"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/route53/types"
"github.com/dogmatiq/dissolve/dnssd"
"golang.org/x/exp/slices"
)
// ptrTTL is the TTL of PTR records that enumerate service instances.
//
// Normally we'd use each service's TTL for its respective PTR record, but with
// Route 53 the only way to return an unlimited number of PTR records with the
// same name is to put them in the same "record set", which means they all share
// a TTL.
const ptrTTL = 30 * time.Second
func (a *advertiser) findPTR(
ctx context.Context,
inst dnssd.ServiceInstance,
) (types.ResourceRecordSet, bool, error) {
return a.findResourceRecordSet(
ctx,
serviceName(inst),
types.RRTypePtr,
)
}
func (a *advertiser) syncPTR(
ctx context.Context,
inst dnssd.ServiceInstance,
cs *types.ChangeBatch,
) error {
desired := types.ResourceRecordSet{
SetIdentifier: marshalGeneration(0),
Weight: aws.Int64(0),
Type: types.RRTypePtr,
Name: serviceName(inst),
TTL: aws.Int64(int64(ptrTTL.Seconds())),
ResourceRecords: convertRecords(
dnssd.NewPTRRecord(inst),
),
}
current, ok, err := a.findPTR(ctx, inst)
if err != nil {
return err
}
if !ok {
cs.Changes = append(
cs.Changes,
types.Change{
Action: types.ChangeActionCreate,
ResourceRecordSet: &desired,
},
)
return nil
}
if indexOf(current, inst) != -1 {
return nil
}
gen, err := unmarshalGeneration(current.SetIdentifier)
if err != nil {
return err
}
desired.SetIdentifier = marshalGeneration(gen + 1)
desired.ResourceRecords = append(desired.ResourceRecords, current.ResourceRecords...)
cs.Changes = append(
cs.Changes,
types.Change{
Action: types.ChangeActionCreate,
ResourceRecordSet: &desired,
},
types.Change{
Action: types.ChangeActionDelete,
ResourceRecordSet: ¤t,
},
)
return nil
}
func (a *advertiser) deletePTR(
ctx context.Context,
inst dnssd.ServiceInstance,
cs *types.ChangeBatch,
) error {
current, ok, err := a.findPTR(ctx, inst)
if !ok || err != nil {
return err
}
index := indexOf(current, inst)
if index == -1 {
return nil
}
gen, err := unmarshalGeneration(current.SetIdentifier)
if err != nil {
return err
}
cs.Changes = append(
cs.Changes,
types.Change{
Action: types.ChangeActionDelete,
ResourceRecordSet: ¤t,
},
)
desired := types.ResourceRecordSet{
SetIdentifier: marshalGeneration(gen + 1),
Weight: aws.Int64(0),
Type: types.RRTypePtr,
Name: serviceName(inst),
TTL: aws.Int64(int64(ptrTTL.Seconds())),
ResourceRecords: slices.Delete(
slices.Clone(current.ResourceRecords),
index,
index+1,
),
}
if len(desired.ResourceRecords) != 0 {
cs.Changes = append(
cs.Changes,
types.Change{
Action: types.ChangeActionCreate,
ResourceRecordSet: &desired,
},
)
}
return nil
}
// indexOf returns the index of the given inst in a PTR resource record set, or
// -1 if it is not present.
func indexOf(set types.ResourceRecordSet, inst dnssd.ServiceInstance) int {
n := instanceName(inst)
for i, rec := range set.ResourceRecords {
if strings.EqualFold(*rec.Value, *n) {
return i
}
}
return -1
}
const generationPrefix = "dogmatiq/proclaim:generation="
// marshalGeneration returns a string representation of the given generation
// number suitable for being encoded in the SetIdentifier field of a Route 53
// resource record set.
//
// Encoding the generation here allows us to identify resource record sets with
// the same name and type by their generation (version).
func marshalGeneration(n uint64) *string {
return aws.String(fmt.Sprintf("%s%d", generationPrefix, n))
}
// unmarshalGeneration returns the generation number encoded in the
// SetIdentifier field of a Route 53 resource record set.
func unmarshalGeneration(gen *string) (uint64, error) {
if gen == nil {
return 0, errors.New("missing rr-set generation")
}
v, ok := strings.CutPrefix(*gen, generationPrefix)
if !ok {
return 0, fmt.Errorf("invalid rr-set generation %q: missing prefix", *gen)
}
n, err := strconv.ParseUint(v, 10, 64)
if err != nil {
return 0, fmt.Errorf("invalid rr-set generation %q: invalid counter component", *gen)
}
return n, nil
}