terraform-provider-nomad icon indicating copy to clipboard operation
terraform-provider-nomad copied to clipboard

Add nomad_job_v2 resource using terraform-plugin-go

Open remilapeyre opened this issue 4 years ago • 2 comments

remilapeyre avatar Jun 16 '21 11:06 remilapeyre

CLA assistant check
All committers have signed the CLA.

hashicorp-cla avatar Mar 12 '22 17:03 hashicorp-cla

This is a very interesting and important work. This would mean that the TF PLAN could show only the differences but not a huge string with "+" and "-" in some lines... Any chance to get this PR reviewed? Thanks for this good work @remilapeyre !

meyerbro avatar Apr 19 '22 15:04 meyerbro

Despite @remilapeyre brave efforts, this approach turned out to be impossible to manage over time as it requires keeping the Terraform spec for jobs in sync with Nomad.

We should investigate the advancements in the Terraform plugin development ecosystem to come up with a better solution to #1.

Thank you again @remilapeyre for all the hard work on this!

lgfa29 avatar Nov 25 '23 03:11 lgfa29

Hi @lgfa29 just came here to comment, something we did, (needs improvement, but maybe it's a good starting point for this).

We made a nomadpack provider, an example resource is something like this:

resource "nomadpack_pack" "autoscaler_cluster" {
  name            = "autoscaler_cluster"
  pack_name       = "autoscaler"
  revision        = "v0.3.7"
  namespace       = var.namespace
  preserve_counts = true
  vars = {
    job_name          = var.job_name
    target            = "aws-asg"
    region            = var.region
    namespace         = var.namespace
    target_creds_path = module.role.creds_path
    config_path       = var.config_path
  }

Under the hood currently this calls the nomad-pack binary to deploy the pack. Having this allows us to chain resources (create a policy and then deploy the job) in the same terraform configuration.

We detect changes by having a shasum of the rendered job and seeing if it changes with the value stored for the previous resource version.

Also if, the nomad-pack plan has changes, we output them in the terraform diagnostics. It's a bit hacky solution but I didn't find another way of getting the plan output displayed nicely.

	planOutput, changes, err := np.Plan(ctx)
	tflog.Info(ctx, planOutput, map[string]interface{}{"changes": changes})
	if err != nil {
		resp.Diagnostics.AddError("Error while plannig", err.Error())
		return
	}

	fullName := np.FullName()
	if planHasChanges(planOutput, changes) {
		resp.Diagnostics.AddWarning(fmt.Sprintf("Changes planned to %s", fullName), planOutput)
		return
	}

What do you thing about an approximation like this? Using a nomadpack resource we wouldn't need to keep track of the changes in the nomad job spec. Also having nomadpack integrated in terraform will allow to create dependencies and handle everything inside terraform.

jorgemarey avatar Nov 30 '23 14:11 jorgemarey

Hi @jorgemarey 👋

Thanks for the input. We have received questions about a Nomad Pack + Terraform integration, but we haven't nailed down a design for that quite yet.

Would you mind moving this comment to a new issue so we can track it and discussed it more broadly?

Thank you!

lgfa29 avatar Dec 16 '23 02:12 lgfa29

Created in https://github.com/hashicorp/terraform-provider-nomad/issues/409

jorgemarey avatar Dec 18 '23 09:12 jorgemarey