diff --git a/grpc-server/hardware.go b/grpc-server/hardware.go index 884c5b9a0..9a4db0a65 100644 --- a/grpc-server/hardware.go +++ b/grpc-server/hardware.go @@ -44,23 +44,20 @@ func (s *server) Push(ctx context.Context, in *hardware.PushRequest) (*hardware. return &hardware.Empty{}, err } - var fn func() error - msg := "" + const msg = "inserting into DB" data, err := json.Marshal(hw) if err != nil { logger.Error(err) } labels["op"] = "insert" - msg = "inserting into DB" - fn = func() error { return s.db.InsertIntoDB(ctx, string(data)) } metrics.CacheTotals.With(labels).Inc() timer := prometheus.NewTimer(metrics.CacheDuration.With(labels)) defer timer.ObserveDuration() logger.Info(msg) - err = fn() + err = s.db.InsertIntoDB(ctx, string(data)) logger.Info("done " + msg) if err != nil { metrics.CacheErrors.With(labels).Inc() @@ -261,17 +258,15 @@ func (s *server) Delete(ctx context.Context, in *hardware.DeleteRequest) (*hardw logger.With("id", in.Id).Info("data deleted") - var fn func() error labels["op"] = "delete" - msg := "deleting into DB" - fn = func() error { return s.db.DeleteFromDB(ctx, in.Id) } + const msg = "deleting into DB" metrics.CacheTotals.With(labels).Inc() timer := prometheus.NewTimer(metrics.CacheDuration.With(labels)) defer timer.ObserveDuration() logger.Info(msg) - err := fn() + err := s.db.DeleteFromDB(ctx, in.Id) logger.Info("done " + msg) if err != nil { metrics.CacheErrors.With(labels).Inc() diff --git a/grpc-server/template.go b/grpc-server/template.go index 2fb95ff09..0bc6c09be 100644 --- a/grpc-server/template.go +++ b/grpc-server/template.go @@ -19,18 +19,16 @@ func (s *server) CreateTemplate(ctx context.Context, in *template.WorkflowTempla metrics.CacheInFlight.With(labels).Inc() defer metrics.CacheInFlight.With(labels).Dec() - msg := "" + const msg = "creating a new Template" labels["op"] = "createtemplate" - msg = "creating a new Template" id, _ := uuid.NewUUID() - fn := func() error { return s.db.CreateTemplate(ctx, in.Name, in.Data, id) } metrics.CacheTotals.With(labels).Inc() timer := prometheus.NewTimer(metrics.CacheDuration.With(labels)) defer timer.ObserveDuration() logger.Info(msg) - err := fn() + err := s.db.CreateTemplate(ctx, in.Name, in.Data, id) if err != nil { metrics.CacheErrors.With(labels).Inc() l := logger @@ -51,17 +49,15 @@ func (s *server) GetTemplate(ctx context.Context, in *template.GetRequest) (*tem metrics.CacheInFlight.With(labels).Inc() defer metrics.CacheInFlight.With(labels).Dec() - msg := "" + const msg = "getting a template" labels["op"] = "get" - msg = "getting a template" - fn := func() (string, string, error) { return s.db.GetTemplate(ctx, in.Id) } metrics.CacheTotals.With(labels).Inc() timer := prometheus.NewTimer(metrics.CacheDuration.With(labels)) defer timer.ObserveDuration() logger.Info(msg) - n, d, err := fn() + n, d, err := s.db.GetTemplate(ctx, in.Id) logger.Info("done " + msg) if err != nil { metrics.CacheErrors.With(labels).Inc() @@ -81,17 +77,15 @@ func (s *server) DeleteTemplate(ctx context.Context, in *template.GetRequest) (* metrics.CacheInFlight.With(labels).Inc() defer metrics.CacheInFlight.With(labels).Dec() - msg := "" + const msg = "deleting a template" labels["op"] = "delete" - msg = "deleting a template" - fn := func() error { return s.db.DeleteTemplate(ctx, in.Id) } metrics.CacheTotals.With(labels).Inc() timer := prometheus.NewTimer(metrics.CacheDuration.With(labels)) defer timer.ObserveDuration() logger.Info(msg) - err := fn() + err := s.db.DeleteTemplate(ctx, in.Id) logger.Info("done " + msg) if err != nil { metrics.CacheErrors.With(labels).Inc() @@ -142,17 +136,15 @@ func (s *server) UpdateTemplate(ctx context.Context, in *template.WorkflowTempla metrics.CacheInFlight.With(labels).Inc() defer metrics.CacheInFlight.With(labels).Dec() - msg := "" + const msg = "updating a template" labels["op"] = "updatetemplate" - msg = "updating a template" - fn := func() error { return s.db.UpdateTemplate(ctx, in.Name, in.Data, uuid.MustParse(in.Id)) } metrics.CacheTotals.With(labels).Inc() timer := prometheus.NewTimer(metrics.CacheDuration.With(labels)) defer timer.ObserveDuration() logger.Info(msg) - err := fn() + err := s.db.UpdateTemplate(ctx, in.Name, in.Data, uuid.MustParse(in.Id)) logger.Info("done " + msg) if err != nil { metrics.CacheErrors.With(labels).Inc() diff --git a/grpc-server/workflow.go b/grpc-server/workflow.go index 1f76f5953..c34766d42 100644 --- a/grpc-server/workflow.go +++ b/grpc-server/workflow.go @@ -30,37 +30,35 @@ func (s *server) CreateWorkflow(ctx context.Context, in *workflow.CreateRequest) labels := prometheus.Labels{"method": "CreateWorkflow", "op": ""} metrics.CacheInFlight.With(labels).Inc() defer metrics.CacheInFlight.With(labels).Dec() - msg := "" + + const msg = "creating a new workflow" labels["op"] = "createworkflow" - msg = "creating a new workflow" id, err := uuid.NewUUID() if err != nil { return &workflow.CreateResponse{}, err } - fn := func() error { - wf := db.Workflow{ - ID: id.String(), - Template: in.Template, - Hardware: in.Hardware, - State: workflow.State_value[workflow.State_PENDING.String()], - } - data, err := createYaml(ctx, s.db, in.Template, in.Hardware) - if err != nil { - return errors.Wrap(err, "Failed to create Yaml") - } - err = s.db.CreateWorkflow(ctx, wf, data, id) - if err != nil { - return err - } - return nil - } metrics.CacheTotals.With(labels).Inc() timer := prometheus.NewTimer(metrics.CacheDuration.With(labels)) defer timer.ObserveDuration() logger.Info(msg) - err = fn() + + data, err := createYaml(ctx, s.db, in.Template, in.Hardware) + if err != nil { + metrics.CacheErrors.With(labels).Inc() + err = errors.Wrap(err, "failed to create Yaml") + logger.Error(err) + return &workflow.CreateResponse{}, err + } + + wf := db.Workflow{ + ID: id.String(), + Template: in.Template, + Hardware: in.Hardware, + State: workflow.State_value[workflow.State_PENDING.String()], + } + err = s.db.CreateWorkflow(ctx, wf, data, id) if err != nil { metrics.CacheErrors.With(labels).Inc() l := logger @@ -70,6 +68,7 @@ func (s *server) CreateWorkflow(ctx context.Context, in *workflow.CreateRequest) l.Error(err) return &workflow.CreateResponse{}, err } + l := logger.With("workflowID", id.String()) l.Info("done " + msg) return &workflow.CreateResponse{Id: id.String()}, err @@ -82,17 +81,15 @@ func (s *server) GetWorkflow(ctx context.Context, in *workflow.GetRequest) (*wor metrics.CacheInFlight.With(labels).Inc() defer metrics.CacheInFlight.With(labels).Dec() - msg := "" + const msg = "getting a workflow" labels["op"] = "get" - msg = "getting a workflow" - fn := func() (db.Workflow, error) { return s.db.GetWorkflow(ctx, in.Id) } metrics.CacheTotals.With(labels).Inc() timer := prometheus.NewTimer(metrics.CacheDuration.With(labels)) defer timer.ObserveDuration() logger.Info(msg) - w, err := fn() + w, err := s.db.GetWorkflow(ctx, in.Id) if err != nil { metrics.CacheErrors.With(labels).Inc() l := logger @@ -124,21 +121,16 @@ func (s *server) DeleteWorkflow(ctx context.Context, in *workflow.GetRequest) (* metrics.CacheInFlight.With(labels).Inc() defer metrics.CacheInFlight.With(labels).Dec() - msg := "" + const msg = "deleting a workflow" labels["op"] = "delete" l := logger.With("workflowID", in.GetId()) - msg = "deleting a workflow" - fn := func() error { - // update only if not in running state - return s.db.DeleteWorkflow(ctx, in.Id, workflow.State_value[workflow.State_RUNNING.String()]) - } metrics.CacheTotals.With(labels).Inc() timer := prometheus.NewTimer(metrics.CacheDuration.With(labels)) defer timer.ObserveDuration() l.Info(msg) - err := fn() + err := s.db.DeleteWorkflow(ctx, in.Id, workflow.State_value[workflow.State_RUNNING.String()]) if err != nil { metrics.CacheErrors.With(labels).Inc() l := logger @@ -195,17 +187,15 @@ func (s *server) GetWorkflowContext(ctx context.Context, in *workflow.GetRequest metrics.CacheInFlight.With(labels).Inc() defer metrics.CacheInFlight.With(labels).Dec() - msg := "" + const msg = "getting a workflow" labels["op"] = "get" - msg = "getting a workflow" - fn := func() (*workflowpb.WorkflowContext, error) { return s.db.GetWorkflowContexts(ctx, in.Id) } metrics.CacheTotals.With(labels).Inc() timer := prometheus.NewTimer(metrics.CacheDuration.With(labels)) defer timer.ObserveDuration() logger.Info(msg) - w, err := fn() + w, err := s.db.GetWorkflowContexts(ctx, in.Id) if err != nil { metrics.CacheErrors.With(labels).Inc() l := logger diff --git a/http-server/http_handlers.go b/http-server/http_handlers.go index 6bb66c5db..ffcb7c16b 100644 --- a/http-server/http_handlers.go +++ b/http-server/http_handlers.go @@ -16,8 +16,8 @@ import ( "github.com/grpc-ecosystem/grpc-gateway/runtime" "github.com/grpc-ecosystem/grpc-gateway/utilities" - "github.com/tinkerbell/tink/protos/hardware" "github.com/tinkerbell/tink/pkg" + "github.com/tinkerbell/tink/protos/hardware" "google.golang.org/grpc" "google.golang.org/grpc/codes" "google.golang.org/grpc/status" diff --git a/protos/protoc.sh b/protos/protoc.sh index c4e50f3fb..f2cba378d 100755 --- a/protos/protoc.sh +++ b/protos/protoc.sh @@ -6,3 +6,4 @@ for proto in hardware packet template workflow; do echo "Generating ${proto}.pb.go..." protoc -I ./ -I ./common/ -I "$GOPATH"/src/github.com/grpc-ecosystem/grpc-gateway/third_party/googleapis "${proto}/${proto}.proto" --go_out=plugins=grpc:./ --grpc-gateway_out=logtostderr=true:. done +goimports -w . diff --git a/protos/workflow/workflow.pb.go b/protos/workflow/workflow.pb.go index 76f517c46..d9d50422d 100644 --- a/protos/workflow/workflow.pb.go +++ b/protos/workflow/workflow.pb.go @@ -8,6 +8,9 @@ package workflow import ( context "context" + reflect "reflect" + sync "sync" + proto "github.com/golang/protobuf/proto" timestamp "github.com/golang/protobuf/ptypes/timestamp" _ "google.golang.org/genproto/googleapis/api/annotations" @@ -16,8 +19,6 @@ import ( status "google.golang.org/grpc/status" protoreflect "google.golang.org/protobuf/reflect/protoreflect" protoimpl "google.golang.org/protobuf/runtime/protoimpl" - reflect "reflect" - sync "sync" ) const (