Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions internal/infoblox/infoblox.go
Original file line number Diff line number Diff line change
Expand Up @@ -424,11 +424,29 @@ func (p *Provider) submitChanges(changes []*infobloxChange) error {
metrics.ApiCallLatency.WithLabelValues("GetObject").Observe(duration.Seconds())
metrics.TotalApiCalls.Inc()
case infobloxDelete:
if refId == "" {
// Nothing in the zone to delete: the desired state is already met.
break
}
_, actionErr = p.client.DeleteObject(refId)
duration := time.Since(startTime)
metrics.ApiCallLatency.WithLabelValues("DeleteObject").Observe(duration.Seconds())
metrics.TotalApiCalls.Inc()
case infobloxUpdate:
if refId == "" {
// Upsert: the TXT registry can emit an UpdateNew for an ownership
// record that is not in the zone. Rebuild it in its create shape --
// that path sets the View, which Infoblox needs to find the parent
// zone and rejects on an update.
var createSet infobloxRecordSet
if createSet, actionErr = p.recordSet(change.Endpoint, false); actionErr == nil {
_, actionErr = p.client.CreateObject(createSet.obj)
}
duration := time.Since(startTime)
metrics.ApiCallLatency.WithLabelValues("CreateObject").Observe(duration.Seconds())
metrics.TotalApiCalls.Inc()
break
}
_, actionErr = p.client.UpdateObject(record.obj, refId)
duration := time.Since(startTime)
metrics.ApiCallLatency.WithLabelValues("UpdateObject").Observe(duration.Seconds())
Expand Down Expand Up @@ -720,7 +738,16 @@ func (p *Provider) findReverseZone(zones []*ibclient.ZoneAuth, name string) *ibc
return networks[maxMask]
}

// recordSet looks the endpoint up in Infoblox when getObject is set, so the caller can
// tell an existing record from an absent one. A lookup that matches nothing is an answer,
// not a failure: err is a named return, so each branch clears it after the tolerated
// NotFoundError -- otherwise it leaks out through buildRecord and the change fails with
// "could not build record: not found".
//
// It works on a copy because the TXT branch rewrites Targets in place, and submitChanges
// calls it twice for a change it has to upsert.
func (p *Provider) recordSet(ep *endpoint.Endpoint, getObject bool) (recordSet infobloxRecordSet, err error) {
ep = ep.DeepCopy()
var ttl uint32
var startTime time.Time
if ep.RecordTTL.IsConfigured() {
Expand Down Expand Up @@ -751,6 +778,7 @@ func (p *Provider) recordSet(ep *endpoint.Endpoint, getObject bool) (recordSet i
err = fmt.Errorf("could not fetch A record ['%s':'%s'] : %w", *obj.Name, *obj.Ipv4Addr, err)
return
}
err = nil
metrics.ApiCallLatency.WithLabelValues("GetObjectA").Observe(duration.Seconds())
} else {
// If getObject is not set (action == create), we need to set the View for Infoblox to find the parent zone
Expand Down Expand Up @@ -779,6 +807,7 @@ func (p *Provider) recordSet(ep *endpoint.Endpoint, getObject bool) (recordSet i
metrics.FailedApiCallsTotal.Inc()
return
}
err = nil
metrics.ApiCallLatency.WithLabelValues("GetObjectPTR").Observe(duration.Seconds())

} else {
Expand Down Expand Up @@ -807,6 +836,7 @@ func (p *Provider) recordSet(ep *endpoint.Endpoint, getObject bool) (recordSet i
metrics.FailedApiCallsTotal.Inc()
return
}
err = nil
metrics.ApiCallLatency.WithLabelValues("GetObjectCNAME").Observe(duration.Seconds())
} else {
// If getObject is not set (action == create), we need to set the View for Infoblox to find the parent zone
Expand All @@ -831,6 +861,7 @@ func (p *Provider) recordSet(ep *endpoint.Endpoint, getObject bool) (recordSet i
metrics.FailedApiCallsTotal.Inc()
return
}
err = nil
metrics.ApiCallLatency.WithLabelValues("GetObjectNS").Observe(duration.Seconds())
} else {
// If getObject is not set (action == create), we need to set the View for Infoblox to find the parent zone
Expand Down Expand Up @@ -864,6 +895,7 @@ func (p *Provider) recordSet(ep *endpoint.Endpoint, getObject bool) (recordSet i
metrics.FailedApiCallsTotal.Inc()
return
}
err = nil
metrics.ApiCallLatency.WithLabelValues("GetObjectTXT").Observe(duration.Seconds())
} else {
// If getObject is not set (action == create), we need to set the View for Infoblox to find the parent zone
Expand Down
98 changes: 98 additions & 0 deletions internal/infoblox/infoblox_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ type mockIBConnector struct {
updatedEndpoints []*endpoint.Endpoint
getObjectRequests []*getObjectRequest
requestBuilder ExtendedRequestBuilder
// txtNotFound makes a TXT lookup that matches nothing answer with the
// NotFoundError the real WAPI client returns, instead of an empty result.
txtNotFound bool
}

type getObjectRequest struct {
Expand Down Expand Up @@ -373,6 +376,9 @@ func (client *mockIBConnector) GetObject(obj ibclient.IBObject, ref string, quer
} else {
*res.(*[]ibclient.RecordTXT) = result
}
if client.txtNotFound && len(result) == 0 {
return ibclient.NewNotFoundError("no TXT record found")
}
case recordPtr:
var result []ibclient.RecordPTR
for _, object := range *client.mockInfobloxObjects {
Expand Down Expand Up @@ -1261,3 +1267,95 @@ func (r *mockRequestor) SendRequest(req *http.Request) (res []byte, err error) {
func validateEndpoints(t *testing.T, endpoints []*endpoint.Endpoint, expected []*endpoint.Endpoint) {
assert.True(t, SameEndpoints(endpoints, expected), "actual and expected endpoints don't match. %s:%s", endpoints, expected)
}

// A zone that holds the A record of an endpoint but not its ownership TXT -- what an
// interrupted batch, or a registry format change, leaves behind -- reaches the provider
// as an UPDATE of a record that is not there: the TXT registry keeps ownership through
// the legacy record and emits the missing type-prefixed one as UpdateNew.
func TestInfobloxApplyChangesUpsertsMissingRecord(t *testing.T) {
client := mockIBConnector{
mockInfobloxZones: &[]ibclient.ZoneAuth{
createMockInfobloxZone("example.com"),
},
mockInfobloxObjects: &[]ibclient.IBObject{
createMockInfobloxObject("foo.example.com", endpoint.RecordTypeA, "1.2.3.4"),
},
txtNotFound: true,
}

providerCfg := newInfobloxProvider(
endpoint.NewDomainFilter([]string{"example.com"}),
provider.NewZoneIDFilter([]string{""}), "", false, false, &client)

// Same targets on both sides, so CountDiff keeps these as updates rather than
// turning them into creates -- the shape the TXT registry produces for an endpoint
// it already owns.
err := providerCfg.ApplyChanges(context.Background(), &plan.Changes{
UpdateOld: []*endpoint.Endpoint{
endpoint.NewEndpoint("foo.example.com", endpoint.RecordTypeA, "1.2.3.4"),
endpoint.NewEndpoint("foo.example.com", endpoint.RecordTypeTXT, "tag"),
},
UpdateNew: []*endpoint.Endpoint{
endpoint.NewEndpoint("foo.example.com", endpoint.RecordTypeA, "1.2.3.4"),
endpoint.NewEndpoint("foo.example.com", endpoint.RecordTypeTXT, "tag"),
},
})
if err != nil {
t.Fatalf("ApplyChanges returned an error: %v", err)
}

// The TXT is not in the zone, so it is created ...
validateEndpoints(t, client.createdEndpoints, []*endpoint.Endpoint{
endpoint.NewEndpoint("foo.example.com", endpoint.RecordTypeTXT, "tag"),
})
// ... while the A record, which is, is updated as before.
validateEndpoints(t, client.updatedEndpoints, []*endpoint.Endpoint{
endpoint.NewEndpoint("foo.example.com", endpoint.RecordTypeA, "1.2.3.4"),
})
}

// The TXT branch of recordSet normalises its target in place with strconv.Unquote, to
// match the Infoblox API stripping the enclosing quotes of a value without whitespace.
// Running recordSet twice for one change would strip twice, and a TXT whose value is
// itself a quoted string would lose a layer. The property asserted here is parity: an
// upsert stores exactly what a plain create of the same endpoint stores.
func TestInfobloxApplyChangesUpsertKeepsQuotedTXTIntact(t *testing.T) {
quoted := `"\"token\""`
txt := func() *endpoint.Endpoint {
return endpoint.NewEndpoint("quoted.example.com", endpoint.RecordTypeTXT, quoted)
}
newClient := func() mockIBConnector {
return mockIBConnector{
mockInfobloxZones: &[]ibclient.ZoneAuth{
createMockInfobloxZone("example.com"),
},
mockInfobloxObjects: &[]ibclient.IBObject{},
txtNotFound: true,
}
}
apply := func(t *testing.T, client *mockIBConnector, changes *plan.Changes) {
t.Helper()
providerCfg := newInfobloxProvider(
endpoint.NewDomainFilter([]string{"example.com"}),
provider.NewZoneIDFilter([]string{""}), "", false, false, client)
if err := providerCfg.ApplyChanges(context.Background(), changes); err != nil {
t.Fatalf("ApplyChanges returned an error: %v", err)
}
if len(client.createdEndpoints) != 1 {
t.Fatalf("expected exactly one created record, got %v", client.createdEndpoints)
}
}

created := newClient()
apply(t, &created, &plan.Changes{Create: []*endpoint.Endpoint{txt()}})

upserted := newClient()
apply(t, &upserted, &plan.Changes{
UpdateOld: []*endpoint.Endpoint{txt()},
UpdateNew: []*endpoint.Endpoint{txt()},
})

if want, got := created.createdEndpoints[0].Targets[0], upserted.createdEndpoints[0].Targets[0]; want != got {
t.Errorf("upsert stored %q, a plain create of the same endpoint stores %q", got, want)
}
}
Loading