added more documentation on JSON encoding to the contributing guide
4.4 KiB
New jobspec
Entry Checklist
Code
- Consider similar features in Consul, Kubernetes, and other tools. Is there prior art we should match? Terminology, structure, etc?
- Add structs/fields to
api/
packageapi/
structs usually have Canonicalize and Copy methods- New fields should be added to existing Canonicalize, Copy methods
- Test the structs/fields via methods mentioned above
- Add structs/fields to
nomad/structs
packagestructs/
structs usually have Copy, Equals, and Validate methods- Validation happens in this package and must be implemented
- Note that analogous struct field names should match with
api/
package - Test the structs/fields via methods mentioned above
- Implement and test other logical methods
- Add conversion between
api/
andnomad/structs/
incommand/agent/job_endpoint.go
- Add test for conversion
- Determine JSON encoding strategy for responses from RPC (see "JSON Encoding" below)
- Write
nomad/structs/
toapi/
conversions if necessary and write tests
- Write
- Implement diff logic for new structs/fields in
nomad/structs/diff.go
- Note that fields must be listed in alphabetical order in
FieldDiff
slices innomad/structs/diff_test.go
- Add test for diff of new structs/fields
- Note that fields must be listed in alphabetical order in
- Add change detection for new structs/fields in
scheduler/util.go/tasksUpdated
- Might be covered by
.Equals
but might not be, check. - Should return true if the task must be replaced as a result of the change.
- Might be covered by
HCL1 (deprecated)
New jobspec entries should only be added to jobspec2
. It makes use of HCL2
and the api
package for automatic parsing. Before, additional parsing was
required in the original jobspec
package.
Parse in(HCL1 only)jobspec/parse.go
Test in(HCL1 only)jobspec/parse_test.go
(preferably with ajobspec/text-fixtures/<feature>.hcl
test file)
Docs
- Changelog
- Jobspec entry https://www.nomadproject.io/docs/job-specification/index.html
- Jobspec sidebar entry https://github.com/hashicorp/nomad/blob/main/website/data/docs-navigation.js
- Job JSON API entry https://www.nomadproject.io/api/json-jobs.html
- Sample Response output in API https://www.nomadproject.io/api/jobs.html
- Consider if it needs a guide https://www.nomadproject.io/guides/index.html
JSON Encoding
As a general rule, HTTP endpoints (under command/agent/
) will make RPC calls that return structs belonging to
nomad/structs/
. These handlers ultimately return an object that is encoded by the Nomad HTTP server. The encoded form
needs to match the Nomad API; specifically, it should have the form of the corresponding struct from api/
. There are
a few ways that this can be accomplished:
- directly return the struct from the RPC call, if it has the same shape as the corresponding struct in
api/
. This is convenient when possible, resulting in the least work for the developer. Examples of this approach include [GET/v1/evaluation/:id
](https://github.com/hashicorp/nomad/blob/v1.0. 0/command/agent/eval_endpoint.go#L88). - convert the struct from the RPC call to the appropriate
api/
struct. This approach is the most developer effort, but it does have a strong guarantee that the HTTP response matches the API, due to the explicit conversion (assuming proper implementation, which requires tests). Examples of this approach include GET/v1/volume/csi/:id
- convert to an intermediate struct with the same shape as the
api/
struct. This approach strikes a balance between the former two approaches. This conversion can be performed in-situ in the agent HTTP handler, as long as the conversion doesn't need to appear in other handlers. Otherwise, it is possible to register an extension on the JSON encoding used by the HTTP agent; these extensions can be put innomad/json/extensions.go
.
Note, for simple transformations to encoding (like renaming or neglecting fields), we can use field tags on the structs
from nomad/structs
. Our msgpack encoding
only uses the codec
tag.
Therefore, the json
tag is available for customizing API output when encoding structs
objects. See structs.Node.SecretID
, for example.