Skip to content

Commit

Permalink
Add CloudBuild
Browse files Browse the repository at this point in the history
  • Loading branch information
ZhuGongpu committed Jul 1, 2020
1 parent 0c19e82 commit 6be7def
Show file tree
Hide file tree
Showing 6 changed files with 144 additions and 5 deletions.
4 changes: 2 additions & 2 deletions internal/cmdexport/cmdexport.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func ExportCommand() *cobra.Command {
return GetExportRunner().Command
}

// Create a ExportRunner instance and wire it to the corresponding Command.
// GetExportRunner creates a ExportRunner instance and wires it to the corresponding Command.
func GetExportRunner() *ExportRunner {
r := &ExportRunner{PipelineConfig: &types.PipelineConfig{}}
c := &cobra.Command{
Expand Down Expand Up @@ -81,7 +81,7 @@ type ExportRunner struct {
Pipeline orchestrators.Pipeline
}

// Generate the pipeline and write it into a file or stdout.
// runE generates the pipeline and writes it into a file or stdout.
func (r *ExportRunner) runE(c *cobra.Command, args []string) error {
pipeline := r.Pipeline.Init(r.PipelineConfig).Generate()

Expand Down
61 changes: 61 additions & 0 deletions internal/cmdexport/orchestrators/cloudbuild.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// Copyright 2020 Google LLC
//
// 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 orchestrators

import (
"github.com/GoogleContainerTools/kpt/internal/cmdexport/types"
"sigs.k8s.io/kustomize/kyaml/yaml"
)

// CloudBuild is a simplified representation of Cloud Build config.
// @see https://cloud.google.com/cloud-build/docs/build-config
type CloudBuild struct {
Steps []CloudBuildStep `yaml:",omitempty"`
}

type CloudBuildStep struct {
Name string `yaml:",omitempty"`
Args []string `yaml:",omitempty"`
}

func (p *CloudBuild) Init(config *types.PipelineConfig) Pipeline {
step := CloudBuildStep{}
step.Name = KptImage
step.Args = []string{
"fn",
"run",
config.Dir,
}

if fnPaths := config.FnPaths; len(fnPaths) > 0 {
step.Args = append(
step.Args,
append(
[]string{"--fn-path"},
fnPaths...
)...
)
}

p.Steps = []CloudBuildStep{step}

return p
}

func (p *CloudBuild) Generate() []byte {
data, _ := yaml.Marshal(p)

return data
}
75 changes: 75 additions & 0 deletions internal/cmdexport/orchestrators/cloudbuild_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
// Copyright 2020 Google LLC
//
// 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 orchestrators

import "github.com/GoogleContainerTools/kpt/internal/cmdexport/types"

var cloudBuildTestCases = []testCase{
{
description: "generates a Cloud Build pipeline",
config: &types.PipelineConfig{
Dir: ".",
FnPaths: nil,
},
expected: `
steps:
- name: gongpu/kpt:latest
args:
- fn
- run
- .
`,
},
{
description: "generates a Cloud Build pipeline with --fn-path",
config: &types.PipelineConfig{
Dir: "resources",
FnPaths: []string{"config/function.yaml"},
},
expected: `
steps:
- name: gongpu/kpt:latest
args:
- fn
- run
- resources
- --fn-path
- config/function.yaml
`,
},
{
description: "generates a Cloud Build pipeline with multiple --fn-path",
config: &types.PipelineConfig{
Dir: "resources",
FnPaths: []string{"config/function1.yaml", "config/function2.yaml"},
},
expected: `
steps:
- name: gongpu/kpt:latest
args:
- fn
- run
- resources
- --fn-path
- config/function1.yaml
- config/function2.yaml
`,
},
}

var cloudBuildTestSuite = testSuite{
pipeline: &CloudBuild{},
testCases: cloudBuildTestCases,
}
2 changes: 1 addition & 1 deletion internal/cmdexport/orchestrators/githubactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
"sigs.k8s.io/kustomize/kyaml/yaml"
)

// Represent a GitHub Actions workflow.
// GitHubActions represents a GitHub Actions workflow.
// @see https://help.github.com/en/actions/reference/workflow-syntax-for-github-actions
type GitHubActions struct {
Name string `yaml:",omitempty"`
Expand Down
5 changes: 4 additions & 1 deletion internal/cmdexport/orchestrators/pipeline_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,10 @@ type testSuite struct {
}

func TestPipeline(t *testing.T) {
testSuites := []testSuite{githubActionsTestSuite}
testSuites := []testSuite{
githubActionsTestSuite,
cloudBuildTestSuite,
}

for _, testSuite := range testSuites {
pipeline := testSuite.pipeline
Expand Down
2 changes: 1 addition & 1 deletion internal/cmdexport/types/PipelineConfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

package types

// Configuration of a pipeline.
// PipelineConfig describes configuration of a pipeline.
type PipelineConfig struct {
Dir string
FnPaths []string
Expand Down

0 comments on commit 6be7def

Please sign in to comment.