Skip to content

Commit

Permalink
Merge pull request #2638 from marlon-gamez/resource
Browse files Browse the repository at this point in the history
Link task resources in generate-pipeline output
  • Loading branch information
tejal29 authored Aug 14, 2019
2 parents 8aa4d28 + fa19b38 commit 686e75f
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 68 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,21 @@ spec:
outputImageDir: ""
targetPath: ""
type: git
outputs:
resources:
- name: source
outputImageDir: ""
targetPath: ""
type: git
steps:
- args:
- build
- --filename
- skaffold.yaml
- --profile
- oncluster
- --file-output
- build.out
command:
- skaffold
- build
env:
- name: GOOGLE_APPLICATION_CREDENTIALS
value: /secret/kaniko-secret
Expand Down Expand Up @@ -62,13 +66,11 @@ spec:
type: git
steps:
- args:
- deploy
- --filename
- skaffold.yaml
- --build-artifacts
- build.out
command:
- skaffold
- deploy
image: gcr.io/k8s-skaffold/skaffold:test-version
name: run-deploy
resources: {}
Expand All @@ -89,15 +91,18 @@ spec:
inputs:
- name: source
resource: source-repo
outputs:
- name: source
resource: source-repo
taskRef:
name: skaffold-build
- name: skaffold-deploy-task
resources:
inputs:
- name: source
- from:
- skaffold-build-task
name: source
resource: source-repo
runAfter:
- skaffold-build-task
taskRef:
name: skaffold-deploy
status: {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,21 @@ spec:
outputImageDir: ""
targetPath: ""
type: git
outputs:
resources:
- name: source
outputImageDir: ""
targetPath: ""
type: git
steps:
- args:
- build
- --filename
- skaffold.yaml
- --profile
- oncluster
- --file-output
- build.out
command:
- skaffold
- build
env:
- name: GOOGLE_APPLICATION_CREDENTIALS
value: /secret/kaniko-secret
Expand Down Expand Up @@ -62,13 +66,11 @@ spec:
type: git
steps:
- args:
- deploy
- --filename
- skaffold.yaml
- --build-artifacts
- build.out
command:
- skaffold
- deploy
image: gcr.io/k8s-skaffold/skaffold:test-version
name: run-deploy
resources: {}
Expand All @@ -89,15 +91,18 @@ spec:
inputs:
- name: source
resource: source-repo
outputs:
- name: source
resource: source-repo
taskRef:
name: skaffold-build
- name: skaffold-deploy-task
resources:
inputs:
- name: source
- from:
- skaffold-build-task
name: source
resource: source-repo
runAfter:
- skaffold-build-task
taskRef:
name: skaffold-deploy
status: {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,21 @@ spec:
outputImageDir: ""
targetPath: ""
type: git
outputs:
resources:
- name: source
outputImageDir: ""
targetPath: ""
type: git
steps:
- args:
- build
- --filename
- skaffold.yaml
- --profile
- oncluster
- --file-output
- build.out
command:
- skaffold
- build
env:
- name: GOOGLE_APPLICATION_CREDENTIALS
value: /secret/kaniko-secret
Expand Down Expand Up @@ -62,13 +66,11 @@ spec:
type: git
steps:
- args:
- deploy
- --filename
- skaffold.yaml
- --build-artifacts
- build.out
command:
- skaffold
- deploy
image: gcr.io/k8s-skaffold/skaffold:test-version
name: run-deploy
resources: {}
Expand All @@ -89,15 +91,18 @@ spec:
inputs:
- name: source
resource: source-repo
outputs:
- name: source
resource: source-repo
taskRef:
name: skaffold-build
- name: skaffold-deploy-task
resources:
inputs:
- name: source
- from:
- skaffold-build-task
name: source
resource: source-repo
runAfter:
- skaffold-build-task
taskRef:
name: skaffold-deploy
status: {}
Expand Down
32 changes: 21 additions & 11 deletions pkg/skaffold/generate_pipeline/generate_pipeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"io"
"os"
"os/exec"
"strings"

"github.com/ghodss/yaml"
"github.com/pkg/errors"
Expand Down Expand Up @@ -125,20 +126,22 @@ func generateBuildTask(buildConfig latest.BuildConfig) (*tekton.Task, error) {
Type: tekton.PipelineResourceTypeGit,
},
}
inputs := &tekton.Inputs{Resources: resources}
outputs := &tekton.Outputs{Resources: resources}
steps := []corev1.Container{
{
Name: "run-build",
Image: fmt.Sprintf("gcr.io/k8s-skaffold/skaffold:%s", skaffoldVersion),
WorkingDir: "/workspace/source",
Command: []string{"skaffold"},
Args: []string{"build",
"--filename", "skaffold.yaml",
Command: []string{"skaffold", "build"},
Args: []string{
"--profile", "oncluster",
"--file-output", "build.out",
},
},
}

// Add secret volume mounting for artifacts that need to be built with kaniko
var volumes []corev1.Volume
if buildConfig.Artifacts[0].KanikoArtifact != nil {
volumes = []corev1.Volume{
Expand All @@ -165,7 +168,7 @@ func generateBuildTask(buildConfig latest.BuildConfig) (*tekton.Task, error) {
}
}

return pipeline.NewTask("skaffold-build", resources, steps, volumes), nil
return pipeline.NewTask("skaffold-build", inputs, outputs, steps, volumes), nil
}

func generateDeployTask(deployConfig latest.DeployConfig) (*tekton.Task, error) {
Expand All @@ -184,21 +187,20 @@ func generateDeployTask(deployConfig latest.DeployConfig) (*tekton.Task, error)
Type: tekton.PipelineResourceTypeGit,
},
}
inputs := &tekton.Inputs{Resources: resources}
steps := []corev1.Container{
{
Name: "run-deploy",
Image: fmt.Sprintf("gcr.io/k8s-skaffold/skaffold:%s", skaffoldVersion),
WorkingDir: "/workspace/source",
Command: []string{"skaffold"},
Command: []string{"skaffold", "deploy"},
Args: []string{
"deploy",
"--filename", "skaffold.yaml",
"--build-artifacts", "build.out",
},
},
}

return pipeline.NewTask("skaffold-deploy", resources, steps, nil), nil
return pipeline.NewTask("skaffold-deploy", inputs, nil, steps, nil), nil
}

func generatePipeline(tasks []*tekton.Task) (*tekton.Pipeline, error) {
Expand All @@ -220,7 +222,6 @@ func generatePipeline(tasks []*tekton.Task) (*tekton.Pipeline, error) {
TaskRef: tekton.TaskRef{
Name: task.Name,
},
RunAfter: []string{},
Resources: &tekton.PipelineTaskResources{
Inputs: []tekton.PipelineTaskInputResource{
{
Expand All @@ -230,9 +231,18 @@ func generatePipeline(tasks []*tekton.Task) (*tekton.Pipeline, error) {
},
},
}
if i > 0 {
pipelineTask.RunAfter = []string{pipelineTasks[i-1].Name}
// Add output resource for build tasks, input for deploy task
if strings.Contains(task.Name, "build") {
pipelineTask.Resources.Outputs = []tekton.PipelineTaskOutputResource{
{
Name: "source",
Resource: "source-repo",
},
}
} else {
pipelineTask.Resources.Inputs[0].From = []string{pipelineTasks[i-1].Name}
}

pipelineTasks = append(pipelineTasks, pipelineTask)
}

Expand Down
34 changes: 22 additions & 12 deletions pkg/skaffold/generate_pipeline/generate_pipeline_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func TestGeneratePipeline(t *testing.T) {
tasks: []*tekton.Task{
{
ObjectMeta: metav1.ObjectMeta{
Name: "test",
Name: "test-build",
},
},
},
Expand All @@ -59,18 +59,23 @@ func TestGeneratePipeline(t *testing.T) {
},
Tasks: []tekton.PipelineTask{
{
Name: "test-task",
Name: "test-build-task",
TaskRef: tekton.TaskRef{
Name: "test",
Name: "test-build",
},
RunAfter: []string{},
Resources: &tekton.PipelineTaskResources{
Inputs: []tekton.PipelineTaskInputResource{
{
Name: "source",
Resource: "source-repo",
},
},
Outputs: []tekton.PipelineTaskOutputResource{
{
Name: "source",
Resource: "source-repo",
},
},
},
},
},
Expand All @@ -83,12 +88,12 @@ func TestGeneratePipeline(t *testing.T) {
tasks: []*tekton.Task{
{
ObjectMeta: metav1.ObjectMeta{
Name: "test1",
Name: "test-build",
},
},
{
ObjectMeta: metav1.ObjectMeta{
Name: "test2",
Name: "test-deploy",
},
},
},
Expand All @@ -109,31 +114,36 @@ func TestGeneratePipeline(t *testing.T) {
},
Tasks: []tekton.PipelineTask{
{
Name: "test1-task",
Name: "test-build-task",
TaskRef: tekton.TaskRef{
Name: "test1",
Name: "test-build",
},
RunAfter: []string{},
Resources: &tekton.PipelineTaskResources{
Inputs: []tekton.PipelineTaskInputResource{
{
Name: "source",
Resource: "source-repo",
},
},
Outputs: []tekton.PipelineTaskOutputResource{
{
Name: "source",
Resource: "source-repo",
},
},
},
},
{
Name: "test2-task",
Name: "test-deploy-task",
TaskRef: tekton.TaskRef{
Name: "test2",
Name: "test-deploy",
},
RunAfter: []string{"test1-task"},
Resources: &tekton.PipelineTaskResources{
Inputs: []tekton.PipelineTaskInputResource{
{
Name: "source",
Resource: "source-repo",
From: []string{"test-build-task"},
},
},
},
Expand Down
7 changes: 3 additions & 4 deletions pkg/skaffold/pipeline/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

func NewTask(taskName string, inResources []tekton.TaskResource, steps []corev1.Container, volumes []corev1.Volume) *tekton.Task {
func NewTask(taskName string, inputs *tekton.Inputs, outputs *tekton.Outputs, steps []corev1.Container, volumes []corev1.Volume) *tekton.Task {
return &tekton.Task{
TypeMeta: metav1.TypeMeta{
Kind: "Task",
Expand All @@ -32,9 +32,8 @@ func NewTask(taskName string, inResources []tekton.TaskResource, steps []corev1.
Name: taskName,
},
Spec: tekton.TaskSpec{
Inputs: &tekton.Inputs{
Resources: inResources,
},
Inputs: inputs,
Outputs: outputs,
Steps: steps,
Volumes: volumes,
},
Expand Down
Loading

0 comments on commit 686e75f

Please sign in to comment.