-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
25 changed files
with
1,219 additions
and
23 deletions.
There are no files selected for viewing
This file contains 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 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 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 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,29 @@ | ||
package artifact | ||
|
||
type Config interface { | ||
ArtifactType() (string, error) | ||
ConfigMediaType() (string, error) | ||
RawConfigFile() ([]byte, error) | ||
} | ||
|
||
func EmptyConfig(artifactType string) Config { | ||
return &emptyConfigArtifact{ | ||
artifactType: artifactType, | ||
} | ||
} | ||
|
||
type emptyConfigArtifact struct { | ||
artifactType string | ||
} | ||
|
||
func (i *emptyConfigArtifact) ArtifactType() (string, error) { | ||
return i.artifactType, nil | ||
} | ||
|
||
func (i *emptyConfigArtifact) ConfigMediaType() (string, error) { | ||
return "application/vnd.oci.empty.v1+json", nil | ||
} | ||
|
||
func (i *emptyConfigArtifact) RawConfigFile() ([]byte, error) { | ||
return []byte("{}"), nil | ||
} |
This file contains 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,148 @@ | ||
package artifact | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"sync/atomic" | ||
|
||
v1 "github.com/google/go-containerregistry/pkg/v1" | ||
"github.com/google/go-containerregistry/pkg/v1/partial" | ||
"github.com/google/go-containerregistry/pkg/v1/types" | ||
"github.com/opencontainers/go-digest" | ||
specv1 "github.com/opencontainers/image-spec/specs-go/v1" | ||
) | ||
|
||
func Artifact(img v1.Image, c Config) (v1.Image, error) { | ||
return &artifactImage{ | ||
Image: img, | ||
config: c, | ||
}, nil | ||
} | ||
|
||
type artifactImage struct { | ||
v1.Image | ||
config Config | ||
|
||
m atomic.Pointer[specv1.Manifest] | ||
} | ||
|
||
func (img *artifactImage) MediaType() (types.MediaType, error) { | ||
return types.OCIManifestSchema1, nil | ||
} | ||
|
||
func (i *artifactImage) ConfigName() (v1.Hash, error) { | ||
return partial.ConfigName(i) | ||
} | ||
|
||
func (img *artifactImage) RawConfigFile() ([]byte, error) { | ||
return img.config.RawConfigFile() | ||
} | ||
|
||
func (img *artifactImage) Manifest() (*v1.Manifest, error) { | ||
raw, err := img.RawManifest() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
m := &v1.Manifest{} | ||
|
||
if err := json.Unmarshal(raw, m); err != nil { | ||
return nil, err | ||
} | ||
|
||
return m, nil | ||
} | ||
|
||
func (img *artifactImage) RawManifest() ([]byte, error) { | ||
m, err := img.OCIManifest() | ||
if err != nil { | ||
return nil, err | ||
} | ||
return json.MarshalIndent(m, "", " ") | ||
} | ||
|
||
func (img *artifactImage) OCIManifest() (*specv1.Manifest, error) { | ||
if m := img.m.Load(); m != nil { | ||
return m, nil | ||
} | ||
|
||
configRaw, err := img.RawConfigFile() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
cfgHash, cfgSize, err := v1.SHA256(bytes.NewReader(configRaw)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
mediaType, err := img.MediaType() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
artifactType, err := img.config.ArtifactType() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
configMediaType, err := img.config.ConfigMediaType() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
m := &specv1.Manifest{ | ||
MediaType: string(mediaType), | ||
ArtifactType: artifactType, | ||
Config: specv1.Descriptor{ | ||
MediaType: configMediaType, | ||
Size: cfgSize, | ||
Digest: digest.Digest(cfgHash.String()), | ||
}, | ||
} | ||
m.SchemaVersion = 2 | ||
|
||
layers, err := img.Image.Layers() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
for _, l := range layers { | ||
desc, err := partial.Descriptor(l) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
d := specv1.Descriptor{ | ||
MediaType: string(desc.MediaType), | ||
Digest: digest.Digest(desc.Digest.String()), | ||
Size: desc.Size, | ||
Annotations: desc.Annotations, | ||
ArtifactType: desc.ArtifactType, | ||
} | ||
|
||
if p := desc.Platform; p != nil { | ||
d.Platform = &specv1.Platform{ | ||
Architecture: p.Architecture, | ||
OS: p.OS, | ||
OSVersion: p.OSVersion, | ||
OSFeatures: p.OSFeatures, | ||
Variant: p.Variant, | ||
} | ||
} | ||
|
||
m.Layers = append(m.Layers, d) | ||
} | ||
|
||
img.m.Store(m) | ||
|
||
return m, nil | ||
} | ||
|
||
func (img *artifactImage) Size() (int64, error) { | ||
return partial.Size(img) | ||
} | ||
|
||
func (img *artifactImage) Digest() (v1.Hash, error) { | ||
return partial.Digest(img) | ||
} |
This file contains 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,101 @@ | ||
package artifact | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"io" | ||
"sync" | ||
|
||
v1 "github.com/google/go-containerregistry/pkg/v1" | ||
"github.com/google/go-containerregistry/pkg/v1/partial" | ||
"github.com/google/go-containerregistry/pkg/v1/types" | ||
) | ||
|
||
type Layer = v1.Layer | ||
|
||
func FromBytes(mediaType string, data []byte) (Layer, error) { | ||
return FromOpener(mediaType, func() (io.ReadCloser, error) { | ||
return io.NopCloser(bytes.NewBuffer(data)), nil | ||
}) | ||
} | ||
|
||
func FromReader(mediaType string, r io.Reader) (Layer, error) { | ||
return FromOpener(mediaType, func() (io.ReadCloser, error) { | ||
return io.NopCloser(r), nil | ||
}) | ||
} | ||
|
||
func FromOpener(mediaType string, uncompressed func() (io.ReadCloser, error)) (Layer, error) { | ||
return NonCompressedLayer(&artifact{ | ||
mediaType: mediaType, | ||
uncompressed: uncompressed, | ||
}), nil | ||
} | ||
|
||
type artifact struct { | ||
mediaType string | ||
uncompressed func() (io.ReadCloser, error) | ||
} | ||
|
||
func (a *artifact) MediaType() (types.MediaType, error) { | ||
return types.MediaType(a.mediaType), nil | ||
} | ||
|
||
func (a *artifact) Uncompressed() (io.ReadCloser, error) { | ||
return a.uncompressed() | ||
} | ||
|
||
func Gzipped(l Layer) Layer { | ||
return &compressed{ | ||
Layer: l, | ||
compressedLayer: sync.OnceValues(func() (v1.Layer, error) { | ||
return partial.UncompressedToLayer(l) | ||
}), | ||
} | ||
} | ||
|
||
type compressed struct { | ||
Layer | ||
compressedLayer func() (v1.Layer, error) | ||
} | ||
|
||
func (a *compressed) MediaType() (types.MediaType, error) { | ||
m, err := a.Layer.MediaType() | ||
if err != nil { | ||
return "", err | ||
} | ||
return types.MediaType(fmt.Sprintf("%s+gzip", m)), nil | ||
} | ||
|
||
func (a *compressed) Compressed() (io.ReadCloser, error) { | ||
l, err := a.compressedLayer() | ||
if err != nil { | ||
return nil, err | ||
} | ||
return l.Compressed() | ||
} | ||
|
||
func (a *compressed) Digest() (v1.Hash, error) { | ||
l, err := a.compressedLayer() | ||
if err != nil { | ||
return v1.Hash{}, err | ||
} | ||
return l.Digest() | ||
} | ||
|
||
func WithDescriptor(l Layer, descriptor v1.Descriptor) Layer { | ||
return &artifactWithDescriptor{ | ||
desc: descriptor, | ||
Layer: l, | ||
} | ||
} | ||
|
||
type artifactWithDescriptor struct { | ||
Layer | ||
|
||
desc v1.Descriptor | ||
} | ||
|
||
func (w *artifactWithDescriptor) Descriptor() (*v1.Descriptor, error) { | ||
return &w.desc, nil | ||
} |
Oops, something went wrong.