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

Change type definition for DeployTargetsByPlugin #5555

Merged
merged 5 commits into from
Feb 7, 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
26 changes: 25 additions & 1 deletion pkg/app/pipectl/cmd/migrate/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (

"github.com/pipe-cd/pipecd/pkg/app/server/service/apiservice"
"github.com/pipe-cd/pipecd/pkg/cli"
"github.com/pipe-cd/pipecd/pkg/model"
)

type database struct {
Expand Down Expand Up @@ -87,11 +88,34 @@ func (c *database) migrateApplication(ctx context.Context, client apiservice.Cli
// Migrate database for the application.
if _, err := client.UpdateApplicationDeployTargets(ctx, &apiservice.UpdateApplicationDeployTargetsRequest{
ApplicationId: appID,
DeployTargets: []string{provider},
DeployTargetsByPlugin: map[string]*model.DeployTargets{
convertApplicationKindToPluginName(app.Application.Kind): {
DeployTargets: []string{provider},
},
},
}); err != nil {
logger.Error("failed to update application deploy targets", zap.Error(err), zap.String("application", appID))
return err
}

return nil
}

// NOTE: Convention for Application plugins migration
// The plugins name for this migration task are defined based on the Application Kind
// Eg: KubernetesApp -> kubernetes | ECSApp -> ecs | ...
func convertApplicationKindToPluginName(k model.ApplicationKind) string {
switch k {
case model.ApplicationKind_KUBERNETES:
return "kubernetes"
case model.ApplicationKind_CLOUDRUN:
return "cloudrun"
case model.ApplicationKind_ECS:
return "ecs"
case model.ApplicationKind_LAMBDA:
return "lambda"
case model.ApplicationKind_TERRAFORM:
return "terraform"
}
return "" // Unexpected
}
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.DeployTargets,
DeployTargetsByPlugin: app.DeployTargetsByPlugin,
Labels: app.Labels,
Status: model.DeploymentStatus_DEPLOYMENT_PENDING,
StatusReason: "The deployment is waiting to be planned",
Expand Down
8 changes: 7 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,13 @@ 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
targets, 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, targets[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
targets, 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, targets[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
54 changes: 45 additions & 9 deletions pkg/app/pipedv1/plugin/kubernetes/deployment/sync_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,11 @@ func TestDeploymentService_executeK8sSyncStage(t *testing.T) {
Deployment: &model.Deployment{
PipedId: "piped-id",
ApplicationId: "app-id",
DeployTargets: []string{"default"},
DeployTargetsByPlugin: map[string]*model.DeployTargets{
"kubernetes": {
DeployTargets: []string{"default"},
},
},
},
Stage: &model.PipelineStage{
Id: "stage-id",
Expand Down Expand Up @@ -125,7 +129,11 @@ func TestDeploymentService_executeK8sSyncStage_withInputNamespace(t *testing.T)
Deployment: &model.Deployment{
PipedId: "piped-id",
ApplicationId: "app-id",
DeployTargets: []string{"default"},
DeployTargetsByPlugin: map[string]*model.DeployTargets{
"kubernetes": {
DeployTargets: []string{"default"},
},
},
},
Stage: &model.PipelineStage{
Id: "stage-id",
Expand Down Expand Up @@ -199,7 +207,11 @@ func TestDeploymentService_executeK8sSyncStage_withPrune(t *testing.T) {
Deployment: &model.Deployment{
PipedId: "piped-id",
ApplicationId: "app-id",
DeployTargets: []string{"default"},
DeployTargetsByPlugin: map[string]*model.DeployTargets{
"kubernetes": {
DeployTargets: []string{"default"},
},
},
},
Stage: &model.PipelineStage{
Id: "stage-id",
Expand Down Expand Up @@ -255,7 +267,11 @@ func TestDeploymentService_executeK8sSyncStage_withPrune(t *testing.T) {
Deployment: &model.Deployment{
PipedId: "piped-id",
ApplicationId: "app-id",
DeployTargets: []string{"default"},
DeployTargetsByPlugin: map[string]*model.DeployTargets{
"kubernetes": {
DeployTargets: []string{"default"},
},
},
},
Stage: &model.PipelineStage{
Id: "stage-id",
Expand Down Expand Up @@ -314,7 +330,11 @@ func TestDeploymentService_executeK8sSyncStage_withPrune_changesNamespace(t *tes
Deployment: &model.Deployment{
PipedId: "piped-id",
ApplicationId: "app-id",
DeployTargets: []string{"default"},
DeployTargetsByPlugin: map[string]*model.DeployTargets{
"kubernetes": {
DeployTargets: []string{"default"},
},
},
},
Stage: &model.PipelineStage{
Id: "stage-id",
Expand Down Expand Up @@ -367,7 +387,11 @@ func TestDeploymentService_executeK8sSyncStage_withPrune_changesNamespace(t *tes
Deployment: &model.Deployment{
PipedId: "piped-id",
ApplicationId: "app-id",
DeployTargets: []string{"default"},
DeployTargetsByPlugin: map[string]*model.DeployTargets{
"kubernetes": {
DeployTargets: []string{"default"},
},
},
},
Stage: &model.PipelineStage{
Id: "stage-id",
Expand Down Expand Up @@ -443,7 +467,11 @@ func TestDeploymentService_executeK8sSyncStage_withPrune_clusterScoped(t *testin
Deployment: &model.Deployment{
PipedId: "piped-id",
ApplicationId: "prepare-app-id",
DeployTargets: []string{"default"},
DeployTargetsByPlugin: map[string]*model.DeployTargets{
"kubernetes": {
DeployTargets: []string{"default"},
},
},
},
Stage: &model.PipelineStage{
Id: "stage-id",
Expand Down Expand Up @@ -481,7 +509,11 @@ func TestDeploymentService_executeK8sSyncStage_withPrune_clusterScoped(t *testin
Deployment: &model.Deployment{
PipedId: "piped-id",
ApplicationId: "app-id",
DeployTargets: []string{"default"},
DeployTargetsByPlugin: map[string]*model.DeployTargets{
"kubernetes": {
DeployTargets: []string{"default"},
},
},
},
Stage: &model.PipelineStage{
Id: "stage-id",
Expand Down Expand Up @@ -527,7 +559,11 @@ func TestDeploymentService_executeK8sSyncStage_withPrune_clusterScoped(t *testin
Deployment: &model.Deployment{
PipedId: "piped-id",
ApplicationId: "app-id",
DeployTargets: []string{"default"},
DeployTargetsByPlugin: map[string]*model.DeployTargets{
"kubernetes": {
DeployTargets: []string{"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.DeployTargets,
DeployTargetsByPlugin: app.DeployTargetsByPlugin,
Labels: app.Labels,
Status: model.DeploymentStatus_DEPLOYMENT_PENDING,
StatusReason: "The deployment is waiting to be planned",
Expand Down
4 changes: 2 additions & 2 deletions pkg/app/server/grpcapi/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ type apiApplicationStore interface {
Disable(ctx context.Context, id string) error
UpdateConfigFilename(ctx context.Context, id, filename string) error
UpdateConfiguration(ctx context.Context, id, pipedID, platformProvider, configFilename string) error
UpdateDeployTargets(ctx context.Context, id string, targets []string) error
UpdateDeployTargets(ctx context.Context, id string, dp map[string]*model.DeployTargets) error
}

type apiDeploymentStore interface {
Expand Down Expand Up @@ -478,7 +478,7 @@ func (a *API) UpdateApplicationDeployTargets(ctx context.Context, req *apiservic
a.logger.Warn("requested application does not belong to your project", zap.String("applicationID", app.Id), zap.String("requestProjectID", key.ProjectId), zap.String("applicationProjectID", app.ProjectId))
return nil, status.Error(codes.PermissionDenied, fmt.Sprintf("requested application %s does not belong to your project", req.GetApplicationId()))
}
if err := a.applicationStore.UpdateDeployTargets(ctx, req.GetApplicationId(), req.GetDeployTargets()); err != nil {
if err := a.applicationStore.UpdateDeployTargets(ctx, req.GetApplicationId(), req.GetDeployTargetsByPlugin()); err != nil {
return nil, gRPCStoreError(err, fmt.Sprintf("failed to update application %s deploy targets", req.ApplicationId))
}
return &apiservice.UpdateApplicationDeployTargetsResponse{}, nil
Expand Down
Loading