Skip to content

Commit

Permalink
Add plugin planner for k8s (#4819)
Browse files Browse the repository at this point in the history
* [WIP] Add planner

Signed-off-by: Yoshiki Fujikane <ffjlabo@gmail.com>

* Not to use out.Version

Signed-off-by: Yoshiki Fujikane <ffjlabo@gmail.com>

* Use last_successful_commit_hash and last_successful_config_file_name

Signed-off-by: Yoshiki Fujikane <ffjlabo@gmail.com>

* Use in.WorkingDir

Signed-off-by: Yoshiki Fujikane <ffjlabo@gmail.com>

* Use in.PipedConfig

Signed-off-by: Yoshiki Fujikane <ffjlabo@gmail.com>

* Create git client

Signed-off-by: Yoshiki Fujikane <ffjlabo@gmail.com>

* Create secret encryptor

Signed-off-by: Yoshiki Fujikane <ffjlabo@gmail.com>

* Add startup server implementation

Signed-off-by: Yoshiki Fujikane <ffjlabo@gmail.com>

* Fix for relocation of proto api

Signed-off-by: Yoshiki Fujikane <ffjlabo@gmail.com>

* Add roughly implementation for planner plugin

Signed-off-by: Yoshiki Fujikane <ffjlabo@gmail.com>

* Rename pkg name

Signed-off-by: Yoshiki Fujikane <ffjlabo@gmail.com>

* Add licence

Signed-off-by: Yoshiki Fujikane <ffjlabo@gmail.com>

* Comment out for the testing code

Signed-off-by: Yoshiki Fujikane <ffjlabo@gmail.com>

---------

Signed-off-by: Yoshiki Fujikane <ffjlabo@gmail.com>
  • Loading branch information
ffjlabo authored and t-kikuc committed Apr 8, 2024
1 parent a52876b commit 095d46c
Show file tree
Hide file tree
Showing 5 changed files with 919 additions and 0 deletions.
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

0 comments on commit 095d46c

Please sign in to comment.