-
Notifications
You must be signed in to change notification settings - Fork 226
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
152 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
// 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 ( | ||
"fmt" | ||
"path" | ||
"strings" | ||
|
||
"github.com/GoogleContainerTools/kpt/internal/cmdexport/types" | ||
"gopkg.in/yaml.v3" | ||
) | ||
|
||
// GitLabCi is a simplified representation of GitLab CI/CD Configuration. | ||
// @see https://docs.gitlab.com/ee/ci/yaml/ | ||
type GitLabCI struct { | ||
Stages []string `yaml:",omitempty"` | ||
// @see https://github.com/go-yaml/yaml/issues/63 | ||
GitLabCIStages `yaml:",inline"` | ||
} | ||
|
||
type GitLabCIStages map[string]GitLabCIStage | ||
|
||
type GitLabCIStage struct { | ||
Stage string `yaml:",omitempty"` | ||
Image string `yaml:",omitempty"` | ||
Services []string `yaml:",omitempty"` | ||
Script string `yaml:",omitempty"` | ||
} | ||
|
||
func (p *GitLabCI) Init(config *types.PipelineConfig) Pipeline { | ||
stage := "run-kpt-functions" | ||
p.Stages = []string{stage} | ||
|
||
p.GitLabCIStages = map[string]GitLabCIStage{ | ||
"kpt": { | ||
Stage: stage, | ||
Image: "docker", | ||
Services: []string{"docker:dind"}, | ||
Script: p.generateScript(config), | ||
}, | ||
} | ||
|
||
return p | ||
} | ||
|
||
func (p *GitLabCI) Generate() (out []byte, err error) { | ||
return yaml.Marshal(p) | ||
} | ||
|
||
func (p *GitLabCI) generateScript(config *types.PipelineConfig) (script string) { | ||
mountDir := "/app" | ||
fnPaths := []string{"--fn-path"} | ||
|
||
for _, p := range config.FnPaths { | ||
// p is preprocessed in ExportRunner and guaranteed to be a relative path. | ||
fnPaths = append(fnPaths, path.Join(mountDir, p)) | ||
} | ||
|
||
parts := []string{ | ||
"docker run", | ||
fmt.Sprintf("-v $PWD:%s", mountDir), | ||
"-v /var/run/docker.sock:/var/run/docker.sock", | ||
KptImage, | ||
"fn run", | ||
path.Join(mountDir, config.Dir), | ||
} | ||
|
||
if len(config.FnPaths) > 0 { | ||
parts = append(parts, fnPaths...) | ||
} | ||
|
||
script = strings.Join(parts, " ") | ||
|
||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
// 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 gitlabCITestCases = []testCase{ | ||
{ | ||
description: "generate a GitLab CI pipeline against the current directory", | ||
config: &types.PipelineConfig{ | ||
Dir: ".", | ||
}, | ||
expected: ` | ||
stages: | ||
- run-kpt-functions | ||
kpt: | ||
stage: run-kpt-functions | ||
image: docker | ||
services: | ||
- docker:dind | ||
script: docker run -v $PWD:/app -v /var/run/docker.sock:/var/run/docker.sock gongpu/kpt:latest | ||
fn run /app | ||
`, | ||
}, | ||
{ | ||
description: "generate a GitLab CI pipeline with --fn-path", | ||
config: &types.PipelineConfig{ | ||
Dir: "resources", | ||
FnPaths: []string{ | ||
"config/label-namespace.yaml", | ||
"config/application-cr.yaml", | ||
}, | ||
}, | ||
expected: ` | ||
stages: | ||
- run-kpt-functions | ||
kpt: | ||
stage: run-kpt-functions | ||
image: docker | ||
services: | ||
- docker:dind | ||
script: docker run -v $PWD:/app -v /var/run/docker.sock:/var/run/docker.sock gongpu/kpt:latest | ||
fn run /app/resources --fn-path /app/config/label-namespace.yaml /app/config/application-cr.yaml | ||
`, | ||
}, | ||
} | ||
|
||
var gitlabCITestSuite = testSuite{ | ||
pipeline: new(GitLabCI), | ||
testCases: gitlabCITestCases, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters