Skip to content

Commit eb4756f

Browse files
grokspawnci-robot
authored andcommitted
use contributions path as fallback search for template inputs (#1221)
Signed-off-by: Jordan Keister <[email protected]> Upstream-repository: operator-registry Upstream-commit: d924b765b93a279dc7af72c3908d88e936262593
1 parent be9c2e5 commit eb4756f

File tree

10 files changed

+178
-19
lines changed

10 files changed

+178
-19
lines changed

staging/operator-registry/alpha/template/composite/builder.go

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,9 @@ const (
2828
)
2929

3030
type BuilderConfig struct {
31-
WorkingDir string
32-
OutputType string
31+
WorkingDir string
32+
OutputType string
33+
ContributionPath string
3334
}
3435

3536
type Builder interface {
@@ -80,7 +81,14 @@ func (bb *BasicBuilder) Build(ctx context.Context, reg image.Registry, dir strin
8081
b := basictemplate.Template{Registry: reg}
8182
reader, err := os.Open(basicConfig.Input)
8283
if err != nil {
83-
return fmt.Errorf("error reading basic template: %v", err)
84+
if os.IsNotExist(err) && bb.builderCfg.ContributionPath != "" {
85+
reader, err = os.Open(path.Join(bb.builderCfg.ContributionPath, basicConfig.Input))
86+
if err != nil {
87+
return fmt.Errorf("error reading basic template: %v (tried contribution-local path: %q)", err, bb.builderCfg.ContributionPath)
88+
}
89+
} else {
90+
return fmt.Errorf("error reading basic template: %v", err)
91+
}
8492
}
8593
defer reader.Close()
8694

@@ -140,7 +148,14 @@ func (sb *SemverBuilder) Build(ctx context.Context, reg image.Registry, dir stri
140148

141149
reader, err := os.Open(semverConfig.Input)
142150
if err != nil {
143-
return fmt.Errorf("error reading semver template: %v", err)
151+
if os.IsNotExist(err) && sb.builderCfg.ContributionPath != "" {
152+
reader, err = os.Open(path.Join(sb.builderCfg.ContributionPath, semverConfig.Input))
153+
if err != nil {
154+
return fmt.Errorf("error reading semver template: %v (tried contribution-local path: %q)", err, sb.builderCfg.ContributionPath)
155+
}
156+
} else {
157+
return fmt.Errorf("error reading semver template: %v", err)
158+
}
144159
}
145160
defer reader.Close()
146161

@@ -202,7 +217,14 @@ func (rb *RawBuilder) Build(ctx context.Context, _ image.Registry, dir string, t
202217

203218
reader, err := os.Open(rawConfig.Input)
204219
if err != nil {
205-
return fmt.Errorf("error reading raw input file: %s, %v", rawConfig.Input, err)
220+
if os.IsNotExist(err) && rb.builderCfg.ContributionPath != "" {
221+
reader, err = os.Open(path.Join(rb.builderCfg.ContributionPath, rawConfig.Input))
222+
if err != nil {
223+
return fmt.Errorf("error reading raw input file: %v (tried contribution-local path: %q)", err, rb.builderCfg.ContributionPath)
224+
}
225+
} else {
226+
return fmt.Errorf("error reading raw input file: %v", err)
227+
}
206228
}
207229
defer reader.Close()
208230

staging/operator-registry/alpha/template/composite/builder_test.go

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"io"
77
"os"
88
"path"
9+
"path/filepath"
910
"testing"
1011

1112
"github.com/stretchr/testify/require"
@@ -210,6 +211,38 @@ func TestBasicBuilder(t *testing.T) {
210211
"basic template configuration is invalid: basic template config must have a non-empty input (templateDefinition.config.input),basic template config must have a non-empty output (templateDefinition.config.output)")
211212
},
212213
},
214+
{
215+
name: "successful basic build yaml output using contribution cwd to find inputs",
216+
validate: true,
217+
basicBuilder: NewBasicBuilder(BuilderConfig{
218+
WorkingDir: testDir,
219+
OutputType: "yaml",
220+
ContributionPath: filepath.Join(testDir, "components"),
221+
}),
222+
templateDefinition: TemplateDefinition{
223+
Schema: BasicBuilderSchema,
224+
Config: []byte(fmt.Sprintf(validConfigTemplate, "basic.yaml", "catalog.yaml")),
225+
},
226+
files: map[string]string{
227+
"components/basic.yaml": basicYaml,
228+
},
229+
buildAssertions: func(t *testing.T, dir string, buildErr error) {
230+
require.NoError(t, buildErr)
231+
// check if the catalog.yaml file exists in the correct place
232+
filePath := path.Join(dir, "catalog.yaml")
233+
_, err := os.Stat(filePath)
234+
require.NoError(t, err)
235+
file, err := os.Open(filePath)
236+
require.NoError(t, err)
237+
defer file.Close()
238+
fileData, err := io.ReadAll(file)
239+
require.NoError(t, err)
240+
require.Equal(t, string(fileData), basicBuiltFbcYaml)
241+
},
242+
validateAssertions: func(t *testing.T, validateErr error) {
243+
require.NoError(t, validateErr)
244+
},
245+
},
213246
}
214247

215248
for i, tc := range testCases {
@@ -673,6 +706,38 @@ func TestSemverBuilder(t *testing.T) {
673706
)
674707
},
675708
},
709+
{
710+
name: "successful semver build json output using contribution cwd to find inputs",
711+
validate: true,
712+
semverBuilder: NewSemverBuilder(BuilderConfig{
713+
WorkingDir: testDir,
714+
OutputType: "json",
715+
ContributionPath: filepath.Join(testDir, "components"),
716+
}),
717+
templateDefinition: TemplateDefinition{
718+
Schema: SemverBuilderSchema,
719+
Config: []byte(fmt.Sprintf(validConfigTemplate, "semver.yaml", "catalog.json")),
720+
},
721+
files: map[string]string{
722+
"components/semver.yaml": semverYaml,
723+
},
724+
buildAssertions: func(t *testing.T, dir string, buildErr error) {
725+
require.NoError(t, buildErr)
726+
// check if the catalog.yaml file exists in the correct place
727+
filePath := path.Join(dir, "catalog.json")
728+
_, err := os.Stat(filePath)
729+
require.NoError(t, err)
730+
file, err := os.Open(filePath)
731+
require.NoError(t, err)
732+
defer file.Close()
733+
fileData, err := io.ReadAll(file)
734+
require.NoError(t, err)
735+
require.Equal(t, semverBuiltFbcJson, string(fileData))
736+
},
737+
validateAssertions: func(t *testing.T, validateErr error) {
738+
require.NoError(t, validateErr)
739+
},
740+
},
676741
}
677742

678743
for i, tc := range testCases {
@@ -1144,6 +1209,38 @@ func TestRawBuilder(t *testing.T) {
11441209
"raw template configuration is invalid: raw template config must have a non-empty input (templateDefinition.config.input),raw template config must have a non-empty output (templateDefinition.config.output)")
11451210
},
11461211
},
1212+
{
1213+
name: "successful raw build json output using contribution cwd to find inputs",
1214+
validate: true,
1215+
rawBuilder: NewRawBuilder(BuilderConfig{
1216+
WorkingDir: testDir,
1217+
OutputType: "json",
1218+
ContributionPath: filepath.Join(testDir, "components"),
1219+
}),
1220+
templateDefinition: TemplateDefinition{
1221+
Schema: RawBuilderSchema,
1222+
Config: []byte(fmt.Sprintf(validConfigTemplate, "raw.yaml", "catalog.json")),
1223+
},
1224+
files: map[string]string{
1225+
"components/raw.yaml": rawYaml,
1226+
},
1227+
buildAssertions: func(t *testing.T, dir string, buildErr error) {
1228+
require.NoError(t, buildErr)
1229+
// check if the catalog.yaml file exists in the correct place
1230+
filePath := path.Join(dir, "catalog.json")
1231+
_, err := os.Stat(filePath)
1232+
require.NoError(t, err)
1233+
file, err := os.Open(filePath)
1234+
require.NoError(t, err)
1235+
defer file.Close()
1236+
fileData, err := io.ReadAll(file)
1237+
require.NoError(t, err)
1238+
require.Equal(t, string(fileData), rawBuiltFbcJson)
1239+
},
1240+
validateAssertions: func(t *testing.T, validateErr error) {
1241+
require.NoError(t, validateErr)
1242+
},
1243+
},
11471244
}
11481245

11491246
for i, tc := range testCases {

staging/operator-registry/alpha/template/composite/composite.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,10 @@ func WithCatalogFile(catalogFile io.Reader) TemplateOption {
1919
}
2020
}
2121

22-
func WithContributionFile(contribFile io.Reader) TemplateOption {
22+
func WithContributionFile(contribFile io.Reader, contribPath string) TemplateOption {
2323
return func(t *Template) {
2424
t.contributionFile = contribFile
25+
t.contributionPath = contribPath
2526
}
2627
}
2728

@@ -230,8 +231,9 @@ func (t *Template) newCatalogBuilderMap(catalogs []Catalog, outputType string) (
230231
builderMap := make(BuilderMap)
231232
for _, schema := range catalog.Builders {
232233
builder, err := t.builderForSchema(schema, BuilderConfig{
233-
WorkingDir: catalog.Destination.WorkingDir,
234-
OutputType: outputType,
234+
WorkingDir: catalog.Destination.WorkingDir,
235+
OutputType: outputType,
236+
ContributionPath: t.contributionPath,
235237
})
236238
if err != nil {
237239
return nil, fmt.Errorf("getting builder %q for catalog %q: %v", schema, catalog.Name, err)

staging/operator-registry/alpha/template/composite/composite_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -436,7 +436,7 @@ func TestParseContributionSpec(t *testing.T) {
436436

437437
for _, tc := range testCases {
438438
t.Run(tc.name, func(t *testing.T) {
439-
template := NewTemplate(WithContributionFile(strings.NewReader(tc.composite)))
439+
template := NewTemplate(WithContributionFile(strings.NewReader(tc.composite), ""))
440440
contrib, err := template.parseContributionSpec()
441441
tc.assertions(t, contrib, err)
442442
})

staging/operator-registry/alpha/template/composite/types.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ type builderFunc func(BuilderConfig) Builder
4343
type Template struct {
4444
catalogFile io.Reader
4545
contributionFile io.Reader
46+
contributionPath string
4647
validate bool
4748
outputType string
4849
registry image.Registry

staging/operator-registry/cmd/opm/alpha/template/composite.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"log"
55
"net/http"
66
"os"
7+
"path/filepath"
78

89
"github.com/spf13/cobra"
910

@@ -49,6 +50,11 @@ and a 'composite template' file`,
4950
}
5051
defer compositeReader.Close()
5152

53+
compositePath, err := filepath.Abs(filepath.Dir(compositeFile))
54+
if err != nil {
55+
log.Fatalf("getting absolute path of composite config file %q: %v", compositeFile, err)
56+
}
57+
5258
// catalog maintainer's 'catalogs.yaml' file
5359
tempCatalog, err := composite.FetchCatalogConfig(catalogFile, http.DefaultClient)
5460
if err != nil {
@@ -58,7 +64,7 @@ and a 'composite template' file`,
5864

5965
template := composite.NewTemplate(
6066
composite.WithCatalogFile(tempCatalog),
61-
composite.WithContributionFile(compositeReader),
67+
composite.WithContributionFile(compositeReader, compositePath),
6268
composite.WithOutputType(output),
6369
composite.WithRegistry(reg),
6470
)

vendor/github.com/operator-framework/operator-registry/alpha/template/composite/builder.go

Lines changed: 27 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/operator-framework/operator-registry/alpha/template/composite/composite.go

Lines changed: 5 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/operator-framework/operator-registry/alpha/template/composite/types.go

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/operator-framework/operator-registry/cmd/opm/alpha/template/composite.go

Lines changed: 7 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)