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

[WAIT] Implement server plan methods of Wait plugin #5504

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
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
3 changes: 3 additions & 0 deletions pkg/app/pipedv1/controller/planner.go
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,9 @@
p.logger.Warn("Unable to determine strategy using current plugin", zap.Error(err))
continue
}
if res.Unsupported {
continue
}

Check warning on line 383 in pkg/app/pipedv1/controller/planner.go

View check run for this annotation

Codecov / codecov/patch

pkg/app/pipedv1/controller/planner.go#L383

Added line #L383 was not covered by tests
strategy = res.SyncStrategy
summary = res.Summary
// If one of plugins returns PIPELINE_SYNC, use that as strategy intermediately
Expand Down
53 changes: 45 additions & 8 deletions pkg/app/pipedv1/plugin/wait/deployment/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,25 @@ package deployment

import (
"context"
"fmt"
"time"

"go.uber.org/zap"
"google.golang.org/grpc"

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/plugin/logpersister"
"github.com/pipe-cd/pipecd/pkg/plugin/signalhandler"
)

type Stage string

const (
stageWait Stage = "WAIT"
)

type deploymentServiceServer struct {
deployment.UnimplementedDeploymentServiceServer

Expand Down Expand Up @@ -85,24 +93,53 @@ func (s *deploymentServiceServer) FetchDefinedStages(ctx context.Context, reques

// DetermineVersions implements deployment.DeploymentServiceServer.
func (s *deploymentServiceServer) DetermineVersions(ctx context.Context, request *deployment.DetermineVersionsRequest) (*deployment.DetermineVersionsResponse, error) {
// TODO: Implement this func
return &deployment.DetermineVersionsResponse{}, nil
// Wait Stage does not have any versioned resources.
return &deployment.DetermineVersionsResponse{
Versions: []*model.ArtifactVersion{},
}, nil
}

// DetermineStrategy implements deployment.DeploymentServiceServer.
func (s *deploymentServiceServer) DetermineStrategy(ctx context.Context, request *deployment.DetermineStrategyRequest) (*deployment.DetermineStrategyResponse, error) {
// TODO: Implement this func
return &deployment.DetermineStrategyResponse{}, nil
return &deployment.DetermineStrategyResponse{Unsupported: true}, nil
}

// BuildPipelineSyncStages implements deployment.BuildPipelineSyncStages.
func (s *deploymentServiceServer) BuildPipelineSyncStages(ctx context.Context, request *deployment.BuildPipelineSyncStagesRequest) (*deployment.BuildPipelineSyncStagesResponse, error) {
// TODO: Implement this func
return &deployment.BuildPipelineSyncStagesResponse{}, nil
stages := make([]*model.PipelineStage, 0, len(request.GetStages()))
for _, stage := range request.GetStages() {
waitStage := newWaitStage()

id := stage.GetId()
if id == "" {
id = fmt.Sprintf("stage-%d", stage.GetIndex())
}
waitStage.Id = id
waitStage.Index = stage.GetIndex()
stages = append(stages, waitStage)
}

return &deployment.BuildPipelineSyncStagesResponse{Stages: stages}, nil
}

// BuildQuickSyncStages implements deployment.BuildQuickSyncStages.
func (s *deploymentServiceServer) BuildQuickSyncStages(ctx context.Context, request *deployment.BuildQuickSyncStagesRequest) (*deployment.BuildQuickSyncStagesResponse, error) {
// TODO: Implement this func
return &deployment.BuildQuickSyncStagesResponse{}, nil
return &deployment.BuildQuickSyncStagesResponse{
Stages: []*model.PipelineStage{},
}, nil
}

// newWaitStage returns a new WAIT stage with the current time.
// WAIT Stages is not used in the Rollback.
func newWaitStage() *model.PipelineStage {
now := time.Now()
return &model.PipelineStage{
Id: string(stageWait),
Name: string(stageWait),
Desc: "Wait for the specified duration",
Rollback: false,
Status: model.StageStatus_STAGE_NOT_STARTED_YET,
CreatedAt: now.Unix(),
UpdatedAt: now.Unix(),
}
}
119 changes: 119 additions & 0 deletions pkg/app/pipedv1/plugin/wait/deployment/server_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
// Copyright 2024 The PipeCD Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package deployment

import (
"context"
"testing"

"github.com/stretchr/testify/assert"
"go.uber.org/zap"

"github.com/pipe-cd/pipecd/pkg/model"
"github.com/pipe-cd/pipecd/pkg/plugin/api/v1alpha1/deployment"
)

func TestFetchDefinedStages(t *testing.T) {
t.Parallel()

server := NewDeploymentService(nil, zap.NewNop(), nil)
resp, err := server.FetchDefinedStages(context.Background(), &deployment.FetchDefinedStagesRequest{})
assert.NoError(t, err)
assert.Equal(t, resp.Stages, []string{"WAIT"})
}

func TestDetermineVersions(t *testing.T) {
t.Parallel()

server := NewDeploymentService(nil, zap.NewNop(), nil)
resp, err := server.DetermineVersions(context.Background(), &deployment.DetermineVersionsRequest{})
assert.NoError(t, err)
assert.Equal(t, resp.Versions, []*model.ArtifactVersion{}) // Empty
}

func TestDetermineStrategy(t *testing.T) {
t.Parallel()

server := NewDeploymentService(nil, zap.NewNop(), nil)
resp, err := server.DetermineStrategy(context.Background(), &deployment.DetermineStrategyRequest{})
assert.NoError(t, err)
assert.Equal(t, resp.Unsupported, true)
}

func TestBuildQuickSyncStages(t *testing.T) {
t.Parallel()

expected := &deployment.BuildQuickSyncStagesResponse{
Stages: []*model.PipelineStage{},
}

server := NewDeploymentService(nil, zap.NewNop(), nil)

resp, err := server.BuildQuickSyncStages(context.Background(), &deployment.BuildQuickSyncStagesRequest{
Rollback: false,
})
assert.NoError(t, err)
assert.Equal(t, resp, expected)
}

func TestBuildPipelineSyncStages(t *testing.T) {
t.Parallel()

req := &deployment.BuildPipelineSyncStagesRequest{
Stages: []*deployment.BuildPipelineSyncStagesRequest_StageConfig{
{
// ID is empty.
Name: "WAIT",
Index: 0,
},
{
Id: "stage-2",
Name: "WAIT",
Index: 2,
},
},
Rollback: true,
}

expected := &deployment.BuildPipelineSyncStagesResponse{
Stages: []*model.PipelineStage{
{
Id: "stage-0",
Name: "WAIT",
Desc: "Wait for the specified duration",
Index: 0,
Rollback: false,
Status: model.StageStatus_STAGE_NOT_STARTED_YET,
},
{
Id: "stage-2",
Name: "WAIT",
Desc: "Wait for the specified duration",
Index: 2,
Rollback: false,
Status: model.StageStatus_STAGE_NOT_STARTED_YET,
},
},
}

server := NewDeploymentService(nil, zap.NewNop(), nil)
resp, err := server.BuildPipelineSyncStages(context.Background(), req)
assert.NoError(t, err)
resp.Stages[0].CreatedAt = 0 // Ignore timestamps
resp.Stages[0].UpdatedAt = 0
resp.Stages[1].CreatedAt = 0
resp.Stages[1].UpdatedAt = 0
assert.Equal(t, resp, expected)
}
7 changes: 2 additions & 5 deletions pkg/app/pipedv1/plugin/wait/deployment/wait.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,9 @@ import (
"github.com/pipe-cd/pipecd/pkg/plugin/api/v1alpha1/deployment"
)

type Stage string

const (
logInterval = 10 * time.Second
startTimeKey = "startTime"
stageWait Stage = "WAIT"
logInterval = 10 * time.Second
startTimeKey = "startTime"
)

// Execute starts waiting for the specified duration.
Expand Down