Skip to content

Commit f1c4853

Browse files
Merge pull request #679 from joelanford/declcfg-ignore
Add declarative config `.indexignore` support
2 parents 9abde61 + 1cdf593 commit f1c4853

File tree

408 files changed

+23901
-2877
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

408 files changed

+23901
-2877
lines changed

go.mod

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ require (
2929
github.com/h2non/filetype v1.1.1
3030
github.com/h2non/go-is-svg v0.0.0-20160927212452-35e8c4b0612c
3131
github.com/hashicorp/go-multierror v1.0.0
32+
github.com/joelanford/ignore v0.0.0-20210607151042-0d25dc18b62d
3233
github.com/mattn/go-sqlite3 v1.10.0
3334
github.com/maxbrunsfeld/counterfeiter/v6 v6.2.2
3435
github.com/onsi/ginkgo v1.14.1
@@ -48,7 +49,7 @@ require (
4849
github.com/yvasiyarov/newrelic_platform_go v0.0.0-20160601141957-9c099fbc30e9 // indirect
4950
go.etcd.io/bbolt v1.3.5
5051
golang.org/x/mod v0.3.0
51-
golang.org/x/net v0.0.0-20201110031124-69a78807bb2b
52+
golang.org/x/net v0.0.0-20210326060303-6b1517762897
5253
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9
5354
google.golang.org/grpc v1.30.0
5455
google.golang.org/grpc/cmd/protoc-gen-go-grpc v0.0.0-20200709232328-d8193ee9cc3e

go.sum

Lines changed: 50 additions & 8 deletions
Large diffs are not rendered by default.

internal/action/render_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ func newRegistry() (image.Registry, error) {
270270
},
271271
image.SimpleReference("test.registry/foo-operator/foo-index-declcfg:v0.2.0"): &image.MockImage{
272272
Labels: map[string]string{
273-
"operators.operatorframework.io.index.configs.v1": "/foo/index.yaml",
273+
"operators.operatorframework.io.index.configs.v1": "/foo",
274274
},
275275
FS: subDeclcfgImage,
276276
},

internal/declcfg/load.go

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@ package declcfg
22

33
import (
44
"encoding/json"
5+
"errors"
56
"fmt"
67
"io"
78
"os"
89
"path/filepath"
910
"strings"
1011

12+
"github.com/joelanford/ignore"
1113
"github.com/operator-framework/api/pkg/operators"
1214
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
1315
"k8s.io/apimachinery/pkg/util/yaml"
@@ -22,7 +24,16 @@ func LoadDir(configDir string) (*DeclarativeConfig, error) {
2224

2325
func loadFS(root string, w fsWalker) (*DeclarativeConfig, error) {
2426
cfg := &DeclarativeConfig{}
27+
28+
matcher, err := ignore.NewMatcher(os.DirFS(root), ".indexignore")
29+
if err != nil {
30+
return nil, err
31+
}
32+
2533
if err := w.WalkFiles(root, func(path string, r io.Reader) error {
34+
if matcher.Match(path, false) {
35+
return nil
36+
}
2637
fileCfg, err := readYAMLOrJSON(r)
2738
if err != nil {
2839
return fmt.Errorf("could not load config file %q: %v", path, err)
@@ -78,14 +89,16 @@ func readYAMLOrJSON(r io.Reader) (*DeclarativeConfig, error) {
7889
for {
7990
doc := json.RawMessage{}
8091
if err := dec.Decode(&doc); err != nil {
81-
break
92+
if errors.Is(err, io.EOF) {
93+
break
94+
}
95+
return nil, err
8296
}
8397
doc = []byte(strings.NewReplacer(`\u003c`, "<", `\u003e`, ">", `\u0026`, "&").Replace(string(doc)))
8498

8599
var in Meta
86100
if err := json.Unmarshal(doc, &in); err != nil {
87-
// Ignore JSON blobs if they are not parsable as meta objects.
88-
continue
101+
return nil, err
89102
}
90103

91104
switch in.Schema {
@@ -102,8 +115,7 @@ func readYAMLOrJSON(r io.Reader) (*DeclarativeConfig, error) {
102115
}
103116
cfg.Bundles = append(cfg.Bundles, b)
104117
case "":
105-
// Ignore meta blobs that don't have a schema.
106-
continue
118+
return nil, fmt.Errorf("object '%s' is missing root schema field", string(doc))
107119
default:
108120
cfg.Others = append(cfg.Others, in)
109121
}
@@ -129,6 +141,7 @@ func (w dirWalker) WalkFiles(root string, f func(string, io.Reader) error) error
129141
if err != nil {
130142
return err
131143
}
144+
defer file.Close()
132145
return f(path, file)
133146
})
134147
}

internal/declcfg/load_test.go

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,20 +22,19 @@ func TestReadYAMLOrJSON(t *testing.T) {
2222
}
2323
specs := []spec{
2424
{
25-
name: "Ignored/NotYAMLOrJSON",
26-
file: "testdata/invalid/not-yaml-or-json.txt",
27-
assertion: require.NoError,
28-
expectNumPackages: 0,
29-
expectNumBundles: 0,
30-
expectNumOthers: 0,
25+
name: "Error/NotYAMLOrJSON",
26+
file: "testdata/invalid/not-yaml-or-json.txt",
27+
assertion: require.Error,
3128
},
3229
{
33-
name: "Ignored/NotJSONObject",
34-
file: "testdata/invalid/not-yaml-or-json-object.json",
35-
assertion: require.NoError,
36-
expectNumPackages: 0,
37-
expectNumBundles: 0,
38-
expectNumOthers: 0,
30+
name: "Error/NotJSONObject",
31+
file: "testdata/invalid/not-yaml-or-json-object.json",
32+
assertion: require.Error,
33+
},
34+
{
35+
name: "Error/NoSchema",
36+
file: "testdata/invalid/no-schema.yaml",
37+
assertion: require.Error,
3938
},
4039
{
4140
name: "Error/InvalidPackageJSON",
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
hello: world
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
This is not yaml or json.
1+
[This is not yaml or json.}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
*
2+
!*.json
3+
!*.yaml
4+
5+
*.clusterserviceversion.yaml
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Valid Declarative Config
2+
3+
This is a README file about this declarative config. It should be ignored
4+
when loading this directory as declarative config due to the patterns
5+
present in the `.indexignore` file.

vendor/github.com/Microsoft/go-winio/go.mod

Lines changed: 1 addition & 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)