Skip to content

Commit

Permalink
jobspec2: isolate package from Nomad core and BUSL.
Browse files Browse the repository at this point in the history
  • Loading branch information
jrasell committed Feb 4, 2025
1 parent fd20f66 commit 20632c0
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 59 deletions.
8 changes: 8 additions & 0 deletions GNUmakefile
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,14 @@ check: ## Lint the source code
@echo "==> Check API package is isolated from rest..."
@cd ./api && if go list --test -f '{{ join .Deps "\n" }}' . | grep github.com/hashicorp/nomad/ | grep -v -e /nomad/api/ -e nomad/api.test; then echo " /api package depends the ^^ above internal nomad packages. Remove such dependency"; exit 1; fi

@echo "==> Check jobspec2 package is isolated from Nomad core..."
@cd ./jobspec2 && \
if go list --test -f '{{ join .Deps "\n" }}' . | \
grep github.com/hashicorp/nomad/ | \
grep -v -e /nomad/jobspec2/ -e nomad/jobspec2.test | \
grep -v -e /nomad/api ; then echo \
" /jobspec2 package depends the ^^ above internal nomad packages. Remove such dependency"; exit 1; fi

@echo "==> Check command package does not import structs..."
@cd ./command && if go list -f '{{ join .Imports "\n" }}' . | grep github.com/hashicorp/nomad/nomad/structs; then echo " /command package imports the structs pkg. Remove such import"; exit 1; fi

Expand Down
3 changes: 1 addition & 2 deletions jobspec2/hcl_conversions.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"github.com/hashicorp/hcl/v2/gohcl"
"github.com/hashicorp/hcl/v2/hcldec"
"github.com/hashicorp/nomad/api"
"github.com/hashicorp/nomad/helper/pointer"
"github.com/zclconf/go-cty/cty"
"github.com/zclconf/go-cty/cty/gocty"
)
Expand Down Expand Up @@ -120,7 +119,7 @@ func decodeAffinity(body hcl.Body, ctx *hcl.EvalContext, val interface{}) hcl.Di
weight := v.GetAttr("weight")
if !weight.IsNull() {
w, _ := weight.AsBigFloat().Int64()
a.Weight = pointer.Of(int8(w))
a.Weight = pointOf(int8(w))
}

// If "version" is provided, set the operand
Expand Down
8 changes: 0 additions & 8 deletions jobspec2/helper_test.go

This file was deleted.

19 changes: 9 additions & 10 deletions jobspec2/parse_job.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"time"

"github.com/hashicorp/nomad/api"
"github.com/hashicorp/nomad/helper/pointer"
)

func normalizeJob(jc *jobConfig) {
Expand Down Expand Up @@ -81,13 +80,13 @@ func normalizeVault(v *api.Vault) {
}

if v.Env == nil {
v.Env = pointer.Of(true)
v.Env = pointOf(true)
}
if v.DisableFile == nil {
v.DisableFile = pointer.Of(false)
v.DisableFile = pointOf(false)
}
if v.ChangeMode == nil {
v.ChangeMode = pointer.Of("restart")
v.ChangeMode = pointOf("restart")
}
}

Expand Down Expand Up @@ -127,16 +126,16 @@ func normalizeTemplates(templates []*api.Template) {

for _, t := range templates {
if t.ChangeMode == nil {
t.ChangeMode = pointer.Of("restart")
t.ChangeMode = pointOf("restart")
}
if t.Perms == nil {
t.Perms = pointer.Of("0644")
t.Perms = pointOf("0644")
}
if t.Splay == nil {
t.Splay = pointer.Of(5 * time.Second)
t.Splay = pointOf(5 * time.Second)
}
if t.ErrMissingKey == nil {
t.ErrMissingKey = pointer.Of(false)
t.ErrMissingKey = pointOf(false)
}
normalizeChangeScript(t.ChangeScript)
}
Expand All @@ -152,10 +151,10 @@ func normalizeChangeScript(ch *api.ChangeScript) {
}

if ch.Timeout == nil {
ch.Timeout = pointer.Of(5 * time.Second)
ch.Timeout = pointOf(5 * time.Second)
}

if ch.FailOnError == nil {
ch.FailOnError = pointer.Of(false)
ch.FailOnError = pointOf(false)
}
}
76 changes: 37 additions & 39 deletions jobspec2/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,12 @@ import (
"time"

"github.com/hashicorp/nomad/api"
"github.com/hashicorp/nomad/ci"
"github.com/hashicorp/nomad/helper/pointer"
"github.com/shoenig/test/must"
"github.com/stretchr/testify/require"
)

func TestParse_ConnectJob(t *testing.T) {
ci.Parallel(t)
t.Parallel()

name := "./test-fixtures/connect-example.hcl"
f, err := os.Open(name)
Expand All @@ -32,7 +30,7 @@ func TestParse_ConnectJob(t *testing.T) {
}

func TestParse_VarsAndFunctions(t *testing.T) {
ci.Parallel(t)
t.Parallel()

hcl := `
variables {
Expand All @@ -58,7 +56,7 @@ job "example" {
}

func TestParse_VariablesDefaultsAndSet(t *testing.T) {
ci.Parallel(t)
t.Parallel()

hcl := `
variables {
Expand Down Expand Up @@ -157,7 +155,7 @@ job "example" {

// TestParse_UnknownVariables asserts that unknown variables are left intact for further processing
func TestParse_UnknownVariables(t *testing.T) {
ci.Parallel(t)
t.Parallel()

hcl := `
variables {
Expand Down Expand Up @@ -192,7 +190,7 @@ job "example" {
// TestParse_UnsetVariables asserts that variables that have neither types nor
// values return early instead of panicking.
func TestParse_UnsetVariables(t *testing.T) {
ci.Parallel(t)
t.Parallel()

hcl := `
variable "region_var" {}
Expand All @@ -214,7 +212,7 @@ job "example" {
}

func TestParse_Locals(t *testing.T) {
ci.Parallel(t)
t.Parallel()

hcl := `
variables {
Expand Down Expand Up @@ -263,7 +261,7 @@ job "example" {
}

func TestParse_FileOperators(t *testing.T) {
ci.Parallel(t)
t.Parallel()

hcl := `
job "example" {
Expand Down Expand Up @@ -300,7 +298,7 @@ job "example" {
}

func TestParseDynamic(t *testing.T) {
ci.Parallel(t)
t.Parallel()

hcl := `
job "example" {
Expand Down Expand Up @@ -363,7 +361,7 @@ job "example" {
}

func TestParse_InvalidHCL(t *testing.T) {
ci.Parallel(t)
t.Parallel()

t.Run("invalid body", func(t *testing.T) {
hcl := `invalid{hcl`
Expand Down Expand Up @@ -408,7 +406,7 @@ job "example" {
}

func TestParse_InvalidScalingSyntax(t *testing.T) {
ci.Parallel(t)
t.Parallel()

cases := []struct {
name string
Expand Down Expand Up @@ -574,7 +572,7 @@ job "example" {
}

func TestParseJob_JobWithFunctionsAndLookups(t *testing.T) {
ci.Parallel(t)
t.Parallel()

hcl := `
variable "env" {
Expand Down Expand Up @@ -613,13 +611,13 @@ job "job-webserver" {
{
"prod",
&api.Job{
ID: pointer.Of("job-webserver"),
Name: pointer.Of("job-webserver"),
ID: pointOf("job-webserver"),
Name: pointOf("job-webserver"),
Datacenters: []string{"prod-dc1", "prod-dc2"},
TaskGroups: []*api.TaskGroup{
{
Name: pointer.Of("group-webserver"),
Count: pointer.Of(20),
Name: pointOf("group-webserver"),
Count: pointOf(20),

Tasks: []*api.Task{
{
Expand All @@ -639,13 +637,13 @@ job "job-webserver" {
{
"staging",
&api.Job{
ID: pointer.Of("job-webserver"),
Name: pointer.Of("job-webserver"),
ID: pointOf("job-webserver"),
Name: pointOf("job-webserver"),
Datacenters: []string{"dc1"},
TaskGroups: []*api.TaskGroup{
{
Name: pointer.Of("group-webserver"),
Count: pointer.Of(3),
Name: pointOf("group-webserver"),
Count: pointOf(3),

Tasks: []*api.Task{
{
Expand All @@ -665,13 +663,13 @@ job "job-webserver" {
{
"unknown",
&api.Job{
ID: pointer.Of("job-webserver"),
Name: pointer.Of("job-webserver"),
ID: pointOf("job-webserver"),
Name: pointOf("job-webserver"),
Datacenters: []string{},
TaskGroups: []*api.TaskGroup{
{
Name: pointer.Of("group-webserver"),
Count: pointer.Of(0),
Name: pointOf("group-webserver"),
Count: pointOf(0),

Tasks: []*api.Task{
{
Expand Down Expand Up @@ -705,7 +703,7 @@ job "job-webserver" {
}

func TestParse_TaskEnvs(t *testing.T) {
ci.Parallel(t)
t.Parallel()

cases := []struct {
name string
Expand Down Expand Up @@ -780,7 +778,7 @@ job "example" {
}

func TestParse_TaskEnvs_Multiple(t *testing.T) {
ci.Parallel(t)
t.Parallel()

hcl := `
job "example" {
Expand All @@ -806,7 +804,7 @@ job "example" {
}

func Test_TaskEnvs_Invalid(t *testing.T) {
ci.Parallel(t)
t.Parallel()

cases := []struct {
name string
Expand Down Expand Up @@ -856,7 +854,7 @@ job "example" {
}

func TestParse_Meta_Alternatives(t *testing.T) {
ci.Parallel(t)
t.Parallel()

hcl := ` job "example" {
group "group" {
Expand Down Expand Up @@ -904,7 +902,7 @@ func TestParse_Meta_Alternatives(t *testing.T) {
}

func TestParse_Constraint_Alternatives(t *testing.T) {
ci.Parallel(t)
t.Parallel()

hclOpVal := `
job "example" {
Expand Down Expand Up @@ -1006,7 +1004,7 @@ job "example" {
// TestParse_UndefinedVariables asserts that values with undefined variables are left
// intact in the job representation
func TestParse_UndefinedVariables(t *testing.T) {
ci.Parallel(t)
t.Parallel()

cases := []string{
"plain",
Expand Down Expand Up @@ -1050,7 +1048,7 @@ func TestParse_UndefinedVariables(t *testing.T) {
}

func TestParseServiceCheck(t *testing.T) {
ci.Parallel(t)
t.Parallel()

hcl := ` job "group_service_check_script" {
group "group" {
Expand All @@ -1074,11 +1072,11 @@ func TestParseServiceCheck(t *testing.T) {
require.NoError(t, err)

expectedJob := &api.Job{
ID: pointer.Of("group_service_check_script"),
Name: pointer.Of("group_service_check_script"),
ID: pointOf("group_service_check_script"),
Name: pointOf("group_service_check_script"),
TaskGroups: []*api.TaskGroup{
{
Name: pointer.Of("group"),
Name: pointOf("group"),
Services: []*api.Service{
{
Name: "foo-service",
Expand All @@ -1101,7 +1099,7 @@ func TestParseServiceCheck(t *testing.T) {
}

func TestWaitConfig(t *testing.T) {
ci.Parallel(t)
t.Parallel()

hclBytes, err := os.ReadFile("test-fixtures/template-wait-config.hcl")
require.NoError(t, err)
Expand All @@ -1122,7 +1120,7 @@ func TestWaitConfig(t *testing.T) {
}

func TestErrMissingKey(t *testing.T) {
ci.Parallel(t)
t.Parallel()
hclBytes, err := os.ReadFile("test-fixtures/template-err-missing-key.hcl")
require.NoError(t, err)
job, err := ParseWithConfig(&ParseConfig{
Expand All @@ -1138,7 +1136,7 @@ func TestErrMissingKey(t *testing.T) {
}

func TestRestartRenderTemplates(t *testing.T) {
ci.Parallel(t)
t.Parallel()
hclBytes, err := os.ReadFile("test-fixtures/restart-render-templates.hcl")
require.NoError(t, err)
job, err := ParseWithConfig(&ParseConfig{
Expand All @@ -1159,7 +1157,7 @@ func TestRestartRenderTemplates(t *testing.T) {
// Identities slice to the pre-1.7 Identity field in case >=1.7 CLIs are used
// with <1.7 APIs.
func TestIdentity(t *testing.T) {
ci.Parallel(t)
t.Parallel()
hclBytes, err := os.ReadFile("test-fixtures/identity-compat.nomad.hcl")
must.NoError(t, err)
job, err := ParseWithConfig(&ParseConfig{
Expand Down
7 changes: 7 additions & 0 deletions jobspec2/util.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package jobspec2

// pointOf returns a pointer to "a". It is duplicated from the helper package
// to isolate the jobspec2 package from the rest of Nomad.
func pointOf[A any](a A) *A {
return &a
}

0 comments on commit 20632c0

Please sign in to comment.