Skip to content

Commit d88b0b0

Browse files
agent: add option to disable agent cache for HTTP endpoints (#8023)
This allows the operator to disable agent caching for the http endpoint. It is on by default for backwards compatibility and if disabled will ignore the url parameter `cached`.
1 parent a79f773 commit d88b0b0

File tree

9 files changed

+69
-7
lines changed

9 files changed

+69
-7
lines changed

agent/catalog_endpoint.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ func (s *HTTPServer) CatalogDatacenters(resp http.ResponseWriter, req *http.Requ
8484
parseCacheControl(resp, req, &args.QueryOptions)
8585
var out []string
8686

87-
if args.QueryOptions.UseCache {
87+
if s.agent.config.HTTPUseCache && args.QueryOptions.UseCache {
8888
raw, m, err := s.agent.cache.Get(cachetype.CatalogDatacentersName, &args)
8989
if err != nil {
9090
metrics.IncrCounterWithLabels([]string{"client", "rpc", "error", "catalog_datacenters"}, 1,
@@ -166,7 +166,7 @@ func (s *HTTPServer) CatalogServices(resp http.ResponseWriter, req *http.Request
166166
var out structs.IndexedServices
167167
defer setMeta(resp, &out.QueryMeta)
168168

169-
if args.QueryOptions.UseCache {
169+
if s.agent.config.HTTPUseCache && args.QueryOptions.UseCache {
170170
raw, m, err := s.agent.cache.Get(cachetype.CatalogListServicesName, &args)
171171
if err != nil {
172172
metrics.IncrCounterWithLabels([]string{"client", "rpc", "error", "catalog_services"}, 1,
@@ -255,7 +255,7 @@ func (s *HTTPServer) catalogServiceNodes(resp http.ResponseWriter, req *http.Req
255255
var out structs.IndexedServiceNodes
256256
defer setMeta(resp, &out.QueryMeta)
257257

258-
if args.QueryOptions.UseCache {
258+
if s.agent.config.HTTPUseCache && args.QueryOptions.UseCache {
259259
raw, m, err := s.agent.cache.Get(cachetype.CatalogServicesName, &args)
260260
if err != nil {
261261
metrics.IncrCounterWithLabels([]string{"client", "rpc", "error", "catalog_service_nodes"}, 1,

agent/config/builder.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -835,6 +835,7 @@ func (b *Builder) Build() (rt RuntimeConfig, err error) {
835835
HTTPBlockEndpoints: c.HTTPConfig.BlockEndpoints,
836836
HTTPResponseHeaders: c.HTTPConfig.ResponseHeaders,
837837
AllowWriteHTTPFrom: b.cidrsVal("allow_write_http_from", c.HTTPConfig.AllowWriteHTTPFrom),
838+
HTTPUseCache: b.boolValWithDefault(c.HTTPConfig.UseCache, true),
838839

839840
// Telemetry
840841
Telemetry: lib.TelemetryConfig{

agent/config/config.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -641,6 +641,7 @@ type HTTPConfig struct {
641641
BlockEndpoints []string `json:"block_endpoints,omitempty" hcl:"block_endpoints" mapstructure:"block_endpoints"`
642642
AllowWriteHTTPFrom []string `json:"allow_write_http_from,omitempty" hcl:"allow_write_http_from" mapstructure:"allow_write_http_from"`
643643
ResponseHeaders map[string]string `json:"response_headers,omitempty" hcl:"response_headers" mapstructure:"response_headers"`
644+
UseCache *bool `json:"use_cache,omitempty" hcl:"use_cache" mapstructure:"use_cache"`
644645
}
645646

646647
type Performance struct {

agent/config/runtime.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,12 @@ type RuntimeConfig struct {
349349
// hcl: dns_config { cache_max_age = "duration" }
350350
DNSCacheMaxAge time.Duration
351351

352+
// HTTPUseCache whether or not to use cache for http queries. Defaults
353+
// to true.
354+
//
355+
// hcl: http_config { use_cache = (true|false) }
356+
HTTPUseCache bool
357+
352358
// HTTPBlockEndpoints is a list of endpoint prefixes to block in the
353359
// HTTP API. Any requests to these will get a 403 response.
354360
//

agent/config/runtime_test.go

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2075,6 +2075,54 @@ func TestConfigFlagsAndEdgecases(t *testing.T) {
20752075
`},
20762076
err: "Serf Advertise WAN address 10.0.0.1:1000 already configured for RPC Advertise",
20772077
},
2078+
{
2079+
desc: "http use_cache defaults to true",
2080+
args: []string{
2081+
`-data-dir=` + dataDir,
2082+
},
2083+
json: []string{`{
2084+
"http_config": {}
2085+
}`},
2086+
hcl: []string{`
2087+
http_config = {}
2088+
`},
2089+
patch: func(rt *RuntimeConfig) {
2090+
rt.DataDir = dataDir
2091+
rt.HTTPUseCache = true
2092+
},
2093+
},
2094+
{
2095+
desc: "http use_cache is enabled when true",
2096+
args: []string{
2097+
`-data-dir=` + dataDir,
2098+
},
2099+
json: []string{`{
2100+
"http_config": { "use_cache": true }
2101+
}`},
2102+
hcl: []string{`
2103+
http_config = { use_cache = true }
2104+
`},
2105+
patch: func(rt *RuntimeConfig) {
2106+
rt.DataDir = dataDir
2107+
rt.HTTPUseCache = true
2108+
},
2109+
},
2110+
{
2111+
desc: "http use_cache is disabled when false",
2112+
args: []string{
2113+
`-data-dir=` + dataDir,
2114+
},
2115+
json: []string{`{
2116+
"http_config": { "use_cache": false }
2117+
}`},
2118+
hcl: []string{`
2119+
http_config = { use_cache = false }
2120+
`},
2121+
patch: func(rt *RuntimeConfig) {
2122+
rt.DataDir = dataDir
2123+
rt.HTTPUseCache = false
2124+
},
2125+
},
20782126
{
20792127
desc: "sidecar_service can't have ID",
20802128
args: []string{
@@ -4134,7 +4182,8 @@ func TestFullConfig(t *testing.T) {
41344182
"response_headers": {
41354183
"M6TKa9NP": "xjuxjOzQ",
41364184
"JRCrHZed": "rl0mTx81"
4137-
}
4185+
},
4186+
"use_cache": false
41384187
},
41394188
"key_file": "IEkkwgIA",
41404189
"leave_on_terminate": true,
@@ -4772,6 +4821,7 @@ func TestFullConfig(t *testing.T) {
47724821
"M6TKa9NP" = "xjuxjOzQ"
47734822
"JRCrHZed" = "rl0mTx81"
47744823
}
4824+
use_cache = false
47754825
}
47764826
key_file = "IEkkwgIA"
47774827
leave_on_terminate = true
@@ -5486,6 +5536,7 @@ func TestFullConfig(t *testing.T) {
54865536
HTTPMaxConnsPerClient: 100,
54875537
HTTPSHandshakeTimeout: 2391 * time.Millisecond,
54885538
HTTPSPort: 15127,
5539+
HTTPUseCache: false,
54895540
KeyFile: "IEkkwgIA",
54905541
KVMaxValueSize: 1234567800000000,
54915542
LeaveDrainTime: 8265 * time.Second,
@@ -6368,6 +6419,7 @@ func TestSanitize(t *testing.T) {
63686419
"HTTPMaxConnsPerClient": 0,
63696420
"HTTPPort": 0,
63706421
"HTTPResponseHeaders": {},
6422+
"HTTPUseCache": false,
63716423
"HTTPSAddrs": [],
63726424
"HTTPSHandshakeTimeout": "0s",
63736425
"HTTPSPort": 0,

agent/discovery_chain_endpoint.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ func (s *HTTPServer) DiscoveryChainRead(resp http.ResponseWriter, req *http.Requ
5959
var out structs.DiscoveryChainResponse
6060
defer setMeta(resp, &out.QueryMeta)
6161

62-
if args.QueryOptions.UseCache {
62+
if s.agent.config.HTTPUseCache && args.QueryOptions.UseCache {
6363
raw, m, err := s.agent.cache.Get(cachetype.CompiledDiscoveryChainName, &args)
6464
if err != nil {
6565
return nil, err

agent/health_endpoint.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ func (s *HTTPServer) healthServiceNodes(resp http.ResponseWriter, req *http.Requ
198198
var out structs.IndexedCheckServiceNodes
199199
defer setMeta(resp, &out.QueryMeta)
200200

201-
if args.QueryOptions.UseCache {
201+
if s.agent.config.HTTPUseCache && args.QueryOptions.UseCache {
202202
raw, m, err := s.agent.cache.Get(cachetype.HealthServicesName, &args)
203203
if err != nil {
204204
return nil, err

agent/prepared_query_endpoint.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ func (s *HTTPServer) preparedQueryExecute(id string, resp http.ResponseWriter, r
121121
var reply structs.PreparedQueryExecuteResponse
122122
defer setMeta(resp, &reply.QueryMeta)
123123

124-
if args.QueryOptions.UseCache {
124+
if s.agent.config.HTTPUseCache && args.QueryOptions.UseCache {
125125
raw, m, err := s.agent.cache.Get(cachetype.PreparedQueryName, &args)
126126
if err != nil {
127127
// Don't return error if StaleIfError is set and we are within it and had

website/pages/docs/agent/options.mdx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1405,6 +1405,8 @@ Valid time units are 'ns', 'us' (or 'µs'), 'ms', 's', 'm', 'h'."
14051405

14061406
- `allow_write_http_from` This object is a list of networks in CIDR notation (eg "127.0.0.0/8") that are allowed to call the agent write endpoints. It defaults to an empty list, which means all networks are allowed. This is used to make the agent read-only, except for select ip ranges. - To block write calls from anywhere, use `[ "255.255.255.255/32" ]`. - To only allow write calls from localhost, use `[ "127.0.0.0/8" ]` - To only allow specific IPs, use `[ "10.0.0.1/32", "10.0.0.2/32" ]`
14071407

1408+
- `use_cache` Defaults to true. If disabled, the agent won't be using [agent caching](/api/features/caching) to answer the request. Even when the url parameter is provided.
1409+
14081410
- `leave_on_terminate` If enabled, when the agent receives a TERM signal, it will send a `Leave` message to the rest of the cluster and gracefully leave. The default behavior for this feature varies based on whether or not the agent is running as a client or a server (prior to Consul 0.7 the default value was unconditionally set to `false`). On agents in client-mode, this defaults to `true` and for agents in server-mode, this defaults to `false`.
14091411

14101412
- `limits` Available in Consul 0.9.3 and later, this is a nested

0 commit comments

Comments
 (0)