forked from meshery/meshkit
/
oci.go
224 lines (190 loc) · 6.29 KB
/
oci.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
package oci
import (
"context"
"fmt"
"os"
"path/filepath"
"time"
"github.com/fluxcd/pkg/oci"
"github.com/fluxcd/pkg/oci/client"
gcrv1 "github.com/google/go-containerregistry/pkg/v1"
"github.com/google/go-containerregistry/pkg/v1/empty"
"github.com/google/go-containerregistry/pkg/v1/mutate"
"github.com/google/go-containerregistry/pkg/v1/static"
"github.com/google/go-containerregistry/pkg/v1/tarball"
"github.com/google/go-containerregistry/pkg/v1/types"
v1 "github.com/opencontainers/image-spec/specs-go/v1"
oras "oras.land/oras-go/v2"
"oras.land/oras-go/v2/content/file"
"oras.land/oras-go/v2/registry/remote"
"oras.land/oras-go/v2/registry/remote/auth"
"oras.land/oras-go/v2/registry/remote/retry"
)
// LayerType is an enumeration of the supported layer types
// when pushing an image.
type LayerType string
const (
// LayerTypeTarball produces a layer that contains a gzipped archive
LayerTypeTarball LayerType = "tarball"
// LayerTypeStatic produces a layer that contains the contents of a
// file without any compression.
LayerTypeStatic LayerType = "static"
)
// BuildOptions are options for configuring the Push operation.
type BuildOptions struct {
layerType LayerType
layerOpts layerOptions
meta client.Metadata
}
// layerOptions are options for configuring a layer.
type layerOptions struct {
mediaTypeExt string
ignorePaths []string
}
// BuildOption is a function for configuring BuildOptions.
type BuildOption func(o *BuildOptions)
// Builds OCI Img for the artifacts in the given path. Returns v1.Image manifest.
func BuildImage(sourcePath string, opts ...BuildOption) (gcrv1.Image, error) {
o := &BuildOptions{
layerType: LayerTypeTarball,
}
for _, opt := range opts {
opt(o)
}
layer, err := createLayer(sourcePath, o.layerType, o.layerOpts)
if err != nil {
return nil, err
}
if o.meta.Created == "" {
ct := time.Now().UTC()
o.meta.Created = ct.Format(time.RFC3339)
}
img := mutate.MediaType(empty.Image, types.OCIManifestSchema1)
img = mutate.ConfigMediaType(img, oci.CanonicalConfigMediaType)
img = mutate.Annotations(img, o.meta.ToAnnotations()).(gcrv1.Image)
img, err = mutate.Append(img, mutate.Addendum{Layer: layer})
if err != nil {
return nil, ErrAppendingLayer(err)
}
return img, nil
}
// createLayer creates a layer depending on the layerType.
func createLayer(path string, layerType LayerType, opts layerOptions) (gcrv1.Layer, error) {
switch layerType {
case LayerTypeTarball:
var ociMediaType = oci.CanonicalContentMediaType
var tmpDir string
tmpDir, err := os.MkdirTemp("", "oci")
if err != nil {
return nil, err
}
defer os.RemoveAll(tmpDir)
tmpFile := filepath.Join(tmpDir, "artifact.tgz")
defaultOpts := client.DefaultOptions()
ociClient := client.NewClient(defaultOpts)
if err := ociClient.Build(tmpFile, path, opts.ignorePaths); err != nil {
return nil, err
}
return tarball.LayerFromFile(tmpFile, tarball.WithMediaType(ociMediaType), tarball.WithCompressedCaching)
case LayerTypeStatic:
var ociMediaType = getLayerMediaType(opts.mediaTypeExt)
content, err := os.ReadFile(path)
if err != nil {
return nil, ErrReadingFile(err)
}
return static.NewLayer(content, ociMediaType), nil
default:
return nil, ErrUnSupportedLayerType(fmt.Errorf("unsupported layer type: '%s'", layerType))
}
}
func getLayerMediaType(extension string) types.MediaType {
if extension == "" {
return oci.CanonicalMediaTypePrefix
}
return types.MediaType(fmt.Sprintf("%s.%s", oci.CanonicalMediaTypePrefix, extension))
}
// function to pull models from any OCI-compatible repository
func PushToOCIRegistry(dirPath, registryAdd, repositoryAdd, imageTag, username, password string) error {
fs, fileErr := file.New(".")
if fileErr != nil {
return ErrWriteFile(fileErr)
}
ctx := context.Background()
mediaType := "application/vnd.test.folder"
fileNames := []string{dirPath}
fileDescriptors := make([]v1.Descriptor, 0, len(fileNames))
for _, name := range fileNames {
fileDescriptor, err := fs.Add(ctx, name, mediaType, "")
if err != nil {
return ErrAddLayer(err)
}
fileDescriptors = append(fileDescriptors, fileDescriptor)
}
// Pack the folder and tag the packed manifest
artifactType := "application/vnd.test.artifact"
opts := oras.PackManifestOptions{
Layers: fileDescriptors,
}
manifestDescriptor, packageErr := oras.PackManifest(ctx, fs, oras.PackManifestVersion1_1_RC4, artifactType, opts)
if packageErr != nil {
return ErrGettingLayer(packageErr)
}
if tagErr := fs.Tag(ctx, manifestDescriptor, imageTag); tagErr != nil {
return ErrWriteFile(tagErr)
}
// Connect to a remote repository
repo, connectErr := remote.NewRepository(registryAdd + "/" + repositoryAdd)
if connectErr != nil {
return ErrConnectingToRegistry(connectErr)
}
// Authenticate to the registry
authErr := AuthToOCIRegistry(repo, registryAdd, username, password)
if authErr != nil {
return ErrAuthenticatingToRegistry(authErr)
}
_, pushErr := oras.Copy(ctx, fs, imageTag, repo, imageTag, oras.DefaultCopyOptions)
if pushErr != nil {
return ErrPushingPackage(pushErr)
}
return nil
}
// authentification to the public oci registry
// registryURL example : docker.io
func AuthToOCIRegistry(repo *remote.Repository, registryURI, username, password string) error {
repo.Client = &auth.Client{
Client: retry.DefaultClient,
Cache: auth.NewCache(),
Credential: auth.StaticCredential(registryURI, auth.Credential{
Username: username,
Password: password,
}),
}
return nil
}
// function to pull images from the public oci repository
func PullFromOCIRegistry(dirPath, registryAdd, repositoryAdd, imageTag, username, password string) error {
// Create a new file store
fs, err := file.New(dirPath)
if err != nil {
return ErrFileNotFound(err, dirPath)
}
defer fs.Close()
ctx := context.Background()
// Connect to remote registry
repo, connectErr := remote.NewRepository(registryAdd + "/" + repositoryAdd)
if connectErr != nil {
return ErrConnectingToRegistry(connectErr)
}
// Authenticate to the registry
if username != "" && password != "" {
authErr := AuthToOCIRegistry(repo, registryAdd, username, password)
if authErr != nil {
return ErrAuthenticatingToRegistry(authErr)
}
}
_, pullErr := oras.Copy(ctx, repo, imageTag, fs, imageTag, oras.DefaultCopyOptions)
if pullErr != nil {
return ErrGettingImage(pullErr)
}
return nil
}