diff --git a/pkg/app/pipedv1/plugin/kubernetes/planner/pipeline.go b/pkg/app/pipedv1/plugin/kubernetes/deployment/pipeline.go similarity index 63% rename from pkg/app/pipedv1/plugin/kubernetes/planner/pipeline.go rename to pkg/app/pipedv1/plugin/kubernetes/deployment/pipeline.go index cd95743cb2..e4292bac00 100644 --- a/pkg/app/pipedv1/plugin/kubernetes/planner/pipeline.go +++ b/pkg/app/pipedv1/plugin/kubernetes/deployment/pipeline.go @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -package planner +package deployment import ( "fmt" @@ -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, @@ -54,7 +90,6 @@ func MakeInitialStageMetadata(cfg config.PipelineStage) map[string]string { } } - func buildQuickSyncPipeline(autoRollback bool, now time.Time) []*model.PipelineStage { var ( preStageID = "" diff --git a/pkg/app/pipedv1/plugin/kubernetes/deployment/server.go b/pkg/app/pipedv1/plugin/kubernetes/deployment/server.go new file mode 100644 index 0000000000..6123c4d7e0 --- /dev/null +++ b/pkg/app/pipedv1/plugin/kubernetes/deployment/server.go @@ -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"), + } +} + +// Register registers all handling of this service into the specified gRPC server. +func (a *DeploymentService) Register(server *grpc.Server) { + deployment.RegisterDeploymentServiceServer(server, a) +} + +// DetermineStrategy implements deployment.DeploymentServiceServer. +func (a *DeploymentService) DetermineStrategy(context.Context, *deployment.DetermineStrategyRequest) (*deployment.DetermineStrategyResponse, error) { + panic("unimplemented") +} + +// 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") +} + +// BuildPipelineSyncStages implements deployment.DeploymentServiceServer. +func (a *DeploymentService) BuildPipelineSyncStages(context.Context, *deployment.BuildPipelineSyncStagesRequest) (*deployment.BuildPipelineSyncStagesResponse, error) { + panic("unimplemented") +} + +// 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 +} + +// 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)) + } + + return &deployment.FetchDefinedStagesResponse{ + Stages: stages, + }, nil +} diff --git a/pkg/app/pipedv1/plugin/kubernetes/planner/server.go b/pkg/app/pipedv1/plugin/kubernetes/planner/server.go deleted file mode 100644 index a84fbf8e78..0000000000 --- a/pkg/app/pipedv1/plugin/kubernetes/planner/server.go +++ /dev/null @@ -1,63 +0,0 @@ -// 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 planner - -import ( - "context" - "fmt" - - "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 secretDecrypter interface { - Decrypt(string) (string, error) -} - -type PlannerService struct { - deployment.UnimplementedDeploymentServiceServer - - Decrypter secretDecrypter - RegexPool *regexpool.Pool - Logger *zap.Logger -} - -// Register registers all handling of this service into the specified gRPC server. -func (a *PlannerService) Register(server *grpc.Server) { - deployment.RegisterDeploymentServiceServer(server, a) -} - -// NewPlannerService creates a new planService. -func NewPlannerService( - decrypter secretDecrypter, - logger *zap.Logger, -) *PlannerService { - return &PlannerService{ - Decrypter: decrypter, - RegexPool: regexpool.DefaultPool(), - Logger: logger.Named("planner"), - } -} - -func (ps *PlannerService) DetermineStrategy(ctx context.Context, in *deployment.DetermineStrategyRequest) (*deployment.DetermineStrategyResponse, error) { - return nil, fmt.Errorf("not implemented yet") -} - -func (ps *PlannerService) DetermineVersions(ctx context.Context, in *deployment.DetermineVersionsRequest) (*deployment.DetermineVersionsResponse, error) { - return nil, fmt.Errorf("not implemented yet") -} diff --git a/pkg/app/pipedv1/plugin/kubernetes/server.go b/pkg/app/pipedv1/plugin/kubernetes/server.go index ef936ec132..76103cb955 100644 --- a/pkg/app/pipedv1/plugin/kubernetes/server.go +++ b/pkg/app/pipedv1/plugin/kubernetes/server.go @@ -18,7 +18,7 @@ import ( "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" @@ -71,8 +71,7 @@ func (s *server) run(ctx context.Context, input cli.Input) (runErr error) { // 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( input.Logger, ) opts = []rpc.Option{