mirror of
https://github.com/coredns/coredns.git
synced 2025-11-28 06:34:08 -05:00
plugin/nomad: Support service filtering (#7724)
Signed-off-by: Olli Janatuinen <olli.janatuinen@gmail.com>
This commit is contained in:
@@ -80,6 +80,7 @@ With only the plugin specified, the *nomad* plugin will default to `service.noma
|
|||||||
~~~ txt
|
~~~ txt
|
||||||
nomad [ZONE] {
|
nomad [ZONE] {
|
||||||
address URL
|
address URL
|
||||||
|
filter FILTER
|
||||||
token TOKEN
|
token TOKEN
|
||||||
ttl DURATION
|
ttl DURATION
|
||||||
}
|
}
|
||||||
@@ -87,6 +88,8 @@ nomad [ZONE] {
|
|||||||
|
|
||||||
* `address` The address where a Nomad agent (server) is available. **URL** defaults to `http://127.0.0.1:4646`.
|
* `address` The address where a Nomad agent (server) is available. **URL** defaults to `http://127.0.0.1:4646`.
|
||||||
|
|
||||||
|
* `filter` allows you to filter Nomad services. **FILTER** defaults to `""`. Uses [filtering](https://developer.hashicorp.com/nomad/api-docs#filtering) syntax.
|
||||||
|
|
||||||
* `token` The SecretID of an ACL token to use to authenticate API requests with if the Nomad cluster has ACL enabled. **TOKEN** defaults to `""`.
|
* `token` The SecretID of an ACL token to use to authenticate API requests with if the Nomad cluster has ACL enabled. **TOKEN** defaults to `""`.
|
||||||
|
|
||||||
* `ttl` allows you to set a custom TTL for responses. **DURATION** defaults to `30 seconds`. The minimum TTL allowed is `0` seconds, and the maximum is capped at `3600` seconds. Setting TTL to 0 will prevent records from being cached. The unit for the value is seconds.
|
* `ttl` allows you to set a custom TTL for responses. **DURATION** defaults to `30 seconds`. The minimum TTL allowed is `0` seconds, and the maximum is capped at `3600` seconds. Setting TTL to 0 will prevent records from being cached. The unit for the value is seconds.
|
||||||
|
|||||||
@@ -29,6 +29,7 @@ type Nomad struct {
|
|||||||
Zone string
|
Zone string
|
||||||
clients []*api.Client
|
clients []*api.Client
|
||||||
current int
|
current int
|
||||||
|
filter string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (n *Nomad) Name() string {
|
func (n *Nomad) Name() string {
|
||||||
@@ -103,7 +104,7 @@ func fetchServiceRegistrations(n Nomad, serviceName, namespace string) ([]*api.S
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, err
|
return nil, nil, err
|
||||||
}
|
}
|
||||||
return nc.Services().Get(serviceName, (&api.QueryOptions{Namespace: namespace}))
|
return nc.Services().Get(serviceName, (&api.QueryOptions{Namespace: namespace, Filter: n.filter}))
|
||||||
}
|
}
|
||||||
|
|
||||||
func handleServiceLookupError(w dns.ResponseWriter, m *dns.Msg, ctx context.Context, namespace string) (int, error) {
|
func handleServiceLookupError(w dns.ResponseWriter, m *dns.Msg, ctx context.Context, namespace string) (int, error) {
|
||||||
|
|||||||
@@ -77,6 +77,12 @@ func parse(c *caddy.Controller, n *Nomad) error {
|
|||||||
return c.Err("at least one address is required")
|
return c.Err("at least one address is required")
|
||||||
}
|
}
|
||||||
addresses = append(addresses, args...)
|
addresses = append(addresses, args...)
|
||||||
|
case "filter":
|
||||||
|
args := c.RemainingArgs()
|
||||||
|
if len(args) != 1 {
|
||||||
|
return c.Err("exactly one filter is required")
|
||||||
|
}
|
||||||
|
n.filter = args[0]
|
||||||
case "token":
|
case "token":
|
||||||
args := c.RemainingArgs()
|
args := c.RemainingArgs()
|
||||||
if len(args) != 1 {
|
if len(args) != 1 {
|
||||||
|
|||||||
@@ -13,6 +13,7 @@ func TestSetupNomad(t *testing.T) {
|
|||||||
name string
|
name string
|
||||||
config string
|
config string
|
||||||
shouldErr bool
|
shouldErr bool
|
||||||
|
expectedFilter string
|
||||||
expectedTTL uint32
|
expectedTTL uint32
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
@@ -23,17 +24,20 @@ nomad service.nomad {
|
|||||||
token test-token
|
token test-token
|
||||||
}`,
|
}`,
|
||||||
shouldErr: false,
|
shouldErr: false,
|
||||||
|
expectedFilter: "",
|
||||||
expectedTTL: uint32(defaultTTL),
|
expectedTTL: uint32(defaultTTL),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "valid_config_custom_ttl",
|
name: "valid_config_custom_filter_and_ttl",
|
||||||
config: `
|
config: `
|
||||||
nomad service.nomad {
|
nomad service.nomad {
|
||||||
address http://127.0.0.1:4646
|
address http://127.0.0.1:4646
|
||||||
|
filter "Tags not contains candidate"
|
||||||
token test-token
|
token test-token
|
||||||
ttl 60
|
ttl 60
|
||||||
}`,
|
}`,
|
||||||
shouldErr: false,
|
shouldErr: false,
|
||||||
|
expectedFilter: "Tags not contains candidate",
|
||||||
expectedTTL: 60,
|
expectedTTL: 60,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -85,6 +89,7 @@ nomad service.nomad {
|
|||||||
ttl: uint32(defaultTTL),
|
ttl: uint32(defaultTTL),
|
||||||
clients: make([]*nomad.Client, 0),
|
clients: make([]*nomad.Client, 0),
|
||||||
current: -1,
|
current: -1,
|
||||||
|
filter: "",
|
||||||
}
|
}
|
||||||
|
|
||||||
err := parse(c, n)
|
err := parse(c, n)
|
||||||
|
|||||||
Reference in New Issue
Block a user