Skip to content

Commit

Permalink
Update plugin planner interface (#5015)
Browse files Browse the repository at this point in the history
Signed-off-by: khanhtc1202 <[email protected]>
  • Loading branch information
khanhtc1202 committed Jul 12, 2024
1 parent d757735 commit 06f2739
Show file tree
Hide file tree
Showing 10 changed files with 1,994 additions and 2,075 deletions.
417 changes: 56 additions & 361 deletions pkg/app/pipedv1/cmd/piped/service/service.pb.go

Large diffs are not rendered by default.

647 changes: 56 additions & 591 deletions pkg/app/pipedv1/cmd/piped/service/service.pb.validate.go

Large diffs are not rendered by default.

36 changes: 6 additions & 30 deletions pkg/app/pipedv1/cmd/piped/service/service.proto
Original file line number Diff line number Diff line change
Expand Up @@ -18,41 +18,17 @@ package grpc.piped.service;
option go_package = "github.com/pipe-cd/pipecd/pkg/app/pipedv1/cmd/piped/service";

import "validate/validate.proto";
import "pkg/model/command.proto";
import "pkg/model/analysis_result.proto";

// PluginService provides the ability to interact with plugins.
service PluginService {
// ListStageCommands returns the list requested commands to the given stage.
rpc ListStageCommands(ListStageCommandsRequest) returns (ListStageCommandsResponse) {}

// Put and Get the latest analysis result of a given application.
// Used by the analysis plugin to store and retrieve the latest analysis result.
rpc GetLatestAnalysisResult(GetLatestAnalysisResultRequest) returns (GetLatestAnalysisResultResponse) {}
rpc PutLatestAnalysisResult(PutLatestAnalysisResultRequest) returns (PutLatestAnalysisResultResponse) {}
}

message ListStageCommandsRequest {
string deployment_id = 1 [(validate.rules).string = {min_len: 1}];
string stage_id = 2 [(validate.rules).string = {min_len: 1}];
}

message ListStageCommandsResponse {
repeated model.Command commands = 1;
}

message GetLatestAnalysisResultRequest {
string application_id = 1 [(validate.rules).string = {min_len: 1}];
}

message GetLatestAnalysisResultResponse {
model.AnalysisResult analysis_result = 1;
// DecryptSecret decrypts the given secret.
rpc DecryptSecret(DecryptSecretRequest) returns (DecryptSecretResponse) {}
}

message PutLatestAnalysisResultRequest {
string application_id = 1 [(validate.rules).string = {min_len: 1}];
model.AnalysisResult analysis_result = 2;
message DecryptSecretRequest {
string secret = 1 [(validate.rules).string.min_len = 1];
}

message PutLatestAnalysisResultResponse {
message DecryptSecretResponse {
string decrypted_secret = 1;
}
108 changes: 16 additions & 92 deletions pkg/app/pipedv1/cmd/piped/service/service_grpc.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

150 changes: 76 additions & 74 deletions pkg/app/pipedv1/controller/planner.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,82 +150,84 @@ func (p *planner) Run(ctx context.Context) error {
controllermetrics.UpdateDeploymentStatus(p.deployment, p.doneDeploymentStatus)
}()

in := &platform.BuildPlanRequest{
Deployment: p.deployment,
WorkingDir: p.workingDir,
LastSuccessfulCommitHash: p.lastSuccessfulCommitHash,
LastSuccessfulConfigFileName: p.lastSuccessfulConfigFilename,
PipedConfig: p.pipedConfig,
}

out, err := p.pluginClient.BuildPlan(ctx, in)

// If the deployment was already cancelled, we ignore the plan result.
select {
case cmd := <-p.cancelledCh:
if cmd != nil {
p.doneDeploymentStatus = model.DeploymentStatus_DEPLOYMENT_CANCELLED
desc := fmt.Sprintf("Deployment was cancelled by %s while planning", cmd.Commander)
p.reportDeploymentCancelled(ctx, cmd.Commander, desc)
return cmd.Report(ctx, model.CommandStatus_COMMAND_SUCCEEDED, nil, nil)
}
default:
}

if err != nil {
p.doneDeploymentStatus = model.DeploymentStatus_DEPLOYMENT_FAILURE
return p.reportDeploymentFailed(ctx, fmt.Sprintf("Unable to plan the deployment (%v)", err))
}

p.doneDeploymentStatus = model.DeploymentStatus_DEPLOYMENT_PLANNED
return p.reportDeploymentPlanned(ctx, out.Plan)
return nil

// in := &platform.BuildPlanRequest{
// Deployment: p.deployment,
// WorkingDir: p.workingDir,
// LastSuccessfulCommitHash: p.lastSuccessfulCommitHash,
// LastSuccessfulConfigFileName: p.lastSuccessfulConfigFilename,
// PipedConfig: p.pipedConfig,
// }

// out, err := p.pluginClient.BuildPlan(ctx, in)

// // If the deployment was already cancelled, we ignore the plan result.
// select {
// case cmd := <-p.cancelledCh:
// if cmd != nil {
// p.doneDeploymentStatus = model.DeploymentStatus_DEPLOYMENT_CANCELLED
// desc := fmt.Sprintf("Deployment was cancelled by %s while planning", cmd.Commander)
// p.reportDeploymentCancelled(ctx, cmd.Commander, desc)
// return cmd.Report(ctx, model.CommandStatus_COMMAND_SUCCEEDED, nil, nil)
// }
// default:
// }

// if err != nil {
// p.doneDeploymentStatus = model.DeploymentStatus_DEPLOYMENT_FAILURE
// return p.reportDeploymentFailed(ctx, fmt.Sprintf("Unable to plan the deployment (%v)", err))
// }

// p.doneDeploymentStatus = model.DeploymentStatus_DEPLOYMENT_PLANNED
// return p.reportDeploymentPlanned(ctx, out.Plan)
}

func (p *planner) reportDeploymentPlanned(ctx context.Context, out *platform.DeploymentPlan) error {
var (
err error
retry = pipedservice.NewRetry(10)
req = &pipedservice.ReportDeploymentPlannedRequest{
DeploymentId: p.deployment.Id,
Summary: out.Summary,
StatusReason: "The deployment has been planned",
RunningCommitHash: p.lastSuccessfulCommitHash,
RunningConfigFilename: p.lastSuccessfulConfigFilename,
Versions: out.Versions,
Stages: out.Stages,
DeploymentChainId: p.deployment.DeploymentChainId,
DeploymentChainBlockIndex: p.deployment.DeploymentChainBlockIndex,
}
)

accounts, err := p.getMentionedAccounts(model.NotificationEventType_EVENT_DEPLOYMENT_PLANNED)
if err != nil {
p.logger.Error("failed to get the list of accounts", zap.Error(err))
}

defer func() {
p.notifier.Notify(model.NotificationEvent{
Type: model.NotificationEventType_EVENT_DEPLOYMENT_PLANNED,
Metadata: &model.NotificationEventDeploymentPlanned{
Deployment: p.deployment,
Summary: out.Summary,
MentionedAccounts: accounts,
},
})
}()

for retry.WaitNext(ctx) {
if _, err = p.apiClient.ReportDeploymentPlanned(ctx, req); err == nil {
return nil
}
err = fmt.Errorf("failed to report deployment status to control-plane: %v", err)
}

if err != nil {
p.logger.Error("failed to mark deployment to be planned", zap.Error(err))
}
return err
}
// func (p *planner) reportDeploymentPlanned(ctx context.Context, out *platform.DeploymentPlan) error {
// var (
// err error
// retry = pipedservice.NewRetry(10)
// req = &pipedservice.ReportDeploymentPlannedRequest{
// DeploymentId: p.deployment.Id,
// Summary: out.Summary,
// StatusReason: "The deployment has been planned",
// RunningCommitHash: p.lastSuccessfulCommitHash,
// RunningConfigFilename: p.lastSuccessfulConfigFilename,
// Versions: out.Versions,
// Stages: out.Stages,
// DeploymentChainId: p.deployment.DeploymentChainId,
// DeploymentChainBlockIndex: p.deployment.DeploymentChainBlockIndex,
// }
// )

// accounts, err := p.getMentionedAccounts(model.NotificationEventType_EVENT_DEPLOYMENT_PLANNED)
// if err != nil {
// p.logger.Error("failed to get the list of accounts", zap.Error(err))
// }

// defer func() {
// p.notifier.Notify(model.NotificationEvent{
// Type: model.NotificationEventType_EVENT_DEPLOYMENT_PLANNED,
// Metadata: &model.NotificationEventDeploymentPlanned{
// Deployment: p.deployment,
// Summary: out.Summary,
// MentionedAccounts: accounts,
// },
// })
// }()

// for retry.WaitNext(ctx) {
// if _, err = p.apiClient.ReportDeploymentPlanned(ctx, req); err == nil {
// return nil
// }
// err = fmt.Errorf("failed to report deployment status to control-plane: %v", err)
// }

// if err != nil {
// p.logger.Error("failed to mark deployment to be planned", zap.Error(err))
// }
// return err
// }

func (p *planner) reportDeploymentFailed(ctx context.Context, reason string) error {
var (
Expand Down
Loading

0 comments on commit 06f2739

Please sign in to comment.