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 2024 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
}
180 changes: 180 additions & 0 deletions pkg/plugin/pipedsdk/deployment.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
// Copyright 2024 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.
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 that be 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 71 in pkg/plugin/pipedsdk/deployment.go

View check run for this annotation

Codecov / codecov/patch

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

Added lines #L70 - L71 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 77 in pkg/plugin/pipedsdk/deployment.go

View check run for this annotation

Codecov / codecov/patch

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

Added lines #L76 - L77 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 101 in pkg/plugin/pipedsdk/deployment.go

View check run for this annotation

Codecov / codecov/patch

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

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

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

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

View check run for this annotation

Codecov / codecov/patch

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

Added lines #L104 - L105 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 110 in pkg/plugin/pipedsdk/deployment.go

View check run for this annotation

Codecov / codecov/patch

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

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

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

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

View check run for this annotation

Codecov / codecov/patch

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

Added lines #L113 - L114 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 118 in pkg/plugin/pipedsdk/deployment.go

View check run for this annotation

Codecov / codecov/patch

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

Added lines #L117 - L118 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 121 in pkg/plugin/pipedsdk/deployment.go

View check run for this annotation

Codecov / codecov/patch

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

Added lines #L120 - L121 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 124 in pkg/plugin/pipedsdk/deployment.go

View check run for this annotation

Codecov / codecov/patch

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

Added lines #L123 - L124 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 127 in pkg/plugin/pipedsdk/deployment.go

View check run for this annotation

Codecov / codecov/patch

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

Added lines #L126 - L127 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 130 in pkg/plugin/pipedsdk/deployment.go

View check run for this annotation

Codecov / codecov/patch

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

Added lines #L129 - L130 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 133 in pkg/plugin/pipedsdk/deployment.go

View check run for this annotation

Codecov / codecov/patch

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

Added lines #L132 - L133 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 146 in pkg/plugin/pipedsdk/deployment.go

View check run for this annotation

Codecov / codecov/patch

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

Added lines #L145 - L146 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 151 in pkg/plugin/pipedsdk/deployment.go

View check run for this annotation

Codecov / codecov/patch

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

Added lines #L150 - L151 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 156 in pkg/plugin/pipedsdk/deployment.go

View check run for this annotation

Codecov / codecov/patch

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

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

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

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

View check run for this annotation

Codecov / codecov/patch

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

Added lines #L159 - L160 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 164 in pkg/plugin/pipedsdk/deployment.go

View check run for this annotation

Codecov / codecov/patch

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

Added lines #L163 - L164 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 167 in pkg/plugin/pipedsdk/deployment.go

View check run for this annotation

Codecov / codecov/patch

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

Added lines #L166 - L167 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 170 in pkg/plugin/pipedsdk/deployment.go

View check run for this annotation

Codecov / codecov/patch

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

Added lines #L169 - L170 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 173 in pkg/plugin/pipedsdk/deployment.go

View check run for this annotation

Codecov / codecov/patch

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

Added lines #L172 - L173 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 176 in pkg/plugin/pipedsdk/deployment.go

View check run for this annotation

Codecov / codecov/patch

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

Added lines #L175 - L176 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 179 in pkg/plugin/pipedsdk/deployment.go

View check run for this annotation

Codecov / codecov/patch

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

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