Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prepare Plugin SDK #5529

Merged
merged 10 commits into from
Feb 13, 2025
33 changes: 33 additions & 0 deletions pkg/plugin/pipedsdk/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Copyright 2025 The PipeCD Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package pipedsdk

import "github.com/pipe-cd/pipecd/pkg/plugin/pipedapi"

// Client is a toolkit for interacting with the piped service.
// It provides methods to call the piped service APIs.
// It's a wrapper around the raw piped service client.
type Client struct {
base *pipedapi.PipedServiceClient

// applicationID is used to identify the application that the client is working with.
applicationID string
// deploymentID is used to identify the deployment that the client is working with.
// This field exists only when the client is working with a specific deployment; for example, when this client is passed as the deployoment plugin's argument.
deploymentID string
// stageID is used to identify the stage that the client is working with.
// This field exists only when the client is working with a specific stage; for example, when this client is passed as the ExecuteStage method's argument.
stageID string
}
181 changes: 181 additions & 0 deletions pkg/plugin/pipedsdk/deployment.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
// Copyright 2025 The PipeCD Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package pipedsdk

import (
"context"

"go.uber.org/zap"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"

config "github.com/pipe-cd/pipecd/pkg/configv1"
"github.com/pipe-cd/pipecd/pkg/plugin/api/v1alpha1/deployment"
"github.com/pipe-cd/pipecd/pkg/plugin/logpersister"
"github.com/pipe-cd/pipecd/pkg/plugin/pipedapi"
)

var (
deploymentServiceServer interface {
Plugin

Register(server *grpc.Server)
setCommonFields(commonFields)
deployment.DeploymentServiceServer
}
)

// DeploymentPlugin is the interface that be implemented by a full-spec deployment plugin.
// This kind of plugin should implement all methods to manage resources and execute stages.
// The Config parameter is the plugin's config defined in piped's config.
type DeploymentPlugin[Config any] interface {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Config parameter is the plugin's config defined in piped's config.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's add a comment to explain that 👍

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added config on this commit.
337e8c8

PipelineSyncPlugin[Config]

// DetermineVersions determines the versions of the resources that will be deployed.
DetermineVersions(context.Context, *Config, *Client, TODO) (TODO, error)
// DetermineStrategy determines the strategy to deploy the resources.
DetermineStrategy(context.Context, *Config, *Client, TODO) (TODO, error)
// BuildQuickSyncStages builds the stages that will be executed during the quick sync process.
BuildQuickSyncStages(context.Context, *Config, *Client, TODO) (TODO, error)
}

// PipelineSyncPlugin is the interface implemented by a pipeline sync plugin.
// This kind of plugin may not implement quick sync stages, and will not manage resources like deployment plugin.
// It only focuses on executing stages which is generic for all kinds of pipeline sync plugins.
type PipelineSyncPlugin[Config any] interface {
Plugin

// FetchDefinedStages returns the list of stages that the plugin can execute.
FetchDefinedStages() []string
// BuildPipelineSyncStages builds the stages that will be executed by the plugin.
BuildPipelineSyncStages(context.Context, *Config, *Client, TODO) (TODO, error)
// ExecuteStage executes the given stage.
ExecuteStage(context.Context, *Config, *Client, logpersister.StageLogPersister, TODO) (TODO, error)
}

// RegisterDeploymentPlugin registers the given deployment plugin.
// It will be used when running the piped.
func RegisterDeploymentPlugin[Config any](plugin DeploymentPlugin[Config]) {
deploymentServiceServer = &DeploymentPluginServiceServer[Config]{base: plugin}

Check warning on line 72 in pkg/plugin/pipedsdk/deployment.go

View check run for this annotation

Codecov / codecov/patch

pkg/plugin/pipedsdk/deployment.go#L71-L72

Added lines #L71 - L72 were not covered by tests
}

// RegisterPipelineSyncPlugin registers the given pipeline sync plugin.
// It will be used when running the piped.
func RegisterPipelineSyncPlugin[Config any](plugin PipelineSyncPlugin[Config]) {
deploymentServiceServer = &PipelineSyncPluginServiceServer[Config]{base: plugin}

Check warning on line 78 in pkg/plugin/pipedsdk/deployment.go

View check run for this annotation

Codecov / codecov/patch

pkg/plugin/pipedsdk/deployment.go#L77-L78

Added lines #L77 - L78 were not covered by tests
}

type logPersister interface {
StageLogPersister(deploymentID, stageID string) logpersister.StageLogPersister
}

type commonFields struct {
config *config.PipedPlugin
logger *zap.Logger
logPersister logPersister
client *pipedapi.PipedServiceClient
}

// DeploymentPluginServiceServer is the gRPC server that handles requests from the piped.
type DeploymentPluginServiceServer[Config any] struct {
deployment.UnimplementedDeploymentServiceServer
commonFields

base DeploymentPlugin[Config]
}

// Name returns the name of the plugin.
func (s *DeploymentPluginServiceServer[Config]) Name() string {
return s.base.Name()

Check warning on line 102 in pkg/plugin/pipedsdk/deployment.go

View check run for this annotation

Codecov / codecov/patch

pkg/plugin/pipedsdk/deployment.go#L101-L102

Added lines #L101 - L102 were not covered by tests
}

func (s *DeploymentPluginServiceServer[Config]) Version() string {
return s.base.Version()

Check warning on line 106 in pkg/plugin/pipedsdk/deployment.go

View check run for this annotation

Codecov / codecov/patch

pkg/plugin/pipedsdk/deployment.go#L105-L106

Added lines #L105 - L106 were not covered by tests
}

// Register registers the server to the given gRPC server.
func (s *DeploymentPluginServiceServer[Config]) Register(server *grpc.Server) {
deployment.RegisterDeploymentServiceServer(server, s)

Check warning on line 111 in pkg/plugin/pipedsdk/deployment.go

View check run for this annotation

Codecov / codecov/patch

pkg/plugin/pipedsdk/deployment.go#L110-L111

Added lines #L110 - L111 were not covered by tests
}

func (s *DeploymentPluginServiceServer[Config]) setCommonFields(fields commonFields) {
s.commonFields = fields

Check warning on line 115 in pkg/plugin/pipedsdk/deployment.go

View check run for this annotation

Codecov / codecov/patch

pkg/plugin/pipedsdk/deployment.go#L114-L115

Added lines #L114 - L115 were not covered by tests
}

func (s *DeploymentPluginServiceServer[Config]) FetchDefinedStages(context.Context, *deployment.FetchDefinedStagesRequest) (*deployment.FetchDefinedStagesResponse, error) {
return &deployment.FetchDefinedStagesResponse{Stages: s.base.FetchDefinedStages()}, nil

Check warning on line 119 in pkg/plugin/pipedsdk/deployment.go

View check run for this annotation

Codecov / codecov/patch

pkg/plugin/pipedsdk/deployment.go#L118-L119

Added lines #L118 - L119 were not covered by tests
}
func (s *DeploymentPluginServiceServer[Config]) DetermineVersions(context.Context, *deployment.DetermineVersionsRequest) (*deployment.DetermineVersionsResponse, error) {
return nil, status.Errorf(codes.Unimplemented, "method DetermineVersions not implemented")

Check warning on line 122 in pkg/plugin/pipedsdk/deployment.go

View check run for this annotation

Codecov / codecov/patch

pkg/plugin/pipedsdk/deployment.go#L121-L122

Added lines #L121 - L122 were not covered by tests
}
func (s *DeploymentPluginServiceServer[Config]) DetermineStrategy(context.Context, *deployment.DetermineStrategyRequest) (*deployment.DetermineStrategyResponse, error) {
return nil, status.Errorf(codes.Unimplemented, "method DetermineStrategy not implemented")

Check warning on line 125 in pkg/plugin/pipedsdk/deployment.go

View check run for this annotation

Codecov / codecov/patch

pkg/plugin/pipedsdk/deployment.go#L124-L125

Added lines #L124 - L125 were not covered by tests
}
func (s *DeploymentPluginServiceServer[Config]) BuildPipelineSyncStages(context.Context, *deployment.BuildPipelineSyncStagesRequest) (*deployment.BuildPipelineSyncStagesResponse, error) {
return nil, status.Errorf(codes.Unimplemented, "method BuildPipelineSyncStages not implemented")

Check warning on line 128 in pkg/plugin/pipedsdk/deployment.go

View check run for this annotation

Codecov / codecov/patch

pkg/plugin/pipedsdk/deployment.go#L127-L128

Added lines #L127 - L128 were not covered by tests
}
func (s *DeploymentPluginServiceServer[Config]) BuildQuickSyncStages(context.Context, *deployment.BuildQuickSyncStagesRequest) (*deployment.BuildQuickSyncStagesResponse, error) {
return nil, status.Errorf(codes.Unimplemented, "method BuildQuickSyncStages not implemented")

Check warning on line 131 in pkg/plugin/pipedsdk/deployment.go

View check run for this annotation

Codecov / codecov/patch

pkg/plugin/pipedsdk/deployment.go#L130-L131

Added lines #L130 - L131 were not covered by tests
}
func (s *DeploymentPluginServiceServer[Config]) ExecuteStage(context.Context, *deployment.ExecuteStageRequest) (*deployment.ExecuteStageResponse, error) {
return nil, status.Errorf(codes.Unimplemented, "method ExecuteStage not implemented")

Check warning on line 134 in pkg/plugin/pipedsdk/deployment.go

View check run for this annotation

Codecov / codecov/patch

pkg/plugin/pipedsdk/deployment.go#L133-L134

Added lines #L133 - L134 were not covered by tests
}

// PipelineSyncPluginServiceServer is the gRPC server that handles requests from the piped.
type PipelineSyncPluginServiceServer[Config any] struct {
deployment.UnimplementedDeploymentServiceServer
commonFields

base PipelineSyncPlugin[Config]
}

// Name returns the name of the plugin.
func (s *PipelineSyncPluginServiceServer[Config]) Name() string {
return s.base.Name()

Check warning on line 147 in pkg/plugin/pipedsdk/deployment.go

View check run for this annotation

Codecov / codecov/patch

pkg/plugin/pipedsdk/deployment.go#L146-L147

Added lines #L146 - L147 were not covered by tests
}

// Version returns the version of the plugin.
func (s *PipelineSyncPluginServiceServer[Config]) Version() string {
return s.base.Version()

Check warning on line 152 in pkg/plugin/pipedsdk/deployment.go

View check run for this annotation

Codecov / codecov/patch

pkg/plugin/pipedsdk/deployment.go#L151-L152

Added lines #L151 - L152 were not covered by tests
}

// Register registers the server to the given gRPC server.
func (s *PipelineSyncPluginServiceServer[Config]) Register(server *grpc.Server) {
deployment.RegisterDeploymentServiceServer(server, s)

Check warning on line 157 in pkg/plugin/pipedsdk/deployment.go

View check run for this annotation

Codecov / codecov/patch

pkg/plugin/pipedsdk/deployment.go#L156-L157

Added lines #L156 - L157 were not covered by tests
}

func (s *PipelineSyncPluginServiceServer[Config]) setCommonFields(fields commonFields) {
s.commonFields = fields

Check warning on line 161 in pkg/plugin/pipedsdk/deployment.go

View check run for this annotation

Codecov / codecov/patch

pkg/plugin/pipedsdk/deployment.go#L160-L161

Added lines #L160 - L161 were not covered by tests
}

func (s *PipelineSyncPluginServiceServer[Config]) FetchDefinedStages(context.Context, *deployment.FetchDefinedStagesRequest) (*deployment.FetchDefinedStagesResponse, error) {
return &deployment.FetchDefinedStagesResponse{Stages: s.base.FetchDefinedStages()}, nil

Check warning on line 165 in pkg/plugin/pipedsdk/deployment.go

View check run for this annotation

Codecov / codecov/patch

pkg/plugin/pipedsdk/deployment.go#L164-L165

Added lines #L164 - L165 were not covered by tests
}
func (s *PipelineSyncPluginServiceServer[Config]) DetermineVersions(context.Context, *deployment.DetermineVersionsRequest) (*deployment.DetermineVersionsResponse, error) {
return nil, status.Errorf(codes.Unimplemented, "method DetermineVersions not implemented")

Check warning on line 168 in pkg/plugin/pipedsdk/deployment.go

View check run for this annotation

Codecov / codecov/patch

pkg/plugin/pipedsdk/deployment.go#L167-L168

Added lines #L167 - L168 were not covered by tests
}
func (s *PipelineSyncPluginServiceServer[Config]) DetermineStrategy(context.Context, *deployment.DetermineStrategyRequest) (*deployment.DetermineStrategyResponse, error) {
return nil, status.Errorf(codes.Unimplemented, "method DetermineStrategy not implemented")

Check warning on line 171 in pkg/plugin/pipedsdk/deployment.go

View check run for this annotation

Codecov / codecov/patch

pkg/plugin/pipedsdk/deployment.go#L170-L171

Added lines #L170 - L171 were not covered by tests
}
func (s *PipelineSyncPluginServiceServer[Config]) BuildPipelineSyncStages(context.Context, *deployment.BuildPipelineSyncStagesRequest) (*deployment.BuildPipelineSyncStagesResponse, error) {
return nil, status.Errorf(codes.Unimplemented, "method BuildPipelineSyncStages not implemented")

Check warning on line 174 in pkg/plugin/pipedsdk/deployment.go

View check run for this annotation

Codecov / codecov/patch

pkg/plugin/pipedsdk/deployment.go#L173-L174

Added lines #L173 - L174 were not covered by tests
}
func (s *PipelineSyncPluginServiceServer[Config]) BuildQuickSyncStages(context.Context, *deployment.BuildQuickSyncStagesRequest) (*deployment.BuildQuickSyncStagesResponse, error) {
return nil, status.Errorf(codes.Unimplemented, "method BuildQuickSyncStages not implemented")

Check warning on line 177 in pkg/plugin/pipedsdk/deployment.go

View check run for this annotation

Codecov / codecov/patch

pkg/plugin/pipedsdk/deployment.go#L176-L177

Added lines #L176 - L177 were not covered by tests
}
func (s *PipelineSyncPluginServiceServer[Config]) ExecuteStage(context.Context, *deployment.ExecuteStageRequest) (*deployment.ExecuteStageResponse, error) {
return nil, status.Errorf(codes.Unimplemented, "method ExecuteStage not implemented")

Check warning on line 180 in pkg/plugin/pipedsdk/deployment.go

View check run for this annotation

Codecov / codecov/patch

pkg/plugin/pipedsdk/deployment.go#L179-L180

Added lines #L179 - L180 were not covered by tests
}
Loading