Skip to content

Commit

Permalink
0th version now shows a diff if a specific diff target is requested
Browse files Browse the repository at this point in the history
  • Loading branch information
philrenaud committed Sep 4, 2024
1 parent d67e9d0 commit 967fc86
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 25 deletions.
3 changes: 0 additions & 3 deletions command/agent/job_endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -771,9 +771,6 @@ func (s *HTTPServer) jobVersions(resp http.ResponseWriter, req *http.Request, jo
diffTagName := req.URL.Query().Get("diff_tag")
diffVersion := req.URL.Query().Get("diff_version")

// log out
s.logger.Debug("++==jobVersions", "diffs", diffsStr, "diff_tag", diffTagName, "diff_version", diffVersion)

var diffsBool bool
if diffsStr != "" {
var err error
Expand Down
8 changes: 3 additions & 5 deletions command/job_history.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,6 @@ func (c *JobHistoryCommand) Run(args []string) int {

// Prefix lookup matched a single job
versions, diffs, _, err := client.Jobs().Versions(jobID, diff, diffTag, diffVersion, q)
// TODO: something about diffs isn't ever giving me something for the 0th version. Maybe in job_endpoint.go instead?
if err != nil {
c.Ui.Error(fmt.Sprintf("Error retrieving job versions: %s", err))
return 1
Expand Down Expand Up @@ -247,15 +246,14 @@ func parseVersion(input string) (uint64, bool, error) {
func (c *JobHistoryCommand) formatJobVersions(versions []*api.Job, diffs []*api.JobDiff, full bool) error {
vLen := len(versions)
dLen := len(diffs)
if dLen != 0 && vLen != dLen+1 {
return fmt.Errorf("Number of job versions %d doesn't match number of diffs %d", vLen, dLen)
}

for i, version := range versions {
var diff *api.JobDiff
var nextVersion uint64
if i+1 <= dLen {
if dLen > i && diffs[i] != nil {
diff = diffs[i]
}
if i+1 < vLen { // if the current version is not the last version
nextVersion = *versions[i+1].Version
}

Expand Down
24 changes: 8 additions & 16 deletions nomad/job_endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -1268,8 +1268,6 @@ func (j *Job) GetJob(args *structs.JobSpecificRequest,
// GetJobVersions is used to retrieve all tracked versions of a job.
func (j *Job) GetJobVersions(args *structs.JobVersionsRequest,
reply *structs.JobVersionsResponse) error {
// log
j.logger.Debug("GetJobVersions", "args.DiffVersion", args.DiffVersion, "args.DiffTagName", args.DiffTagName)
authErr := j.srv.Authenticate(j.ctx, args)
if done, err := j.srv.forward("Job.GetJobVersions", args, args, reply); done {
return err
Expand Down Expand Up @@ -1310,17 +1308,10 @@ func (j *Job) GetJobVersions(args *structs.JobVersionsRequest,
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 {
Expand All @@ -1334,8 +1325,6 @@ func (j *Job) GetJobVersions(args *structs.JobVersionsRequest,
}
}

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.
var maxModifyIndex uint64
Expand All @@ -1358,11 +1347,16 @@ func (j *Job) GetJobVersions(args *structs.JobVersionsRequest,

// Compute the diffs
if args.Diffs {
for i := 0; i < len(out)-1; i++ {
for i := 0; i < len(out); i++ {
// For each of your job's versions, counting from highest version to lowest
// it should either be compared to the NEXT version in out[], or to the pre-specified version from compareStatic.
if !compareStatic {
compareVersion = out[i+1]
if i+1 < len(out) && out[i+1] != nil {
compareVersion = out[i+1]
} else {
break
}
}
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)
Expand Down Expand Up @@ -1878,8 +1872,6 @@ func (j *Job) Plan(args *structs.JobPlanRequest, reply *structs.JobPlanResponse)
// var existingJob interface{}
var existingJob *structs.Job

j.logger.Debug("===== Plan args for specific diffs", "diffVersion", args.DiffVersion, "diffTagName", args.DiffTagName)

if args.DiffVersion != "" {
// Convert from string to uint64
diffVersion, err := strconv.ParseUint(args.DiffVersion, 10, 64)
Expand Down
2 changes: 1 addition & 1 deletion nomad/state/state_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -2281,7 +2281,7 @@ func (s *StateStore) jobsByIDPrefixAllNamespaces(ws memdb.WatchSet, prefix strin
return wrap, nil
}

// JobVersionsByID returns all the tracked versions of a job.
// JobVersionsByID returns all the tracked versions of a job, sorted in from highest version to lowest.
func (s *StateStore) JobVersionsByID(ws memdb.WatchSet, namespace, id string) ([]*structs.Job, error) {
txn := s.db.ReadTxn()

Expand Down

0 comments on commit 967fc86

Please sign in to comment.