Skip to content

Commit

Permalink
job: read job submission source
Browse files Browse the repository at this point in the history
If available, use the job submission source to detect changes to
`jobspec`. This can mitigate drift detection problems such as #1.
  • Loading branch information
lgfa29 committed Dec 19, 2023
1 parent e5b6c01 commit 5ad8e6b
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
38 changes: 38 additions & 0 deletions nomad/resource_job.go
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,11 @@ type HCL1JobParserConfig struct {
type HCL2JobParserConfig struct {
AllowFS bool
Vars map[string]string

// Deprecated: Starting in v2.0.0 the provider assumes HCL2 parsing by
// default. This field should only be used to update the `hcl2` attribute
// in state without causing a diff.
Enabled bool
}

// ResourceFieldGetter are able to retrieve field values.
Expand Down Expand Up @@ -643,6 +648,28 @@ func resourceJobRead(d *schema.ResourceData, meta interface{}) error {
d.Set("allocation_ids", nil)
}

// Update jobspec submission data if available.
// Safely ignore errors as this is an optional step.
sub, _, err := client.Jobs().Submission(*job.ID, int(*job.Version), opts)
if err != nil {
log.Printf("[WARN] failed to read job submission: %v", err)
} else if sub != nil {
if sub.Source != "" {
d.Set("jobspec", sub.Source)
}

// Update HCL2 variables if present.
if sub.Format == "hcl2" {
hcl2Config, err := parseHCL2JobParserConfig(d.Get("hcl2"))
if err != nil {
log.Printf("[WARN] failed to parse HCL2 config: %v", err)
} else {
hcl2Config.Vars = sub.VariableFlags
d.Set("hcl2", flattenHCL2JobParserConfig(hcl2Config))
}
}
}

return nil
}

Expand Down Expand Up @@ -829,6 +856,9 @@ func parseHCL2JobParserConfig(raw interface{}) (HCL2JobParserConfig, error) {
if allowFS, ok := hcl2Map["allow_fs"].(bool); ok {
config.AllowFS = allowFS
}
if enabled, ok := hcl2Map["enabled"].(bool); ok {
config.Enabled = enabled
}
if vars, ok := hcl2Map["vars"].(map[string]interface{}); ok {
config.Vars = make(map[string]string)
for k, v := range vars {
Expand All @@ -839,6 +869,14 @@ func parseHCL2JobParserConfig(raw interface{}) (HCL2JobParserConfig, error) {
return config, nil
}

func flattenHCL2JobParserConfig(c HCL2JobParserConfig) []any {
return []any{map[string]any{
"allow_fs": c.AllowFS,
"enabled": c.Enabled,
"vars": c.Vars,
}}
}

func parseJobspec(raw string, config JobParserConfig, vaultToken *string, consulToken *string) (*api.Job, error) {
var job *api.Job
var err error
Expand Down
7 changes: 7 additions & 0 deletions website/docs/r/job.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,13 @@ EOF
}
```

## Tracking Jobspec Changes

The Nomad API allows [submitting the raw jobspec when registering and updating
jobs](https://developer.hashicorp.com/nomad/api-docs/jobs#submission). If
available, the job submission source is used to detect changes to the `jobspec`
and `hcl2.vars` arguments.

## Argument Reference

The following arguments are supported:
Expand Down

0 comments on commit 5ad8e6b

Please sign in to comment.