Skip to content
This repository has been archived by the owner on Jan 8, 2024. It is now read-only.

Commit

Permalink
Include generated plugin report and overall status in message
Browse files Browse the repository at this point in the history
  • Loading branch information
briancain committed May 24, 2021
1 parent dfcbaf3 commit 9c0c8e9
Show file tree
Hide file tree
Showing 5 changed files with 848 additions and 814 deletions.
55 changes: 37 additions & 18 deletions internal/core/app_status_report.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/zclconf/go-cty/cty"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/types/known/anypb"

"github.com/hashicorp/go-argmapper"
"github.com/hashicorp/go-hclog"
Expand All @@ -22,35 +23,35 @@ import (
func (a *App) DeploymentStatusReport(
ctx context.Context,
deployTarget *pb.Deployment,
) (*pb.StatusReport, *sdk.StatusReport, error) {
) (*pb.StatusReport, error) {
var evalCtx hcl.EvalContext
// Load the deployment variables context
if err := a.deployStatusReportEvalContext(ctx, deployTarget, &evalCtx); err != nil {
return nil, nil, err
return nil, err
}

// Load variables from deploy
hclCtx := evalCtx.NewChild()
if _, err := a.deployEvalContext(ctx, hclCtx); err != nil {
return nil, nil, err
return nil, err
}

c, err := a.createStatusReporter(ctx, hclCtx, component.PlatformType)
if status.Code(err) == codes.Unimplemented {
c = nil
err = nil
a.logger.Debug("status report is not implemented in plugin, cannot report on status")
return &pb.StatusReport{}, nil
}
if err != nil {
a.logger.Error("error creating component in platform", "error", err)
return nil, nil, err
return nil, err
}

a.logger.Debug("starting status report operation")
statusReporter, ok := c.Value.(component.Status)

if !ok || statusReporter.StatusFunc() == nil {
a.logger.Debug("component is not a Status or has no StatusFunc()")
return nil, nil, nil
return nil, nil
}
defer c.Close()

Expand All @@ -60,35 +61,35 @@ func (a *App) DeploymentStatusReport(
func (a *App) ReleaseStatusReport(
ctx context.Context,
releaseTarget *pb.Release,
) (*pb.StatusReport, *sdk.StatusReport, error) {
) (*pb.StatusReport, error) {
var evalCtx hcl.EvalContext
// Load the deployment variables context
if err := a.releaseStatusReportEvalContext(ctx, releaseTarget, &evalCtx); err != nil {
return nil, nil, err
return nil, err
}

// Load variables from deploy
hclCtx := evalCtx.NewChild()
if _, err := a.deployEvalContext(ctx, hclCtx); err != nil {
return nil, nil, err
return nil, err
}

c, err := a.createStatusReporter(ctx, &evalCtx, component.ReleaseManagerType)
if status.Code(err) == codes.Unimplemented {
c = nil
err = nil
a.logger.Debug("status report is not implemented in plugin, cannot report on status")
return &pb.StatusReport{}, nil
}
if err != nil {
a.logger.Error("error creating component in platform", "error", err)
return nil, nil, err
return nil, err
}

a.logger.Debug("starting status report operation")
statusReporter, ok := c.Value.(component.Status)

if !ok || statusReporter.StatusFunc() == nil {
a.logger.Debug("component is not a Status or has no StatusFunc()")
return nil, nil, nil
return nil, nil
}
defer c.Close()

Expand All @@ -102,7 +103,7 @@ func (a *App) statusReport(
loggerName string,
component *Component,
target interface{},
) (*pb.StatusReport, *sdk.StatusReport, error) {
) (*pb.StatusReport, error) {
if loggerName == "" {
loggerName = "statusreport"
}
Expand All @@ -112,7 +113,7 @@ func (a *App) statusReport(
Target: target,
})
if err != nil {
return nil, nil, err
return nil, err
}

var statusReport *sdk.StatusReport
Expand All @@ -122,11 +123,29 @@ func (a *App) statusReport(

reportResp, ok := msg.(*pb.StatusReport)
if !ok {
return nil, nil,
return nil,
status.Errorf(codes.FailedPrecondition, "unsupported status report response returned from plugin")
} else {
// Load Status Report message compiled by the plugin into the overall generated report
report, err := anypb.New(statusReport)
if err != nil {
return nil, err
}
reportResp.StatusReport = report

// Populate top level resource health with health in plugin compiled report
resourcesHealth := make([]*pb.StatusReport_Health, len(statusReport.Resources))
for i, r := range statusReport.Resources {
resourcesHealth[i] = &pb.StatusReport_Health{
HealthStatus: r.Health.String(),
HealthMessage: r.HealthMessage,
Name: r.Name,
}
}
reportResp.ResourcesHealth = resourcesHealth
}

return reportResp, statusReport, nil
return reportResp, nil
}

// Sets up the eval context for a status report for deployments
Expand Down
20 changes: 12 additions & 8 deletions internal/core/app_status_report_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import (
pb "github.com/hashicorp/waypoint/internal/server/gen"
serverptypes "github.com/hashicorp/waypoint/internal/server/ptypes"
"github.com/stretchr/testify/require"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/known/anypb"
)

func TestAppDeploymentStatusReport(t *testing.T) {
Expand Down Expand Up @@ -62,9 +64,8 @@ func TestAppDeploymentStatusReport(t *testing.T) {
mock.Status.On("StatusFunc").Return(nil)

// Status Report
srResp, statusReport, err := app.DeploymentStatusReport(context.Background(), deploy)
srResp, err := app.DeploymentStatusReport(context.Background(), deploy)
require.NoError(err)
require.Nil(statusReport)
require.Nil(srResp)

})
Expand Down Expand Up @@ -117,9 +118,11 @@ func TestAppDeploymentStatusReport(t *testing.T) {
})

// Status Report
_, statusReport, err := app.DeploymentStatusReport(context.Background(), deploy)
srResp, err := app.DeploymentStatusReport(context.Background(), deploy)
statusReport := &sdk.StatusReport{}
anypb.UnmarshalTo(srResp.StatusReport, statusReport, proto.UnmarshalOptions{})
require.NoError(err)
require.NotNil(statusReport)
require.NotNil(srResp.StatusReport)
require.NotNil(statusReport.Health)

})
Expand Down Expand Up @@ -181,9 +184,8 @@ func TestAppReleaseStatusReport(t *testing.T) {
mock.Status.On("StatusFunc").Return(nil)

// Status Report
srResp, statusReport, err := app.ReleaseStatusReport(context.Background(), release)
srResp, err := app.ReleaseStatusReport(context.Background(), release)
require.NoError(err)
require.Nil(statusReport)
require.Nil(srResp)

})
Expand Down Expand Up @@ -243,9 +245,11 @@ func TestAppReleaseStatusReport(t *testing.T) {
})

// Status Report
_, statusReport, err := app.ReleaseStatusReport(context.Background(), release)
srResp, err := app.ReleaseStatusReport(context.Background(), release)
statusReport := &sdk.StatusReport{}
anypb.UnmarshalTo(srResp.StatusReport, statusReport, proto.UnmarshalOptions{})
require.NoError(err)
require.NotNil(statusReport)
require.NotNil(srResp.StatusReport)
require.NotNil(statusReport.Health)

})
Expand Down
20 changes: 3 additions & 17 deletions internal/runner/operation_status_report.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ func (r *Runner) executeStatusReportOp(

switch t := op.StatusReport.Target.(type) {
case *pb.Job_StatusReportOp_Deployment:
statusReportResult, _, err = app.DeploymentStatusReport(ctx, t.Deployment)
statusReportResult, err = app.DeploymentStatusReport(ctx, t.Deployment)
case *pb.Job_StatusReportOp_Release:
statusReportResult, _, err = app.ReleaseStatusReport(ctx, t.Release)
statusReportResult, err = app.ReleaseStatusReport(ctx, t.Release)
default:
err = fmt.Errorf("unknown status report target: %T", op.StatusReport.Target)
}
Expand All @@ -40,23 +40,9 @@ func (r *Runner) executeStatusReportOp(
return nil, err
}

// Update to the latest deployment in order to get all the preload data.
var statusReport *pb.StatusReport

if statusReportResult != nil {
statusReport, err = r.client.GetStatusReport(ctx, &pb.GetStatusReportRequest{
Ref: &pb.Ref_Operation{
Target: &pb.Ref_Operation_Id{Id: statusReportResult.Id},
},
})
if err != nil {
return nil, err
}
}

return &pb.Job_Result{
StatusReport: &pb.Job_StatusReportResult{
StatusReport: statusReport,
StatusReport: statusReportResult,
},
}, nil
}
Loading

0 comments on commit 9c0c8e9

Please sign in to comment.