Skip to content

Commit 122748f

Browse files
committed
refactor(plan): reduce Calculate() cyclomatic complexity
1 parent 627ba0a commit 122748f

File tree

1 file changed

+87
-62
lines changed

1 file changed

+87
-62
lines changed

plan/plan.go

Lines changed: 87 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -183,75 +183,41 @@ func (p *Plan) Calculate() *Plan {
183183
t.addCandidate(desired)
184184
}
185185

186+
changes := p.calculateChanges(t)
187+
188+
plan := &Plan{
189+
Current: p.Current,
190+
Desired: p.Desired,
191+
Changes: changes,
192+
// The default for ExternalDNS is to always only consider A/AAAA and CNAMEs.
193+
// Everything else is an add on or something to be considered.
194+
ManagedRecords: []string{endpoint.RecordTypeA, endpoint.RecordTypeAAAA, endpoint.RecordTypeCNAME},
195+
}
196+
197+
return plan
198+
}
199+
200+
func (p *Plan) calculateChanges(t planTable) *Changes {
186201
changes := &Changes{}
187202

188203
for key, row := range t.rows {
204+
switch {
189205
// dns name not taken
190-
if len(row.current) == 0 {
206+
case len(row.current) == 0:
191207
recordsByType := t.resolver.ResolveRecordTypes(key, row)
192208
for _, records := range recordsByType {
193209
if len(records.candidates) > 0 {
194210
changes.Create = append(changes.Create, t.resolver.ResolveCreate(records.candidates))
195211
}
196212
}
197-
}
198213

199214
// dns name released or possibly owned by a different external dns
200-
if len(row.current) > 0 && len(row.candidates) == 0 {
215+
case len(row.candidates) == 0:
201216
changes.Delete = append(changes.Delete, row.current...)
202-
}
203217

204218
// dns name is taken
205-
if len(row.current) > 0 && len(row.candidates) > 0 {
206-
creates := []*endpoint.Endpoint{}
207-
208-
// apply changes for each record type
209-
recordsByType := t.resolver.ResolveRecordTypes(key, row)
210-
for _, records := range recordsByType {
211-
// record type not desired
212-
if records.current != nil && len(records.candidates) == 0 {
213-
changes.Delete = append(changes.Delete, records.current)
214-
}
215-
216-
// new record type desired
217-
if records.current == nil && len(records.candidates) > 0 {
218-
update := t.resolver.ResolveCreate(records.candidates)
219-
// creates are evaluated after all domain records have been processed to
220-
// validate that this external dns has ownership claim on the domain before
221-
// adding the records to planned changes.
222-
creates = append(creates, update)
223-
}
224-
225-
// update existing record
226-
if records.current != nil && len(records.candidates) > 0 {
227-
update := t.resolver.ResolveUpdate(records.current, records.candidates)
228-
229-
if shouldUpdateTTL(update, records.current) || targetChanged(update, records.current) || p.shouldUpdateProviderSpecific(update, records.current) ||
230-
p.isOldOwnerIdSetAndDifferent(records.current) {
231-
inheritOwner(records.current, update)
232-
changes.UpdateNew = append(changes.UpdateNew, update)
233-
changes.UpdateOld = append(changes.UpdateOld, records.current)
234-
}
235-
}
236-
}
237-
238-
if len(creates) > 0 {
239-
// only add creates if the external dns has ownership claim on the domain
240-
ownersMatch := true
241-
for _, current := range row.current {
242-
if p.OwnerID != "" && !current.IsOwnedBy(p.OwnerID) {
243-
ownersMatch = false
244-
}
245-
}
246-
247-
if ownersMatch {
248-
changes.Create = append(changes.Create, creates...)
249-
} else if log.GetLevel() == log.DebugLevel {
250-
for _, current := range row.current {
251-
log.Debugf(`Skipping endpoint %v because owner id does not match for one or more items to create, found: "%s", required: "%s"`, current, current.Labels[endpoint.OwnerLabelKey], p.OwnerID)
252-
}
253-
}
254-
}
219+
case len(row.candidates) > 0:
220+
p.appendTakenDNSNameChanges(t, changes, key, row)
255221
}
256222
}
257223

@@ -267,16 +233,75 @@ func (p *Plan) Calculate() *Plan {
267233
changes.UpdateNew = endpoint.FilterEndpointsByOwnerID(p.OwnerID, changes.UpdateNew)
268234
}
269235

270-
plan := &Plan{
271-
Current: p.Current,
272-
Desired: p.Desired,
273-
Changes: changes,
274-
// The default for ExternalDNS is to always only consider A/AAAA and CNAMEs.
275-
// Everything else is an add on or something to be considered.
276-
ManagedRecords: []string{endpoint.RecordTypeA, endpoint.RecordTypeAAAA, endpoint.RecordTypeCNAME},
236+
return changes
237+
}
238+
239+
func (p *Plan) appendTakenDNSNameChanges(t planTable, changes *Changes, key planKey, row *planTableRow) {
240+
// apply changes for each record type
241+
rowChanges := p.calculatePlanTableRowChanges(t, key, row)
242+
changes.Delete = append(changes.Delete, rowChanges.Delete...)
243+
changes.UpdateNew = append(changes.UpdateNew, rowChanges.UpdateNew...)
244+
changes.UpdateOld = append(changes.UpdateOld, rowChanges.UpdateOld...)
245+
if len(rowChanges.Create) == 0 {
246+
return
277247
}
278248

279-
return plan
249+
// only add creates if the external dns has ownership claim on the domain
250+
ownersMatch := true
251+
if p.OwnerID != "" {
252+
for _, current := range row.current {
253+
if !current.IsOwnedBy(p.OwnerID) {
254+
ownersMatch = false
255+
break
256+
}
257+
}
258+
}
259+
260+
if ownersMatch {
261+
changes.Create = append(changes.Create, rowChanges.Create...)
262+
} else if log.GetLevel() == log.DebugLevel {
263+
for _, current := range row.current {
264+
log.Debugf(`Skipping endpoint %v because owner id does not match for one or more items to create, found: "%s", required: "%s"`, current, current.Labels[endpoint.OwnerLabelKey], p.OwnerID)
265+
}
266+
}
267+
}
268+
269+
func (p *Plan) calculatePlanTableRowChanges(t planTable, key planKey, row *planTableRow) *Changes {
270+
changes := &Changes{}
271+
272+
recordsByType := t.resolver.ResolveRecordTypes(key, row)
273+
for _, records := range recordsByType {
274+
switch {
275+
// record type not desired
276+
case records.current != nil && len(records.candidates) == 0:
277+
changes.Delete = append(changes.Delete, records.current)
278+
279+
// new record type desired
280+
case records.current == nil && len(records.candidates) > 0:
281+
update := t.resolver.ResolveCreate(records.candidates)
282+
// creates are evaluated after all domain records have been processed to
283+
// validate that this external dns has ownership claim on the domain before
284+
// adding the records to planned changes.
285+
changes.Create = append(changes.Create, update)
286+
287+
// update existing record
288+
case records.current != nil && len(records.candidates) > 0:
289+
p.appendEndpointUpdates(t, changes, records.current, records.candidates)
290+
}
291+
}
292+
293+
return changes
294+
}
295+
296+
func (p *Plan) appendEndpointUpdates(t planTable, changes *Changes, current *endpoint.Endpoint, candidates []*endpoint.Endpoint) {
297+
update := t.resolver.ResolveUpdate(current, candidates)
298+
299+
if shouldUpdateTTL(update, current) || targetChanged(update, current) ||
300+
p.shouldUpdateProviderSpecific(update, current) || p.isOldOwnerIdSetAndDifferent(current) {
301+
inheritOwner(current, update)
302+
changes.UpdateNew = append(changes.UpdateNew, update)
303+
changes.UpdateOld = append(changes.UpdateOld, current)
304+
}
280305
}
281306

282307
func (p *Plan) isOldOwnerIdSetAndDifferent(current *endpoint.Endpoint) bool {

0 commit comments

Comments
 (0)