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.

Signed-off-by: Ilya Dmitrichenko <errordeveloper@gmail.com>
  • Loading branch information
errordeveloper committed Sep 26, 2023
1 parent 83f76d6 commit 9250da2
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 36 deletions.
9 changes: 4 additions & 5 deletions oci/client/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,10 @@ func (c *Client) List(ctx context.Context, url string, opts ListOptions) ([]Meta
return nil, fmt.Errorf("parsing manifest failed: %w", err)
}

if m, err := MetadataFromAnnotations(manifest.Annotations); err == nil {
meta.Revision = m.Revision
meta.Source = m.Source
meta.Created = m.Created
}
manifestMetadata := MetadataFromAnnotations(manifest.Annotations)
meta.Revision = manifestMetadata.Revision
meta.Source = manifestMetadata.Source
meta.Created = manifestMetadata.Created

digest, err := crane.Digest(meta.URL, c.optionsWithContext(ctx)...)
if err != nil {
Expand Down
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
78 changes: 78 additions & 0 deletions oci/client/pull_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
Copyright 2023 The Flux authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package client

import (
"context"
"fmt"
"path/filepath"
"testing"

"github.com/fluxcd/pkg/oci"
"github.com/google/go-containerregistry/pkg/crane"
"github.com/google/go-containerregistry/pkg/v1/empty"
"github.com/google/go-containerregistry/pkg/v1/mutate"
"github.com/google/go-containerregistry/pkg/v1/tarball"
"github.com/google/go-containerregistry/pkg/v1/types"
. "github.com/onsi/gomega"
)

func Test_PullAnyTarball(t *testing.T) {
g := NewWithT(t)
ctx := context.Background()
c := NewClient(DefaultOptions())
testDir := "testdata/artifact"

tag := "latest"
repo := "test-no-annotations" + randStringRunes(5)

dst := fmt.Sprintf("%s/%s:%s", dockerReg, repo, tag)

artifact := filepath.Join(t.TempDir(), "artifact.tgz")
g.Expect(c.Build(artifact, testDir, nil)).To(Succeed())

img := mutate.MediaType(empty.Image, types.OCIManifestSchema1)
img = mutate.ConfigMediaType(img, oci.CanonicalConfigMediaType)

layer, err := tarball.LayerFromFile(artifact, tarball.WithMediaType(oci.CanonicalContentMediaType))
g.Expect(err).ToNot(HaveOccurred())

img, err = mutate.Append(img, mutate.Addendum{Layer: layer})
g.Expect(err).ToNot(HaveOccurred())

g.Expect(crane.Push(img, dst, c.optionsWithContext(ctx)...)).ToNot(HaveOccurred())

extractTo := filepath.Join(t.TempDir(), "artifact")
m, err := c.Pull(ctx, dst, extractTo)
g.Expect(err).ToNot(HaveOccurred())
g.Expect(m).ToNot(BeNil())
g.Expect(extractTo).To(BeADirectory())

for _, entry := range []string{
"deploy",
"deploy/repo.yaml",
"deployment.yaml",
"ignore-dir",
"ignore-dir/deployment.yaml",
"ignore.txt",
"somedir",
"somedir/repo.yaml",
"somedir/git/repo.yaml",
} {
g.Expect(extractTo + "/" + entry).To(Or(BeAnExistingFile(), BeADirectory()))
}
}

0 comments on commit 9250da2

Please sign in to comment.