Skip to content

Commit

Permalink
Add deploy target by plugin to deployment (#5544)
Browse files Browse the repository at this point in the history
* Add DeployTargetByPlugin to Deployment

Signed-off-by: Yoshiki Fujikane <[email protected]>

* Add deployTargetsByPlugin to model.Deployment

Signed-off-by: Yoshiki Fujikane <[email protected]>

---------

Signed-off-by: Yoshiki Fujikane <[email protected]>
  • Loading branch information
ffjlabo authored Feb 6, 2025
1 parent ea25ab8 commit 8911199
Show file tree
Hide file tree
Showing 12 changed files with 496 additions and 323 deletions.
2 changes: 1 addition & 1 deletion pkg/app/piped/trigger/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func buildDeployment(
GitPath: app.GitPath,
CloudProvider: app.CloudProvider,
PlatformProvider: app.PlatformProvider,
DeployTargets: app.GetDeployTargets(),
DeployTargetsByPlugin: app.GetDeployTargetsByPlugin(),
Labels: app.Labels,
Status: model.DeploymentStatus_DEPLOYMENT_PENDING,
StatusReason: "The deployment is waiting to be planned",
Expand Down
7 changes: 6 additions & 1 deletion pkg/app/pipedv1/plugin/kubernetes/deployment/rollback.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,12 @@ func (a *DeploymentService) executeK8sRollbackStage(ctx context.Context, lp logp
}

// Get the deploy target config.
deployTargetConfig, err := kubeconfig.FindDeployTarget(a.pluginConfig, input.GetDeployment().GetDeployTargets()[0]) // TODO: check if there is a deploy target
deployTargets, err := input.GetDeployment().GetDeployTargets(a.pluginConfig.Name)
if err != nil {
lp.Errorf("Failed while finding deploy target config (%v)", err)
return model.StageStatus_STAGE_FAILURE
}
deployTargetConfig, err := kubeconfig.FindDeployTarget(a.pluginConfig, deployTargets[0]) // TODO: consider multiple targets
if err != nil {
lp.Errorf("Failed while unmarshalling deploy target config (%v)", err)
return model.StageStatus_STAGE_FAILURE
Expand Down
7 changes: 6 additions & 1 deletion pkg/app/pipedv1/plugin/kubernetes/deployment/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,12 @@ func (a *DeploymentService) executeK8sSyncStage(ctx context.Context, lp logpersi
}

// Get the deploy target config.
deployTargetConfig, err := kubeconfig.FindDeployTarget(a.pluginConfig, input.GetDeployment().GetDeployTargets()[0]) // TODO: check if there is a deploy target
deployTargets, err := input.GetDeployment().GetDeployTargets(a.pluginConfig.Name)
if err != nil {
lp.Errorf("Failed while finding deploy target config (%v)", err)
return model.StageStatus_STAGE_FAILURE
}
deployTargetConfig, err := kubeconfig.FindDeployTarget(a.pluginConfig, deployTargets[0]) // TODO: consider multiple targets
if err != nil {
lp.Errorf("Failed while unmarshalling deploy target config (%v)", err)
return model.StageStatus_STAGE_FAILURE
Expand Down
109 changes: 100 additions & 9 deletions pkg/app/pipedv1/plugin/kubernetes/deployment/sync_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.uber.org/zap/zaptest"
"google.golang.org/protobuf/types/known/structpb"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
Expand Down Expand Up @@ -52,7 +53,17 @@ func TestDeploymentService_executeK8sSyncStage(t *testing.T) {
Deployment: &model.Deployment{
PipedId: "piped-id",
ApplicationId: "app-id",
DeployTargets: []string{"default"},
DeployTargetsByPlugin: map[string]*structpb.ListValue{
"kubernetes": &structpb.ListValue{
Values: []*structpb.Value{
{
Kind: &structpb.Value_StringValue{
StringValue: "default",
},
},
},
},
},
},
Stage: &model.PipelineStage{
Id: "stage-id",
Expand Down Expand Up @@ -125,7 +136,17 @@ func TestDeploymentService_executeK8sSyncStage_withInputNamespace(t *testing.T)
Deployment: &model.Deployment{
PipedId: "piped-id",
ApplicationId: "app-id",
DeployTargets: []string{"default"},
DeployTargetsByPlugin: map[string]*structpb.ListValue{
"kubernetes": &structpb.ListValue{
Values: []*structpb.Value{
{
Kind: &structpb.Value_StringValue{
StringValue: "default",
},
},
},
},
},
},
Stage: &model.PipelineStage{
Id: "stage-id",
Expand Down Expand Up @@ -199,7 +220,17 @@ func TestDeploymentService_executeK8sSyncStage_withPrune(t *testing.T) {
Deployment: &model.Deployment{
PipedId: "piped-id",
ApplicationId: "app-id",
DeployTargets: []string{"default"},
DeployTargetsByPlugin: map[string]*structpb.ListValue{
"kubernetes": &structpb.ListValue{
Values: []*structpb.Value{
{
Kind: &structpb.Value_StringValue{
StringValue: "default",
},
},
},
},
},
},
Stage: &model.PipelineStage{
Id: "stage-id",
Expand Down Expand Up @@ -255,7 +286,17 @@ func TestDeploymentService_executeK8sSyncStage_withPrune(t *testing.T) {
Deployment: &model.Deployment{
PipedId: "piped-id",
ApplicationId: "app-id",
DeployTargets: []string{"default"},
DeployTargetsByPlugin: map[string]*structpb.ListValue{
"kubernetes": &structpb.ListValue{
Values: []*structpb.Value{
{
Kind: &structpb.Value_StringValue{
StringValue: "default",
},
},
},
},
},
},
Stage: &model.PipelineStage{
Id: "stage-id",
Expand Down Expand Up @@ -314,7 +355,17 @@ func TestDeploymentService_executeK8sSyncStage_withPrune_changesNamespace(t *tes
Deployment: &model.Deployment{
PipedId: "piped-id",
ApplicationId: "app-id",
DeployTargets: []string{"default"},
DeployTargetsByPlugin: map[string]*structpb.ListValue{
"kubernetes": &structpb.ListValue{
Values: []*structpb.Value{
{
Kind: &structpb.Value_StringValue{
StringValue: "default",
},
},
},
},
},
},
Stage: &model.PipelineStage{
Id: "stage-id",
Expand Down Expand Up @@ -367,7 +418,17 @@ func TestDeploymentService_executeK8sSyncStage_withPrune_changesNamespace(t *tes
Deployment: &model.Deployment{
PipedId: "piped-id",
ApplicationId: "app-id",
DeployTargets: []string{"default"},
DeployTargetsByPlugin: map[string]*structpb.ListValue{
"kubernetes": &structpb.ListValue{
Values: []*structpb.Value{
{
Kind: &structpb.Value_StringValue{
StringValue: "default",
},
},
},
},
},
},
Stage: &model.PipelineStage{
Id: "stage-id",
Expand Down Expand Up @@ -443,7 +504,17 @@ func TestDeploymentService_executeK8sSyncStage_withPrune_clusterScoped(t *testin
Deployment: &model.Deployment{
PipedId: "piped-id",
ApplicationId: "prepare-app-id",
DeployTargets: []string{"default"},
DeployTargetsByPlugin: map[string]*structpb.ListValue{
"kubernetes": &structpb.ListValue{
Values: []*structpb.Value{
{
Kind: &structpb.Value_StringValue{
StringValue: "default",
},
},
},
},
},
},
Stage: &model.PipelineStage{
Id: "stage-id",
Expand Down Expand Up @@ -481,7 +552,17 @@ func TestDeploymentService_executeK8sSyncStage_withPrune_clusterScoped(t *testin
Deployment: &model.Deployment{
PipedId: "piped-id",
ApplicationId: "app-id",
DeployTargets: []string{"default"},
DeployTargetsByPlugin: map[string]*structpb.ListValue{
"kubernetes": &structpb.ListValue{
Values: []*structpb.Value{
{
Kind: &structpb.Value_StringValue{
StringValue: "default",
},
},
},
},
},
},
Stage: &model.PipelineStage{
Id: "stage-id",
Expand Down Expand Up @@ -527,7 +608,17 @@ func TestDeploymentService_executeK8sSyncStage_withPrune_clusterScoped(t *testin
Deployment: &model.Deployment{
PipedId: "piped-id",
ApplicationId: "app-id",
DeployTargets: []string{"default"},
DeployTargetsByPlugin: map[string]*structpb.ListValue{
"kubernetes": &structpb.ListValue{
Values: []*structpb.Value{
{
Kind: &structpb.Value_StringValue{
StringValue: "default",
},
},
},
},
},
},
Stage: &model.PipelineStage{
Id: "stage-id",
Expand Down
2 changes: 1 addition & 1 deletion pkg/app/pipedv1/trigger/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func buildDeployment(
GitPath: app.GitPath,
CloudProvider: app.CloudProvider,
PlatformProvider: app.PlatformProvider,
DeployTargets: app.GetDeployTargets(),
DeployTargetsByPlugin: app.GetDeployTargetsByPlugin(),
Labels: app.Labels,
Status: model.DeploymentStatus_DEPLOYMENT_PENDING,
StatusReason: "The deployment is waiting to be planned",
Expand Down
14 changes: 14 additions & 0 deletions pkg/model/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,20 @@ func (d *Deployment) SetUpdatedAt(t int64) {
d.UpdatedAt = t
}

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

values := make([]string, 0, len(dps.GetValues()))
for _, v := range dps.GetValues() {
values = append(values, v.GetStringValue())
}

return values, nil
}

// Implement sort.Interface for PipelineStages.
type PipelineStages []*PipelineStage

Expand Down
Loading

0 comments on commit 8911199

Please sign in to comment.