Skip to content

Commit 00a4cce

Browse files
grokspawnoceanc80everettraven
authored
refactor to move more processing to the API (#1082)
* refactor to move more processing to the template Signed-off-by: Jordan Keister <[email protected]> * better diagnostics on custom exec error Signed-off-by: Jordan Keister <[email protected]> * Remove obsolete fields Signed-off-by: Catherine Chan-Tse <[email protected]> Signed-off-by: Jordan Keister <[email protected]> * caching wip Signed-off-by: Jordan Keister <[email protected]> * refactor a bit and update unit tests (#2) * refactor a bit and update unit tests Signed-off-by: Bryce Palmer <[email protected]> * add more utests and minor tweaks Signed-off-by: Bryce Palmer <[email protected]> --------- Signed-off-by: Bryce Palmer <[email protected]> Signed-off-by: Jordan Keister <[email protected]> * whitespace sanity hell Signed-off-by: Jordan Keister <[email protected]> * drop STDERR for nominal case Signed-off-by: Jordan Keister <[email protected]> --------- Signed-off-by: Jordan Keister <[email protected]> Signed-off-by: Catherine Chan-Tse <[email protected]> Signed-off-by: Bryce Palmer <[email protected]> Co-authored-by: Catherine Chan-Tse <[email protected]> Co-authored-by: Bryce Palmer <[email protected]>
1 parent 45f491b commit 00a4cce

File tree

5 files changed

+805
-489
lines changed

5 files changed

+805
-489
lines changed

alpha/template/composite/builder.go

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,9 @@ const (
2727
CustomBuilderSchema = "olm.builder.custom"
2828
)
2929

30-
type ContainerConfig struct {
31-
ContainerTool string
32-
BaseImage string
33-
WorkingDir string
34-
}
35-
3630
type BuilderConfig struct {
37-
ContainerCfg ContainerConfig
38-
OutputType string
39-
InputDirectory string
31+
WorkingDir string
32+
OutputType string
4033
}
4134

4235
type Builder interface {
@@ -96,13 +89,13 @@ func (bb *BasicBuilder) Build(ctx context.Context, reg image.Registry, dir strin
9689
return fmt.Errorf("error rendering basic template: %v", err)
9790
}
9891

99-
destPath := path.Join(bb.builderCfg.ContainerCfg.WorkingDir, dir, basicConfig.Output)
92+
destPath := path.Join(bb.builderCfg.WorkingDir, dir, basicConfig.Output)
10093

10194
return build(dcfg, destPath, bb.builderCfg.OutputType)
10295
}
10396

10497
func (bb *BasicBuilder) Validate(dir string) error {
105-
return validate(bb.builderCfg.ContainerCfg, dir)
98+
return validate(bb.builderCfg, dir)
10699
}
107100

108101
type SemverBuilder struct {
@@ -158,13 +151,13 @@ func (sb *SemverBuilder) Build(ctx context.Context, reg image.Registry, dir stri
158151
return fmt.Errorf("error rendering semver template: %v", err)
159152
}
160153

161-
destPath := path.Join(sb.builderCfg.ContainerCfg.WorkingDir, dir, semverConfig.Output)
154+
destPath := path.Join(sb.builderCfg.WorkingDir, dir, semverConfig.Output)
162155

163156
return build(dcfg, destPath, sb.builderCfg.OutputType)
164157
}
165158

166159
func (sb *SemverBuilder) Validate(dir string) error {
167-
return validate(sb.builderCfg.ContainerCfg, dir)
160+
return validate(sb.builderCfg, dir)
168161
}
169162

170163
type RawBuilder struct {
@@ -218,13 +211,13 @@ func (rb *RawBuilder) Build(ctx context.Context, _ image.Registry, dir string, t
218211
return fmt.Errorf("error parsing raw input file: %s, %v", rawConfig.Input, err)
219212
}
220213

221-
destPath := path.Join(rb.builderCfg.ContainerCfg.WorkingDir, dir, rawConfig.Output)
214+
destPath := path.Join(rb.builderCfg.WorkingDir, dir, rawConfig.Output)
222215

223216
return build(dcfg, destPath, rb.builderCfg.OutputType)
224217
}
225218

226219
func (rb *RawBuilder) Validate(dir string) error {
227-
return validate(rb.builderCfg.ContainerCfg, dir)
220+
return validate(rb.builderCfg, dir)
228221
}
229222

230223
type CustomBuilder struct {
@@ -268,13 +261,12 @@ func (cb *CustomBuilder) Build(ctx context.Context, reg image.Registry, dir stri
268261
}
269262
// build the command to execute
270263
cmd := exec.Command(customConfig.Command, customConfig.Args...)
271-
cmd.Dir = cb.builderCfg.ContainerCfg.WorkingDir
272264

273265
// custom template should output a valid FBC to STDOUT so we can
274266
// build the FBC just like all the other templates.
275267
v, err := cmd.Output()
276268
if err != nil {
277-
return fmt.Errorf("running command %q: %v", cmd.String(), err)
269+
return fmt.Errorf("running command %q: %v: %v", cmd.String(), err, v)
278270
}
279271

280272
reader := bytes.NewReader(v)
@@ -286,15 +278,15 @@ func (cb *CustomBuilder) Build(ctx context.Context, reg image.Registry, dir stri
286278
return fmt.Errorf("error parsing custom command output: %s, %v", strings.Join(cmdString, "'"), err)
287279
}
288280

289-
destPath := path.Join(cb.builderCfg.ContainerCfg.WorkingDir, dir, customConfig.Output)
281+
destPath := path.Join(cb.builderCfg.WorkingDir, dir, customConfig.Output)
290282

291283
// custom template should output a valid FBC to STDOUT so we can
292284
// build the FBC just like all the other templates.
293285
return build(dcfg, destPath, cb.builderCfg.OutputType)
294286
}
295287

296288
func (cb *CustomBuilder) Validate(dir string) error {
297-
return validate(cb.builderCfg.ContainerCfg, dir)
289+
return validate(cb.builderCfg, dir)
298290
}
299291

300292
func writeDeclCfg(dcfg declcfg.DeclarativeConfig, w io.Writer, output string) error {
@@ -308,12 +300,12 @@ func writeDeclCfg(dcfg declcfg.DeclarativeConfig, w io.Writer, output string) er
308300
}
309301
}
310302

311-
func validate(containerCfg ContainerConfig, dir string) error {
303+
func validate(builderCfg BuilderConfig, dir string) error {
312304

313-
path := path.Join(containerCfg.WorkingDir, dir)
305+
path := path.Join(builderCfg.WorkingDir, dir)
314306
s, err := os.Stat(path)
315307
if err != nil {
316-
return fmt.Errorf("directory not found. validation path needs to be composed of ContainerConfig.WorkingDir+Component[].Destination.Path: %q: %v", path, err)
308+
return fmt.Errorf("directory not found. validation path needs to be composed of BuilderConfig.WorkingDir+Component[].Destination.Path: %q: %v", path, err)
317309
}
318310
if !s.IsDir() {
319311
return fmt.Errorf("%q is not a directory", path)

0 commit comments

Comments
 (0)