Skip to content

Commit

Permalink
refactor to move more processing to the API (#1082)
Browse files Browse the repository at this point in the history
* 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 (openshift#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]>
Upstream-repository: operator-registry
Upstream-commit: 00a4cce4847d75f71bbcb574e0e7ac364fd9f546
  • Loading branch information
3 people authored and tmshort committed Jul 11, 2023
1 parent 917ca29 commit 4f191e9
Show file tree
Hide file tree
Showing 9 changed files with 1,062 additions and 663 deletions.
4 changes: 2 additions & 2 deletions pkg/manifests/csv.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ metadata:
name: packageserver
namespace: openshift-operator-lifecycle-manager
labels:
olm.version: 0.0.0-6d4414878dd94df7601785c550cffb3c8deb7c0a
olm.version: 0.0.0-539b8a8334f53c607548e71ae40d3d2b95617962
olm.clusteroperator.name: operator-lifecycle-manager-packageserver
annotations:
include.release.openshift.io/self-managed-high-availability: "true"
Expand Down Expand Up @@ -159,7 +159,7 @@ spec:
- packageserver
topologyKey: "kubernetes.io/hostname"
maturity: alpha
version: 0.0.0-6d4414878dd94df7601785c550cffb3c8deb7c0a
version: 0.0.0-539b8a8334f53c607548e71ae40d3d2b95617962
apiservicedefinitions:
owned:
- group: packages.operators.coreos.com
Expand Down
36 changes: 14 additions & 22 deletions staging/operator-registry/alpha/template/composite/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,9 @@ const (
CustomBuilderSchema = "olm.builder.custom"
)

type ContainerConfig struct {
ContainerTool string
BaseImage string
WorkingDir string
}

type BuilderConfig struct {
ContainerCfg ContainerConfig
OutputType string
InputDirectory string
WorkingDir string
OutputType string
}

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

destPath := path.Join(bb.builderCfg.ContainerCfg.WorkingDir, dir, basicConfig.Output)
destPath := path.Join(bb.builderCfg.WorkingDir, dir, basicConfig.Output)

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

func (bb *BasicBuilder) Validate(dir string) error {
return validate(bb.builderCfg.ContainerCfg, dir)
return validate(bb.builderCfg, dir)
}

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

destPath := path.Join(sb.builderCfg.ContainerCfg.WorkingDir, dir, semverConfig.Output)
destPath := path.Join(sb.builderCfg.WorkingDir, dir, semverConfig.Output)

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

func (sb *SemverBuilder) Validate(dir string) error {
return validate(sb.builderCfg.ContainerCfg, dir)
return validate(sb.builderCfg, dir)
}

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

destPath := path.Join(rb.builderCfg.ContainerCfg.WorkingDir, dir, rawConfig.Output)
destPath := path.Join(rb.builderCfg.WorkingDir, dir, rawConfig.Output)

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

func (rb *RawBuilder) Validate(dir string) error {
return validate(rb.builderCfg.ContainerCfg, dir)
return validate(rb.builderCfg, dir)
}

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

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

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

destPath := path.Join(cb.builderCfg.ContainerCfg.WorkingDir, dir, customConfig.Output)
destPath := path.Join(cb.builderCfg.WorkingDir, dir, customConfig.Output)

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

func (cb *CustomBuilder) Validate(dir string) error {
return validate(cb.builderCfg.ContainerCfg, dir)
return validate(cb.builderCfg, dir)
}

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

func validate(containerCfg ContainerConfig, dir string) error {
func validate(builderCfg BuilderConfig, dir string) error {

path := path.Join(containerCfg.WorkingDir, dir)
path := path.Join(builderCfg.WorkingDir, dir)
s, err := os.Stat(path)
if err != nil {
return fmt.Errorf("directory not found. validation path needs to be composed of ContainerConfig.WorkingDir+Component[].Destination.Path: %q: %v", path, err)
return fmt.Errorf("directory not found. validation path needs to be composed of BuilderConfig.WorkingDir+Component[].Destination.Path: %q: %v", path, err)
}
if !s.IsDir() {
return fmt.Errorf("%q is not a directory", path)
Expand Down
Loading

0 comments on commit 4f191e9

Please sign in to comment.