Skip to content

Commit

Permalink
Refactor manifest loading in DeploymentService to reduce code duplica…
Browse files Browse the repository at this point in the history
…tion (#5366)

* Refactor manifest loading in DeploymentService to reduce code duplication

Signed-off-by: Shinnosuke Sawada-Dazai <[email protected]>

* Fix error handling in loadManifests to return original error

Signed-off-by: Shinnosuke Sawada-Dazai <[email protected]>

---------

Signed-off-by: Shinnosuke Sawada-Dazai <[email protected]>
  • Loading branch information
Warashi authored Nov 27, 2024
1 parent fd8dc17 commit 57f0672
Showing 1 changed file with 23 additions and 30 deletions.
53 changes: 23 additions & 30 deletions pkg/app/pipedv1/plugin/kubernetes/deployment/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
kubeconfig "github.com/pipe-cd/pipecd/pkg/app/pipedv1/plugin/kubernetes/config"
"github.com/pipe-cd/pipecd/pkg/app/pipedv1/plugin/kubernetes/provider"
config "github.com/pipe-cd/pipecd/pkg/configv1"
"github.com/pipe-cd/pipecd/pkg/model"
"github.com/pipe-cd/pipecd/pkg/plugin/api/v1alpha1/deployment"
"github.com/pipe-cd/pipecd/pkg/regexpool"

Expand Down Expand Up @@ -82,31 +83,13 @@ func (a *DeploymentService) DetermineStrategy(ctx context.Context, request *depl
return nil, status.Error(codes.InvalidArgument, err.Error())
}

runnings, err := a.Loader.LoadManifests(ctx, provider.LoaderInput{
AppName: request.GetInput().GetDeployment().GetApplicationName(),
AppDir: request.GetInput().GetRunningDeploymentSource().GetApplicationDirectory(),
ConfigFilename: request.GetInput().GetRunningDeploymentSource().GetApplicationConfigFilename(),
Manifests: cfg.Spec.Input.Manifests,
Namespace: cfg.Spec.Input.Namespace,
TemplatingMethod: provider.TemplatingMethodNone, // TODO: Implement detection of templating method or add it to the config spec.

// TODO: Define other fields for LoaderInput
})
runnings, err := a.loadManifests(ctx, request.GetInput().GetDeployment(), cfg.Spec, request.GetInput().GetRunningDeploymentSource())

if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}

targets, err := a.Loader.LoadManifests(ctx, provider.LoaderInput{
AppName: request.GetInput().GetDeployment().GetApplicationName(),
AppDir: request.GetInput().GetTargetDeploymentSource().GetApplicationDirectory(),
ConfigFilename: request.GetInput().GetTargetDeploymentSource().GetApplicationConfigFilename(),
Manifests: cfg.Spec.Input.Manifests,
Namespace: cfg.Spec.Input.Namespace,
TemplatingMethod: provider.TemplatingMethodNone, // TODO: Implement detection of templating method or add it to the config spec.

// TODO: Define other fields for LoaderInput
})
targets, err := a.loadManifests(ctx, request.GetInput().GetDeployment(), cfg.Spec, request.GetInput().GetTargetDeploymentSource())

if err != nil {
return nil, status.Error(codes.Internal, err.Error())
Expand All @@ -128,16 +111,7 @@ func (a *DeploymentService) DetermineVersions(ctx context.Context, request *depl
return nil, status.Error(codes.InvalidArgument, err.Error())
}

manifests, err := a.Loader.LoadManifests(ctx, provider.LoaderInput{
AppName: request.GetInput().GetDeployment().GetApplicationName(),
AppDir: request.GetInput().GetTargetDeploymentSource().GetApplicationDirectory(),
ConfigFilename: request.GetInput().GetTargetDeploymentSource().GetApplicationConfigFilename(),
Manifests: cfg.Spec.Input.Manifests,
Namespace: cfg.Spec.Input.Namespace,
TemplatingMethod: provider.TemplatingMethodNone, // TODO: Implement detection of templating method or add it to the config spec.

// TODO: Define other fields for LoaderInput
})
manifests, err := a.loadManifests(ctx, request.GetInput().GetDeployment(), cfg.Spec, request.GetInput().GetTargetDeploymentSource())

if err != nil {
return nil, status.Error(codes.Internal, err.Error())
Expand Down Expand Up @@ -182,3 +156,22 @@ func (a *DeploymentService) FetchDefinedStages(context.Context, *deployment.Fetc
Stages: stages,
}, nil
}

func (a *DeploymentService) loadManifests(ctx context.Context, deploy *model.Deployment, spec *kubeconfig.KubernetesApplicationSpec, deploymentSource *deployment.DeploymentSource) ([]provider.Manifest, error) {
manifests, err := a.Loader.LoadManifests(ctx, provider.LoaderInput{
AppName: deploy.GetApplicationName(),
AppDir: deploymentSource.GetApplicationDirectory(),
ConfigFilename: deploymentSource.GetApplicationConfigFilename(),
Manifests: spec.Input.Manifests,
Namespace: spec.Input.Namespace,
TemplatingMethod: provider.TemplatingMethodNone, // TODO: Implement detection of templating method or add it to the config spec.

// TODO: Define other fields for LoaderInput
})

if err != nil {
return nil, err
}

return manifests, nil
}

0 comments on commit 57f0672

Please sign in to comment.