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

Add DeployTargetConfig and unmarshal config json #5582

Merged
merged 1 commit into from
Feb 19, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 68 additions & 32 deletions pkg/plugin/pipedsdk/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

import (
"context"
"encoding/json"
"fmt"

"go.uber.org/zap"
"google.golang.org/grpc"
Expand All @@ -34,15 +36,22 @@

Register(server *grpc.Server)
setCommonFields(commonFields)
setConfig([]byte) error
deployment.DeploymentServiceServer
}
)

// DeployTargetsNone is a type alias for a slice of pointers to DeployTarget
// with an empty struct as the generic type parameter. It represents a case
// where there are no deployment targets.
// This utility is defined for plugins which has no deploy targets handling in ExecuteStage.
type DeployTargetsNone = []*DeployTarget[struct{}]

// 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 {
PipelineSyncPlugin[Config]
type DeploymentPlugin[Config, DeployTargetConfig any] interface {
PipelineSyncPlugin[Config, DeployTargetConfig]

// DetermineVersions determines the versions of the resources that will be deployed.
DetermineVersions(context.Context, *Config, *Client, TODO) (TODO, error)
Expand All @@ -55,27 +64,38 @@
// 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 {
// The Config parameter is the plugin's config defined in piped's config.
type PipelineSyncPlugin[Config, DeployTargetConfig 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)
ExecuteStage(context.Context, *Config, []*DeployTarget[DeployTargetConfig], *Client, logpersister.StageLogPersister, TODO) (TODO, error)
}

// DeployTarget defines the deploy target configuration for the piped.
type DeployTarget[Config any] struct {
// The name of the deploy target.
Name string `json:"name"`
// The labes of the deploy target.
Labels map[string]string `json:"labels,omitempty"`
// The configuration of the deploy target.
Config Config `json:"config"`
}

// 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}
func RegisterDeploymentPlugin[Config, DeployTargetConfig any](plugin DeploymentPlugin[Config, DeployTargetConfig]) {
deploymentServiceServer = &DeploymentPluginServiceServer[Config, DeployTargetConfig]{base: plugin}

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

View check run for this annotation

Codecov / codecov/patch

pkg/plugin/pipedsdk/deployment.go#L91-L92

Added lines #L91 - L92 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}
func RegisterPipelineSyncPlugin[Config, DeployTargetConfig any](plugin PipelineSyncPlugin[Config, DeployTargetConfig]) {
deploymentServiceServer = &PipelineSyncPluginServiceServer[Config, DeployTargetConfig]{base: plugin}

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

View check run for this annotation

Codecov / codecov/patch

pkg/plugin/pipedsdk/deployment.go#L97-L98

Added lines #L97 - L98 were not covered by tests
}

type logPersister interface {
Expand All @@ -90,92 +110,108 @@
}

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

base DeploymentPlugin[Config]
base DeploymentPlugin[Config, DeployTargetConfig]
config Config
}

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

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#L122

Added line #L122 was not covered by tests
return s.base.Name()
}

func (s *DeploymentPluginServiceServer[Config]) Version() string {
func (s *DeploymentPluginServiceServer[Config, DeployTargetConfig]) Version() string {

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

View check run for this annotation

Codecov / codecov/patch

pkg/plugin/pipedsdk/deployment.go#L126

Added line #L126 was not covered by tests
return s.base.Version()
}

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

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#L131

Added line #L131 was not covered by tests
deployment.RegisterDeploymentServiceServer(server, s)
}

func (s *DeploymentPluginServiceServer[Config]) setCommonFields(fields commonFields) {
func (s *DeploymentPluginServiceServer[Config, DeployTargetConfig]) setCommonFields(fields commonFields) {

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

View check run for this annotation

Codecov / codecov/patch

pkg/plugin/pipedsdk/deployment.go#L135

Added line #L135 was not covered by tests
s.commonFields = fields
}

func (s *DeploymentPluginServiceServer[Config]) FetchDefinedStages(context.Context, *deployment.FetchDefinedStagesRequest) (*deployment.FetchDefinedStagesResponse, error) {
func (s *DeploymentPluginServiceServer[Config, DeployTargetConfig]) setConfig(bytes []byte) error {
if err := json.Unmarshal(bytes, &s.config); err != nil {
return fmt.Errorf("failed to unmarshal the plugin config: %v", err)
}
return nil

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

View check run for this annotation

Codecov / codecov/patch

pkg/plugin/pipedsdk/deployment.go#L139-L143

Added lines #L139 - L143 were not covered by tests
}

func (s *DeploymentPluginServiceServer[Config, DeployTargetConfig]) FetchDefinedStages(context.Context, *deployment.FetchDefinedStagesRequest) (*deployment.FetchDefinedStagesResponse, error) {

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#L146

Added line #L146 was not covered by tests
return &deployment.FetchDefinedStagesResponse{Stages: s.base.FetchDefinedStages()}, nil
}
func (s *DeploymentPluginServiceServer[Config]) DetermineVersions(context.Context, *deployment.DetermineVersionsRequest) (*deployment.DetermineVersionsResponse, error) {
func (s *DeploymentPluginServiceServer[Config, DeployTargetConfig]) DetermineVersions(context.Context, *deployment.DetermineVersionsRequest) (*deployment.DetermineVersionsResponse, error) {

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

View check run for this annotation

Codecov / codecov/patch

pkg/plugin/pipedsdk/deployment.go#L149

Added line #L149 was not covered by tests
return nil, status.Errorf(codes.Unimplemented, "method DetermineVersions not implemented")
}
func (s *DeploymentPluginServiceServer[Config]) DetermineStrategy(context.Context, *deployment.DetermineStrategyRequest) (*deployment.DetermineStrategyResponse, error) {
func (s *DeploymentPluginServiceServer[Config, DeployTargetConfig]) DetermineStrategy(context.Context, *deployment.DetermineStrategyRequest) (*deployment.DetermineStrategyResponse, error) {

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#L152

Added line #L152 was not covered by tests
return nil, status.Errorf(codes.Unimplemented, "method DetermineStrategy not implemented")
}
func (s *DeploymentPluginServiceServer[Config]) BuildPipelineSyncStages(context.Context, *deployment.BuildPipelineSyncStagesRequest) (*deployment.BuildPipelineSyncStagesResponse, error) {
func (s *DeploymentPluginServiceServer[Config, DeployTargetConfig]) BuildPipelineSyncStages(ctx context.Context, request *deployment.BuildPipelineSyncStagesRequest) (*deployment.BuildPipelineSyncStagesResponse, error) {

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

View check run for this annotation

Codecov / codecov/patch

pkg/plugin/pipedsdk/deployment.go#L155

Added line #L155 was not covered by tests
return nil, status.Errorf(codes.Unimplemented, "method BuildPipelineSyncStages not implemented")
}
func (s *DeploymentPluginServiceServer[Config]) BuildQuickSyncStages(context.Context, *deployment.BuildQuickSyncStagesRequest) (*deployment.BuildQuickSyncStagesResponse, error) {
func (s *DeploymentPluginServiceServer[Config, DeployTargetConfig]) BuildQuickSyncStages(context.Context, *deployment.BuildQuickSyncStagesRequest) (*deployment.BuildQuickSyncStagesResponse, error) {

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

View check run for this annotation

Codecov / codecov/patch

pkg/plugin/pipedsdk/deployment.go#L158

Added line #L158 was not covered by tests
return nil, status.Errorf(codes.Unimplemented, "method BuildQuickSyncStages not implemented")
}
func (s *DeploymentPluginServiceServer[Config]) ExecuteStage(context.Context, *deployment.ExecuteStageRequest) (*deployment.ExecuteStageResponse, error) {
func (s *DeploymentPluginServiceServer[Config, DeployTargetConfig]) ExecuteStage(context.Context, *deployment.ExecuteStageRequest) (*deployment.ExecuteStageResponse, error) {

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#L161

Added line #L161 was not covered by tests
return nil, status.Errorf(codes.Unimplemented, "method ExecuteStage not implemented")
}

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

base PipelineSyncPlugin[Config]
base PipelineSyncPlugin[Config, DeployTargetConfig]
config Config
}

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

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

View check run for this annotation

Codecov / codecov/patch

pkg/plugin/pipedsdk/deployment.go#L175

Added line #L175 was not covered by tests
return s.base.Name()
}

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

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#L180

Added line #L180 was not covered by tests
return s.base.Version()
}

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

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

View check run for this annotation

Codecov / codecov/patch

pkg/plugin/pipedsdk/deployment.go#L185

Added line #L185 was not covered by tests
deployment.RegisterDeploymentServiceServer(server, s)
}

func (s *PipelineSyncPluginServiceServer[Config]) setCommonFields(fields commonFields) {
func (s *PipelineSyncPluginServiceServer[Config, DeployTargetConfig]) setCommonFields(fields commonFields) {

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

View check run for this annotation

Codecov / codecov/patch

pkg/plugin/pipedsdk/deployment.go#L189

Added line #L189 was not covered by tests
s.commonFields = fields
}

func (s *PipelineSyncPluginServiceServer[Config]) FetchDefinedStages(context.Context, *deployment.FetchDefinedStagesRequest) (*deployment.FetchDefinedStagesResponse, error) {
func (s *PipelineSyncPluginServiceServer[Config, DeployTargetConfig]) setConfig(bytes []byte) error {
if err := json.Unmarshal(bytes, &s.config); err != nil {
return fmt.Errorf("failed to unmarshal the plugin config: %v", err)
}
return nil

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

View check run for this annotation

Codecov / codecov/patch

pkg/plugin/pipedsdk/deployment.go#L193-L197

Added lines #L193 - L197 were not covered by tests
}

func (s *PipelineSyncPluginServiceServer[Config, DeployTargetConfig]) FetchDefinedStages(context.Context, *deployment.FetchDefinedStagesRequest) (*deployment.FetchDefinedStagesResponse, error) {

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

View check run for this annotation

Codecov / codecov/patch

pkg/plugin/pipedsdk/deployment.go#L200

Added line #L200 was not covered by tests
return &deployment.FetchDefinedStagesResponse{Stages: s.base.FetchDefinedStages()}, nil
}
func (s *PipelineSyncPluginServiceServer[Config]) DetermineVersions(context.Context, *deployment.DetermineVersionsRequest) (*deployment.DetermineVersionsResponse, error) {
func (s *PipelineSyncPluginServiceServer[Config, DeployTargetConfig]) DetermineVersions(context.Context, *deployment.DetermineVersionsRequest) (*deployment.DetermineVersionsResponse, error) {

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

View check run for this annotation

Codecov / codecov/patch

pkg/plugin/pipedsdk/deployment.go#L203

Added line #L203 was not covered by tests
return nil, status.Errorf(codes.Unimplemented, "method DetermineVersions not implemented")
}
func (s *PipelineSyncPluginServiceServer[Config]) DetermineStrategy(context.Context, *deployment.DetermineStrategyRequest) (*deployment.DetermineStrategyResponse, error) {
func (s *PipelineSyncPluginServiceServer[Config, DeployTargetConfig]) DetermineStrategy(context.Context, *deployment.DetermineStrategyRequest) (*deployment.DetermineStrategyResponse, error) {

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

View check run for this annotation

Codecov / codecov/patch

pkg/plugin/pipedsdk/deployment.go#L206

Added line #L206 was not covered by tests
return nil, status.Errorf(codes.Unimplemented, "method DetermineStrategy not implemented")
}
func (s *PipelineSyncPluginServiceServer[Config]) BuildPipelineSyncStages(context.Context, *deployment.BuildPipelineSyncStagesRequest) (*deployment.BuildPipelineSyncStagesResponse, error) {
func (s *PipelineSyncPluginServiceServer[Config, DeployTargetConfig]) BuildPipelineSyncStages(ctx context.Context, request *deployment.BuildPipelineSyncStagesRequest) (*deployment.BuildPipelineSyncStagesResponse, error) {

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

View check run for this annotation

Codecov / codecov/patch

pkg/plugin/pipedsdk/deployment.go#L209

Added line #L209 was not covered by tests
return nil, status.Errorf(codes.Unimplemented, "method BuildPipelineSyncStages not implemented")
}
func (s *PipelineSyncPluginServiceServer[Config]) BuildQuickSyncStages(context.Context, *deployment.BuildQuickSyncStagesRequest) (*deployment.BuildQuickSyncStagesResponse, error) {
func (s *PipelineSyncPluginServiceServer[Config, DeployTargetConfig]) BuildQuickSyncStages(context.Context, *deployment.BuildQuickSyncStagesRequest) (*deployment.BuildQuickSyncStagesResponse, error) {

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

View check run for this annotation

Codecov / codecov/patch

pkg/plugin/pipedsdk/deployment.go#L212

Added line #L212 was not covered by tests
return nil, status.Errorf(codes.Unimplemented, "method BuildQuickSyncStages not implemented")
}
func (s *PipelineSyncPluginServiceServer[Config]) ExecuteStage(context.Context, *deployment.ExecuteStageRequest) (*deployment.ExecuteStageResponse, error) {
func (s *PipelineSyncPluginServiceServer[Config, DeployTargetConfig]) ExecuteStage(context.Context, *deployment.ExecuteStageRequest) (*deployment.ExecuteStageResponse, error) {

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

View check run for this annotation

Codecov / codecov/patch

pkg/plugin/pipedsdk/deployment.go#L215

Added line #L215 was not covered by tests
return nil, status.Errorf(codes.Unimplemented, "method ExecuteStage not implemented")
}
4 changes: 4 additions & 0 deletions pkg/plugin/pipedsdk/sdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,10 @@
logPersister: persister,
client: pipedapiClient,
})
if err := deploymentServiceServer.setConfig(cfg.Config); err != nil {
input.Logger.Error("failed to set configuration", zap.Error(err))
return err
}

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

View check run for this annotation

Codecov / codecov/patch

pkg/plugin/pipedsdk/sdk.go#L165-L168

Added lines #L165 - L168 were not covered by tests
var (
opts = []rpc.Option{
rpc.WithPort(cfg.Port),
Expand Down