Skip to content

Commit

Permalink
TaggedVersion information in structs, rather than job_endpoint (#23841)
Browse files Browse the repository at this point in the history
* TaggedVersion information in structs, rather than job_endpoint

* Test for taggedVersion description length

* Some API plumbing
  • Loading branch information
philrenaud committed Sep 24, 2024
1 parent 93bf7ca commit a5a02e7
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 0 deletions.
19 changes: 19 additions & 0 deletions api/jobs.go
Original file line number Diff line number Diff line change
Expand Up @@ -988,6 +988,24 @@ func (j *JobUILink) Copy() *JobUILink {
}
}

type JobTaggedVersion struct {
Name string
Description string
TaggedTime int64
}

func (j *JobTaggedVersion) Copy() *JobTaggedVersion {
if j == nil {
return nil
}

return &JobTaggedVersion{
Name: j.Name,
Description: j.Description,
TaggedTime: j.TaggedTime,
}
}

func (js *JobSubmission) Canonicalize() {
if js == nil {
return
Expand Down Expand Up @@ -1066,6 +1084,7 @@ type Job struct {
CreateIndex *uint64
ModifyIndex *uint64
JobModifyIndex *uint64
TaggedVersion *JobTaggedVersion
}

// IsPeriodic returns whether a job is periodic.
Expand Down
13 changes: 13 additions & 0 deletions command/agent/job_endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -1034,6 +1034,7 @@ func ApiJobToStructJob(job *api.Job) *structs.Job {
Constraints: ApiConstraintsToStructs(job.Constraints),
Affinities: ApiAffinitiesToStructs(job.Affinities),
UI: ApiJobUIConfigToStructs(job.UI),
TaggedVersion: ApiJobTaggedVersionToStructs(job.TaggedVersion),
}

// Update has been pushed into the task groups. stagger and max_parallel are
Expand Down Expand Up @@ -2138,6 +2139,18 @@ func ApiJobUIConfigToStructs(jobUI *api.JobUIConfig) *structs.JobUIConfig {
}
}

func ApiJobTaggedVersionToStructs(jobTaggedVersion *api.JobTaggedVersion) *structs.JobTaggedVersion {
if jobTaggedVersion == nil {
return nil
}

return &structs.JobTaggedVersion{
Name: jobTaggedVersion.Name,
Description: jobTaggedVersion.Description,
TaggedTime: jobTaggedVersion.TaggedTime,
}
}

func ApiAffinityToStructs(a1 *api.Affinity) *structs.Affinity {
return &structs.Affinity{
LTarget: a1.LTarget,
Expand Down
32 changes: 32 additions & 0 deletions command/agent/job_endpoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4448,3 +4448,35 @@ func TestConversion_ApiJobUIConfigToStructs(t *testing.T) {
must.Eq(t, expected, result)
})
}

func TestConversion_ApiJobTaggedVersionToStructs(t *testing.T) {
t.Run("nil tagged version", func(t *testing.T) {
must.Nil(t, ApiJobTaggedVersionToStructs(nil))
})

t.Run("empty tagged version", func(t *testing.T) {
taggedVersion := &api.JobTaggedVersion{}
expected := &structs.JobTaggedVersion{
Name: "",
Description: "",
TaggedTime: 0,
}
result := ApiJobTaggedVersionToStructs(taggedVersion)
must.Eq(t, expected, result)
})

t.Run("tagged version with tag and version", func(t *testing.T) {
taggedVersion := &api.JobTaggedVersion{
Name: "low-latency",
Description: "Low latency version",
TaggedTime: 1234567890,
}
expected := &structs.JobTaggedVersion{
Name: "low-latency",
Description: "Low latency version",
TaggedTime: 1234567890,
}
result := ApiJobTaggedVersionToStructs(taggedVersion)
must.Eq(t, expected, result)
})
}
27 changes: 27 additions & 0 deletions nomad/structs/structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -4574,6 +4574,26 @@ type Job struct {

// Links and Description fields for the Web UI
UI *JobUIConfig

// Metadata related to a tagged Job Version (which itself is really a Job)
TaggedVersion *JobTaggedVersion
}

type JobTaggedVersion struct {
Name string
Description string
TaggedTime int64
}

func (tv *JobTaggedVersion) Copy() *JobTaggedVersion {
if tv == nil {
return nil
}
return &JobTaggedVersion{
Name: tv.Name,
Description: tv.Description,
TaggedTime: tv.TaggedTime,
}
}

type JobUIConfig struct {
Expand Down Expand Up @@ -4730,6 +4750,7 @@ func (j *Job) Copy() *Job {
nj.Affinities = CopySliceAffinities(j.Affinities)
nj.Multiregion = j.Multiregion.Copy()
nj.UI = j.UI.Copy()
nj.TaggedVersion = j.TaggedVersion.Copy()

if j.TaskGroups != nil {
tgs := make([]*TaskGroup, len(j.TaskGroups))
Expand Down Expand Up @@ -4827,6 +4848,12 @@ func (j *Job) Validate() error {
}
}

if j.TaggedVersion != nil {
if len(j.TaggedVersion.Description) > MaxDescriptionCharacters {
mErr.Errors = append(mErr.Errors, fmt.Errorf("Tagged version description must be under 1000 characters, currently %d", len(j.TaggedVersion.Description)))
}
}

// Check for duplicate task groups
taskGroups := make(map[string]int)
for idx, tg := range j.TaskGroups {
Expand Down
12 changes: 12 additions & 0 deletions nomad/structs/structs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,18 @@ func TestJob_Validate(t *testing.T) {
"Task Group web should have an ephemeral disk object",
},
},
{
name: "TaggedVersion Description length",
job: &Job{
Type: JobTypeService,
TaggedVersion: &JobTaggedVersion{
Description: strings.Repeat("a", 1001),
},
},
expErr: []string{
"Tagged version description must be under 1000 characters",
},
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
Expand Down

0 comments on commit a5a02e7

Please sign in to comment.