Skip to content

Commit

Permalink
Merge pull request #2722 from marlon-gamez/ns-flag
Browse files Browse the repository at this point in the history
Allow users to specify --namespace flag for tasks generated by generate-pipeline
  • Loading branch information
tejal29 authored Sep 3, 2019
2 parents e2ecf51 + e293720 commit 81c50fe
Show file tree
Hide file tree
Showing 6 changed files with 260 additions and 22 deletions.
7 changes: 4 additions & 3 deletions pkg/skaffold/generate_pipeline/generate_pipeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
tekton "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1alpha1"

"github.com/GoogleContainerTools/skaffold/pkg/skaffold/pipeline"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/runner/runcontext"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/latest"
)

Expand All @@ -41,7 +42,7 @@ type ConfigFile struct {
Profile *latest.Profile
}

func Yaml(out io.Writer, configFiles []*ConfigFile) (*bytes.Buffer, error) {
func Yaml(out io.Writer, runCtx *runcontext.RunContext, configFiles []*ConfigFile) (*bytes.Buffer, error) {
// Generate git resource for pipeline
gitResource, err := generateGitResource()
if err != nil {
Expand All @@ -50,14 +51,14 @@ func Yaml(out io.Writer, configFiles []*ConfigFile) (*bytes.Buffer, error) {

// Generate build task for pipeline
var tasks []*tekton.Task
buildTasks, err := generateBuildTasks(configFiles)
buildTasks, err := generateBuildTasks(runCtx.Opts.Namespace, configFiles)
if err != nil {
return nil, errors.Wrap(err, "generating build task")
}
tasks = append(tasks, buildTasks...)

// Generate deploy task for pipeline
deployTasks, err := generateDeployTasks(configFiles)
deployTasks, err := generateDeployTasks(runCtx.Opts.Namespace, configFiles)
if err != nil {
return nil, errors.Wrap(err, "generating deploy task")
}
Expand Down
13 changes: 10 additions & 3 deletions pkg/skaffold/generate_pipeline/profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,13 @@ import (
"github.com/pkg/errors"

"github.com/GoogleContainerTools/skaffold/pkg/skaffold/color"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/runner/runcontext"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/latest"

yamlv2 "gopkg.in/yaml.v2"
)

func CreateSkaffoldProfile(out io.Writer, configFile *ConfigFile) error {
func CreateSkaffoldProfile(out io.Writer, runCtx *runcontext.RunContext, configFile *ConfigFile) error {
reader := bufio.NewReader(os.Stdin)

// Check for existing oncluster profile, if none exists then prompt to create one
Expand Down Expand Up @@ -63,7 +64,7 @@ confirmLoop:
}

color.Default.Fprintln(out, "Creating skaffold profile \"oncluster\"...")
profile, err := generateProfile(out, configFile.Config)
profile, err := generateProfile(out, runCtx.Opts.Namespace, configFile.Config)
if err != nil {
return errors.Wrap(err, "generating profile \"oncluster\"")
}
Expand Down Expand Up @@ -106,7 +107,7 @@ confirmLoop:
return nil
}

func generateProfile(out io.Writer, config *latest.SkaffoldConfig) (*latest.Profile, error) {
func generateProfile(out io.Writer, namespace string, config *latest.SkaffoldConfig) (*latest.Profile, error) {
if len(config.Build.Artifacts) == 0 {
return nil, errors.New("No Artifacts to add to profile")
}
Expand Down Expand Up @@ -141,6 +142,12 @@ func generateProfile(out io.Writer, config *latest.SkaffoldConfig) (*latest.Prof
}
profile.Build.LocalBuild = nil
}
if namespace != "" {
if profile.Build.Cluster == nil {
profile.Build.Cluster = &latest.ClusterDetails{}
}
profile.Build.Cluster.Namespace = namespace
}

return profile, nil
}
49 changes: 48 additions & 1 deletion pkg/skaffold/generate_pipeline/profile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ func TestGenerateProfile(t *testing.T) {
skaffoldConfig *latest.SkaffoldConfig
expectedProfile *latest.Profile
responses []string
namespace string
shouldErr bool
}{
{
Expand All @@ -48,6 +49,7 @@ func TestGenerateProfile(t *testing.T) {
},
},
},
namespace: "",
expectedProfile: &latest.Profile{
Name: "oncluster",
Pipeline: latest.Pipeline{
Expand Down Expand Up @@ -94,6 +96,7 @@ func TestGenerateProfile(t *testing.T) {
},
},
},
namespace: "",
expectedProfile: &latest.Profile{
Name: "oncluster",
Pipeline: latest.Pipeline{
Expand All @@ -114,6 +117,50 @@ func TestGenerateProfile(t *testing.T) {
},
shouldErr: false,
},
{
description: "kaniko artifact with namespace",
skaffoldConfig: &latest.SkaffoldConfig{
Pipeline: latest.Pipeline{
Build: latest.BuildConfig{
Artifacts: []*latest.Artifact{
{
ImageName: "test",
ArtifactType: latest.ArtifactType{
DockerArtifact: &latest.DockerArtifact{},
},
},
},
},
},
},
namespace: "test-ns",
expectedProfile: &latest.Profile{
Name: "oncluster",
Pipeline: latest.Pipeline{
Build: latest.BuildConfig{
Artifacts: []*latest.Artifact{
{
ImageName: "test-pipeline",
ArtifactType: latest.ArtifactType{
KanikoArtifact: &latest.KanikoArtifact{
BuildContext: &latest.KanikoBuildContext{
GCSBucket: "skaffold-kaniko",
},
},
},
},
},
BuildType: latest.BuildType{
Cluster: &latest.ClusterDetails{
PullSecretName: "kaniko-secret",
Namespace: "test-ns",
},
},
},
},
},
shouldErr: false,
},
{
description: "failed profile generation",
skaffoldConfig: &latest.SkaffoldConfig{
Expand All @@ -130,7 +177,7 @@ func TestGenerateProfile(t *testing.T) {

for _, test := range tests {
testutil.Run(t, test.description, func(t *testutil.T) {
profile, err := generateProfile(ioutil.Discard, test.skaffoldConfig)
profile, err := generateProfile(ioutil.Discard, test.namespace, test.skaffoldConfig)
t.CheckErrorAndDeepEqual(test.shouldErr, err, test.expectedProfile, profile)
})
}
Expand Down
14 changes: 12 additions & 2 deletions pkg/skaffold/generate_pipeline/tasks.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,19 @@ import (
corev1 "k8s.io/api/core/v1"
)

func generateBuildTasks(configFiles []*ConfigFile) ([]*tekton.Task, error) {
func generateBuildTasks(namespace string, configFiles []*ConfigFile) ([]*tekton.Task, error) {
var tasks []*tekton.Task
for _, configFile := range configFiles {
task, err := generateBuildTask(configFile)
if err != nil {
return nil, err
}

if namespace != "" {
nsFlag := []string{"--namespace", namespace}
task.Spec.Steps[0].Args = append(task.Spec.Steps[0].Args, nsFlag...)
}

tasks = append(tasks, task)
}

Expand Down Expand Up @@ -108,14 +113,19 @@ func generateBuildTask(configFile *ConfigFile) (*tekton.Task, error) {
return pipeline.NewTask("skaffold-build", inputs, outputs, steps, volumes), nil
}

func generateDeployTasks(configFiles []*ConfigFile) ([]*tekton.Task, error) {
func generateDeployTasks(namespace string, configFiles []*ConfigFile) ([]*tekton.Task, error) {
var tasks []*tekton.Task
for _, configFile := range configFiles {
task, err := generateDeployTask(configFile)
if err != nil {
return nil, err
}

if namespace != "" {
nsFlag := []string{"--namespace", namespace}
task.Spec.Steps[0].Args = append(task.Spec.Steps[0].Args, nsFlag...)
}

tasks = append(tasks, task)
}

Expand Down
Loading

0 comments on commit 81c50fe

Please sign in to comment.