forked from CloudSnorkel/standalone-soci-indexer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandler.go
More file actions
185 lines (155 loc) · 5.79 KB
/
handler.go
File metadata and controls
185 lines (155 loc) · 5.79 KB
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
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
package main
import (
"context"
"errors"
"fmt"
"os"
"path"
"github.com/sofatutor/standalone-soci-indexer/utils/log"
registryutils "github.com/sofatutor/standalone-soci-indexer/utils/registry"
"github.com/containerd/containerd/images"
"oras.land/oras-go/v2/content/oci"
"github.com/awslabs/soci-snapshotter/soci"
"github.com/awslabs/soci-snapshotter/soci/store"
"github.com/containerd/containerd/content"
"github.com/containerd/containerd/content/local"
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
)
// TODO: Remove this once the SOCI library exports this error.
var (
ErrEmptyIndex = errors.New("no ztocs created, all layers either skipped or produced errors")
)
const (
BuildFailedMessage = "SOCI index build error"
PushFailedMessage = "SOCI index push error"
PushOnEmptyIndexMessage = "SOCI index does not contain any zTOCs"
BuildAndPushSuccessMessage = "Successfully built and pushed SOCI index"
artifactsStoreName = "store"
artifactsDbName = "artifacts.db"
)
func indexAndPush(ctx context.Context, repo string, tag string, newTags []string, registryUrl string, authToken string) (string, error) {
ctx = context.WithValue(ctx, "RegistryURL", registryUrl)
registry, err := registryutils.Init(ctx, registryUrl, authToken)
if err != nil {
return logAndReturnError(ctx, "Remote registry initialization error", err)
}
digests, err := registry.GetImageDigests(ctx, repo, tag)
if err != nil {
log.Warn(ctx, fmt.Sprintf("Image manifest validation error: %v", err))
// Returning a non error to skip retries
return "Exited early due to manifest validation error", nil
}
// Directory in lambda storage to store images and SOCI artifacts
dataDir, err := createTempDir(ctx)
if err != nil {
return logAndReturnError(ctx, "Directory create error", err)
}
defer cleanUp(ctx, dataDir)
sociStore, err := initSociStore(ctx, dataDir)
if err != nil {
return logAndReturnError(ctx, "OCI storage initialization error", err)
}
for _, digest := range digests {
ctx := context.WithValue(ctx, "ImageDigest", digest)
desc, err := registry.Pull(ctx, repo, sociStore, digest)
if err != nil {
return logAndReturnError(ctx, "Image pull error", err)
}
image := images.Image{
Name: repo + "@" + digest,
Target: *desc,
}
indexDescriptor, err := buildIndex(ctx, dataDir, sociStore, image)
if err != nil {
if err.Error() == ErrEmptyIndex.Error() {
log.Warn(ctx, PushOnEmptyIndexMessage)
// tag when using --new-tag
// the user will be expecting those tags to exist whether or not we created an index
for _, newTag := range newTags {
if newTag != tag {
err = registry.Tag(ctx, *desc, repo, newTag)
if err != nil {
return logAndReturnError(ctx, PushFailedMessage, err)
}
}
}
return PushOnEmptyIndexMessage, nil
}
return logAndReturnError(ctx, BuildFailedMessage, err)
}
ctx = context.WithValue(ctx, "SOCIIndexDigest", indexDescriptor.Digest.String())
err = registry.Push(ctx, sociStore, *indexDescriptor, repo)
if err != nil {
return logAndReturnError(ctx, PushFailedMessage, err)
}
for _, newTag := range newTags {
err = registry.Tag(ctx, *indexDescriptor, repo, newTag)
if err != nil {
return logAndReturnError(ctx, PushFailedMessage, err)
}
}
log.Info(ctx, BuildAndPushSuccessMessage)
}
return BuildAndPushSuccessMessage, nil
}
// Create a temp directory in /tmp or $TMPDIR
// The directory is prefixed by the Lambda's request id
func createTempDir(ctx context.Context) (string, error) {
log.Info(ctx, "Creating a directory to store images and SOCI artifacts")
tempDir, err := os.MkdirTemp("", "soci") // The temp dir name is prefixed by the request id
return tempDir, err
}
// Clean up the data written by the Lambda
func cleanUp(ctx context.Context, dataDir string) {
log.Info(ctx, fmt.Sprintf("Removing all files in %s", dataDir))
if err := os.RemoveAll(dataDir); err != nil {
log.Error(ctx, "Clean up error", err)
}
}
// Init containerd store
func initContainerdStore(dataDir string) (content.Store, error) {
containerdStore, err := local.NewStore(path.Join(dataDir, artifactsStoreName))
return containerdStore, err
}
// Init SOCI artifact store
func initSociStore(ctx context.Context, dataDir string) (*store.SociStore, error) {
// Note: We are wrapping an *oci.Store in a store.SociStore because soci.WriteSociIndex
// expects a store.Store, an interface that extends the oci.Store to provide support
// for garbage collection.
ociStore, err := oci.NewWithContext(ctx, path.Join(dataDir, artifactsStoreName))
return &store.SociStore{ociStore}, err
}
// Init a new instance of SOCI artifacts DB
func initSociArtifactsDb(dataDir string) (*soci.ArtifactsDb, error) {
artifactsDbPath := path.Join(dataDir, artifactsDbName)
artifactsDb, err := soci.NewDB(artifactsDbPath)
if err != nil {
return nil, err
}
return artifactsDb, nil
}
// Build soci index for an image and returns its ocispec.Descriptor
func buildIndex(ctx context.Context, dataDir string, sociStore *store.SociStore, image images.Image) (*ocispec.Descriptor, error) {
log.Info(ctx, "Building SOCI index")
artifactsDb, err := initSociArtifactsDb(dataDir)
if err != nil {
return nil, err
}
containerdStore, err := initContainerdStore(dataDir)
if err != nil {
return nil, err
}
builder, err := soci.NewIndexBuilder(containerdStore, sociStore, soci.WithArtifactsDb(artifactsDb), soci.WithBuildToolIdentifier("github.com/sofatutor/standalone-soci-indexer"))
if err != nil {
return nil, err
}
index, err := builder.Convert(ctx, image)
return index, err
}
// Log and return error
func logAndReturnError(ctx context.Context, msg string, err error) (string, error) {
log.Error(ctx, msg, err)
return msg, err
}