Skip to content

Commit

Permalink
versions API now takes compare_to flags
Browse files Browse the repository at this point in the history
  • Loading branch information
philrenaud committed Aug 29, 2024
1 parent 0e6233a commit f331d39
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 23 deletions.
4 changes: 2 additions & 2 deletions api/jobs.go
Original file line number Diff line number Diff line change
Expand Up @@ -266,9 +266,9 @@ func (j *Jobs) ScaleStatus(jobID string, q *QueryOptions) (*JobScaleStatusRespon

// Versions is used to retrieve all versions of a particular job given its
// unique ID.
func (j *Jobs) Versions(jobID string, diffs bool, q *QueryOptions) ([]*Job, []*JobDiff, *QueryMeta, error) {
func (j *Jobs) Versions(jobID string, diffs bool, diffTag string, diffVersion string, q *QueryOptions) ([]*Job, []*JobDiff, *QueryMeta, error) {
var resp JobVersionsResponse
qm, err := j.client.query(fmt.Sprintf("/v1/job/%s/versions?diffs=%v", url.PathEscape(jobID), diffs), &resp, q)
qm, err := j.client.query(fmt.Sprintf("/v1/job/%s/versions?diffs=%t&compare_to_tag=%s&compare_to_version=%d", url.PathEscape(jobID), diffs, diffTag, diffVersion), &resp, q)
if err != nil {
return nil, nil, nil, err
}
Expand Down
9 changes: 7 additions & 2 deletions command/agent/job_endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -768,6 +768,9 @@ func (s *HTTPServer) jobScaleAction(resp http.ResponseWriter, req *http.Request,
func (s *HTTPServer) jobVersions(resp http.ResponseWriter, req *http.Request, jobID string) (interface{}, error) {

diffsStr := req.URL.Query().Get("diffs")
diffTagName := req.URL.Query().Get("compare_to_tag")
diffVersion := req.URL.Query().Get("compare_to_version")

var diffsBool bool
if diffsStr != "" {
var err error
Expand All @@ -778,8 +781,10 @@ func (s *HTTPServer) jobVersions(resp http.ResponseWriter, req *http.Request, jo
}

args := structs.JobVersionsRequest{
JobID: jobID,
Diffs: diffsBool,
JobID: jobID,
Diffs: diffsBool,
DiffVersion: diffVersion,
DiffTagName: diffTagName,
}
if s.parse(resp, req, &args.Region, &args.QueryOptions) {
return nil, nil
Expand Down
3 changes: 2 additions & 1 deletion command/job_history.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,8 @@ func (c *JobHistoryCommand) Run(args []string) int {
q := &api.QueryOptions{Namespace: namespace}

// Prefix lookup matched a single job
versions, diffs, _, err := client.Jobs().Versions(jobID, diff, q)
// TODO: the empty string params here should probably be new DiffVersion/DiffTagName params.
versions, diffs, _, err := client.Jobs().Versions(jobID, diff, "", "", q)
if err != nil {
c.Ui.Error(fmt.Sprintf("Error retrieving job versions: %s", err))
return 1
Expand Down
2 changes: 1 addition & 1 deletion command/job_inspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ func getJob(client *api.Client, namespace, jobID string, version *uint64) (*api.
return job, err
}

versions, _, _, err := client.Jobs().Versions(jobID, false, q)
versions, _, _, err := client.Jobs().Versions(jobID, false, "", "", q)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion command/job_restart.go
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ func (c *JobRestartCommand) Run(args []string) int {

// Retrieve the job history so we can properly determine if a group or task
// exists in the specific allocation job version.
jobVersions, _, _, err := c.client.Jobs().Versions(c.jobID, false, nil)
jobVersions, _, _, err := c.client.Jobs().Versions(c.jobID, false, "", "", nil)
if err != nil {
c.Ui.Error(fmt.Sprintf("Error retrieving versions of job %q: %s", c.jobID, err))
return 1
Expand Down
6 changes: 3 additions & 3 deletions command/job_tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,14 @@ func (c *JobTagCommand) Name() string { return "job tag apply" }

func (c *JobTagCommand) Run(args []string) int {
var name, description, versionStr string
var version uint64
// var version uint64

flags := c.Meta.FlagSet(c.Name(), FlagSetClient)
flags.Usage = func() { c.Ui.Output(c.Help()) }
flags.StringVar(&name, "name", "", "")
flags.StringVar(&description, "description", "", "")
// flags.StringVar(&versionStr, "version", "", "")
flags.Uint64Var(&version, "version", 0, "")
flags.StringVar(&versionStr, "version", "", "")
// flags.Uint64Var(&version, "version", 0, "")

if err := flags.Parse(args); err != nil {
return 1
Expand Down
57 changes: 49 additions & 8 deletions nomad/job_endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -1300,8 +1300,39 @@ func (j *Job) GetJobVersions(args *structs.JobVersionsRequest,
reply.Versions = out
if len(out) != 0 {

compareVersionNumber := 0
var compareVersionNumber uint64
var compareVersion *structs.Job
var compareStatic bool

if args.DiffTagName != "" {
compareStatic = true
tagFound := false
for _, version := range out {
j.logger.Debug("=== checking for tag version match: ", args.DiffTagName, args.DiffTagName)
if version.TaggedVersion != nil {
j.logger.Debug("=== checking for tag version match check: ", version.TaggedVersion.Name)
}
if version.TaggedVersion != nil && version.TaggedVersion.Name == args.DiffTagName {
j.logger.Debug("=== VERSION TAG MATCH FOUND", "version.Version", version.Version, "Version.Tag.Name", version.TaggedVersion.Name)
tagFound = true
compareVersionNumber = version.Version
break
} else {
j.logger.Debug("=== Version not a match", version.Version)
}
}
if !tagFound {
return fmt.Errorf("tag %q not found", args.DiffTagName)
}
} else if args.DiffVersion != "" {
compareStatic = true
compareVersionNumber, err = strconv.ParseUint(args.DiffVersion, 10, 64)
if err != nil {
return fmt.Errorf("failed to parse diff version: %v", err)
}
}

j.logger.Debug("===== GetJobVersions and compareVersionNumber is", "compareVersionNumber", compareVersionNumber, "compareStatic", compareStatic, "compareVersionTag", args.DiffTagName)

// Note: a previous assumption here was that the 0th job was the latest, and that we don't modify "old" versions.
// Adding version tags breaks this assumption (you can tag an old version, which should unblock /versions queries) so we now look for the highest ModifyIndex.
Expand All @@ -1310,20 +1341,27 @@ func (j *Job) GetJobVersions(args *structs.JobVersionsRequest,
if job.ModifyIndex > maxModifyIndex {
maxModifyIndex = job.ModifyIndex
}
if job.Version == uint64(compareVersionNumber) {
if compareStatic && job.Version == uint64(compareVersionNumber) {

Check failure on line 1344 in nomad/job_endpoint.go

View workflow job for this annotation

GitHub Actions / checks / checks

unnecessary conversion (unconvert)
compareVersion = job
}
}
reply.Index = maxModifyIndex

if compareStatic && compareVersion == nil {
if args.DiffTagName != "" {
return fmt.Errorf("tag %q not found", args.DiffTagName)
}
return fmt.Errorf("version %q not found", args.DiffVersion)
}

// Compute the diffs
if args.Diffs {
for i := 0; i < len(out)-1; i++ {
// log out the version number and i
j.logger.Debug("diffing job versions", "version", out[i].Version, "i", i)
// old, new := out[i+1], out[i]
// old.Diff(new) always runs a diff against its previous version;
// let's have it run against version 0 always
if !compareStatic {
compareVersion = out[i+1]
}
j.logger.Debug("diffing job versions", "version", out[i].Version, "i", i, "compareStatic", compareStatic, "compare-version", compareVersion.Version)

old, new := compareVersion, out[i]
d, err := old.Diff(new, true)
if err != nil {
Expand Down Expand Up @@ -1839,7 +1877,10 @@ func (j *Job) Plan(args *structs.JobPlanRequest, reply *structs.JobPlanResponse)

if args.DiffVersion != "" {
// Convert from string to uint64
diffVersion, _ := strconv.ParseUint(args.DiffVersion, 10, 64)
diffVersion, err := strconv.ParseUint(args.DiffVersion, 10, 64)
if err != nil {
return err
}
existingJob, err = snap.JobByIDAndVersion(ws, args.RequestNamespace(), args.Job.ID, diffVersion)
if err != nil {
return err
Expand Down
12 changes: 7 additions & 5 deletions nomad/structs/structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -845,9 +845,9 @@ type JobStubFields struct {
// evaluation of the Job.
type JobPlanRequest struct {
Job *Job
Diff bool // Toggles an annotated diff
DiffVersion string // shrug emoji
DiffTagName string // shrug emoji
Diff bool // Toggles an annotated diff
DiffVersion string
DiffTagName string
// PolicyOverride is set when the user is attempting to override any policies
PolicyOverride bool
WriteRequest
Expand Down Expand Up @@ -1652,8 +1652,10 @@ type JobListResponse struct {

// JobVersionsRequest is used to get a jobs versions
type JobVersionsRequest struct {
JobID string
Diffs bool
JobID string
Diffs bool
DiffVersion string
DiffTagName string
QueryOptions
}

Expand Down

0 comments on commit f331d39

Please sign in to comment.