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

Add plugin planner for k8s #4819

Merged
merged 13 commits into from
Mar 15, 2024
17 changes: 17 additions & 0 deletions pkg/app/pipedv1/controller/pluginregistry.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,21 @@ func DefaultPluginRegistry() PluginRegistry {

func init() {
// TODO: Register all available built-in plugins.

// NOTE: If you want to directry test the plugin, you can use the following code.

// defaultPluginRegistry.mu.Lock()
// defer defaultPluginRegistry.mu.Unlock()

// options := []rpcclient.DialOption{
// rpcclient.WithBlock(),
// rpcclient.WithInsecure(),
// }

// cli, err := platform.NewClient(context.Background(), "localhost:10000", options...)
// if err != nil {
// panic(err)
// }

// defaultPluginRegistry.plugins[model.ApplicationKind_KUBERNETES] = cli
}
34 changes: 34 additions & 0 deletions pkg/app/pipedv1/plugin/platform/kubernetes/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// 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 main

import (
"log"

"github.com/pipe-cd/pipecd/pkg/cli"
)

func main() {
app := cli.NewApp(
"pipecd-plugin-kubernetes",
"Plugin component to deploy Kubernetes Application.",
)
app.AddCommands(
NewServerCommand(),
)
if err := app.Run(); err != nil {
log.Fatal(err)
}
}
146 changes: 146 additions & 0 deletions pkg/app/pipedv1/plugin/platform/kubernetes/planner/pipeline.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
// 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 (
"encoding/json"
"fmt"
"time"

"github.com/pipe-cd/pipecd/pkg/app/pipedv1/planner"
"github.com/pipe-cd/pipecd/pkg/config"
"github.com/pipe-cd/pipecd/pkg/model"
)

func buildQuickSyncPipeline(autoRollback bool, now time.Time) []*model.PipelineStage {
var (
preStageID = ""
stage, _ = planner.GetPredefinedStage(planner.PredefinedStageK8sSync)
stages = []config.PipelineStage{stage}
out = make([]*model.PipelineStage, 0, len(stages))
)

for i, s := range stages {
id := s.ID
if id == "" {
id = fmt.Sprintf("stage-%d", i)
}
stage := &model.PipelineStage{
Id: id,
Name: s.Name.String(),
Desc: s.Desc,
Index: int32(i),
Predefined: true,
Visible: true,
Status: model.StageStatus_STAGE_NOT_STARTED_YET,
Metadata: planner.MakeInitialStageMetadata(s),
CreatedAt: now.Unix(),
UpdatedAt: now.Unix(),
}
if preStageID != "" {
stage.Requires = []string{preStageID}
}
preStageID = id
out = append(out, stage)
}

if autoRollback {
s, _ := planner.GetPredefinedStage(planner.PredefinedStageRollback)
out = append(out, &model.PipelineStage{
Id: s.ID,
Name: s.Name.String(),
Desc: s.Desc,
Predefined: true,
Visible: false,
Status: model.StageStatus_STAGE_NOT_STARTED_YET,
CreatedAt: now.Unix(),
UpdatedAt: now.Unix(),
})
}

return out
}

func buildProgressivePipeline(pp *config.DeploymentPipeline, autoRollback bool, now time.Time) []*model.PipelineStage {
var (
preStageID = ""
out = make([]*model.PipelineStage, 0, len(pp.Stages))
)

for i, s := range pp.Stages {
id := s.ID
if id == "" {
id = fmt.Sprintf("stage-%d", i)
}
stage := &model.PipelineStage{
Id: id,
Name: s.Name.String(),
Desc: s.Desc,
Index: int32(i),
Predefined: false,
Visible: true,
Status: model.StageStatus_STAGE_NOT_STARTED_YET,
Metadata: planner.MakeInitialStageMetadata(s),
CreatedAt: now.Unix(),
UpdatedAt: now.Unix(),
}
if preStageID != "" {
stage.Requires = []string{preStageID}
}
preStageID = id
out = append(out, stage)
}

if autoRollback {
s, _ := planner.GetPredefinedStage(planner.PredefinedStageRollback)
out = append(out, &model.PipelineStage{
Id: s.ID,
Name: s.Name.String(),
Desc: s.Desc,
Predefined: true,
Visible: false,
Status: model.StageStatus_STAGE_NOT_STARTED_YET,
CreatedAt: now.Unix(),
UpdatedAt: now.Unix(),
})

// Add a stage for rolling back script run stages.
for i, s := range pp.Stages {
if s.Name == model.StageScriptRun {
// Use metadata as a way to pass parameters to the stage.
envStr, _ := json.Marshal(s.ScriptRunStageOptions.Env)
metadata := map[string]string{
"baseStageID": out[i].Id,
"onRollback": s.ScriptRunStageOptions.OnRollback,
"env": string(envStr),
}
ss, _ := planner.GetPredefinedStage(planner.PredefinedStageScriptRunRollback)
out = append(out, &model.PipelineStage{
Id: ss.ID,
Name: ss.Name.String(),
Desc: ss.Desc,
Predefined: true,
Visible: false,
Status: model.StageStatus_STAGE_NOT_STARTED_YET,
Metadata: metadata,
CreatedAt: now.Unix(),
UpdatedAt: now.Unix(),
})
}
}
}

return out
}
Loading
Loading