hashicorp / nomad-pack

Mozilla Public License 2.0
393 stars 51 forks source link

Declarative interface #368

Open thomaslepoix opened 1 year ago

thomaslepoix commented 1 year ago

Hello, I am really interested in using a registry of nomad jobs, but I am not interested in deploying jobs through imperative commands such as nomad-pack run <pack>. I'd rather not deviate from my current flow where all my jobs are described in files, deployed thanks to Terraform -> everything as code.

Is there any plan to make nomad-pack available from either one of the following way?

By the way, I would be glad to use the nomad-pack CLI for various other tasks such as consulting registry list, generating pack from template, and so on.

Jamesits commented 5 months ago

Beside the user interface part, there is a serious problem on the current nomad-pack impl: if a job is removed from a pack, nomad-pack run xxx will not remove it from the deployment. (I've created a separate issue: https://github.com/hashicorp/nomad-pack/issues/503)

But you don't need nomad-pack! It is possible to implement most nomad-pack features in Terraform's Nomad provider:

Here's some demo that you can try out.

Job File Templating

Terraform:

resource "nomad_job" "xxx" {
  jobspec = templatefile("${path.module}/job.nomad.hcl.tftpl", {
    "key" = "value",
  })
  hcl2 {}
  purge_on_destroy = true
}

Template:

job "name-${key}" {
 // ...
}

(Escape all $ in the job template as $$)

Pseudo "Pack" UI on Nomad

Just inject the relevant meta values by hand.

job "hello_world" {
  type      = "service"
  namespace = "default"

  meta = {
    "pack.deployment_name" = "hello_world"
    "pack.job" = "hello_world"
    "pack.name" = "hello_world"
    "pack.path" = "/nonexistent"
    "pack.registry" = "<<local folder>>"
    "pack.version" = "<<none>>"
  }

  group "hello" {
    task "hello" {
      driver = "docker"

      config {
        image = "k8s.gcr.io/pause:3.2"
      }

      resources {
        cpu        = 100
        memory     = 100
        memory_max = 250
      }
    }
  }
}

Reusable Packs

Use either Terraform modules in a git repository, or Terragrunt. It works like a charm.