api: add `CheckRegisterOpts` method to Agent API (#18943)

Ongoing work to support Nomad Workload Identity for authenticating with Consul
will mean that Nomad's service registration sync with Consul will want to use
Consul tokens scoped to individual workloads for registering services and
checks. The `CheckRegister` method in the API doesn't have an option to pass the
token in, which prevent us from sharing the same Consul connection for all
workloads. Add a `CheckRegisterOpts` to match the behavior of
`ServiceRegisterOpts`.
This commit is contained in:
Tim Gross 2023-09-25 11:25:02 -04:00 committed by GitHub
parent aedc03b7ae
commit e5f5fc9301
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 24 additions and 1 deletions

3
.changelog/18943.txt Normal file
View File

@ -0,0 +1,3 @@
```release-note:improvement
api: added `CheckRegisterOpts` to Agent API
```

View File

@ -998,7 +998,14 @@ func (a *Agent) UpdateTTLOpts(checkID, output, status string, q *QueryOptions) e
// CheckRegister is used to register a new check with
// the local agent
func (a *Agent) CheckRegister(check *AgentCheckRegistration) error {
return a.CheckRegisterOpts(check, nil)
}
// CheckRegisterOpts is used to register a new check with
// the local agent using query options
func (a *Agent) CheckRegisterOpts(check *AgentCheckRegistration, q *QueryOptions) error {
r := a.c.newRequest("PUT", "/v1/agent/check/register")
r.setQueryOptions(q)
r.obj = check
_, resp, err := a.c.doRequest(r)
if err != nil {

View File

@ -1073,7 +1073,7 @@ func TestAPI_AgentChecks(t *testing.T) {
Name: "foo",
}
reg.TTL = "15s"
if err := agent.CheckRegister(reg); err != nil {
if err := agent.CheckRegisterOpts(reg, nil); err != nil {
t.Fatalf("err: %v", err)
}
@ -1097,6 +1097,19 @@ func TestAPI_AgentChecks(t *testing.T) {
}
}
func TestAgent_AgentChecksRegisterOpts_WithContextTimeout(t *testing.T) {
c, err := NewClient(DefaultConfig())
require.NoError(t, err)
ctx, cancel := context.WithTimeout(context.Background(), time.Nanosecond)
t.Cleanup(cancel)
opts := &QueryOptions{}
opts = opts.WithContext(ctx)
err = c.Agent().CheckRegisterOpts(&AgentCheckRegistration{}, opts)
require.True(t, errors.Is(err, context.DeadlineExceeded), "expected timeout")
}
func TestAPI_AgentChecksWithFilterOpts(t *testing.T) {
t.Parallel()
c, s := makeClient(t)