Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor code used by pkg/skaffold/runner/generate_pipeline.go #2617

Merged
merged 3 commits into from
Aug 8, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
152 changes: 152 additions & 0 deletions pkg/skaffold/generate_pipeline/generate_pipeline.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
/*
Copyright 2019 The Skaffold 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 generatepipeline

import (
"fmt"
"os"
"os/exec"

"github.com/pkg/errors"

"github.com/GoogleContainerTools/skaffold/pkg/skaffold/pipeline"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/latest"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/version"

tekton "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1alpha1"
corev1 "k8s.io/api/core/v1"
)

func GenerateGitResource() (*tekton.PipelineResource, error) {
marlon-gamez marked this conversation as resolved.
Show resolved Hide resolved
// Get git repo url
gitURL := os.Getenv("PIPELINE_GIT_URL")
if gitURL == "" {
getGitRepo := exec.Command("git", "config", "--get", "remote.origin.url")
bGitRepo, err := getGitRepo.Output()
if err != nil {
return nil, errors.Wrap(err, "getting git repo from git config")
}
gitURL = string(bGitRepo)
}

return pipeline.NewGitResource("source-git", gitURL), nil
}

func GenerateBuildTask(buildConfig latest.BuildConfig) (*tekton.Task, error) {
if len(buildConfig.Artifacts) == 0 {
return nil, errors.New("no artifacts to build")
}

skaffoldVersion := os.Getenv("PIPELINE_SKAFFOLD_VERSION")
if skaffoldVersion == "" {
skaffoldVersion = version.Get().Version
}

resources := []tekton.TaskResource{
{
Name: "source",
Type: tekton.PipelineResourceTypeGit,
},
}
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",
"--profile", "oncluster",
"--file-output", "build.out",
},
},
}

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

func GenerateDeployTask(deployConfig latest.DeployConfig) (*tekton.Task, error) {
if deployConfig.HelmDeploy == nil && deployConfig.KubectlDeploy == nil && deployConfig.KustomizeDeploy == nil {
return nil, errors.New("no Helm/Kubectl/Kustomize deploy config")
}

skaffoldVersion := os.Getenv("PIPELINE_SKAFFOLD_VERSION")
if skaffoldVersion == "" {
skaffoldVersion = version.Get().Version
}

resources := []tekton.TaskResource{
{
Name: "source",
Type: tekton.PipelineResourceTypeGit,
},
}
steps := []corev1.Container{
{
Name: "run-deploy",
Image: fmt.Sprintf("gcr.io/k8s-skaffold/skaffold:%s", skaffoldVersion),
WorkingDir: "/workspace/source",
Command: []string{"skaffold"},
Args: []string{
"deploy",
"--filename", "skaffold.yaml",
"--profile", "oncluster",
"--build-artifacts", "build.out",
},
},
}

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

func GeneratePipeline(tasks []*tekton.Task) (*tekton.Pipeline, error) {
if len(tasks) == 0 {
return nil, errors.New("no tasks to add to pipeline")
}

resources := []tekton.PipelineDeclaredResource{
{
Name: "source-repo",
Type: tekton.PipelineResourceTypeGit,
},
}
// Create tasks in pipeline spec for all corresponding tasks
pipelineTasks := make([]tekton.PipelineTask, 0)
for i, task := range tasks {
pipelineTask := tekton.PipelineTask{
Name: fmt.Sprintf("%s-task", task.Name),
TaskRef: tekton.TaskRef{
Name: task.Name,
},
RunAfter: []string{},
Resources: &tekton.PipelineTaskResources{
Inputs: []tekton.PipelineTaskInputResource{
{
Name: "source",
Resource: "source-repo",
},
},
},
}
if i > 0 {
pipelineTask.RunAfter = []string{pipelineTasks[i-1].Name}
}
pipelineTasks = append(pipelineTasks, pipelineTask)
}

return pipeline.NewPipeline("skaffold-pipeline", resources, pipelineTasks), nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,9 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package runner
package generatepipeline

import (
"io/ioutil"
"testing"

"github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/latest"
Expand Down Expand Up @@ -152,7 +151,7 @@ func TestGeneratePipeline(t *testing.T) {

for _, test := range tests {
testutil.Run(t, test.description, func(t *testutil.T) {
pipeline, err := generatePipeline(test.tasks)
pipeline, err := GeneratePipeline(test.tasks)
t.CheckErrorAndDeepEqual(test.shouldErr, err, test.expectedPipeline, pipeline)
})
}
Expand Down Expand Up @@ -186,7 +185,7 @@ func TestGenerateBuildTask(t *testing.T) {

for _, test := range tests {
testutil.Run(t, test.description, func(t *testutil.T) {
_, err := generateBuildTask(test.buildConfig)
_, err := GenerateBuildTask(test.buildConfig)
t.CheckError(test.shouldErr, err)
})
}
Expand Down Expand Up @@ -222,58 +221,8 @@ func TestGenerateDeployTask(t *testing.T) {

for _, test := range tests {
testutil.Run(t, test.description, func(t *testutil.T) {
_, err := generateDeployTask(test.deployConfig)
_, err := GenerateDeployTask(test.deployConfig)
t.CheckError(test.shouldErr, err)
})
}
}

func TestGenerateProfile(t *testing.T) {
var tests = []struct {
description string
skaffoldConfig *latest.SkaffoldConfig
expectedProfile *latest.Profile
responses []string
shouldErr bool
}{
{
description: "successful profile generation",
skaffoldConfig: &latest.SkaffoldConfig{
Pipeline: latest.Pipeline{
Build: latest.BuildConfig{
Artifacts: []*latest.Artifact{
{
ImageName: "test",
},
},
},
},
},
expectedProfile: &latest.Profile{
Name: "oncluster",
Pipeline: latest.Pipeline{
Build: latest.BuildConfig{
Artifacts: []*latest.Artifact{
{
ImageName: "test-pipeline",
},
},
BuildType: latest.BuildType{
Cluster: &latest.ClusterDetails{
PullSecretName: "kaniko-secret",
},
},
},
},
},
shouldErr: false,
},
}

for _, test := range tests {
testutil.Run(t, test.description, func(t *testutil.T) {
profile, err := generateProfile(ioutil.Discard, test.skaffoldConfig)
t.CheckErrorAndDeepEqual(test.shouldErr, err, test.expectedProfile, profile)
})
}
}
143 changes: 143 additions & 0 deletions pkg/skaffold/generate_pipeline/profile.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
/*
Copyright 2019 The Skaffold 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 generatepipeline

import (
"bufio"
"fmt"
"io"
"io/ioutil"
"os"
"strings"

"github.com/GoogleContainerTools/skaffold/pkg/skaffold/color"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/latest"
"github.com/pkg/errors"

yamlv2 "gopkg.in/yaml.v2"
)

func CreateSkaffoldProfile(out io.Writer, config *latest.SkaffoldConfig, configFile string) error {
reader := bufio.NewReader(os.Stdin)

color.Default.Fprintln(out, "Checking for oncluster skaffold profile...")
profileExists := false
for _, profile := range config.Profiles {
if profile.Name == "oncluster" {
profileExists = true
break
}
}

// Check for existing oncluster profile, if none exists then prompt to create one
if profileExists {
color.Default.Fprintln(out, "profile \"oncluster\" found!")
return nil
}

confirmLoop:
for {
color.Default.Fprintf(out, "No profile \"oncluster\" found. Create one? [y/n]: ")
response, err := reader.ReadString('\n')
if err != nil {
return errors.Wrap(err, "reading user confirmation")
}

response = strings.ToLower(strings.TrimSpace(response))
switch response {
case "y", "yes":
break confirmLoop
case "n", "no":
return nil
}
}

color.Default.Fprintln(out, "Creating skaffold profile \"oncluster\"...")
profile, err := generateProfile(out, config)
if err != nil {
return errors.Wrap(err, "generating profile \"oncluster\"")
}

bProfile, err := yamlv2.Marshal([]*latest.Profile{profile})
if err != nil {
return errors.Wrap(err, "marshaling new profile")
}

fileContents, err := ioutil.ReadFile(configFile)
if err != nil {
return errors.Wrap(err, "reading file contents")
}
fileStrings := strings.Split(strings.TrimSpace(string(fileContents)), "\n")

var profilePos int
if len(config.Profiles) == 0 {
// Create new profiles section
fileStrings = append(fileStrings, "profiles:")
profilePos = len(fileStrings)
} else {
for i, line := range fileStrings {
if line == "profiles:" {
profilePos = i + 1
}
}
}

fileStrings = append(fileStrings, "")
copy(fileStrings[profilePos+1:], fileStrings[profilePos:])
fileStrings[profilePos] = strings.TrimSpace(string(bProfile))

fileContents = []byte((strings.Join(fileStrings, "\n")))

if err := ioutil.WriteFile(configFile, fileContents, 0644); err != nil {
return errors.Wrap(err, "writing profile to skaffold config")
}

return nil
}

func generateProfile(out io.Writer, config *latest.SkaffoldConfig) (*latest.Profile, error) {
if len(config.Build.Artifacts) == 0 {
return nil, errors.New("No Artifacts to add to profile")
}

profile := &latest.Profile{
Name: "oncluster",
Pipeline: latest.Pipeline{
Build: config.Pipeline.Build,
Deploy: latest.DeployConfig{},
},
}
profile.Build.Cluster = &latest.ClusterDetails{
PullSecretName: "kaniko-secret",
}
profile.Build.LocalBuild = nil
// Add kaniko build config for artifacts
for _, artifact := range profile.Build.Artifacts {
artifact.ImageName = fmt.Sprintf("%s-pipeline", artifact.ImageName)
if artifact.DockerArtifact != nil {
color.Default.Fprintf(out, "Cannot use Docker to build %s on cluster. Adding config for building with Kaniko.\n", artifact.ImageName)
artifact.DockerArtifact = nil
artifact.KanikoArtifact = &latest.KanikoArtifact{
BuildContext: &latest.KanikoBuildContext{
GCSBucket: "skaffold-kaniko",
},
}
}
}

return profile, nil
}
Loading