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

Apply default values to profiles #533

Merged
merged 1 commit into from
May 14, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
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
81 changes: 65 additions & 16 deletions pkg/skaffold/config/profile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ import (

func TestApplyProfiles(t *testing.T) {
tests := []struct {
description string
config SkaffoldConfig
profile string
expectedConfig SkaffoldConfig
shouldErr bool
description string
config SkaffoldConfig
profile string
expected SkaffoldConfig
shouldErr bool
}{
{
description: "unknown profile",
Expand Down Expand Up @@ -61,14 +61,25 @@ func TestApplyProfiles(t *testing.T) {
},
},
},
expectedConfig: SkaffoldConfig{
expected: SkaffoldConfig{
Build: v1alpha2.BuildConfig{
Artifacts: []*v1alpha2.Artifact{
{ImageName: "image"},
{
ImageName: "image",
Workspace: ".",
ArtifactType: v1alpha2.ArtifactType{
DockerArtifact: &v1alpha2.DockerArtifact{
DockerfilePath: "Dockerfile",
},
},
},
},
BuildType: v1alpha2.BuildType{
GoogleCloudBuild: &v1alpha2.GoogleCloudBuild{},
},
TagPolicy: v1alpha2.TagPolicy{
GitTagger: &v1alpha2.GitTagger{},
},
},
Deploy: v1alpha2.DeployConfig{},
},
Expand All @@ -93,12 +104,23 @@ func TestApplyProfiles(t *testing.T) {
},
},
},
expectedConfig: SkaffoldConfig{
expected: SkaffoldConfig{
Build: v1alpha2.BuildConfig{
Artifacts: []*v1alpha2.Artifact{
{ImageName: "image"},
{
ImageName: "image",
Workspace: ".",
ArtifactType: v1alpha2.ArtifactType{
DockerArtifact: &v1alpha2.DockerArtifact{
DockerfilePath: "Dockerfile",
},
},
},
},
TagPolicy: v1alpha2.TagPolicy{ShaTagger: &v1alpha2.ShaTagger{}},
BuildType: v1alpha2.BuildType{
LocalBuild: &v1alpha2.LocalBuild{},
},
},
Deploy: v1alpha2.DeployConfig{},
},
Expand Down Expand Up @@ -126,13 +148,32 @@ func TestApplyProfiles(t *testing.T) {
},
},
},
expectedConfig: SkaffoldConfig{
expected: SkaffoldConfig{
Build: v1alpha2.BuildConfig{
Artifacts: []*v1alpha2.Artifact{
{ImageName: "image"},
{ImageName: "imageProd"},
{
ImageName: "image",
Workspace: ".",
ArtifactType: v1alpha2.ArtifactType{
DockerArtifact: &v1alpha2.DockerArtifact{
DockerfilePath: "Dockerfile",
},
},
},
{
ImageName: "imageProd",
Workspace: ".",
ArtifactType: v1alpha2.ArtifactType{
DockerArtifact: &v1alpha2.DockerArtifact{
DockerfilePath: "Dockerfile",
},
},
},
},
TagPolicy: v1alpha2.TagPolicy{GitTagger: &v1alpha2.GitTagger{}},
BuildType: v1alpha2.BuildType{
LocalBuild: &v1alpha2.LocalBuild{},
},
},
Deploy: v1alpha2.DeployConfig{},
},
Expand All @@ -158,21 +199,29 @@ func TestApplyProfiles(t *testing.T) {
},
},
},
expectedConfig: SkaffoldConfig{
Build: v1alpha2.BuildConfig{},
expected: SkaffoldConfig{
Build: v1alpha2.BuildConfig{
TagPolicy: v1alpha2.TagPolicy{
GitTagger: &v1alpha2.GitTagger{},
},
BuildType: v1alpha2.BuildType{
LocalBuild: &v1alpha2.LocalBuild{},
},
},
Deploy: v1alpha2.DeployConfig{
DeployType: v1alpha2.DeployType{
HelmDeploy: &v1alpha2.HelmDeploy{},
},
},
},
}}
},
}

for _, test := range tests {
t.Run(test.description, func(t *testing.T) {
err := test.config.ApplyProfiles([]string{test.profile})

testutil.CheckErrorAndDeepEqual(t, test.shouldErr, err, test.expectedConfig, test.config)
testutil.CheckErrorAndDeepEqual(t, test.shouldErr, err, test.expected, test.config)
})
}
}
72 changes: 40 additions & 32 deletions pkg/skaffold/schema/v1alpha2/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ type SkaffoldConfig struct {
Profiles []Profile `yaml:"profiles,omitempty"`
}

func (config *SkaffoldConfig) GetVersion() string {
return config.APIVersion
func (c *SkaffoldConfig) GetVersion() string {
return c.APIVersion
}

// BuildConfig contains all the configuration for the build steps
Expand Down Expand Up @@ -156,17 +156,38 @@ type BazelArtifact struct {
BuildTarget string `yaml:"target"`
}

func defaultToLocalBuild(config *SkaffoldConfig) {
if config.Build.BuildType != (BuildType{}) {
// Parse reads a SkaffoldConfig from yaml.
func (c *SkaffoldConfig) Parse(contents []byte, useDefaults bool) error {
if err := yaml.Unmarshal(contents, c); err != nil {
return err
}

if useDefaults {
c.setDefaultValues()
}

return nil
}

func (c *SkaffoldConfig) setDefaultValues() {
c.defaultToLocalBuild()
c.defaultToDockerArtifacts()
c.setDefaultTagger()
c.setDefaultDockerfiles()
c.setDefaultWorkspaces()
}

func (c *SkaffoldConfig) defaultToLocalBuild() {
if c.Build.BuildType != (BuildType{}) {
return
}

logrus.Debugf("Defaulting build type to local build")
config.Build.BuildType.LocalBuild = &LocalBuild{}
c.Build.BuildType.LocalBuild = &LocalBuild{}
}

func defaultToDockerArtifacts(config *SkaffoldConfig) {
for _, artifact := range config.Build.Artifacts {
func (c *SkaffoldConfig) defaultToDockerArtifacts() {
for _, artifact := range c.Build.Artifacts {
if artifact.ArtifactType != (ArtifactType{}) {
continue
}
Expand All @@ -177,24 +198,24 @@ func defaultToDockerArtifacts(config *SkaffoldConfig) {
}
}

func setDefaultTagger(cfg *SkaffoldConfig) {
if cfg.Build.TagPolicy != (TagPolicy{}) {
func (c *SkaffoldConfig) setDefaultTagger() {
if c.Build.TagPolicy != (TagPolicy{}) {
return
}

cfg.Build.TagPolicy = TagPolicy{GitTagger: &GitTagger{}}
c.Build.TagPolicy = TagPolicy{GitTagger: &GitTagger{}}
}

func setDefaultDockerfiles(config *SkaffoldConfig) {
for _, artifact := range config.Build.Artifacts {
func (c *SkaffoldConfig) setDefaultDockerfiles() {
for _, artifact := range c.Build.Artifacts {
if artifact.DockerArtifact != nil && artifact.DockerArtifact.DockerfilePath == "" {
artifact.DockerArtifact.DockerfilePath = constants.DefaultDockerfilePath
}
}
}

func setDefaultWorkspaces(config *SkaffoldConfig) {
for _, artifact := range config.Build.Artifacts {
func (c *SkaffoldConfig) setDefaultWorkspaces() {
for _, artifact := range c.Build.Artifacts {
if artifact.Workspace == "" {
artifact.Workspace = "."
}
Expand All @@ -203,23 +224,24 @@ func setDefaultWorkspaces(config *SkaffoldConfig) {

// ApplyProfiles returns configuration modified by the application
// of a list of profiles.
func (config *SkaffoldConfig) ApplyProfiles(profiles []string) error {
func (c *SkaffoldConfig) ApplyProfiles(profiles []string) error {
var err error

byName := profilesByName(config.Profiles)
byName := profilesByName(c.Profiles)
for _, name := range profiles {
profile, present := byName[name]
if !present {
return fmt.Errorf("couldn't find profile %s", name)
}

err = applyProfile(config, profile)
err = applyProfile(c, profile)
if err != nil {
return errors.Wrapf(err, "applying profile %s", name)
}
}

config.Profiles = nil
c.setDefaultValues()
c.Profiles = nil

return nil
}
Expand All @@ -242,17 +264,3 @@ func profilesByName(profiles []Profile) map[string]Profile {
}
return byName
}

func (config *SkaffoldConfig) Parse(contents []byte, useDefaults bool) error {
if err := yaml.Unmarshal(contents, config); err != nil {
return err
}
if useDefaults {
defaultToLocalBuild(config)
defaultToDockerArtifacts(config)
setDefaultTagger(config)
setDefaultDockerfiles(config)
setDefaultWorkspaces(config)
}
return nil
}