Skip to content

Commit ce438ce

Browse files
authored
Pass DeployTarget in SDK (#5616)
* Pass DeployTarget in SDK Signed-off-by: Yoshiki Fujikane <[email protected]> * Remove comment Signed-off-by: Yoshiki Fujikane <[email protected]> * Refactor to do early continue Signed-off-by: Yoshiki Fujikane <[email protected]> * Fix error message Signed-off-by: Yoshiki Fujikane <[email protected]> * Add the number of the deployTargets Signed-off-by: Yoshiki Fujikane <[email protected]> * Add comment for caching Signed-off-by: Yoshiki Fujikane <[email protected]> * Allow no deploy targets in the plugin config considering the plugins which don't need them Signed-off-by: Yoshiki Fujikane <[email protected]> * Refactor to return error early Signed-off-by: Yoshiki Fujikane <[email protected]> --------- Signed-off-by: Yoshiki Fujikane <[email protected]>
1 parent 4afa3e6 commit ce438ce

File tree

5 files changed

+98
-10
lines changed

5 files changed

+98
-10
lines changed

pkg/app/pipedv1/plugin/kubernetes/deployment/rollback.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,9 @@ func (a *DeploymentService) executeK8sRollbackStage(ctx context.Context, lp logp
8383
}
8484

8585
// Get the deploy target config.
86-
targets, err := input.GetDeployment().GetDeployTargets(a.pluginConfig.Name)
87-
if err != nil {
88-
lp.Errorf("Failed while finding deploy target config (%v)", err)
86+
targets := input.GetDeployment().GetDeployTargets(a.pluginConfig.Name)
87+
if len(targets) == 0 {
88+
lp.Errorf("No deploy target was found for the plugin %s", a.pluginConfig.Name)
8989
return model.StageStatus_STAGE_FAILURE
9090
}
9191

pkg/app/pipedv1/plugin/kubernetes/deployment/sync.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,9 @@ func (a *DeploymentService) executeK8sSyncStage(ctx context.Context, lp logpersi
8282
}
8383

8484
// Get the deploy target config.
85-
targets, err := input.GetDeployment().GetDeployTargets(a.pluginConfig.Name)
86-
if err != nil {
87-
lp.Errorf("Failed while finding deploy target config (%v)", err)
85+
targets := input.GetDeployment().GetDeployTargets(a.pluginConfig.Name)
86+
if len(targets) == 0 {
87+
lp.Errorf("No deploy target was found for the plugin %s", a.pluginConfig.Name)
8888
return model.StageStatus_STAGE_FAILURE
8989
}
9090
deployTargetConfig, err := kubeconfig.FindDeployTarget(a.pluginConfig, targets[0]) // TODO: consider multiple targets

pkg/model/deployment.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -214,13 +214,13 @@ func (d *Deployment) SetUpdatedAt(t int64) {
214214
d.UpdatedAt = t
215215
}
216216

217-
func (d *Deployment) GetDeployTargets(pluginName string) ([]string, error) {
217+
func (d *Deployment) GetDeployTargets(pluginName string) []string {
218218
dps, ok := d.GetDeployTargetsByPlugin()[pluginName]
219219
if !ok || len(dps.GetDeployTargets()) == 0 {
220-
return nil, fmt.Errorf("deploy target not found for plugin %v", pluginName)
220+
return []string{}
221221
}
222222

223-
return dps.GetDeployTargets(), nil
223+
return dps.GetDeployTargets()
224224
}
225225

226226
// Implement sort.Interface for PipelineStages.

pkg/model/deployment_test.go

+65
Original file line numberDiff line numberDiff line change
@@ -658,3 +658,68 @@ func TestSortPipelineStagesByIndex(t *testing.T) {
658658
{Index: 4},
659659
}, stages)
660660
}
661+
662+
func TestDeployment_GetDeployTargets(t *testing.T) {
663+
t.Parallel()
664+
665+
tests := []struct {
666+
name string
667+
deployment *Deployment
668+
pluginName string
669+
expected []string
670+
}{
671+
{
672+
name: "plugin with deploy targets",
673+
deployment: &Deployment{
674+
DeployTargetsByPlugin: map[string]*DeployTargets{
675+
"pluginA": {
676+
DeployTargets: []string{"target1", "target2"},
677+
},
678+
},
679+
},
680+
pluginName: "pluginA",
681+
expected: []string{"target1", "target2"},
682+
},
683+
{
684+
name: "plugin without deploy targets",
685+
deployment: &Deployment{
686+
DeployTargetsByPlugin: map[string]*DeployTargets{
687+
"pluginA": {
688+
DeployTargets: []string{},
689+
},
690+
},
691+
},
692+
pluginName: "pluginA",
693+
expected: []string{},
694+
},
695+
{
696+
name: "plugin not found",
697+
deployment: &Deployment{
698+
DeployTargetsByPlugin: map[string]*DeployTargets{
699+
"pluginA": {
700+
DeployTargets: []string{"target1", "target2"},
701+
},
702+
},
703+
},
704+
pluginName: "pluginB",
705+
expected: []string{},
706+
},
707+
{
708+
name: "no deploy targets by plugin",
709+
deployment: &Deployment{
710+
DeployTargetsByPlugin: map[string]*DeployTargets{},
711+
},
712+
pluginName: "pluginA",
713+
expected: []string{},
714+
},
715+
}
716+
717+
for _, tt := range tests {
718+
t.Run(tt.name, func(t *testing.T) {
719+
t.Parallel()
720+
721+
got := tt.deployment.GetDeployTargets(tt.pluginName)
722+
assert.Equal(t, tt.expected, got)
723+
})
724+
}
725+
}

pkg/plugin/sdk/deployment.go

+24-1
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,30 @@ func (s *DeploymentPluginServiceServer[Config, DeployTargetConfig]) ExecuteStage
206206
stageID: request.GetInput().GetStage().GetId(),
207207
logPersister: lp,
208208
}
209-
return executeStage(ctx, s.base, &s.config, nil, client, request, s.logger) // TODO: pass the deployTargets
209+
210+
// Get the deploy targets set on the deployment from the piped plugin config.
211+
dtNames := request.GetInput().GetDeployment().GetDeployTargets(s.commonFields.config.Name)
212+
deployTargets := make([]*DeployTarget[DeployTargetConfig], 0, len(dtNames))
213+
for _, name := range dtNames {
214+
dt := s.commonFields.config.FindDeployTarget(name)
215+
if dt == nil {
216+
return nil, status.Errorf(codes.Internal, "the deploy target %s is not found in the piped plugin config", name)
217+
}
218+
219+
// TODO: cache the unmarshaled config to avoid unmarshaling it multiple times.
220+
var sdkDt DeployTargetConfig
221+
if err := json.Unmarshal(dt.Config, &sdkDt); err != nil {
222+
return nil, status.Errorf(codes.Internal, "failed to unmarshal deploy target config: %v", err)
223+
}
224+
225+
deployTargets = append(deployTargets, &DeployTarget[DeployTargetConfig]{
226+
Name: name,
227+
Labels: dt.Labels,
228+
Config: sdkDt,
229+
})
230+
}
231+
232+
return executeStage(ctx, s.base, &s.config, deployTargets, client, request, s.logger)
210233
}
211234

212235
// StagePluginServiceServer is the gRPC server that handles requests from the piped.

0 commit comments

Comments
 (0)