Skip to content
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

Duplicate job block error when using nomad-pack plan for cron jobs #579

Closed
FirstPiterSky opened this issue Sep 30, 2024 · 2 comments
Closed

Comments

@FirstPiterSky
Copy link

Description: We are encountering a 400 error when attempting to create tasks in Nomad using the nomad-pack plan command. The error is related to duplicate job blocks in the rendered job file.

Steps to Reproduce:

  • use any template to generate the job file:
[[ range $key, $ObjAll := var "jobs" . ]]
job "[[ var "job_prefix" $ ]]_cron_[[ $ObjAll.name ]]" {
~~~~~
[ [- end ]]
  • render command works successfully
./nomad-pack render ./cron --var-file="./cron/cron.hcl" --to-dir ./out
  • Run the following command:
./nomad-pack plan ./cron --var-file="./cron/cron.hcl"
  • Error message is as follows:
! Failed To Parse Job Specification

    Error:   Unexpected response code: 400 (Failed to parse job: input.hcl:164,1-48: Duplicate job block; Only one block of type "job" is allowed. Previous definition was at input.hcl:2,1-32.
input.hcl:326,1-59: Duplicate job block; Only one block of type "job" is allowed. Previous definition was at input.hcl:2,1-32.
input.hcl:488,1-34: Duplicate job block; Only one block of type "job" is allowed. Previous definition was at input.hcl:2,1-32.
~~~~~
input.hcl:3890,1-45: Duplicate job block; Only one block of type "job" is allowed. Previous definition was at input.hcl:2,1-32.
input.hcl:4052,1-45: Duplicate job block; Only one block of type "job" is allowed. Previous definition was at input.hcl:2,1-32.)
    Context: 
        - Template Name: fita_cron/templates/cron.nomad.tpl
        - Registry Name: <<local folder>>
        - Pack Name: cron
        - Pack Ref: <<none>>
        - Pack Path: /worckflow/fita/deploy/nomad/packs/cron
        - Deployment Name: cron

Expected Behavior: The job specification should be parsed and tasks should be created successfully in Nomad.

Actual Behavior: The command fails with a 400 error due to multiple "job" blocks in the generated file.

@pkazmierczak
Copy link
Contributor

Hi @FirstPiterSky, thanks for reporting this issue. We'll look into it!

@pkazmierczak pkazmierczak moved this from Needs Triage to Triaging in Nomad - Community Issues Triage Oct 2, 2024
@tgross
Copy link
Member

tgross commented Feb 10, 2025

Hi @FirstPiterSky! This fell thru the cracks a bit in the triage process, sorry about that. This is expected behavior. Suppose I have a pack like the following:

template file file
[[ range $key, $ObjAll := var "jobs" . ]]
job "[[ var "job_prefix" $ ]]_cron_[[ $ObjAll ]]" {

  group "app" {
    type = "batch"
    task "server" {
      driver = "raw_exec"

      config {
        command = "/bin/bash"
        args = ["-c", "sleep", "100"]
      }
    }
  }
}

[[- end ]]
var file
variable "job_prefix" {
  description = "The prefix"
  type        = string
  default     = "example"
}

variable "jobs" {
  description = "A list of jobs"
  type        = list(string)
  default     = ["foo", "bar"]
}

If I render that, I get something like the following:

$ nomad-pack render --to-dir out/ .
example/example.nomad:


job "example_cron_foo" {

  group "app" {
    type = "batch"
    task "server" {
      driver = "raw_exec"

      config {
        command = "/bin/bash"
        args    = ["-c", "sleep", "100"]
      }
    }
  }
}
job "example_cron_bar" {

  group "app" {
    type = "batch"
    task "server" {
      driver = "raw_exec"

      config {
        command = "/bin/bash"
        args    = ["-c", "sleep", "100"]
      }
    }
  }
}

But if I check the contents of the out directory, I'll see that both jobs have been mashed together in a single jobspec file. That's not permitted by Nomad's parser -- you can only have one job per jobspec. If you want multiple jobs in a pack, you need to define them as separate template files. You can see an example of this in this pack which runs two different jobs.

Rendering to disk via --to-dir is in general going to be a pretty good way to debug problems, because at the end of the day the jobspecs generated by Pack to be a valid Nomad jobspec.

@tgross tgross closed this as not planned Won't fix, can't repro, duplicate, stale Feb 10, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Development

No branches or pull requests

3 participants