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

Refactor the plugin SDK to set common fields and configs in a single method #5623

Merged
merged 2 commits into from
Mar 5, 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
71 changes: 39 additions & 32 deletions pkg/plugin/sdk/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@
Plugin

Register(server *grpc.Server)
setCommonFields(commonFields)
setConfig([]byte) error
// setFields sets the common fields and configs to the server.
setFields(commonFields) error
deployment.DeploymentServiceServer
}
)
Expand Down Expand Up @@ -123,8 +123,9 @@
deployment.UnimplementedDeploymentServiceServer
commonFields

base DeploymentPlugin[Config, DeployTargetConfig]
config Config
base DeploymentPlugin[Config, DeployTargetConfig]
config Config
deployTargets map[string]*DeployTarget[DeployTargetConfig]
}

// Name returns the name of the plugin.
Expand All @@ -141,17 +142,32 @@
deployment.RegisterDeploymentServiceServer(server, s)
}

func (s *DeploymentPluginServiceServer[Config, DeployTargetConfig]) setCommonFields(fields commonFields) {
// setFields sets the common fields and configs to the server.
func (s *DeploymentPluginServiceServer[Config, DeployTargetConfig]) setFields(fields commonFields) error {

Check warning on line 146 in pkg/plugin/sdk/deployment.go

View check run for this annotation

Codecov / codecov/patch

pkg/plugin/sdk/deployment.go#L146

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

func (s *DeploymentPluginServiceServer[Config, DeployTargetConfig]) setConfig(bytes []byte) error {
if bytes == nil {
return nil
cfg := fields.config
if cfg.Config != nil {
if err := json.Unmarshal(cfg.Config, &s.config); err != nil {
s.logger.Fatal("failed to unmarshal the plugin config", zap.Error(err))
return err
}

Check warning on line 154 in pkg/plugin/sdk/deployment.go

View check run for this annotation

Codecov / codecov/patch

pkg/plugin/sdk/deployment.go#L149-L154

Added lines #L149 - L154 were not covered by tests
}
if err := json.Unmarshal(bytes, &s.config); err != nil {
return fmt.Errorf("failed to unmarshal the plugin config: %v", err)

s.deployTargets = make(map[string]*DeployTarget[DeployTargetConfig], len(cfg.DeployTargets))
for _, dt := range cfg.DeployTargets {
var sdkDt DeployTargetConfig
if err := json.Unmarshal(dt.Config, &sdkDt); err != nil {
s.logger.Fatal("failed to unmarshal deploy target config", zap.Error(err))
return err
}
s.deployTargets[dt.Name] = &DeployTarget[DeployTargetConfig]{
Name: dt.Name,
Labels: dt.Labels,
Config: sdkDt,
}

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

View check run for this annotation

Codecov / codecov/patch

pkg/plugin/sdk/deployment.go#L157-L168

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

return nil
}

Expand Down Expand Up @@ -214,22 +230,12 @@
dtNames := request.GetInput().GetDeployment().GetDeployTargets(s.commonFields.config.Name)
deployTargets := make([]*DeployTarget[DeployTargetConfig], 0, len(dtNames))
for _, name := range dtNames {
dt := s.commonFields.config.FindDeployTarget(name)
if dt == nil {
dt, ok := s.deployTargets[name]
if !ok {

Check warning on line 234 in pkg/plugin/sdk/deployment.go

View check run for this annotation

Codecov / codecov/patch

pkg/plugin/sdk/deployment.go#L233-L234

Added lines #L233 - L234 were not covered by tests
return nil, status.Errorf(codes.Internal, "the deploy target %s is not found in the piped plugin config", name)
}

// TODO: cache the unmarshaled config to avoid unmarshaling it multiple times.
var sdkDt DeployTargetConfig
if err := json.Unmarshal(dt.Config, &sdkDt); err != nil {
return nil, status.Errorf(codes.Internal, "failed to unmarshal deploy target config: %v", err)
}

deployTargets = append(deployTargets, &DeployTarget[DeployTargetConfig]{
Name: name,
Labels: dt.Labels,
Config: sdkDt,
})
deployTargets = append(deployTargets, dt)

Check warning on line 238 in pkg/plugin/sdk/deployment.go

View check run for this annotation

Codecov / codecov/patch

pkg/plugin/sdk/deployment.go#L238

Added line #L238 was not covered by tests
}

return executeStage(ctx, s.base, &s.config, deployTargets, client, request, s.logger)
Expand Down Expand Up @@ -259,17 +265,18 @@
deployment.RegisterDeploymentServiceServer(server, s)
}

func (s *StagePluginServiceServer[Config, DeployTargetConfig]) setCommonFields(fields commonFields) {
// setFields sets the common fields and configs to the server.
func (s *StagePluginServiceServer[Config, DeployTargetConfig]) setFields(fields commonFields) error {

Check warning on line 269 in pkg/plugin/sdk/deployment.go

View check run for this annotation

Codecov / codecov/patch

pkg/plugin/sdk/deployment.go#L269

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

func (s *StagePluginServiceServer[Config, DeployTargetConfig]) setConfig(bytes []byte) error {
if bytes == nil {
return nil
}
if err := json.Unmarshal(bytes, &s.config); err != nil {
return fmt.Errorf("failed to unmarshal the plugin config: %v", err)
cfg := fields.config
if cfg.Config != nil {
if err := json.Unmarshal(cfg.Config, &s.config); err != nil {
s.logger.Fatal("failed to unmarshal the plugin config", zap.Error(err))
return err
}

Check warning on line 277 in pkg/plugin/sdk/deployment.go

View check run for this annotation

Codecov / codecov/patch

pkg/plugin/sdk/deployment.go#L272-L277

Added lines #L272 - L277 were not covered by tests
}

return nil
}

Expand Down
7 changes: 3 additions & 4 deletions pkg/plugin/sdk/sdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,15 +157,14 @@

// Start a gRPC server for handling external API requests.
{
deploymentServiceServer.setCommonFields(commonFields{
if err := deploymentServiceServer.setFields(commonFields{

Check warning on line 160 in pkg/plugin/sdk/sdk.go

View check run for this annotation

Codecov / codecov/patch

pkg/plugin/sdk/sdk.go#L160

Added line #L160 was not covered by tests
config: cfg,
logger: input.Logger.Named("deployment-service"),
logPersister: persister,
client: pipedapiClient,
toolRegistry: toolregistry.NewToolRegistry(pipedapiClient),
})
if err := deploymentServiceServer.setConfig(cfg.Config); err != nil {
input.Logger.Error("failed to set configuration", zap.Error(err))
}); err != nil {
input.Logger.Error("failed to set fields", zap.Error(err))

Check warning on line 167 in pkg/plugin/sdk/sdk.go

View check run for this annotation

Codecov / codecov/patch

pkg/plugin/sdk/sdk.go#L166-L167

Added lines #L166 - L167 were not covered by tests
return err
}
var (
Expand Down