Skip to content

Commit

Permalink
Merge pull request #2832 from marlon-gamez/config-files-flag
Browse files Browse the repository at this point in the history
Add --config-files flag for generate-pipeline command (v2)
  • Loading branch information
tejal29 authored Sep 6, 2019
2 parents 9374e5c + e10a90d commit d07374b
Show file tree
Hide file tree
Showing 14 changed files with 387 additions and 41 deletions.
10 changes: 8 additions & 2 deletions cmd/skaffold/app/cmd/generate_pipeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,24 @@ import (
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/latest"
)

var (
configFiles []string
)

func NewCmdGeneratePipeline() *cobra.Command {
return NewCmd("generate-pipeline").
Hidden().
WithDescription("[ALPHA] Generate tekton pipeline from skaffold.yaml").
WithCommonFlags().
WithFlags(func(f *pflag.FlagSet) {}).
WithFlags(func(f *pflag.FlagSet) {
f.StringSliceVar(&configFiles, "config-files", nil, "Select additional files whose artifacts to use when generating pipeline.")
}).
NoArgs(cancelWithCtrlC(context.Background(), doGeneratePipeline))
}

func doGeneratePipeline(ctx context.Context, out io.Writer) error {
return withRunner(ctx, func(r runner.Runner, config *latest.SkaffoldConfig) error {
if err := r.GeneratePipeline(ctx, out, config, "pipeline.yaml"); err != nil {
if err := r.GeneratePipeline(ctx, out, config, configFiles, "pipeline.yaml"); err != nil {
return errors.Wrap(err, "generating ")
}
color.Default.Fprintln(out, "Pipeline config written to pipeline.yaml!")
Expand Down
56 changes: 51 additions & 5 deletions integration/generate_pipeline_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,17 @@ package integration
import (
"bytes"
"io/ioutil"
"os"
"testing"

"github.com/GoogleContainerTools/skaffold/integration/skaffold"
)

type configContents struct {
path string
data []byte
}

func TestGeneratePipeline(t *testing.T) {
if testing.Short() {
t.Skip("skipping integration test")
Expand All @@ -35,45 +41,85 @@ func TestGeneratePipeline(t *testing.T) {
tests := []struct {
description string
dir string
responses []byte
input []byte
args []string
configFiles []string
}{
{
description: "no profiles",
dir: "testdata/generate_pipeline/no_profiles",
responses: []byte("y"),
input: []byte("y\n"),
},
{
description: "existing oncluster profile",
dir: "testdata/generate_pipeline/existing_oncluster",
responses: []byte(""),
input: []byte(""),
},
{
description: "existing other profile",
dir: "testdata/generate_pipeline/existing_other",
responses: []byte("y"),
input: []byte("y\n"),
},
{
description: "multiple skaffold.yamls to create pipeline from",
dir: "testdata/generate_pipeline/multiple_configs",
input: []byte{'y', '\n', 'y', '\n'},
configFiles: []string{"sub-app/skaffold.yaml"},
},
}

for _, test := range tests {
t.Run(test.description, func(t *testing.T) {

args, contents, err := getOriginalContents(test.args, test.dir, test.configFiles)
if err != nil {
t.Fatal(err)
}
defer writeOriginalContents(contents)

originalConfig, err := ioutil.ReadFile(test.dir + "/skaffold.yaml")
if err != nil {
t.Error("error reading skaffold yaml")
}
defer ioutil.WriteFile(test.dir+"/skaffold.yaml", originalConfig, 0755)
defer os.Remove(test.dir + "/pipeline.yaml")

skaffoldEnv := []string{
"PIPELINE_GIT_URL=this-is-a-test",
"PIPELINE_SKAFFOLD_VERSION=test-version",
}
skaffold.GeneratePipeline().WithStdin([]byte("y\n")).WithEnv(skaffoldEnv).InDir(test.dir).RunOrFail(t)
skaffold.GeneratePipeline(args...).WithStdin(test.input).WithEnv(skaffoldEnv).InDir(test.dir).RunOrFail(t)

checkFileContents(t, test.dir+"/expectedSkaffold.yaml", test.dir+"/skaffold.yaml")
checkFileContents(t, test.dir+"/expectedPipeline.yaml", test.dir+"/pipeline.yaml")
})
}
}

func getOriginalContents(testArgs []string, testDir string, configFiles []string) ([]string, []configContents, error) {
var originalConfigs []configContents
if len(configFiles) != 0 {
for _, configFile := range configFiles {
testArgs = append(testArgs, []string{"--config-files", configFile}...)

path := testDir + "/" + configFile
contents, err := ioutil.ReadFile(path)
if err != nil {
return nil, nil, err
}
originalConfigs = append(originalConfigs, configContents{path, contents})
}
}

return testArgs, originalConfigs, nil
}

func writeOriginalContents(contents []configContents) {
for _, content := range contents {
ioutil.WriteFile(content.path, content.data, 0755)
}
}

func checkFileContents(t *testing.T, wantFile, gotFile string) {
wantContents, err := ioutil.ReadFile(wantFile)
if err != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ apiVersion: tekton.dev/v1alpha1
kind: Task
metadata:
creationTimestamp: null
name: skaffold-build
name: skaffold-build-0
spec:
inputs:
resources:
Expand Down Expand Up @@ -58,7 +58,7 @@ apiVersion: tekton.dev/v1alpha1
kind: Task
metadata:
creationTimestamp: null
name: skaffold-deploy
name: skaffold-deploy-0
spec:
inputs:
resources:
Expand Down Expand Up @@ -90,7 +90,7 @@ spec:
- name: source-repo
type: git
tasks:
- name: skaffold-build-task
- name: skaffold-build-0-task
resources:
inputs:
- name: source
Expand All @@ -99,15 +99,15 @@ spec:
- name: source
resource: source-repo
taskRef:
name: skaffold-build
- name: skaffold-deploy-task
name: skaffold-build-0
- name: skaffold-deploy-0-task
resources:
inputs:
- from:
- skaffold-build-task
- skaffold-build-0-task
name: source
resource: source-repo
taskRef:
name: skaffold-deploy
name: skaffold-deploy-0
status: {}
---
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ apiVersion: tekton.dev/v1alpha1
kind: Task
metadata:
creationTimestamp: null
name: skaffold-build
name: skaffold-build-0
spec:
inputs:
resources:
Expand Down Expand Up @@ -58,7 +58,7 @@ apiVersion: tekton.dev/v1alpha1
kind: Task
metadata:
creationTimestamp: null
name: skaffold-deploy
name: skaffold-deploy-0
spec:
inputs:
resources:
Expand Down Expand Up @@ -90,7 +90,7 @@ spec:
- name: source-repo
type: git
tasks:
- name: skaffold-build-task
- name: skaffold-build-0-task
resources:
inputs:
- name: source
Expand All @@ -99,15 +99,15 @@ spec:
- name: source
resource: source-repo
taskRef:
name: skaffold-build
- name: skaffold-deploy-task
name: skaffold-build-0
- name: skaffold-deploy-0-task
resources:
inputs:
- from:
- skaffold-build-task
- skaffold-build-0-task
name: source
resource: source-repo
taskRef:
name: skaffold-deploy
name: skaffold-deploy-0
status: {}
---
Loading

0 comments on commit d07374b

Please sign in to comment.