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

Implement DetermineStrategy with SDK #5671

Merged
merged 3 commits into from
Mar 17, 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
16 changes: 16 additions & 0 deletions pkg/app/pipedv1/plugin/kubernetes/deployment/determine.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,3 +249,19 @@

return model.SyncStrategy_QUICK_SYNC, "Quick sync by applying all manifests"
}

// determineStrategySDK decides the sync strategy and summary message based on the given manifests.
// TODO: rewrite this function to determineStrategy after the current determineStrategy is removed.
func determineStrategySDK(olds, news []provider.Manifest, workloadRefs []config.K8sResourceReference, logger *zap.Logger) (strategy sdk.SyncStrategy, summary string) {
mStrategy, summary := determineStrategy(olds, news, workloadRefs, logger)

var s sdk.SyncStrategy
switch mStrategy {
case model.SyncStrategy_QUICK_SYNC:
s = sdk.SyncStrategyQuickSync
case model.SyncStrategy_PIPELINE:
s = sdk.SyncStrategyQuickSync

Check warning on line 263 in pkg/app/pipedv1/plugin/kubernetes/deployment/determine.go

View check run for this annotation

Codecov / codecov/patch

pkg/app/pipedv1/plugin/kubernetes/deployment/determine.go#L255-L263

Added lines #L255 - L263 were not covered by tests
}

return s, summary

Check warning on line 266 in pkg/app/pipedv1/plugin/kubernetes/deployment/determine.go

View check run for this annotation

Codecov / codecov/patch

pkg/app/pipedv1/plugin/kubernetes/deployment/determine.go#L266

Added line #L266 was not covered by tests
}
33 changes: 30 additions & 3 deletions pkg/app/pipedv1/plugin/kubernetes/deployment/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -361,9 +361,36 @@
}, nil
}

// FIXME
func (p *Plugin) DetermineStrategy(context.Context, *sdk.ConfigNone, *sdk.Client, *sdk.DetermineStrategyInput) (*sdk.DetermineStrategyResponse, error) {
return &sdk.DetermineStrategyResponse{}, nil
func (p *Plugin) DetermineStrategy(ctx context.Context, _ *sdk.ConfigNone, _ *sdk.Client, input *sdk.DetermineStrategyInput) (*sdk.DetermineStrategyResponse, error) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

reference implementation:

func (a *DeploymentService) DetermineStrategy(ctx context.Context, request *deployment.DetermineStrategyRequest) (*deployment.DetermineStrategyResponse, error) {
cfg, err := config.DecodeYAML[*kubeconfig.KubernetesApplicationSpec](request.GetInput().GetTargetDeploymentSource().GetApplicationConfig())
if err != nil {
return nil, status.Error(codes.InvalidArgument, err.Error())
}
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.loadManifests(ctx, request.GetInput().GetDeployment(), cfg.Spec, request.GetInput().GetTargetDeploymentSource())
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
strategy, summary := determineStrategy(runnings, targets, cfg.Spec.Workloads, a.logger)
return &deployment.DetermineStrategyResponse{
SyncStrategy: strategy,
Summary: summary,
}, nil
}

logger := input.Logger
loader := provider.NewLoader(toolregistry.NewRegistry(input.Client.ToolRegistry()))

cfg, err := config.DecodeYAML[*kubeconfig.KubernetesApplicationSpec](input.Request.TargetDeploymentSource.ApplicationConfig)
if err != nil {
logger.Error("Failed while decoding application config", zap.Error(err))
return nil, err
}

Check warning on line 372 in pkg/app/pipedv1/plugin/kubernetes/deployment/plugin.go

View check run for this annotation

Codecov / codecov/patch

pkg/app/pipedv1/plugin/kubernetes/deployment/plugin.go#L364-L372

Added lines #L364 - L372 were not covered by tests

runnings, err := p.loadManifests(ctx, &input.Request.Deployment, cfg.Spec, &input.Request.RunningDeploymentSource, loader)

if err != nil {
logger.Error("Failed while loading running manifests", zap.Error(err))
return nil, err
}

Check warning on line 379 in pkg/app/pipedv1/plugin/kubernetes/deployment/plugin.go

View check run for this annotation

Codecov / codecov/patch

pkg/app/pipedv1/plugin/kubernetes/deployment/plugin.go#L374-L379

Added lines #L374 - L379 were not covered by tests

targets, err := p.loadManifests(ctx, &input.Request.Deployment, cfg.Spec, &input.Request.TargetDeploymentSource, loader)

if err != nil {
logger.Error("Failed while loading target manifests", zap.Error(err))
return nil, err
}

Check warning on line 386 in pkg/app/pipedv1/plugin/kubernetes/deployment/plugin.go

View check run for this annotation

Codecov / codecov/patch

pkg/app/pipedv1/plugin/kubernetes/deployment/plugin.go#L381-L386

Added lines #L381 - L386 were not covered by tests

strategy, summary := determineStrategySDK(runnings, targets, cfg.Spec.Workloads, logger)

return &sdk.DetermineStrategyResponse{
Strategy: strategy,
Summary: summary,
}, nil

Check warning on line 393 in pkg/app/pipedv1/plugin/kubernetes/deployment/plugin.go

View check run for this annotation

Codecov / codecov/patch

pkg/app/pipedv1/plugin/kubernetes/deployment/plugin.go#L388-L393

Added lines #L388 - L393 were not covered by tests
}

func (p *Plugin) BuildQuickSyncStages(ctx context.Context, _ *sdk.ConfigNone, input *sdk.BuildQuickSyncStagesInput) (*sdk.BuildQuickSyncStagesResponse, error) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,11 @@ import (
"time"

"github.com/google/go-cmp/cmp"
"github.com/pipe-cd/pipecd/pkg/plugin/sdk"
"github.com/stretchr/testify/assert"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime/schema"

"github.com/pipe-cd/pipecd/pkg/plugin/sdk"
)

func TestManifest_AddStringMapValues(t *testing.T) {
Expand Down