Skip to content

Commit

Permalink
Merge pull request kubernetes-sigs#3392 from craigapple/feature/infob…
Browse files Browse the repository at this point in the history
…lox-name-regex

Added name~ regex match to infoblox with --infoblox-name-regex argument
  • Loading branch information
k8s-ci-robot authored Mar 1, 2023
2 parents 0f4d143 + 7cd5d57 commit fd8d757
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 11 deletions.
8 changes: 8 additions & 0 deletions docs/tutorials/infoblox.md
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,14 @@ There is also the ability to filter results from the Infoblox zone_auth service
--infoblox-fqdn-regex=^staging.*test.com$
```

## Ability to filter A, Host, CNAME and TXT records from the by name using a regular expression

Infoblox supports filtering records by name using a regular expression. See the [Infoblox API document](https://www.infoblox.com/wp-content/uploads/infoblox-deployment-infoblox-rest-api.pdf) for examples. To use this feature, set the parameter infoblox-name-regex for external-dns to a regular expression that makes sense for you. For instance, if all your dns records end with `cluster1.example.com`, you can fetch records matching this style by setting the following:

```
--infoblox-name-regex=cluster1.example.com
```

## Infoblox PTR record support

There is an option to enable PTR records support for infoblox provider. PTR records allow to do reverse dns search. To enable PTR records support, add following into arguments for external-dns:
Expand Down
3 changes: 2 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,8 @@ func main() {
View: cfg.InfobloxView,
MaxResults: cfg.InfobloxMaxResults,
DryRun: cfg.DryRun,
FQDNRexEx: cfg.InfobloxFQDNRegEx,
FQDNRegEx: cfg.InfobloxFQDNRegEx,
NameRegEx: cfg.InfobloxNameRegEx,
CreatePTR: cfg.InfobloxCreatePTR,
CacheDuration: cfg.InfobloxCacheDuration,
},
Expand Down
2 changes: 2 additions & 0 deletions pkg/apis/externaldns/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ type Config struct {
InfobloxView string
InfobloxMaxResults int
InfobloxFQDNRegEx string
InfobloxNameRegEx string
InfobloxCreatePTR bool
InfobloxCacheDuration int
DynCustomerName string
Expand Down Expand Up @@ -489,6 +490,7 @@ func (cfg *Config) ParseFlags(args []string) error {
app.Flag("infoblox-view", "DNS view (default: \"\")").Default(defaultConfig.InfobloxView).StringVar(&cfg.InfobloxView)
app.Flag("infoblox-max-results", "Add _max_results as query parameter to the URL on all API requests. The default is 0 which means _max_results is not set and the default of the server is used.").Default(strconv.Itoa(defaultConfig.InfobloxMaxResults)).IntVar(&cfg.InfobloxMaxResults)
app.Flag("infoblox-fqdn-regex", "Apply this regular expression as a filter for obtaining zone_auth objects. This is disabled by default.").Default(defaultConfig.InfobloxFQDNRegEx).StringVar(&cfg.InfobloxFQDNRegEx)
app.Flag("infoblox-name-regex", "Apply this regular expression as a filter on the name field for obtaining infoblox records. This is disabled by default.").Default(defaultConfig.InfobloxNameRegEx).StringVar(&cfg.InfobloxNameRegEx)
app.Flag("infoblox-create-ptr", "When using the Infoblox provider, create a ptr entry in addition to an entry").Default(strconv.FormatBool(defaultConfig.InfobloxCreatePTR)).BoolVar(&cfg.InfobloxCreatePTR)
app.Flag("infoblox-cache-duration", "When using the Infoblox provider, set the record TTL (0s to disable).").Default(strconv.Itoa(defaultConfig.InfobloxCacheDuration)).IntVar(&cfg.InfobloxCacheDuration)
app.Flag("dyn-customer-name", "When using the Dyn provider, specify the Customer Name").Default("").StringVar(&cfg.DynCustomerName)
Expand Down
23 changes: 16 additions & 7 deletions provider/infoblox/infoblox.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ type StartupConfig struct {
DryRun bool
View string
MaxResults int
FQDNRexEx string
FQDNRegEx string
NameRegEx string
CreatePTR bool
CacheDuration int
}
Expand All @@ -85,15 +86,17 @@ type infobloxRecordSet struct {
// additional query parameter on all get requests
type ExtendedRequestBuilder struct {
fqdnRegEx string
nameRegEx string
maxResults int
ibclient.WapiRequestBuilder
}

// NewExtendedRequestBuilder returns a ExtendedRequestBuilder which adds
// _max_results query parameter to all GET requests
func NewExtendedRequestBuilder(maxResults int, fqdnRegEx string) *ExtendedRequestBuilder {
func NewExtendedRequestBuilder(maxResults int, fqdnRegEx string, nameRegEx string) *ExtendedRequestBuilder {
return &ExtendedRequestBuilder{
fqdnRegEx: fqdnRegEx,
nameRegEx: nameRegEx,
maxResults: maxResults,
}
}
Expand All @@ -107,10 +110,16 @@ func (mrb *ExtendedRequestBuilder) BuildRequest(t ibclient.RequestType, obj ibcl
if mrb.maxResults > 0 {
query.Set("_max_results", strconv.Itoa(mrb.maxResults))
}
_, ok := obj.(*ibclient.ZoneAuth)
if ok && t == ibclient.GET && mrb.fqdnRegEx != "" {
_, zoneAuthQuery := obj.(*ibclient.ZoneAuth)
if zoneAuthQuery && t == ibclient.GET && mrb.fqdnRegEx != "" {
query.Set("fqdn~", mrb.fqdnRegEx)
}

// if we are not doing a ZoneAuth query, support the name filter
if !zoneAuthQuery && mrb.nameRegEx != "" {
query.Set("name~", mrb.nameRegEx)
}

req.URL.RawQuery = query.Encode()
}
return
Expand Down Expand Up @@ -142,9 +151,9 @@ func NewInfobloxProvider(ibStartupCfg StartupConfig) (*ProviderConfig, error) {
requestBuilder ibclient.HttpRequestBuilder
err error
)
if ibStartupCfg.MaxResults != 0 || ibStartupCfg.FQDNRexEx != "" {
if ibStartupCfg.MaxResults != 0 || ibStartupCfg.FQDNRegEx != "" || ibStartupCfg.NameRegEx != "" {
// use our own HttpRequestBuilder which sets _max_results parameter on GET requests
requestBuilder = NewExtendedRequestBuilder(ibStartupCfg.MaxResults, ibStartupCfg.FQDNRexEx)
requestBuilder = NewExtendedRequestBuilder(ibStartupCfg.MaxResults, ibStartupCfg.FQDNRegEx, ibStartupCfg.NameRegEx)
} else {
// use the default HttpRequestBuilder of the infoblox client
requestBuilder, err = ibclient.NewWapiRequestBuilder(hostCfg, authCfg)
Expand All @@ -166,7 +175,7 @@ func NewInfobloxProvider(ibStartupCfg StartupConfig) (*ProviderConfig, error) {
zoneIDFilter: ibStartupCfg.ZoneIDFilter,
dryRun: ibStartupCfg.DryRun,
view: ibStartupCfg.View,
fqdnRegEx: ibStartupCfg.FQDNRexEx,
fqdnRegEx: ibStartupCfg.FQDNRegEx,
createPTR: ibStartupCfg.CreatePTR,
cacheDuration: ibStartupCfg.CacheDuration,
}
Expand Down
33 changes: 30 additions & 3 deletions provider/infoblox/infoblox_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -698,7 +698,7 @@ func TestExtendedRequestFDQDRegExBuilder(t *testing.T) {
Password: "abcd",
}

requestBuilder := NewExtendedRequestBuilder(0, "^staging.*test.com$")
requestBuilder := NewExtendedRequestBuilder(0, "^staging.*test.com$", "")
requestBuilder.Init(hostCfg, authCfg)

obj := ibclient.NewZoneAuth(ibclient.ZoneAuth{})
Expand All @@ -712,6 +712,33 @@ func TestExtendedRequestFDQDRegExBuilder(t *testing.T) {
assert.True(t, req.URL.Query().Get("fqdn~") == "")
}

func TestExtendedRequestNameRegExBuilder(t *testing.T) {
hostCfg := ibclient.HostConfig{
Host: "localhost",
Port: "8080",
Version: "2.3.1",
}

authCfg := ibclient.AuthConfig{
Username: "user",
Password: "abcd",
}

requestBuilder := NewExtendedRequestBuilder(0, "", "^staging.*test.com$")
requestBuilder.Init(hostCfg, authCfg)

obj := ibclient.NewEmptyRecordCNAME()

req, _ := requestBuilder.BuildRequest(ibclient.GET, obj, "", &ibclient.QueryParams{})

assert.True(t, req.URL.Query().Get("name~") == "^staging.*test.com$")

req, _ = requestBuilder.BuildRequest(ibclient.CREATE, obj, "", &ibclient.QueryParams{})

assert.True(t, req.URL.Query().Get("name~") == "")
}


func TestExtendedRequestMaxResultsBuilder(t *testing.T) {
hostCfg := ibclient.HostConfig{
Host: "localhost",
Expand All @@ -724,7 +751,7 @@ func TestExtendedRequestMaxResultsBuilder(t *testing.T) {
Password: "abcd",
}

requestBuilder := NewExtendedRequestBuilder(54321, "")
requestBuilder := NewExtendedRequestBuilder(54321, "", "")
requestBuilder.Init(hostCfg, authCfg)

obj := ibclient.NewEmptyRecordCNAME()
Expand All @@ -743,7 +770,7 @@ func TestGetObject(t *testing.T) {
hostCfg := ibclient.HostConfig{}
authCfg := ibclient.AuthConfig{}
transportConfig := ibclient.TransportConfig{}
requestBuilder := NewExtendedRequestBuilder(1000, "mysite.com")
requestBuilder := NewExtendedRequestBuilder(1000, "mysite.com", "")
requestor := mockRequestor{}
client, _ := ibclient.NewConnector(hostCfg, authCfg, transportConfig, requestBuilder, &requestor)

Expand Down

0 comments on commit fd8d757

Please sign in to comment.