Skip to content

Commit

Permalink
make pvn-wrapper exec post results to Prodvana API (#40)
Browse files Browse the repository at this point in the history
  • Loading branch information
naphatkrit authored Mar 19, 2024
1 parent 778cb25 commit ce98628
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 20 deletions.
8 changes: 5 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
module github.com/prodvana/pvn-wrapper

go 1.21
go 1.22

toolchain go1.22.1

require (
github.com/spf13/cobra v1.7.0
google.golang.org/grpc v1.59.0
google.golang.org/protobuf v1.31.0
google.golang.org/protobuf v1.33.0
k8s.io/api v0.27.6
knative.dev/pkg v0.0.0-20231023151236-29775d7c9e5c
knative.dev/serving v0.39.2
Expand Down Expand Up @@ -56,7 +58,7 @@ require (
require (
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/pkg/errors v0.9.1
github.com/prodvana/prodvana-public/go/prodvana-sdk v0.3.20
github.com/prodvana/prodvana-public/go/prodvana-sdk v0.3.33
github.com/spf13/pflag v1.0.5 // indirect
github.com/stretchr/testify v1.8.4
golang.org/x/sync v0.5.0
Expand Down
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@ github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/prodvana/prodvana-public/go/prodvana-sdk v0.3.20 h1:+AUELjPpwwkQ9Tw1w3AhfmoFYzNQb9lCB71ZgXz8jkY=
github.com/prodvana/prodvana-public/go/prodvana-sdk v0.3.20/go.mod h1:akKYnhWn4eXFdE01XtseyD19KYyIfdd3+G8wGDbV+og=
github.com/prodvana/prodvana-public/go/prodvana-sdk v0.3.33 h1:EVmgwqJy7qLB4UtFbxkHudBs0wFlh3z3MZaffZ7MSVk=
github.com/prodvana/prodvana-public/go/prodvana-sdk v0.3.33/go.mod h1:FzjJuPW6kjapJfmOqSjrPC19uMmNJuIdozPR6C/ZTsQ=
github.com/prometheus/client_golang v1.17.0 h1:rl2sfwZMtSthVU752MqfjQozy7blglC+1SOtjMAMh+Q=
github.com/prometheus/client_golang v1.17.0/go.mod h1:VeL+gMmOAxkS2IqfCq0ZmHSL+LjWfWDUmp1mBz9JgUY=
github.com/prometheus/client_model v0.4.1-0.20230718164431-9a2bf3000d16 h1:v7DLqVdK4VrYkVD5diGdl4sxJurKJEMnODWRJlxV9oM=
Expand Down Expand Up @@ -184,8 +184,8 @@ google.golang.org/grpc v1.59.0 h1:Z5Iec2pjwb+LEOqzpB2MR12/eKFhDPhuqW91O+4bwUk=
google.golang.org/grpc v1.59.0/go.mod h1:aUPDwccQo6OTjy7Hct4AfBPD1GptF4fyUjIkQ9YtF98=
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
google.golang.org/protobuf v1.31.0 h1:g0LDEJHgrBl9N9r17Ru3sqWhkIx2NB67okBHPwC7hs8=
google.golang.org/protobuf v1.31.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
google.golang.org/protobuf v1.33.0 h1:uNO2rsAINq/JlFpSdYEKIZ0uKD/R9cpdv0T+yoGwGmI=
google.golang.org/protobuf v1.33.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
Expand Down
51 changes: 38 additions & 13 deletions result/result.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
"github.com/pkg/errors"
"github.com/prodvana/prodvana-public/go/prodvana-sdk/client"
blobs_pb "github.com/prodvana/prodvana-public/go/prodvana-sdk/proto/prodvana/blobs"
"github.com/prodvana/prodvana-public/go/prodvana-sdk/proto/prodvana/pvn_wrapper"
pvn_wrapper_pb "github.com/prodvana/prodvana-public/go/prodvana-sdk/proto/prodvana/pvn_wrapper"
"google.golang.org/grpc"
"google.golang.org/protobuf/encoding/protojson"
)
Expand Down Expand Up @@ -138,22 +138,32 @@ func downloadBlob(ctx context.Context, blobsClient blobs_pb.BlobsManagerClient,

// Handle the "main" function of wrapper commands.
// This function never returns.
func RunWrapper(inputFiles []InputFile, successExitCodes []int32, run func(context.Context) (*pvn_wrapper.Output, []OutputFileUpload, error)) {
func RunWrapper(inputFiles []InputFile, successExitCodes []int32, run func(context.Context) (*pvn_wrapper_pb.Output, []OutputFileUpload, error)) {
ctx := context.Background()
var conn *grpc.ClientConn
getProdvanaConnection := func() *grpc.ClientConn {
var err error
conn, err = client.MakeProdvanaConnection(client.DefaultConnectionOptions())
if err != nil {
// TODO(naphat) should we return json in the event of infra errors too?
log.Fatal(err)
}
return conn
}
var blobsClient blobs_pb.BlobsManagerClient
getBlobsClient := func() blobs_pb.BlobsManagerClient {
if blobsClient == nil {
var err error
conn, err = client.MakeProdvanaConnection(client.DefaultConnectionOptions())
if err != nil {
// TODO(naphat) should we return json in the event of infra errors too?
log.Fatal(err)
}
blobsClient = blobs_pb.NewBlobsManagerClient(conn)
blobsClient = blobs_pb.NewBlobsManagerClient(getProdvanaConnection())
}
return blobsClient
}
var jobClient pvn_wrapper_pb.JobManagerClient
getJobClient := func() pvn_wrapper_pb.JobManagerClient {
if jobClient == nil {
jobClient = pvn_wrapper_pb.NewJobManagerClient(getProdvanaConnection())
}
return jobClient
}
defer func() {
if conn != nil {
_ = conn.Close()
Expand All @@ -168,10 +178,14 @@ func RunWrapper(inputFiles []InputFile, successExitCodes []int32, run func(conte
result, outputFiles, err := run(ctx)
duration := time.Since(startTs)
if err != nil {
result := &pvn_wrapper.Output{}
result := &pvn_wrapper_pb.Output{}
result.ExecError = err.Error()
result.ExitCode = -1
}
hostname, err := os.Hostname()
if err == nil {
result.Hostname = hostname
}
result.StartTimestampNs = startTs.UnixNano()
result.DurationNs = duration.Nanoseconds()
result.Version = PvnWrapperVersion
Expand Down Expand Up @@ -236,14 +250,25 @@ func RunWrapper(inputFiles []InputFile, successExitCodes []int32, run func(conte
}
result.StderrBlobId = id
} else {
result.Files = append(result.Files, &pvn_wrapper.OutputFile{
result.Files = append(result.Files, &pvn_wrapper_pb.OutputFile{
Name: file.Name,
ContentBlobId: id,
})
}
}
}

jobId := os.Getenv("PVN_JOB_ID")
if jobId != "" {
_, err := getJobClient().ReportJobResult(ctx, &pvn_wrapper_pb.ReportJobResultReq{
JobId: jobId,
Output: result,
})
if err != nil {
log.Fatal(err)
}
}

output, err := protojson.Marshal(result)
if err != nil {
// If something went wrong during encode/write to stdout, indicate that in stderr and exit non-zero.
Expand All @@ -260,13 +285,13 @@ func RunWrapper(inputFiles []InputFile, successExitCodes []int32, run func(conte
os.Exit(int(result.ExitCode))
}

func RunCmd(cmd *exec.Cmd) (*pvn_wrapper.Output, []OutputFileUpload, error) {
func RunCmd(cmd *exec.Cmd) (*pvn_wrapper_pb.Output, []OutputFileUpload, error) {
stdout := new(bytes.Buffer)
stderr := new(bytes.Buffer)
cmd.Stdout = stdout
cmd.Stderr = stderr

var result pvn_wrapper.Output
var result pvn_wrapper_pb.Output

err := cmd.Run()

Expand Down

0 comments on commit ce98628

Please sign in to comment.