-
Notifications
You must be signed in to change notification settings - Fork 79
pkg/metadata: bundle metadata utils and types #60
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| package metadata | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "os" | ||
| "path/filepath" | ||
| "strings" | ||
|
|
||
| log "github.com/sirupsen/logrus" | ||
| "github.com/spf13/afero" | ||
| ) | ||
|
|
||
| // The bundle root-relative default annotations.yaml path. | ||
| var defaultRelPath = filepath.Join("metadata", "annotations.yaml") | ||
|
|
||
| // FindAnnotations walks bundleRoot searching for bundle metadata in an annotations.yaml file, | ||
| // and returns this metadata and its path if found. If one is not found, an error is returned. | ||
| func FindAnnotations(bundleRoot string) (AnnotationsFile, string, error) { | ||
| return findAnnotations(afero.NewOsFs(), bundleRoot) | ||
| } | ||
|
|
||
| func findAnnotations(fs afero.Fs, bundleRoot string) (AnnotationsFile, string, error) { | ||
| // Check the default path first, and return annotations if they were found or an error if that error | ||
| // is not because the path does not exist (it exists or there was an unmarshaling error). | ||
| annotationsPath := filepath.Join(bundleRoot, defaultRelPath) | ||
| af, err := readAnnotations(fs, annotationsPath) | ||
| if err != nil { | ||
| // Ignore this error, since the annotations might be in some other file. | ||
| log.Debug(err) | ||
| } else if !af.IsEmpty() { | ||
| return af, annotationsPath, nil | ||
| } | ||
|
|
||
| // Annotations are not at the default path, so search recursively. | ||
| foundAnnotations := false | ||
| annotationsPath = "" | ||
| err = afero.Walk(fs, bundleRoot, func(path string, info os.FileInfo, err error) error { | ||
| if err != nil { | ||
| return err | ||
| } | ||
| // Skip if annotations were already found, or path is a directory or hidden file. | ||
| if foundAnnotations || info.IsDir() || strings.HasPrefix(path, ".") { | ||
| return nil | ||
| } | ||
|
|
||
| annotationsPath = path | ||
| if af, err = readAnnotations(fs, path); err != nil { | ||
| // We don't want to stop early, so ignore this error and try other files. | ||
| log.Debug(err) | ||
| } else if !af.IsEmpty() { | ||
| foundAnnotations = true | ||
| } | ||
| return nil | ||
| }) | ||
| if err != nil { | ||
| return af, "", err | ||
| } | ||
|
|
||
| if !foundAnnotations { | ||
| return af, "", fmt.Errorf("metadata not found in %s", bundleRoot) | ||
| } | ||
|
|
||
| return af, annotationsPath, nil | ||
| } | ||
|
|
||
| // readAnnotations attempts to read annotations from annotationsPath. | ||
| func readAnnotations(fs afero.Fs, annotationsPath string) (af AnnotationsFile, err error) { | ||
| b, err := afero.ReadFile(fs, annotationsPath) | ||
| if err != nil { | ||
| return af, err | ||
| } | ||
| if err = af.Unmarshal(b); err != nil { | ||
| return af, fmt.Errorf("error unmarshaling potential bundle metadata %s: %v", annotationsPath, err) | ||
| } | ||
| return af, nil | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,169 @@ | ||
| package metadata | ||
|
|
||
| import ( | ||
| "path/filepath" | ||
|
|
||
| . "github.com/onsi/ginkgo" | ||
| . "github.com/onsi/gomega" | ||
| "github.com/spf13/afero" | ||
| ) | ||
|
|
||
| var _ = Describe("Annotations", func() { | ||
| Describe("FindAnnotations", func() { | ||
| var ( | ||
| fs afero.Fs | ||
| err error | ||
| defaultPath = "/bundle/metadata/annotations.yaml" | ||
| ) | ||
|
|
||
| Context("with valid annotations contents", func() { | ||
| var ( | ||
| annotationsFile AnnotationsFile | ||
| path, expPath string | ||
| ) | ||
| BeforeEach(func() { | ||
| fs = afero.NewMemMapFs() | ||
| }) | ||
|
|
||
| // Location | ||
| It("finds registry metadata in the default location", func() { | ||
| expPath = defaultPath | ||
| writeMetadataHelper(fs, expPath, annotationsStringValidV1) | ||
| annotationsFile, path, err = findAnnotations(fs, "/bundle") | ||
| Expect(err).To(BeNil()) | ||
| Expect(path).To(Equal(expPath)) | ||
| Expect(annotationsFile).To(BeEquivalentTo(annotationsValidV1)) | ||
| }) | ||
| It("finds registry metadata in the a custom file name", func() { | ||
| expPath = "/bundle/metadata/my-metadata.yaml" | ||
| writeMetadataHelper(fs, expPath, annotationsStringValidV1) | ||
| annotationsFile, path, err = findAnnotations(fs, "/bundle") | ||
| Expect(err).To(BeNil()) | ||
| Expect(path).To(Equal(expPath)) | ||
| Expect(annotationsFile).To(BeEquivalentTo(annotationsValidV1)) | ||
| }) | ||
| It("finds registry metadata in a custom single-depth location", func() { | ||
| expPath = "/bundle/my-dir/my-metadata.yaml" | ||
| writeMetadataHelper(fs, expPath, annotationsStringValidV1) | ||
| annotationsFile, path, err = findAnnotations(fs, "/bundle") | ||
| Expect(err).To(BeNil()) | ||
| Expect(path).To(Equal(expPath)) | ||
| Expect(annotationsFile).To(BeEquivalentTo(annotationsValidV1)) | ||
| }) | ||
| It("finds registry metadata in a custom multi-depth location", func() { | ||
| expPath = "/bundle/my-parent-dir/my-dir/annotations.yaml" | ||
| writeMetadataHelper(fs, expPath, annotationsStringValidV1) | ||
| annotationsFile, path, err = findAnnotations(fs, "/bundle") | ||
| Expect(err).To(BeNil()) | ||
| Expect(path).To(Equal(expPath)) | ||
| Expect(annotationsFile).To(BeEquivalentTo(annotationsValidV1)) | ||
| }) | ||
| It("returns registry metadata from default path when metadata is also in another location", func() { | ||
| expPath = defaultPath | ||
| writeMetadataHelper(fs, expPath, annotationsStringValidV1) | ||
| writeMetadataHelper(fs, "/bundle/other-metadata/annotations.yaml", annotationsStringValidNoRegLabels) | ||
| annotationsFile, path, err = findAnnotations(fs, "/bundle") | ||
| Expect(err).To(BeNil()) | ||
| Expect(path).To(Equal(expPath)) | ||
| Expect(annotationsFile).To(BeEquivalentTo(annotationsValidV1)) | ||
| }) | ||
| It("returns registry metadata from the first path, when metadata is also in another location", func() { | ||
| expPath = "/bundle/custom1/annotations.yaml" | ||
| writeMetadataHelper(fs, expPath, annotationsStringValidV1) | ||
| writeMetadataHelper(fs, "/bundle/custom2/annotations.yaml", annotationsStringValidNoRegLabels) | ||
| annotationsFile, path, err = findAnnotations(fs, "/bundle") | ||
| Expect(err).To(BeNil()) | ||
| Expect(path).To(Equal(expPath)) | ||
| Expect(annotationsFile).To(BeEquivalentTo(annotationsValidV1)) | ||
| }) | ||
|
|
||
| // Format | ||
| It("finds non-registry metadata", func() { | ||
| expPath = defaultPath | ||
| writeMetadataHelper(fs, defaultPath, annotationsStringValidNoRegLabels) | ||
| annotationsFile, path, err = findAnnotations(fs, "/bundle") | ||
| Expect(err).To(BeNil()) | ||
| Expect(path).To(Equal(expPath)) | ||
| Expect(annotationsFile).To(BeEquivalentTo(annotationsValidNoRegLabels)) | ||
| }) | ||
| }) | ||
|
|
||
| Context("with invalid annotations contents", func() { | ||
| BeforeEach(func() { | ||
| fs = afero.NewMemMapFs() | ||
| }) | ||
|
|
||
| It("returns an error for no metadata file (YAML error)", func() { | ||
| writeMetadataHelper(fs, defaultPath, annotationsStringInvalidBadIndent) | ||
| _, _, err = findAnnotations(fs, "/bundle") | ||
| Expect(err).To(MatchError("metadata not found in /bundle")) | ||
| }) | ||
| It("returns an error for no metadata file (empty file)", func() { | ||
| writeMetadataHelper(fs, defaultPath, annotationsStringInvalidEmpty) | ||
| _, _, err = findAnnotations(fs, "/bundle") | ||
| Expect(err).To(MatchError("metadata not found in /bundle")) | ||
| }) | ||
| It("returns an error for no metadata file (invalid top-level key)", func() { | ||
| writeMetadataHelper(fs, defaultPath, annotationsStringInvalidTopKey) | ||
| _, _, err = findAnnotations(fs, "/bundle") | ||
| Expect(err).To(MatchError("metadata not found in /bundle")) | ||
| }) | ||
| It("returns an error for no labels in a metadata file", func() { | ||
| writeMetadataHelper(fs, defaultPath, annotationsStringInvalidNoLabels) | ||
| _, _, err = findAnnotations(fs, "/bundle") | ||
| Expect(err).To(MatchError("metadata not found in /bundle")) | ||
| }) | ||
| }) | ||
| }) | ||
|
|
||
| }) | ||
|
|
||
| func writeMetadataHelper(fs afero.Fs, path, contents string) { | ||
| ExpectWithOffset(1, fs.MkdirAll(filepath.Dir(path), 0755)).Should(Succeed()) | ||
| ExpectWithOffset(1, afero.WriteFile(fs, path, []byte(contents), 0666)).Should(Succeed()) | ||
| } | ||
|
|
||
| var annotationsValidV1 = AnnotationsFile{ | ||
| Annotations: AnnotationsV1{ | ||
| MediaType: "registry+v1", | ||
| MetadataDir: "metadata/", | ||
| AnnotationsRaw: map[string]string{ | ||
| "foo": "bar", | ||
| }, | ||
| }, | ||
| } | ||
|
|
||
| const annotationsStringValidV1 = `annotations: | ||
| operators.operatorframework.io.bundle.mediatype.v1: registry+v1 | ||
| operators.operatorframework.io.bundle.metadata.v1: metadata/ | ||
| foo: bar | ||
| ` | ||
|
|
||
| var annotationsValidNoRegLabels = AnnotationsFile{ | ||
| Annotations: AnnotationsV1{ | ||
| AnnotationsRaw: map[string]string{ | ||
| "foo": "bar", | ||
| "baz": "buf", | ||
| }, | ||
| }, | ||
| } | ||
|
|
||
| const annotationsStringValidNoRegLabels = `annotations: | ||
| foo: bar | ||
| baz: buf | ||
| ` | ||
|
|
||
| const annotationsStringInvalidBadIndent = `annotations: | ||
| operators.operatorframework.io.bundle.mediatype.v1: registry+v1 | ||
| ` | ||
|
|
||
| const annotationsStringInvalidEmpty = `` | ||
|
|
||
| const annotationsStringInvalidNoLabels = `annotations: | ||
| ` | ||
|
|
||
| const annotationsStringInvalidTopKey = `not-annotations: | ||
| operators.operatorframework.io.bundle.mediatype.v1: registry+v1 | ||
| operators.operatorframework.io.bundle.metadata.v1: metadata/ | ||
| foo: bar | ||
| ` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| package metadata | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| . "github.com/onsi/ginkgo" | ||
| . "github.com/onsi/gomega" | ||
| ) | ||
|
|
||
| func TestMetadata(t *testing.T) { | ||
| RegisterFailHandler(Fail) | ||
| RunSpecs(t, "Metadata Suite") | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nice find!