Skip to content
Merged
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
37 changes: 35 additions & 2 deletions pkg/plugin/sdk/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ import (
"net/http/pprof"
"time"

"github.com/pipe-cd/piped-plugin-sdk-go/logpersister"
"github.com/pipe-cd/piped-plugin-sdk-go/toolregistry"
"github.com/spf13/cobra"
"go.uber.org/zap"
"golang.org/x/sync/errgroup"
Expand All @@ -32,6 +30,9 @@ import (
"github.com/pipe-cd/pipecd/pkg/cli"
config "github.com/pipe-cd/pipecd/pkg/configv1"
"github.com/pipe-cd/pipecd/pkg/rpc"

"github.com/pipe-cd/piped-plugin-sdk-go/logpersister"
"github.com/pipe-cd/piped-plugin-sdk-go/toolregistry"
)

// DeployTargetsNone is a type alias for a slice of pointers to DeployTarget
Expand Down Expand Up @@ -60,6 +61,8 @@ type InitializeInput[Config, DeployTargetConfig any] struct {
Config *Config
// DeployTargets is the deploy targets of the plugin.
DeployTargets map[string]*DeployTarget[DeployTargetConfig]
// Client is the client to interact with the piped.
Client *Client
// Logger is the logger for the plugin.
Logger *zap.Logger
}
Expand Down Expand Up @@ -97,6 +100,14 @@ func (c commonFields[Config, DeployTargetConfig]) withLogger(logger *zap.Logger)
// PluginOption is a function that configures the plugin.
type PluginOption[Config, DeployTargetConfig, ApplicationConfigSpec any] func(*Plugin[Config, DeployTargetConfig, ApplicationConfigSpec])

// WithInitializer is a function that appends the initializer.
// The order of the execution for Initializers is the order in which they are added.
func WithInitializer[ApplicationConfigSpec, Config, DeployTargetConfig any](initializer Initializer[Config, DeployTargetConfig]) PluginOption[Config, DeployTargetConfig, ApplicationConfigSpec] {
return func(plugin *Plugin[Config, DeployTargetConfig, ApplicationConfigSpec]) {
plugin.initializers = append(plugin.initializers, initializer)
}
}

// WithStagePlugin is a function that sets the stage plugin.
// This is mutually exclusive with WithDeploymentPlugin.
func WithStagePlugin[Config, DeployTargetConfig, ApplicationConfigSpec any](stagePlugin StagePlugin[Config, DeployTargetConfig, ApplicationConfigSpec]) PluginOption[Config, DeployTargetConfig, ApplicationConfigSpec] {
Expand Down Expand Up @@ -136,6 +147,9 @@ type Plugin[Config, DeployTargetConfig, ApplicationConfigSpec any] struct {
// name is the name of the plugin defined in the piped plugin config.
name string

// initializers
initializers []Initializer[Config, DeployTargetConfig]

// plugin implementations
stagePlugin StagePlugin[Config, DeployTargetConfig, ApplicationConfigSpec]
deploymentPlugin DeploymentPlugin[Config, DeployTargetConfig, ApplicationConfigSpec]
Expand Down Expand Up @@ -321,12 +335,31 @@ func (p *Plugin[Config, DeployTargetConfig, ApplicationConfigSpec]) run(ctx cont
}
}

client := &Client{
base: commonFields.client,
pluginName: commonFields.name,
toolRegistry: commonFields.toolRegistry,
// These fields are not available at initializing state.
applicationID: "",
deploymentID: "",
stageID: "",
logPersister: nil,
}

initializeInput := &InitializeInput[Config, DeployTargetConfig]{
Config: commonFields.pluginConfig,
DeployTargets: commonFields.deployTargets,
Client: client,
Logger: logger.Named("plugin-initializer"),
}

for _, initializer := range p.initializers {
if err := initializer.Initialize(ctx, initializeInput); err != nil {
logger.Error("failed to initialize plugin", zap.Error(err))
return err
}
}

var services []rpc.Service

if p.stagePlugin != nil {
Expand Down