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

nomad/api: maps are declared as hcl blocks #25006

Open
realpedrokrapp opened this issue Feb 3, 2025 · 4 comments
Open

nomad/api: maps are declared as hcl blocks #25006

realpedrokrapp opened this issue Feb 3, 2025 · 4 comments

Comments

@realpedrokrapp
Copy link

Nomad version

1.9.5

Operating system and Environment details

ubuntu 20.04 on wsl2

Issue

I am having a issue while decoding hcl files which contains jobs, taskgrup or tasks with the hclsimple package of hcl. I think all map properties defined in the DTO's of the nomad client in the api package are not correct.

Example:

This is the current defnition of the TaskGroup:

type TaskGroup struct {
	Name             *string                   `hcl:"name,label"`
	Count            *int                      `hcl:"count,optional"`
	Constraints      []*Constraint             `hcl:"constraint,block"`
	Affinities       []*Affinity               `hcl:"affinity,block"`
	Tasks            []*Task                   `hcl:"task,block"`
	Spreads          []*Spread                 `hcl:"spread,block"`
	Volumes          map[string]*VolumeRequest `hcl:"volume,block"`
	RestartPolicy    *RestartPolicy            `hcl:"restart,block"`
	Disconnect       *DisconnectStrategy       `hcl:"disconnect,block"`
	ReschedulePolicy *ReschedulePolicy         `hcl:"reschedule,block"`
	EphemeralDisk    *EphemeralDisk            `hcl:"ephemeral_disk,block"`
	Update           *UpdateStrategy           `hcl:"update,block"`
	Migrate          *MigrateStrategy          `hcl:"migrate,block"`
	Networks         []*NetworkResource        `hcl:"network,block"`
	Meta             map[string]string         `hcl:"meta,block"`
	Services         []*Service                `hcl:"service,block"`
	ShutdownDelay    *time.Duration            `mapstructure:"shutdown_delay" hcl:"shutdown_delay,optional"`
	// Deprecated: StopAfterClientDisconnect is deprecated in Nomad 1.8. Use Disconnect.StopOnClientAfter instead.
	StopAfterClientDisconnect *time.Duration `mapstructure:"stop_after_client_disconnect" hcl:"stop_after_client_disconnect,optional"`
	// To be deprecated after 1.8.0 infavour of Disconnect.LostAfter
	MaxClientDisconnect *time.Duration `mapstructure:"max_client_disconnect" hcl:"max_client_disconnect,optional"`
	Scaling             *ScalingPolicy `hcl:"scaling,block"`
	Consul              *Consul        `hcl:"consul,block"`
	// To be deprecated after 1.8.0 infavour of Disconnect.Replace
	PreventRescheduleOnLost *bool `hcl:"prevent_reschedule_on_lost,optional"`
}

The property Meta is a map[string]string with hcl:"meta,block", but when I am trying to parse a hcl file with a task group definition, I am getting the following error:

hcl 'block' tag kind cannot be applied to map[string]string field Meta: struct required
/home/krapppdr/.gvm/pkgsets/go1.23.2/global/pkg/mod/github.com/hashicorp/hcl/[email protected]/gohcl/schema.go:87 (0xaad964)
        ImpliedBodySchema: panic(fmt.Sprintf(
/home/krapppdr/.gvm/pkgsets/go1.23.2/global/pkg/mod/github.com/hashicorp/hcl/[email protected]/gohcl/decode.go:55 (0xaa93f4)
        decodeBodyToStruct: schema, partial := ImpliedBodySchema(val.Interface())
/home/krapppdr/.gvm/pkgsets/go1.23.2/global/pkg/mod/github.com/hashicorp/hcl/[email protected]/gohcl/decode.go:46 (0xaa9299)
        decodeBodyToValue: return decodeBodyToStruct(body, ctx, val)
/home/krapppdr/.gvm/pkgsets/go1.23.2/global/pkg/mod/github.com/hashicorp/hcl/[email protected]/gohcl/decode.go:264 (0xaac3e5)
        decodeBlockToValue: diags := decodeBodyToValue(block.Body, ctx, v)
/home/krapppdr/.gvm/pkgsets/go1.23.2/global/pkg/mod/github.com/hashicorp/hcl/[email protected]/gohcl/decode.go:201 (0xaab6c5)
        decodeBodyToStruct: diags = append(diags, decodeBlockToValue(block, ctx, v.Elem())...)
/home/krapppdr/.gvm/pkgsets/go1.23.2/global/pkg/mod/github.com/hashicorp/hcl/[email protected]/gohcl/decode.go:46 (0xaa9299)
        decodeBodyToValue: return decodeBodyToStruct(body, ctx, val)
/home/krapppdr/.gvm/pkgsets/go1.23.2/global/pkg/mod/github.com/hashicorp/hcl/[email protected]/gohcl/decode.go:264 (0xaac3e5)
        decodeBlockToValue: diags := decodeBodyToValue(block.Body, ctx, v)
/home/krapppdr/.gvm/pkgsets/go1.23.2/global/pkg/mod/github.com/hashicorp/hcl/[email protected]/gohcl/decode.go:201 (0xaab6c5)
        decodeBodyToStruct: diags = append(diags, decodeBlockToValue(block, ctx, v.Elem())...)
/home/krapppdr/.gvm/pkgsets/go1.23.2/global/pkg/mod/github.com/hashicorp/hcl/[email protected]/gohcl/decode.go:46 (0xaa9299)
        decodeBodyToValue: return decodeBodyToStruct(body, ctx, val)
/home/krapppdr/.gvm/pkgsets/go1.23.2/global/pkg/mod/github.com/hashicorp/hcl/[email protected]/gohcl/decode.go:39 (0xaa9104)
        DecodeBody: return decodeBodyToValue(body, ctx, rv.Elem())
/home/krapppdr/.gvm/pkgsets/go1.23.2/global/pkg/mod/github.com/hashicorp/hcl/[email protected]/hclsimple/hclsimple.go:80 (0xaae3c4)
        Decode: diags = gohcl.DecodeBody(file.Body, ctx, target)

I don't know if addressing this issue here in this repository is the correct place, but I think you cannot declare your properties which are of type map as hcl block. Is this already a known issue, is there a workaround or is my approach just simply wrong? Couldn't find anything so I thought I just ask nicely here :).

Reproduction steps

This is more a theoretical question, but you can reproduce this behaviour by decoding a hcl file with the hclsimple package of hcl.

Expected Result

I am expecting that I am able to decode valid hcl files which contains objects whic are defined by this repositories api package.

Actual Result

I am getting a panic while decoding.

Job file (if appropriate)

Nomad Server logs (if appropriate)

Nomad Client logs (if appropriate)

@jrasell
Copy link
Member

jrasell commented Feb 4, 2025

Hi @realpedrokrapp and thanks for raising this issue.

This originates from the original job specification design and has maintained been over the years to ensure backwards compatibility. To parse Nomad job specification files (HCLv2+tweaks) you can import the following dependencies and versions into your project:

$ go get github.com/hashicorp/hcl/[email protected]
$ go get github.com/hashicorp/[email protected]

You can then use these imports and the jobspec2.ParseWithConfig function to parse the raw bytes of a proposed job specification file to using HCLv2 variable interpolation as required.

I hope this helps and please let me know if you have any followup questions.

@realpedrokrapp
Copy link
Author

Hi @realpedrokrapp and thanks for raising this issue.

This originates from the original job specification design and has maintained been over the years to ensure backwards compatibility. To parse Nomad job specification files (HCLv2+tweaks) you can import the following dependencies and versions into your project:

$ go get github.com/hashicorp/hcl/[email protected]
$ go get github.com/hashicorp/[email protected]
You can then use these imports and the jobspec2.ParseWithConfig function to parse the raw bytes of a proposed job specification file to using HCLv2 variable interpolation as required.

I hope this helps and please let me know if you have any followup questions.

Hi @jrasell, first of all thank you very much for your fast reply.
Right now I am trying to follow your steps by importing the modules you suggested, but now I am running into the next problem while attempting to import github.com/hashicorp/[email protected] into my project:

get github.com/hashicorp/[email protected]
go: github.com/hashicorp/nomad imports
        github.com/hashicorp/nomad/client/allocrunner/taskrunner/getter imports
        github.com/hashicorp/nomad/client/config imports
        github.com/armon/go-metrics: github.com/armon/[email protected]: parsing go.mod:
        module declares its path as: github.com/hashicorp/go-metrics
                but was required as: github.com/armon/go-metrics
go: github.com/hashicorp/nomad imports
        github.com/hashicorp/nomad/command imports
        github.com/hashicorp/nomad/command/agent imports
        github.com/armon/go-metrics/circonus: github.com/armon/[email protected]: parsing go.mod:
        module declares its path as: github.com/hashicorp/go-metrics
                but was required as: github.com/armon/go-metrics
go: github.com/hashicorp/nomad imports
        github.com/hashicorp/nomad/command imports
        github.com/hashicorp/nomad/command/agent imports
        github.com/armon/go-metrics/datadog: github.com/armon/[email protected]: parsing go.mod:
        module declares its path as: github.com/hashicorp/go-metrics
                but was required as: github.com/armon/go-metrics
go: github.com/hashicorp/nomad imports
        github.com/hashicorp/nomad/command imports
        github.com/hashicorp/nomad/command/agent imports
        github.com/armon/go-metrics/prometheus: github.com/armon/[email protected]: parsing go.mod:
        module declares its path as: github.com/hashicorp/go-metrics
                but was required as: github.com/armon/go-metrics

I am having this issue in my current project and as well on my private computer.
Because I have a tight time schedule I have to admit that I did not start a deeper research on this importing issue. I just saw that the go-metrics module was renamed at some point. Since I am trying to import an official released version of nomad, I am pretty sure there is a solution to this. Maybe you could help me out again :)

@jrasell
Copy link
Member

jrasell commented Feb 4, 2025

Hi @realpedrokrapp, no problem.

Until Nomad 1.10 releases which includes the metrics migration and compatibility update, you should be able to add the following replace statement into your go.mod file to overcome the build problem:

// Fix error tidying due to Nomad downstream dependencies and the recent
// migration of the metrics library and maintain the version required by Nomad.
replace github.com/armon/go-metrics => github.com/hashicorp/go-metrics v0.0.0-20230509193637-d9ca9af9f1f9

@realpedrokrapp
Copy link
Author

Hi @realpedrokrapp, no problem.

Until Nomad 1.10 releases which includes the metrics migration and compatibility update, you should be able to add the following replace statement into your go.mod file to overcome the build problem:

// Fix error tidying due to Nomad downstream dependencies and the recent
// migration of the metrics library and maintain the version required by Nomad.
replace github.com/armon/go-metrics => github.com/hashicorp/go-metrics v0.0.0-20230509193637-d9ca9af9f1f9

Hi @jrasell.
Thanks a lot man. Now I am able to build the project and to decode Job files. For me, that's enough and everything I need :)

Maybe one last suggestion before I am going to drop out of this conversation:

In order to decode job files with jobspec2 package I have to import the whole nomad module. I am sure this has it reasons, but maybe there are some more people like me who are only interested in parsing, so maybe you guys can make a jobspec2 module like you did it with the api.

As I said this is just a suggestion which does not have anything to do with this issue so feel free to close it.

Best regards from Germany
Pedro

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Development

No branches or pull requests

2 participants