Skip to content

Commit 88ec254

Browse files
authored
Add DeployTargetConfig and unmarshal config json (#5582)
Signed-off-by: Shinnosuke Sawada-Dazai <[email protected]>
1 parent a281dcd commit 88ec254

File tree

2 files changed

+72
-32
lines changed

2 files changed

+72
-32
lines changed

pkg/plugin/pipedsdk/deployment.go

+68-32
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ package pipedsdk
1616

1717
import (
1818
"context"
19+
"encoding/json"
20+
"fmt"
1921

2022
"go.uber.org/zap"
2123
"google.golang.org/grpc"
@@ -34,15 +36,22 @@ var (
3436

3537
Register(server *grpc.Server)
3638
setCommonFields(commonFields)
39+
setConfig([]byte) error
3740
deployment.DeploymentServiceServer
3841
}
3942
)
4043

44+
// DeployTargetsNone is a type alias for a slice of pointers to DeployTarget
45+
// with an empty struct as the generic type parameter. It represents a case
46+
// where there are no deployment targets.
47+
// This utility is defined for plugins which has no deploy targets handling in ExecuteStage.
48+
type DeployTargetsNone = []*DeployTarget[struct{}]
49+
4150
// DeploymentPlugin is the interface that be implemented by a full-spec deployment plugin.
4251
// This kind of plugin should implement all methods to manage resources and execute stages.
4352
// The Config parameter is the plugin's config defined in piped's config.
44-
type DeploymentPlugin[Config any] interface {
45-
PipelineSyncPlugin[Config]
53+
type DeploymentPlugin[Config, DeployTargetConfig any] interface {
54+
PipelineSyncPlugin[Config, DeployTargetConfig]
4655

4756
// DetermineVersions determines the versions of the resources that will be deployed.
4857
DetermineVersions(context.Context, *Config, *Client, TODO) (TODO, error)
@@ -55,27 +64,38 @@ type DeploymentPlugin[Config any] interface {
5564
// PipelineSyncPlugin is the interface implemented by a pipeline sync plugin.
5665
// This kind of plugin may not implement quick sync stages, and will not manage resources like deployment plugin.
5766
// It only focuses on executing stages which is generic for all kinds of pipeline sync plugins.
58-
type PipelineSyncPlugin[Config any] interface {
67+
// The Config parameter is the plugin's config defined in piped's config.
68+
type PipelineSyncPlugin[Config, DeployTargetConfig any] interface {
5969
Plugin
6070

6171
// FetchDefinedStages returns the list of stages that the plugin can execute.
6272
FetchDefinedStages() []string
6373
// BuildPipelineSyncStages builds the stages that will be executed by the plugin.
6474
BuildPipelineSyncStages(context.Context, *Config, *Client, TODO) (TODO, error)
6575
// ExecuteStage executes the given stage.
66-
ExecuteStage(context.Context, *Config, *Client, logpersister.StageLogPersister, TODO) (TODO, error)
76+
ExecuteStage(context.Context, *Config, []*DeployTarget[DeployTargetConfig], *Client, logpersister.StageLogPersister, TODO) (TODO, error)
77+
}
78+
79+
// DeployTarget defines the deploy target configuration for the piped.
80+
type DeployTarget[Config any] struct {
81+
// The name of the deploy target.
82+
Name string `json:"name"`
83+
// The labes of the deploy target.
84+
Labels map[string]string `json:"labels,omitempty"`
85+
// The configuration of the deploy target.
86+
Config Config `json:"config"`
6787
}
6888

6989
// RegisterDeploymentPlugin registers the given deployment plugin.
7090
// It will be used when running the piped.
71-
func RegisterDeploymentPlugin[Config any](plugin DeploymentPlugin[Config]) {
72-
deploymentServiceServer = &DeploymentPluginServiceServer[Config]{base: plugin}
91+
func RegisterDeploymentPlugin[Config, DeployTargetConfig any](plugin DeploymentPlugin[Config, DeployTargetConfig]) {
92+
deploymentServiceServer = &DeploymentPluginServiceServer[Config, DeployTargetConfig]{base: plugin}
7393
}
7494

7595
// RegisterPipelineSyncPlugin registers the given pipeline sync plugin.
7696
// It will be used when running the piped.
77-
func RegisterPipelineSyncPlugin[Config any](plugin PipelineSyncPlugin[Config]) {
78-
deploymentServiceServer = &PipelineSyncPluginServiceServer[Config]{base: plugin}
97+
func RegisterPipelineSyncPlugin[Config, DeployTargetConfig any](plugin PipelineSyncPlugin[Config, DeployTargetConfig]) {
98+
deploymentServiceServer = &PipelineSyncPluginServiceServer[Config, DeployTargetConfig]{base: plugin}
7999
}
80100

81101
type logPersister interface {
@@ -90,92 +110,108 @@ type commonFields struct {
90110
}
91111

92112
// DeploymentPluginServiceServer is the gRPC server that handles requests from the piped.
93-
type DeploymentPluginServiceServer[Config any] struct {
113+
type DeploymentPluginServiceServer[Config, DeployTargetConfig any] struct {
94114
deployment.UnimplementedDeploymentServiceServer
95115
commonFields
96116

97-
base DeploymentPlugin[Config]
117+
base DeploymentPlugin[Config, DeployTargetConfig]
118+
config Config
98119
}
99120

100121
// Name returns the name of the plugin.
101-
func (s *DeploymentPluginServiceServer[Config]) Name() string {
122+
func (s *DeploymentPluginServiceServer[Config, DeployTargetConfig]) Name() string {
102123
return s.base.Name()
103124
}
104125

105-
func (s *DeploymentPluginServiceServer[Config]) Version() string {
126+
func (s *DeploymentPluginServiceServer[Config, DeployTargetConfig]) Version() string {
106127
return s.base.Version()
107128
}
108129

109130
// Register registers the server to the given gRPC server.
110-
func (s *DeploymentPluginServiceServer[Config]) Register(server *grpc.Server) {
131+
func (s *DeploymentPluginServiceServer[Config, DeployTargetConfig]) Register(server *grpc.Server) {
111132
deployment.RegisterDeploymentServiceServer(server, s)
112133
}
113134

114-
func (s *DeploymentPluginServiceServer[Config]) setCommonFields(fields commonFields) {
135+
func (s *DeploymentPluginServiceServer[Config, DeployTargetConfig]) setCommonFields(fields commonFields) {
115136
s.commonFields = fields
116137
}
117138

118-
func (s *DeploymentPluginServiceServer[Config]) FetchDefinedStages(context.Context, *deployment.FetchDefinedStagesRequest) (*deployment.FetchDefinedStagesResponse, error) {
139+
func (s *DeploymentPluginServiceServer[Config, DeployTargetConfig]) setConfig(bytes []byte) error {
140+
if err := json.Unmarshal(bytes, &s.config); err != nil {
141+
return fmt.Errorf("failed to unmarshal the plugin config: %v", err)
142+
}
143+
return nil
144+
}
145+
146+
func (s *DeploymentPluginServiceServer[Config, DeployTargetConfig]) FetchDefinedStages(context.Context, *deployment.FetchDefinedStagesRequest) (*deployment.FetchDefinedStagesResponse, error) {
119147
return &deployment.FetchDefinedStagesResponse{Stages: s.base.FetchDefinedStages()}, nil
120148
}
121-
func (s *DeploymentPluginServiceServer[Config]) DetermineVersions(context.Context, *deployment.DetermineVersionsRequest) (*deployment.DetermineVersionsResponse, error) {
149+
func (s *DeploymentPluginServiceServer[Config, DeployTargetConfig]) DetermineVersions(context.Context, *deployment.DetermineVersionsRequest) (*deployment.DetermineVersionsResponse, error) {
122150
return nil, status.Errorf(codes.Unimplemented, "method DetermineVersions not implemented")
123151
}
124-
func (s *DeploymentPluginServiceServer[Config]) DetermineStrategy(context.Context, *deployment.DetermineStrategyRequest) (*deployment.DetermineStrategyResponse, error) {
152+
func (s *DeploymentPluginServiceServer[Config, DeployTargetConfig]) DetermineStrategy(context.Context, *deployment.DetermineStrategyRequest) (*deployment.DetermineStrategyResponse, error) {
125153
return nil, status.Errorf(codes.Unimplemented, "method DetermineStrategy not implemented")
126154
}
127-
func (s *DeploymentPluginServiceServer[Config]) BuildPipelineSyncStages(context.Context, *deployment.BuildPipelineSyncStagesRequest) (*deployment.BuildPipelineSyncStagesResponse, error) {
155+
func (s *DeploymentPluginServiceServer[Config, DeployTargetConfig]) BuildPipelineSyncStages(ctx context.Context, request *deployment.BuildPipelineSyncStagesRequest) (*deployment.BuildPipelineSyncStagesResponse, error) {
128156
return nil, status.Errorf(codes.Unimplemented, "method BuildPipelineSyncStages not implemented")
129157
}
130-
func (s *DeploymentPluginServiceServer[Config]) BuildQuickSyncStages(context.Context, *deployment.BuildQuickSyncStagesRequest) (*deployment.BuildQuickSyncStagesResponse, error) {
158+
func (s *DeploymentPluginServiceServer[Config, DeployTargetConfig]) BuildQuickSyncStages(context.Context, *deployment.BuildQuickSyncStagesRequest) (*deployment.BuildQuickSyncStagesResponse, error) {
131159
return nil, status.Errorf(codes.Unimplemented, "method BuildQuickSyncStages not implemented")
132160
}
133-
func (s *DeploymentPluginServiceServer[Config]) ExecuteStage(context.Context, *deployment.ExecuteStageRequest) (*deployment.ExecuteStageResponse, error) {
161+
func (s *DeploymentPluginServiceServer[Config, DeployTargetConfig]) ExecuteStage(context.Context, *deployment.ExecuteStageRequest) (*deployment.ExecuteStageResponse, error) {
134162
return nil, status.Errorf(codes.Unimplemented, "method ExecuteStage not implemented")
135163
}
136164

137165
// PipelineSyncPluginServiceServer is the gRPC server that handles requests from the piped.
138-
type PipelineSyncPluginServiceServer[Config any] struct {
166+
type PipelineSyncPluginServiceServer[Config, DeployTargetConfig any] struct {
139167
deployment.UnimplementedDeploymentServiceServer
140168
commonFields
141169

142-
base PipelineSyncPlugin[Config]
170+
base PipelineSyncPlugin[Config, DeployTargetConfig]
171+
config Config
143172
}
144173

145174
// Name returns the name of the plugin.
146-
func (s *PipelineSyncPluginServiceServer[Config]) Name() string {
175+
func (s *PipelineSyncPluginServiceServer[Config, DeployTargetConfig]) Name() string {
147176
return s.base.Name()
148177
}
149178

150179
// Version returns the version of the plugin.
151-
func (s *PipelineSyncPluginServiceServer[Config]) Version() string {
180+
func (s *PipelineSyncPluginServiceServer[Config, DeployTargetConfig]) Version() string {
152181
return s.base.Version()
153182
}
154183

155184
// Register registers the server to the given gRPC server.
156-
func (s *PipelineSyncPluginServiceServer[Config]) Register(server *grpc.Server) {
185+
func (s *PipelineSyncPluginServiceServer[Config, DeployTargetConfig]) Register(server *grpc.Server) {
157186
deployment.RegisterDeploymentServiceServer(server, s)
158187
}
159188

160-
func (s *PipelineSyncPluginServiceServer[Config]) setCommonFields(fields commonFields) {
189+
func (s *PipelineSyncPluginServiceServer[Config, DeployTargetConfig]) setCommonFields(fields commonFields) {
161190
s.commonFields = fields
162191
}
163192

164-
func (s *PipelineSyncPluginServiceServer[Config]) FetchDefinedStages(context.Context, *deployment.FetchDefinedStagesRequest) (*deployment.FetchDefinedStagesResponse, error) {
193+
func (s *PipelineSyncPluginServiceServer[Config, DeployTargetConfig]) setConfig(bytes []byte) error {
194+
if err := json.Unmarshal(bytes, &s.config); err != nil {
195+
return fmt.Errorf("failed to unmarshal the plugin config: %v", err)
196+
}
197+
return nil
198+
}
199+
200+
func (s *PipelineSyncPluginServiceServer[Config, DeployTargetConfig]) FetchDefinedStages(context.Context, *deployment.FetchDefinedStagesRequest) (*deployment.FetchDefinedStagesResponse, error) {
165201
return &deployment.FetchDefinedStagesResponse{Stages: s.base.FetchDefinedStages()}, nil
166202
}
167-
func (s *PipelineSyncPluginServiceServer[Config]) DetermineVersions(context.Context, *deployment.DetermineVersionsRequest) (*deployment.DetermineVersionsResponse, error) {
203+
func (s *PipelineSyncPluginServiceServer[Config, DeployTargetConfig]) DetermineVersions(context.Context, *deployment.DetermineVersionsRequest) (*deployment.DetermineVersionsResponse, error) {
168204
return nil, status.Errorf(codes.Unimplemented, "method DetermineVersions not implemented")
169205
}
170-
func (s *PipelineSyncPluginServiceServer[Config]) DetermineStrategy(context.Context, *deployment.DetermineStrategyRequest) (*deployment.DetermineStrategyResponse, error) {
206+
func (s *PipelineSyncPluginServiceServer[Config, DeployTargetConfig]) DetermineStrategy(context.Context, *deployment.DetermineStrategyRequest) (*deployment.DetermineStrategyResponse, error) {
171207
return nil, status.Errorf(codes.Unimplemented, "method DetermineStrategy not implemented")
172208
}
173-
func (s *PipelineSyncPluginServiceServer[Config]) BuildPipelineSyncStages(context.Context, *deployment.BuildPipelineSyncStagesRequest) (*deployment.BuildPipelineSyncStagesResponse, error) {
209+
func (s *PipelineSyncPluginServiceServer[Config, DeployTargetConfig]) BuildPipelineSyncStages(ctx context.Context, request *deployment.BuildPipelineSyncStagesRequest) (*deployment.BuildPipelineSyncStagesResponse, error) {
174210
return nil, status.Errorf(codes.Unimplemented, "method BuildPipelineSyncStages not implemented")
175211
}
176-
func (s *PipelineSyncPluginServiceServer[Config]) BuildQuickSyncStages(context.Context, *deployment.BuildQuickSyncStagesRequest) (*deployment.BuildQuickSyncStagesResponse, error) {
212+
func (s *PipelineSyncPluginServiceServer[Config, DeployTargetConfig]) BuildQuickSyncStages(context.Context, *deployment.BuildQuickSyncStagesRequest) (*deployment.BuildQuickSyncStagesResponse, error) {
177213
return nil, status.Errorf(codes.Unimplemented, "method BuildQuickSyncStages not implemented")
178214
}
179-
func (s *PipelineSyncPluginServiceServer[Config]) ExecuteStage(context.Context, *deployment.ExecuteStageRequest) (*deployment.ExecuteStageResponse, error) {
215+
func (s *PipelineSyncPluginServiceServer[Config, DeployTargetConfig]) ExecuteStage(context.Context, *deployment.ExecuteStageRequest) (*deployment.ExecuteStageResponse, error) {
180216
return nil, status.Errorf(codes.Unimplemented, "method ExecuteStage not implemented")
181217
}

pkg/plugin/pipedsdk/sdk.go

+4
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,10 @@ func (s *plugin) run(ctx context.Context, input cli.Input) (runErr error) {
162162
logPersister: persister,
163163
client: pipedapiClient,
164164
})
165+
if err := deploymentServiceServer.setConfig(cfg.Config); err != nil {
166+
input.Logger.Error("failed to set configuration", zap.Error(err))
167+
return err
168+
}
165169
var (
166170
opts = []rpc.Option{
167171
rpc.WithPort(cfg.Port),

0 commit comments

Comments
 (0)