Skip to content

Commit

Permalink
jobs: Remove errors from struct transformation
Browse files Browse the repository at this point in the history
  • Loading branch information
endocrimes committed Jul 4, 2019
1 parent 1761dda commit 48b1a56
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 34 deletions.
47 changes: 14 additions & 33 deletions command/agent/job_endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,10 +148,8 @@ func (s *HTTPServer) jobPlan(resp http.ResponseWriter, req *http.Request,
}

// If no region given, region is canonicalized to 'global'
sJob, err := ApiJobToStructJob(args.Job)
if err != nil {
return nil, err
}
sJob := ApiJobToStructJob(args.Job)

planReq := structs.JobPlanRequest{
Job: sJob,
Diff: args.Diff,
Expand Down Expand Up @@ -185,10 +183,8 @@ func (s *HTTPServer) ValidateJobRequest(resp http.ResponseWriter, req *http.Requ
return nil, CodedError(400, "Job must be specified")
}

job, err := ApiJobToStructJob(validateRequest.Job)
if err != nil {
return nil, err
}
job := ApiJobToStructJob(validateRequest.Job)

args := structs.JobValidateRequest{
Job: job,
WriteRequest: structs.WriteRequest{
Expand Down Expand Up @@ -396,10 +392,7 @@ func (s *HTTPServer) jobUpdate(resp http.ResponseWriter, req *http.Request,
}

// If no region given, region is canonicalized to 'global'
sJob, err := ApiJobToStructJob(args.Job)
if err != nil {
return nil, err
}
sJob := ApiJobToStructJob(args.Job)

regReq := structs.JobRegisterRequest{
Job: sJob,
Expand Down Expand Up @@ -613,7 +606,7 @@ func (s *HTTPServer) JobsParseRequest(resp http.ResponseWriter, req *http.Reques
return jobStruct, nil
}

func ApiJobToStructJob(job *api.Job) (*structs.Job, error) {
func ApiJobToStructJob(job *api.Job) *structs.Job {
job.Canonicalize()

j := &structs.Job{
Expand Down Expand Up @@ -680,18 +673,15 @@ func ApiJobToStructJob(job *api.Job) (*structs.Job, error) {
j.TaskGroups = make([]*structs.TaskGroup, l)
for i, taskGroup := range job.TaskGroups {
tg := &structs.TaskGroup{}
err := ApiTgToStructsTG(taskGroup, tg)
if err != nil {
return nil, err
}
ApiTgToStructsTG(taskGroup, tg)
j.TaskGroups[i] = tg
}
}

return j, nil
return j
}

func ApiTgToStructsTG(taskGroup *api.TaskGroup, tg *structs.TaskGroup) error {
func ApiTgToStructsTG(taskGroup *api.TaskGroup, tg *structs.TaskGroup) {
tg.Name = *taskGroup.Name
tg.Count = *taskGroup.Count
tg.Meta = taskGroup.Meta
Expand Down Expand Up @@ -741,23 +731,16 @@ func ApiTgToStructsTG(taskGroup *api.TaskGroup, tg *structs.TaskGroup) error {
if l := len(taskGroup.Volumes); l != 0 {
tg.HostVolumes = make(map[string]*structs.HostVolumeRequest, l)
for k, v := range taskGroup.Volumes {

// TODO(dani): Clean this up. Currently we parse host volume configs here to
// optimise the scheduling path. This might be a sign that we want to
// change the task configuration. Given there will only be two types
// 'csi' and 'host' however, denormalising on submission may be ok.

if v.Type != structs.VolumeTypeHost {
return CodedError(400, fmt.Sprintf("Unkown volume type: %s", v.Type))
// Ignore none host volumes in this iteration currently.
continue
}

var cfg structs.HostVolumeConfig
err := mapstructure.Decode(v.Config, &cfg)

if err != nil {
return CodedError(400, fmt.Sprintf("failed to decode volume config: %v", err))
}
if cfg.Source == "" {
return CodedError(400, fmt.Sprintf("volume '%s' missing required config key 'source'", v.Name))
// TODO: HACK: Ignore config parse errors for now and skip the volume -_-
continue
}

vol := &structs.Volume{
Expand Down Expand Up @@ -804,8 +787,6 @@ func ApiTgToStructsTG(taskGroup *api.TaskGroup, tg *structs.TaskGroup) error {
tg.Tasks[l] = t
}
}

return nil
}

// ApiTaskToStructsTask is a copy and type conversion between the API
Expand Down
2 changes: 1 addition & 1 deletion command/job_validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ func (c *JobValidateCommand) validateLocal(aj *api.Job) (*api.JobValidateRespons
var out api.JobValidateResponse

// TODO: Handle errors from volumes here / move those errors into canonicalize
job, _ := agent.ApiJobToStructJob(aj)
job := agent.ApiJobToStructJob(aj)
canonicalizeWarnings := job.Canonicalize()

if vErr := job.Validate(); vErr != nil {
Expand Down

0 comments on commit 48b1a56

Please sign in to comment.