Skip to content

Commit

Permalink
Support test and (re)submit submissions by ID
Browse files Browse the repository at this point in the history
  • Loading branch information
discordianfish committed Feb 10, 2023
1 parent 46a4235 commit cc75e7f
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 23 deletions.
7 changes: 5 additions & 2 deletions pkg/cmd/agent/submit.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ func NewSubmitCmd(logger *log.Logger) *cobra.Command {
Long: `This takes a docker image or submission manifest and submits it for evaluation.`,
Args: cobra.MaximumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
var manifest *client.Manifest
if len(args) > 0 {
submissionConfig.Image = args[0]
} else if submissionConfig.ManifestPath == "" {
Expand All @@ -53,8 +54,10 @@ func NewSubmitCmd(logger *log.Logger) *cobra.Command {
level.Error(logger).Log("msg", err.Error())
os.Exit(1)
}

submission, err := submissionConfig.Submission()
if submissionConfig.ManifestPath != "" {
manifest, err = client.ManifestFromPath(submissionConfig.ManifestPath)
}
submission, err := submissionConfig.Submission(manifest)
if err != nil {
level.Error(logger).Log("msg", "failed to configure manifest", "err", err.Error())
os.Exit(1)
Expand Down
32 changes: 30 additions & 2 deletions pkg/cmd/agent/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,42 @@ func NewTestCmd(logger *log.Logger) *cobra.Command {
to DIAMBRA. This is useful for testing your agent before submitting it. Optionally, you can pass in commands to run instead of the configured entrypoint.`,
Args: cobra.MaximumNArgs(2),
Run: func(cmd *cobra.Command, args []string) {
nargs := len(args)
var (
nargs = len(args)
manifest *client.Manifest
)
if nargs > 0 {
submissionConfig.Image = args[0]
}
if nargs > 1 {
submissionConfig.Command = args[1:]
}
submission, err := submissionConfig.Submission()
switch {
case submissionConfig.SubmissionID != 0:
cl, err := client.NewClient(logger, c.CredPath)
if err != nil {
level.Error(logger).Log("msg", "failed to create client", "err", err.Error())
os.Exit(1)
}
s, err := cl.Submission(submissionConfig.SubmissionID)
if err != nil {
level.Error(logger).Log("msg", "failed to get submission", "err", err.Error())
os.Exit(1)
}
manifest = &s.Manifest
case submissionConfig.ManifestPath != "":
manifest, err = client.ManifestFromPath(submissionConfig.ManifestPath)
if err != nil {
level.Error(logger).Log("msg", "failed to read manifest", "err", err.Error())
os.Exit(1)
}
default:
if submissionConfig.Image == "" {
level.Error(logger).Log("msg", "either image, manifest path or submission id must be provided")
os.Exit(1)
}
}
submission, err := submissionConfig.Submission(manifest)
if err != nil {
level.Error(logger).Log("msg", "failed to configure manifest", "err", err.Error())
os.Exit(1)
Expand Down
2 changes: 1 addition & 1 deletion pkg/diambra/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func (c *Client) Request(method string, path string, body io.Reader, authenticat
if err != nil {
return nil, err
}
level.Debug(c.logger).Log("msg", "Request", "method", method, "path", path, "body", body, "authenticated", authenticated, "apiURL", apiURL)
level.Debug(c.logger).Log("msg", "Request", "method", method, "url", surl, "authenticated", authenticated)

req, err := http.NewRequest(
method,
Expand Down
32 changes: 32 additions & 0 deletions pkg/diambra/client/submit.go → pkg/diambra/client/submission.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ import (
"fmt"
"io"
"net/http"
"os"

"github.com/go-kit/log/level"
"gopkg.in/yaml.v2"
)

// Mode Enum
Expand Down Expand Up @@ -62,3 +64,33 @@ func (c *Client) Submit(submission *Submission) (int, error) {
}
return s.ID, nil
}

func (c *Client) Submission(id int) (*Submission, error) {
resp, err := c.Request("GET", fmt.Sprintf("submissions/%d", id), nil, true)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
errResp, err := io.ReadAll(resp.Body)
if err != nil {
errResp = []byte(fmt.Sprintf("failed to read error response: %s", err))
}
return nil, fmt.Errorf("failed to get submission: %s: %s", resp.Status, errResp)
}
var s Submission
if err := json.NewDecoder(resp.Body).Decode(&s); err != nil {
return nil, err
}
return &s, nil
}

func ManifestFromPath(path string) (*Manifest, error) {
manifest := &Manifest{}
f, err := os.Open(path)
if err != nil {
return nil, fmt.Errorf("failed to open manifest: %w", err)
}
defer f.Close()
return manifest, yaml.NewDecoder(f).Decode(manifest)
}
24 changes: 6 additions & 18 deletions pkg/diambra/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import (
"github.com/go-kit/log"
"github.com/go-kit/log/level"
"github.com/spf13/pflag"
"gopkg.in/yaml.v2"
)

const (
Expand Down Expand Up @@ -238,6 +237,7 @@ type SubmissionConfig struct {
Secrets map[string]string
Command []string
ManifestPath string
SubmissionID int
}

func NewSubmissionConfig(logger log.Logger) *SubmissionConfig {
Expand All @@ -253,25 +253,13 @@ func (c *SubmissionConfig) AddFlags(flags *pflag.FlagSet) {
flags.StringToStringVarP(&c.Sources, "submission.source", "u", nil, "Source urls to pass to the agent")
flags.StringToStringVar(&c.Secrets, "submission.secret", nil, "Secrets to pass to the agent")
flags.StringVar(&c.ManifestPath, "submission.manifest", "", "Path to manifest file.")
flags.IntVar(&c.SubmissionID, "submission.id", 0, "Submission ID to retrieve manifest from")
}

func (c *SubmissionConfig) Submission() (*client.Submission, error) {
if c.Image == "" && c.ManifestPath == "" {
return nil, fmt.Errorf("either image or manifest path must be provided")
func (c *SubmissionConfig) Submission(manifest *client.Manifest) (*client.Submission, error) {
if manifest == nil {
manifest = &client.Manifest{}
}
// Decode manifestPath
var manifest client.Manifest
if c.ManifestPath != "" {
f, err := os.Open(c.ManifestPath)
if err != nil {
return nil, fmt.Errorf("failed to open manifest: %w", err)
}
defer f.Close()
if err := yaml.NewDecoder(f).Decode(&manifest); err != nil {
return nil, fmt.Errorf("failed to decode manifest: %w", err)
}
}

if c.Image != "" {
manifest.Image = c.Image
}
Expand Down Expand Up @@ -304,7 +292,7 @@ func (c *SubmissionConfig) Submission() (*client.Submission, error) {
}

return &client.Submission{
Manifest: manifest,
Manifest: *manifest,
Secrets: c.Secrets,
}, nil
}

0 comments on commit cc75e7f

Please sign in to comment.