Skip to content

Commit 25f77f1

Browse files
authored
Simplify skaffold init code (#3406)
* Remove duplication Signed-off-by: David Gageot <[email protected]> * Remove some more duplication Signed-off-by: David Gageot <[email protected]> * Simplify code Signed-off-by: David Gageot <[email protected]> * Remove duplication Signed-off-by: David Gageot <[email protected]> * Fix #3405 Signed-off-by: David Gageot <[email protected]> * Simplify code Signed-off-by: David Gageot <[email protected]> * Use similar code for every artifact type Signed-off-by: David Gageot <[email protected]> * Fix typo Signed-off-by: David Gageot <[email protected]> * Make sure we generate the same yaml as we used to Signed-off-by: David Gageot <[email protected]>
1 parent dc7ea85 commit 25f77f1

File tree

8 files changed

+263
-273
lines changed

8 files changed

+263
-273
lines changed

pkg/skaffold/build/buildpacks/init.go

Lines changed: 16 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -25,58 +25,48 @@ import (
2525

2626
// For testing
2727
var (
28-
ValidateConfig = validateConfig
28+
Validate = validate
2929
)
3030

3131
// Name is the name of the Buildpack builder
3232
var Name = "Buildpacks"
3333

34-
// Buildpack holds information about a Buildpack project
35-
type Buildpacks struct {
34+
// ArtifactConfig holds information about a Buildpack project
35+
type ArtifactConfig struct {
3636
File string `json:"path,omitempty"`
3737
}
3838

3939
// Name returns the name of the builder
40-
func (b Buildpacks) Name() string {
40+
func (c ArtifactConfig) Name() string {
4141
return Name
4242
}
4343

4444
// Describe returns the initBuilder's string representation, used when prompting the user to choose a builder.
45-
func (b Buildpacks) Describe() string {
46-
return fmt.Sprintf("%s (%s)", b.Name(), b.File)
45+
func (c ArtifactConfig) Describe() string {
46+
return fmt.Sprintf("%s (%s)", c.Name(), c.File)
4747
}
4848

4949
// CreateArtifact creates an Artifact to be included in the generated Build Config
50-
func (b Buildpacks) CreateArtifact(manifestImage string) *latest.Artifact {
51-
a := &latest.Artifact{
52-
ImageName: manifestImage,
53-
ArtifactType: latest.ArtifactType{
54-
BuildpackArtifact: &latest.BuildpackArtifact{
55-
Builder: "heroku/buildpacks",
56-
},
50+
func (c ArtifactConfig) UpdateArtifact(a *latest.Artifact) {
51+
a.ArtifactType = latest.ArtifactType{
52+
BuildpackArtifact: &latest.BuildpackArtifact{
53+
Builder: "heroku/buildpacks",
5754
},
5855
}
59-
60-
workspace := filepath.Dir(b.File)
61-
if workspace != "." {
62-
a.Workspace = workspace
63-
}
64-
65-
return a
6656
}
6757

6858
// ConfiguredImage returns the target image configured by the builder, or empty string if no image is configured
69-
func (b Buildpacks) ConfiguredImage() string {
70-
// Target image is not configured in dockerfiles
59+
func (c ArtifactConfig) ConfiguredImage() string {
60+
// Target image is not configured in buildpacks
7161
return ""
7262
}
7363

7464
// Path returns the path to the build definition
75-
func (b Buildpacks) Path() string {
76-
return b.File
65+
func (c ArtifactConfig) Path() string {
66+
return c.File
7767
}
7868

79-
// validateConfig checks if a file is a valid Buildpack configuration.
80-
func validateConfig(path string) bool {
69+
// validate checks if a file is a valid Buildpack configuration.
70+
func validate(path string) bool {
8171
return filepath.Base(path) == "package.json"
8272
}

pkg/skaffold/build/buildpacks/init_test.go

Lines changed: 11 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import (
2424
"github.com/GoogleContainerTools/skaffold/testutil"
2525
)
2626

27-
func TestValidateConfig(t *testing.T) {
27+
func TestValidate(t *testing.T) {
2828
var tests = []struct {
2929
description string
3030
path string
@@ -45,7 +45,7 @@ func TestValidateConfig(t *testing.T) {
4545
testutil.Run(t, test.description, func(t *testutil.T) {
4646
tmpDir := t.NewTempDir().Touch(test.path)
4747

48-
isValid := ValidateConfig(tmpDir.Path(test.path))
48+
isValid := Validate(tmpDir.Path(test.path))
4949

5050
t.CheckDeepEqual(test.expectedValid, isValid)
5151
})
@@ -55,12 +55,12 @@ func TestValidateConfig(t *testing.T) {
5555
func TestDescribe(t *testing.T) {
5656
var tests = []struct {
5757
description string
58-
config Buildpacks
58+
config ArtifactConfig
5959
expectedPrompt string
6060
}{
6161
{
6262
description: "buildpacks",
63-
config: Buildpacks{File: "/path/to/package.json"},
63+
config: ArtifactConfig{File: "/path/to/package.json"},
6464
expectedPrompt: "Buildpacks (/path/to/package.json)",
6565
},
6666
}
@@ -71,32 +71,16 @@ func TestDescribe(t *testing.T) {
7171
}
7272
}
7373

74-
func TestCreateArtifact(t *testing.T) {
74+
func TestUpdateArtifact(t *testing.T) {
7575
var tests = []struct {
7676
description string
77-
config Buildpacks
78-
manifestImage string
77+
config ArtifactConfig
7978
expectedArtifact latest.Artifact
80-
expectedImage string
8179
}{
8280
{
83-
description: "buildpacks",
84-
config: Buildpacks{File: filepath.Join("path", "to", "package.json")},
85-
manifestImage: "image",
81+
description: "buildpacks",
82+
config: ArtifactConfig{File: filepath.Join("path", "to", "package.json")},
8683
expectedArtifact: latest.Artifact{
87-
ImageName: "image",
88-
Workspace: filepath.Join("path", "to"),
89-
ArtifactType: latest.ArtifactType{BuildpackArtifact: &latest.BuildpackArtifact{
90-
Builder: "heroku/buildpacks",
91-
}},
92-
},
93-
},
94-
{
95-
description: "ignore workspace",
96-
config: Buildpacks{File: "build.gradle"},
97-
manifestImage: "other-image",
98-
expectedArtifact: latest.Artifact{
99-
ImageName: "other-image",
10084
ArtifactType: latest.ArtifactType{BuildpackArtifact: &latest.BuildpackArtifact{
10185
Builder: "heroku/buildpacks",
10286
}},
@@ -105,7 +89,9 @@ func TestCreateArtifact(t *testing.T) {
10589
}
10690
for _, test := range tests {
10791
testutil.Run(t, test.description, func(t *testutil.T) {
108-
artifact := test.config.CreateArtifact(test.manifestImage)
92+
artifact := &latest.Artifact{}
93+
94+
test.config.UpdateArtifact(artifact)
10995

11096
t.CheckDeepEqual(test.expectedArtifact, *artifact)
11197
})

pkg/skaffold/build/jib/init.go

Lines changed: 29 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -34,57 +34,47 @@ import (
3434

3535
// For testing
3636
var (
37-
ValidateJibConfigFunc = ValidateJibConfig
37+
Validate = validate
3838
)
3939

40-
// Jib holds information about a Jib project
41-
type Jib struct {
40+
// ArtifactConfig holds information about a Jib project
41+
type ArtifactConfig struct {
4242
BuilderName string `json:"-"`
4343
Image string `json:"image,omitempty"`
44-
FilePath string `json:"path,omitempty"`
44+
File string `json:"path,omitempty"`
4545
Project string `json:"project,omitempty"`
4646
}
4747

4848
// Name returns the name of the builder
49-
func (j Jib) Name() string {
50-
return j.BuilderName
49+
func (c ArtifactConfig) Name() string {
50+
return c.BuilderName
5151
}
5252

5353
// Describe returns the initBuilder's string representation, used when prompting the user to choose a builder.
54-
func (j Jib) Describe() string {
55-
if j.Project != "" {
56-
return fmt.Sprintf("%s (%s, %s)", j.BuilderName, j.Project, j.FilePath)
54+
func (c ArtifactConfig) Describe() string {
55+
if c.Project != "" {
56+
return fmt.Sprintf("%s (%s, %s)", c.BuilderName, c.Project, c.File)
5757
}
58-
return fmt.Sprintf("%s (%s)", j.BuilderName, j.FilePath)
58+
return fmt.Sprintf("%s (%s)", c.BuilderName, c.File)
5959
}
6060

6161
// CreateArtifact creates an Artifact to be included in the generated Build Config
62-
func (j Jib) CreateArtifact(manifestImage string) *latest.Artifact {
63-
workspace := filepath.Dir(j.FilePath)
64-
65-
a := &latest.Artifact{ImageName: manifestImage}
66-
67-
if workspace != "." {
68-
a.Workspace = workspace
62+
func (c ArtifactConfig) UpdateArtifact(a *latest.Artifact) {
63+
a.ArtifactType = latest.ArtifactType{
64+
JibArtifact: &latest.JibArtifact{
65+
Project: c.Project,
66+
},
6967
}
70-
71-
jib := &latest.JibArtifact{}
72-
if j.Project != "" {
73-
jib.Project = j.Project
74-
}
75-
a.ArtifactType = latest.ArtifactType{JibArtifact: jib}
76-
77-
return a
7868
}
7969

8070
// ConfiguredImage returns the target image configured by the builder, or empty string if no image is configured
81-
func (j Jib) ConfiguredImage() string {
82-
return j.Image
71+
func (c ArtifactConfig) ConfiguredImage() string {
72+
return c.Image
8373
}
8474

8575
// Path returns the path to the build definition
86-
func (j Jib) Path() string {
87-
return j.FilePath
76+
func (c ArtifactConfig) Path() string {
77+
return c.File
8878
}
8979

9080
// BuilderConfig contains information about inferred Jib configurations
@@ -93,8 +83,8 @@ type jibJSON struct {
9383
Project string `json:"project"`
9484
}
9585

96-
// ValidateJibConfig checks if a file is a valid Jib configuration. Returns the list of Config objects corresponding to each Jib project built by the file, or nil if Jib is not configured.
97-
func ValidateJibConfig(path string) []Jib {
86+
// validate checks if a file is a valid Jib configuration. Returns the list of Config objects corresponding to each Jib project built by the file, or nil if Jib is not configured.
87+
func validate(path string) []ArtifactConfig {
9888
// Determine whether maven or gradle
9989
var builderType PluginType
10090
var executable, wrapper, taskName, searchString string
@@ -137,8 +127,8 @@ func ValidateJibConfig(path string) []Jib {
137127
return nil
138128
}
139129

140-
results := make([]Jib, len(matches))
141-
for i, match := range matches {
130+
var results []ArtifactConfig
131+
for _, match := range matches {
142132
// Escape windows path separators
143133
line := bytes.Replace(match[1], []byte(`\`), []byte(`\\`), -1)
144134
parsedJSON := jibJSON{}
@@ -147,7 +137,12 @@ func ValidateJibConfig(path string) []Jib {
147137
return nil
148138
}
149139

150-
results[i] = Jib{BuilderName: PluginName(builderType), Image: parsedJSON.Image, FilePath: path, Project: parsedJSON.Project}
140+
results = append(results, ArtifactConfig{
141+
BuilderName: PluginName(builderType),
142+
Image: parsedJSON.Image,
143+
File: path,
144+
Project: parsedJSON.Project,
145+
})
151146
}
152147
return results
153148
}

0 commit comments

Comments
 (0)