worker: allow passing different jobs to workers

Until now, all jobs were put as "osbuild" jobs into the job queue and
the worker API hard-coded sending an osbuild manifest and upload
targets.

Change the API to take a "type" and "args" keys, which are equivalent to
the job-queue's type and args. Workers continue to support only osbuild
jobs, but this makes other jobs possible in the future.
This commit is contained in:
Lars Karlitski 2020-09-20 17:22:31 +02:00 committed by Tom Gundersen
parent 5b57814664
commit d3c99b8e93
5 changed files with 39 additions and 20 deletions

View file

@ -49,19 +49,20 @@ paths:
type: object
additionalProperties: false
properties:
manifest: {}
targets:
type: array
items: {}
id:
type: string
format: uuid
location:
type: string
artifact_location:
type: string
id:
type:
type: string
format: uuid
enum:
- osbuild
args: {}
required:
- manifest
- type
- location
- id
4XX:

View file

@ -5,6 +5,7 @@ import (
"context"
"crypto/tls"
"encoding/json"
"errors"
"fmt"
"io"
"net"
@ -35,10 +36,10 @@ type Job interface {
type job struct {
requester *http.Client
id uuid.UUID
manifest distro.Manifest
targets []*target.Target
location string
artifactLocation string
jobType string
args json.RawMessage
}
func NewClient(baseURL string, conf *tls.Config) (*Client, error) {
@ -115,8 +116,8 @@ func (c *Client) RequestJob() (Job, error) {
return &job{
requester: c.requester,
id: jr.Id,
manifest: jr.Manifest,
targets: jr.Targets,
jobType: jr.Type,
args: jr.Args,
location: location.String(),
artifactLocation: artifactLocation.String(),
}, nil
@ -127,7 +128,17 @@ func (j *job) Id() uuid.UUID {
}
func (j *job) OSBuildArgs() (distro.Manifest, []*target.Target, error) {
return j.manifest, j.targets, nil
if j.jobType != "osbuild" {
return nil, nil, errors.New("not an osbuild job")
}
var args OSBuildJob
err := json.Unmarshal(j.args, &args)
if err != nil {
return nil, nil, fmt.Errorf("error parsing osbuild job arguments: %v", err)
}
return args.Manifest, args.Targets, nil
}
func (j *job) Update(status common.ImageBuildState, result *osbuild.Result) error {

View file

@ -1,6 +1,8 @@
package worker
import (
"encoding/json"
"github.com/google/uuid"
"github.com/osbuild/osbuild-composer/internal/common"
"github.com/osbuild/osbuild-composer/internal/distro"
@ -30,11 +32,11 @@ type statusResponse struct {
}
type requestJobResponse struct {
Id uuid.UUID `json:"id"`
Manifest distro.Manifest `json:"manifest"`
Targets []*target.Target `json:"targets,omitempty"`
Location string `json:"location"`
ArtifactLocation string `json:"artifact_location"`
Id uuid.UUID `json:"id"`
Location string `json:"location"`
ArtifactLocation string `json:"artifact_location"`
Type string `json:"type"`
Args json.RawMessage `json:"args,omitempty"`
}
type getJobResponse struct {

View file

@ -263,12 +263,17 @@ func (h *apiHandlers) RequestJob(ctx echo.Context) error {
return err
}
serializedArgs, err := json.Marshal(jobArgs)
if err != nil {
return err
}
return ctx.JSON(http.StatusCreated, requestJobResponse{
Id: jobId,
Manifest: jobArgs.Manifest,
Targets: jobArgs.Targets,
Location: fmt.Sprintf("/jobs/%v", token),
ArtifactLocation: fmt.Sprintf("/jobs/%v/artifacts/", token),
Type: "osbuild",
Args: serializedArgs,
})
}

View file

@ -68,7 +68,7 @@ func TestCreate(t *testing.T) {
require.NoError(t, err)
test.TestRoute(t, server, false, "POST", "/jobs", `{}`, http.StatusCreated,
`{"manifest":{"sources":{},"pipeline":{}}}`, "id", "location", "artifact_location")
`{"type":"osbuild","args":{"manifest":{"pipeline":{},"sources":{}}}}`, "id", "location", "artifact_location")
}
func TestCancel(t *testing.T) {