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 k8s plugin's FetchDefinedStages and BuildQuickSyncStages #5223

Merged
merged 11 commits into from
Sep 25, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package planner
package deployment

import (
"fmt"
Expand All @@ -23,11 +23,47 @@ import (
"github.com/pipe-cd/pipecd/pkg/model"
)

type Stage string

const (
// StageK8sSync represents the state where
// all resources should be synced with the Git state.
StageK8sSync Stage = "K8S_SYNC"
// StageK8sPrimaryRollout represents the state where
// the PRIMARY variant resources has been updated to the new version/configuration.
StageK8sPrimaryRollout Stage = "K8S_PRIMARY_ROLLOUT"
// StageK8sCanaryRollout represents the state where
// the CANARY variant resources has been rolled out with the new version/configuration.
StageK8sCanaryRollout Stage = "K8S_CANARY_ROLLOUT"
// StageK8sCanaryClean represents the state where
// the CANARY variant resources has been cleaned.
StageK8sCanaryClean Stage = "K8S_CANARY_CLEAN"
// StageK8sBaselineRollout represents the state where
// the BASELINE variant resources has been rolled out.
StageK8sBaselineRollout Stage = "K8S_BASELINE_ROLLOUT"
// StageK8sBaselineClean represents the state where
// the BASELINE variant resources has been cleaned.
StageK8sBaselineClean Stage = "K8S_BASELINE_CLEAN"
// StageK8sTrafficRouting represents the state where the traffic to application
// should be splitted as the specified percentage to PRIMARY, CANARY, BASELINE variants.
StageK8sTrafficRouting Stage = "K8S_TRAFFIC_ROUTING"
)

var AllStages = []Stage{
StageK8sSync,
StageK8sPrimaryRollout,
StageK8sCanaryRollout,
StageK8sCanaryClean,
StageK8sBaselineRollout,
StageK8sBaselineClean,
StageK8sTrafficRouting,
}

const (
PredefinedStageK8sSync = "K8sSync"
PredefinedStageRollback = "Rollback"
PredefinedStageK8sSync = "K8sSync"
PredefinedStageRollback = "Rollback"
)

var predefinedStages = map[string]config.PipelineStage{
PredefinedStageK8sSync: {
ID: PredefinedStageK8sSync,
Expand All @@ -54,7 +90,6 @@ func MakeInitialStageMetadata(cfg config.PipelineStage) map[string]string {
}
}


func buildQuickSyncPipeline(autoRollback bool, now time.Time) []*model.PipelineStage {
var (
preStageID = ""
Expand Down
85 changes: 85 additions & 0 deletions pkg/app/pipedv1/plugin/kubernetes/deployment/server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
// 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"
"time"

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

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

type DeploymentService struct {
deployment.UnimplementedDeploymentServiceServer

RegexPool *regexpool.Pool
Logger *zap.Logger
}

// NewDeploymentService creates a new planService.
func NewDeploymentService(
logger *zap.Logger,
) *DeploymentService {
return &DeploymentService{
RegexPool: regexpool.DefaultPool(),
Logger: logger.Named("planner"),
}

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

View check run for this annotation

Codecov / codecov/patch

pkg/app/pipedv1/plugin/kubernetes/deployment/server.go#L38-L42

Added lines #L38 - L42 were not covered by tests
}

// Register registers all handling of this service into the specified gRPC server.
func (a *DeploymentService) Register(server *grpc.Server) {
deployment.RegisterDeploymentServiceServer(server, a)

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

View check run for this annotation

Codecov / codecov/patch

pkg/app/pipedv1/plugin/kubernetes/deployment/server.go#L46-L47

Added lines #L46 - L47 were not covered by tests
}

// DetermineStrategy implements deployment.DeploymentServiceServer.
func (a *DeploymentService) DetermineStrategy(context.Context, *deployment.DetermineStrategyRequest) (*deployment.DetermineStrategyResponse, error) {
panic("unimplemented")

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

View check run for this annotation

Codecov / codecov/patch

pkg/app/pipedv1/plugin/kubernetes/deployment/server.go#L51-L52

Added lines #L51 - L52 were not covered by tests
}

// DetermineVersions implements deployment.DeploymentServiceServer.
func (a *DeploymentService) DetermineVersions(context.Context, *deployment.DetermineVersionsRequest) (*deployment.DetermineVersionsResponse, error) {
// TODO: how to determine whether the runnning or target deployment to use?
panic("unimplemented")

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

View check run for this annotation

Codecov / codecov/patch

pkg/app/pipedv1/plugin/kubernetes/deployment/server.go#L56-L58

Added lines #L56 - L58 were not covered by tests
}

// BuildPipelineSyncStages implements deployment.DeploymentServiceServer.
func (a *DeploymentService) BuildPipelineSyncStages(context.Context, *deployment.BuildPipelineSyncStagesRequest) (*deployment.BuildPipelineSyncStagesResponse, error) {
panic("unimplemented")

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

View check run for this annotation

Codecov / codecov/patch

pkg/app/pipedv1/plugin/kubernetes/deployment/server.go#L62-L63

Added lines #L62 - L63 were not covered by tests
}

// BuildQuickSyncStages implements deployment.DeploymentServiceServer.
func (a *DeploymentService) BuildQuickSyncStages(ctx context.Context, request *deployment.BuildQuickSyncStagesRequest) (*deployment.BuildQuickSyncStagesResponse, error) {
now := time.Now()
stages := buildQuickSyncPipeline(request.GetRollback(), now)
return &deployment.BuildQuickSyncStagesResponse{
Stages: stages,
}, nil

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

View check run for this annotation

Codecov / codecov/patch

pkg/app/pipedv1/plugin/kubernetes/deployment/server.go#L67-L72

Added lines #L67 - L72 were not covered by tests
}

// FetchDefinedStages implements deployment.DeploymentServiceServer.
func (a *DeploymentService) FetchDefinedStages(context.Context, *deployment.FetchDefinedStagesRequest) (*deployment.FetchDefinedStagesResponse, error) {
stages := make([]string, 0, len(AllStages))
for _, s := range AllStages {
stages = append(stages, string(s))
}

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

View check run for this annotation

Codecov / codecov/patch

pkg/app/pipedv1/plugin/kubernetes/deployment/server.go#L76-L80

Added lines #L76 - L80 were not covered by tests

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

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

View check run for this annotation

Codecov / codecov/patch

pkg/app/pipedv1/plugin/kubernetes/deployment/server.go#L82-L84

Added lines #L82 - L84 were not covered by tests
}
63 changes: 0 additions & 63 deletions pkg/app/pipedv1/plugin/kubernetes/planner/server.go

This file was deleted.

5 changes: 2 additions & 3 deletions pkg/app/pipedv1/plugin/kubernetes/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"context"
"time"

"github.com/pipe-cd/pipecd/pkg/app/pipedv1/plugin/kubernetes/planner"
"github.com/pipe-cd/pipecd/pkg/app/pipedv1/plugin/kubernetes/deployment"
"github.com/pipe-cd/pipecd/pkg/cli"
"github.com/pipe-cd/pipecd/pkg/rpc"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -71,8 +71,7 @@
// Start a gRPC server for handling external API requests.
{
var (
service = planner.NewPlannerService(
nil, // TODO: Inject the real secret decrypter. It should be a instance of pipedv1/plugin/secrets.Decrypter.
service = deployment.NewDeploymentService(

Check warning on line 74 in pkg/app/pipedv1/plugin/kubernetes/server.go

View check run for this annotation

Codecov / codecov/patch

pkg/app/pipedv1/plugin/kubernetes/server.go#L74

Added line #L74 was not covered by tests
input.Logger,
)
opts = []rpc.Option{
Expand Down
Loading