-
Notifications
You must be signed in to change notification settings - Fork 101
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
job: add job submission on register #405
Conversation
b227986
to
7ba1df8
Compare
7ba1df8
to
5ed1058
Compare
5ed1058
to
0be3c49
Compare
0be3c49
to
e5b6c01
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Couple comments regarding diff detection to consider, but overall LGTM!
nomad/resource_job.go
Outdated
log.Printf("[WARN] failed to read job submission: %v", err) | ||
} else if sub != nil { | ||
if sub.Source != "" { | ||
d.Set("jobspec", sub.Source) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To clarify the implication here, considering your recent comment on issue #1 -- This means that if the jobspec HCL is updated via UI or CLI (or appropriately crafted API call) outside of TF, this will pull that down for comparison, so remote HCL changes can be detected that the current jobspecEqual()
might otherwise miss.
It does not affect what Nomad considers to be a "change" (as with nomad plan
), like if semantically unimportant things (like hcl comments) change, then there still would be no change detected, as is the current state, but if there is some meaningful change, then it may be better detected and compared with this new ability.
Do I have that right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
so remote HCL changes can be detected that the current
jobspecEqual()
might otherwise miss.
Yes, currently jobspecEqual()
doesn't even detect any remote changes because, up until now, it didn't have access to the remote jobspec.
if semantically unimportant things (like hcl comments) change, then there still would be no change detected
Right, but that's not because of this change, this is because jobspecEqual()
does a semantic comparison, meaning, it parses the raw string and compares the result job object, so changes like comments and formatting are ignored.
terraform-provider-nomad/nomad/resource_job.go
Lines 989 to 1032 in 5afd8e7
func jobspecEqual(k, old, new string, d ResourceFieldGetter) bool { | |
var oldJob *api.Job | |
var newJob *api.Job | |
var oldErr error | |
var newErr error | |
// Read job parsing config. | |
jobParserConfig, err := parseJobParserConfig(d) | |
if err != nil { | |
log.Printf("[ERROR] %v", err) | |
return false | |
} | |
switch { | |
case jobParserConfig.JSON.Enabled: | |
oldJob, oldErr = parseJSONJobspec(old) | |
newJob, newErr = parseJSONJobspec(new) | |
case jobParserConfig.HCL1.Enabled: | |
oldJob, oldErr = jobspec.Parse(strings.NewReader(old)) | |
newJob, newErr = jobspec.Parse(strings.NewReader(new)) | |
default: | |
oldJob, oldErr = parseHCL2Jobspec(old, jobParserConfig.HCL2) | |
newJob, newErr = parseHCL2Jobspec(new, jobParserConfig.HCL2) | |
} | |
if oldErr != nil { | |
log.Println("error parsing old jobspec") | |
log.Printf("%v\n", oldJob) | |
log.Printf("%v", oldErr) | |
return false | |
} | |
if newErr != nil { | |
log.Println("error parsing new jobspec") | |
log.Printf("%v\n", newJob) | |
log.Printf("%v", newErr) | |
return false | |
} | |
// Init | |
oldJob.Canonicalize() | |
newJob.Canonicalize() | |
// Check for jobspec equality | |
return reflect.DeepEqual(oldJob, newJob) | |
} |
nomad/resource_job.go
Outdated
// Update HCL2 variables if present. | ||
hcl2, ok := d.GetOk("hcl2") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If I do not have hcl2{}
in my TF resource, but I then manually (UI or CLI) update the job with a variable value, TF will see "no changes."
Put differently, only if my nomad_job
resource has hcl2{}
in it (per this d.GetOk()
) will TF state contain anything about variables (per d.Set()
below), otherwise remote changes are invisible to TF.
Maybe that's an intended feature? but is a little surprising to me.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's right, but I kind of did that on purpose to avoid changes to state as much as possible. If you're not using HCL2, you probably have a reason for that, so switching to it outside of Terraform means you're doing something funky 😛
But if that's surprising then 070092d forces updates if variables change. Is this better?
If available, use the job submission source to detect changes to `jobspec`. This can mitigate drift detection problems such as #1.
Read HCL2 variables from job submission even if the `nomad_job` resource does not speify an `hcl2` block.
629174e
to
070092d
Compare
Send job submission on register to retain original job source.
Closes #392