Skip to content

Commit

Permalink
oci/client: relax annotation parser
Browse files Browse the repository at this point in the history
This is in order to allow `flux pull artifact` to retrieve any kind
of OCI artifact, e.g. a Helm chart for debugging.
  • Loading branch information
errordeveloper committed Sep 22, 2023
1 parent 83f76d6 commit f166075
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 31 deletions.
35 changes: 8 additions & 27 deletions oci/client/meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,15 @@ limitations under the License.
package client

import (
"fmt"

"github.com/fluxcd/pkg/oci"
)

// Metadata holds the upstream information about on artifact's source.
// https://github.com/opencontainers/image-spec/blob/main/annotations.md
type Metadata struct {
Created string `json:"created"`
Source string `json:"source_url"`
Revision string `json:"source_revision"`
Created string `json:"created,omitempty"`
Source string `json:"source_url,omitempty"`
Revision string `json:"source_revision,omitempty"`
Digest string `json:"digest"`
URL string `json:"url"`
Annotations map[string]string `json:"annotations,omitempty"`
Expand All @@ -49,28 +47,11 @@ func (m *Metadata) ToAnnotations() map[string]string {
}

// MetadataFromAnnotations parses the OpenContainers annotations and returns a Metadata object.
func MetadataFromAnnotations(annotations map[string]string) (*Metadata, error) {
created, ok := annotations[oci.CreatedAnnotation]
if !ok {
return nil, fmt.Errorf("'%s' annotation not found", oci.CreatedAnnotation)
}

source, ok := annotations[oci.SourceAnnotation]
if !ok {
return nil, fmt.Errorf("'%s' annotation not found", oci.SourceAnnotation)
}

revision, ok := annotations[oci.RevisionAnnotation]
if !ok {
return nil, fmt.Errorf("'%s' annotation not found", oci.RevisionAnnotation)
}

m := Metadata{
Created: created,
Source: source,
Revision: revision,
func MetadataFromAnnotations(annotations map[string]string) *Metadata {
return &Metadata{
Created: annotations[oci.CreatedAnnotation],
Source: annotations[oci.SourceAnnotation],
Revision: annotations[oci.RevisionAnnotation],
Annotations: annotations,
}

return &m, nil
}
5 changes: 1 addition & 4 deletions oci/client/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,7 @@ func (c *Client) Pull(ctx context.Context, url, outDir string) (*Metadata, error
return nil, fmt.Errorf("parsing manifest failed: %w", err)
}

meta, err := MetadataFromAnnotations(manifest.Annotations)
if err != nil {
return nil, err
}
meta := MetadataFromAnnotations(manifest.Annotations)
meta.Digest = ref.Context().Digest(digest.String()).String()

layers, err := img.Layers()
Expand Down

0 comments on commit f166075

Please sign in to comment.