|
| 1 | +package azblob |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "context" |
| 6 | + "encoding/json" |
| 7 | + "fmt" |
| 8 | + "io" |
| 9 | + "time" |
| 10 | + |
| 11 | + "github.com/Azure/azure-sdk-for-go/sdk/azcore" |
| 12 | + "github.com/Azure/azure-sdk-for-go/sdk/azcore/to" |
| 13 | + "github.com/Azure/azure-sdk-for-go/sdk/storage/azblob/blob" |
| 14 | + "github.com/Azure/azure-sdk-for-go/sdk/storage/azblob/bloberror" |
| 15 | + "github.com/Azure/azure-sdk-for-go/sdk/storage/azblob/blockblob" |
| 16 | + "github.com/Azure/azure-sdk-for-go/sdk/storage/azblob/container" |
| 17 | + "github.com/containerd/containerd/v2/core/content" |
| 18 | + "github.com/containerd/containerd/v2/pkg/labels" |
| 19 | + "github.com/moby/buildkit/cache/remotecache" |
| 20 | + v1 "github.com/moby/buildkit/cache/remotecache/v1" |
| 21 | + "github.com/moby/buildkit/session" |
| 22 | + "github.com/moby/buildkit/solver" |
| 23 | + "github.com/moby/buildkit/util/bklog" |
| 24 | + "github.com/moby/buildkit/util/compression" |
| 25 | + "github.com/moby/buildkit/util/progress" |
| 26 | + digest "github.com/opencontainers/go-digest" |
| 27 | + "github.com/pkg/errors" |
| 28 | +) |
| 29 | + |
| 30 | +// ResolveCacheExporterFunc for "azblob" cache exporter. |
| 31 | +func ResolveCacheExporterFunc() remotecache.ResolveCacheExporterFunc { |
| 32 | + return func(ctx context.Context, g session.Group, attrs map[string]string) (remotecache.Exporter, error) { |
| 33 | + config, err := getConfig(attrs) |
| 34 | + if err != nil { |
| 35 | + return nil, errors.Wrap(err, "failed to create azblob config") |
| 36 | + } |
| 37 | + |
| 38 | + containerClient, err := createContainerClient(ctx, config) |
| 39 | + if err != nil { |
| 40 | + return nil, errors.Wrap(err, "failed to create container client") |
| 41 | + } |
| 42 | + |
| 43 | + cc := v1.NewCacheChains() |
| 44 | + return &exporter{ |
| 45 | + CacheExporterTarget: cc, |
| 46 | + chains: cc, |
| 47 | + containerClient: containerClient, |
| 48 | + config: config, |
| 49 | + }, nil |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +var _ remotecache.Exporter = &exporter{} |
| 54 | + |
| 55 | +type exporter struct { |
| 56 | + solver.CacheExporterTarget |
| 57 | + chains *v1.CacheChains |
| 58 | + containerClient *container.Client |
| 59 | + config *Config |
| 60 | +} |
| 61 | + |
| 62 | +func (ce *exporter) Name() string { |
| 63 | + return "exporting cache to Azure Blob Storage" |
| 64 | +} |
| 65 | + |
| 66 | +func (ce *exporter) Finalize(ctx context.Context) (map[string]string, error) { |
| 67 | + config, descs, err := ce.chains.Marshal(ctx) |
| 68 | + if err != nil { |
| 69 | + return nil, err |
| 70 | + } |
| 71 | + |
| 72 | + for i, l := range config.Layers { |
| 73 | + dgstPair, ok := descs[l.Blob] |
| 74 | + if !ok { |
| 75 | + return nil, errors.Errorf("missing blob %s", l.Blob) |
| 76 | + } |
| 77 | + if dgstPair.Descriptor.Annotations == nil { |
| 78 | + return nil, errors.Errorf("invalid descriptor without annotations") |
| 79 | + } |
| 80 | + var diffID digest.Digest |
| 81 | + v, ok := dgstPair.Descriptor.Annotations[labels.LabelUncompressed] |
| 82 | + if !ok { |
| 83 | + return nil, errors.Errorf("invalid descriptor without uncompressed annotation") |
| 84 | + } |
| 85 | + dgst, err := digest.Parse(v) |
| 86 | + if err != nil { |
| 87 | + return nil, errors.Wrap(err, "failed to parse uncompressed annotation") |
| 88 | + } |
| 89 | + diffID = dgst |
| 90 | + |
| 91 | + key := blobKey(ce.config, dgstPair.Descriptor.Digest) |
| 92 | + |
| 93 | + exists, err := blobExists(ctx, ce.containerClient, key) |
| 94 | + if err != nil { |
| 95 | + return nil, err |
| 96 | + } |
| 97 | + |
| 98 | + bklog.G(ctx).Debugf("layers %s exists = %t", key, exists) |
| 99 | + |
| 100 | + if !exists { |
| 101 | + layerDone := progress.OneOff(ctx, fmt.Sprintf("writing layer %s", l.Blob)) |
| 102 | + ra, err := dgstPair.Provider.ReaderAt(ctx, dgstPair.Descriptor) |
| 103 | + if err != nil { |
| 104 | + err = errors.Wrapf(err, "failed to get reader for %s", dgstPair.Descriptor.Digest) |
| 105 | + return nil, layerDone(err) |
| 106 | + } |
| 107 | + if err := ce.uploadBlobIfNotExists(ctx, key, content.NewReader(ra)); err != nil { |
| 108 | + return nil, layerDone(err) |
| 109 | + } |
| 110 | + layerDone(nil) |
| 111 | + } |
| 112 | + |
| 113 | + la := &v1.LayerAnnotations{ |
| 114 | + DiffID: diffID, |
| 115 | + Size: dgstPair.Descriptor.Size, |
| 116 | + MediaType: dgstPair.Descriptor.MediaType, |
| 117 | + } |
| 118 | + if v, ok := dgstPair.Descriptor.Annotations["buildkit/createdat"]; ok { |
| 119 | + var t time.Time |
| 120 | + if err := (&t).UnmarshalText([]byte(v)); err != nil { |
| 121 | + return nil, err |
| 122 | + } |
| 123 | + la.CreatedAt = t.UTC() |
| 124 | + } |
| 125 | + config.Layers[i].Annotations = la |
| 126 | + } |
| 127 | + |
| 128 | + dt, err := json.Marshal(config) |
| 129 | + if err != nil { |
| 130 | + return nil, errors.Wrap(err, "failed to marshal config") |
| 131 | + } |
| 132 | + |
| 133 | + for _, name := range ce.config.Names { |
| 134 | + if innerError := ce.uploadManifest(ctx, manifestKey(ce.config, name), bytesToReadSeekCloser(dt)); innerError != nil { |
| 135 | + return nil, errors.Wrapf(innerError, "error writing manifest %s", name) |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + return nil, nil |
| 140 | +} |
| 141 | + |
| 142 | +func (ce *exporter) Config() remotecache.Config { |
| 143 | + return remotecache.Config{ |
| 144 | + Compression: compression.New(compression.Default), |
| 145 | + } |
| 146 | +} |
| 147 | + |
| 148 | +// For uploading manifests, use the Upload API which follows "last writer wins" sematics |
| 149 | +// This is slightly slower than UploadStream call but is safe to call concurrently from multiple threads. Refer to: |
| 150 | +// https://github.com/Azure/azure-sdk-for-go/issues/18490#issuecomment-1170806877 |
| 151 | +func (ce *exporter) uploadManifest(ctx context.Context, manifestKey string, reader io.ReadSeekCloser) error { |
| 152 | + defer reader.Close() |
| 153 | + blobClient := ce.containerClient.NewBlockBlobClient(manifestKey) |
| 154 | + |
| 155 | + ctx, cnclFn := context.WithCancelCause(ctx) |
| 156 | + ctx, _ = context.WithTimeoutCause(ctx, time.Minute*5, errors.WithStack(context.DeadlineExceeded)) |
| 157 | + defer cnclFn(errors.WithStack(context.Canceled)) |
| 158 | + |
| 159 | + _, err := blobClient.Upload(ctx, reader, &blockblob.UploadOptions{}) |
| 160 | + if err != nil { |
| 161 | + return errors.Wrapf(err, "failed to upload blob %s: %v", manifestKey, err) |
| 162 | + } |
| 163 | + |
| 164 | + return nil |
| 165 | +} |
| 166 | + |
| 167 | +// For uploading blobs, use the UploadStream with access conditions which state that only upload if the blob |
| 168 | +// does not already exist. Since blobs are content addressable, this is the right thing to do for blobs and it gives |
| 169 | +// a performance improvement over the Upload API used for uploading manifests. |
| 170 | +func (ce *exporter) uploadBlobIfNotExists(ctx context.Context, blobKey string, reader io.Reader) error { |
| 171 | + blobClient := ce.containerClient.NewBlockBlobClient(blobKey) |
| 172 | + |
| 173 | + uploadCtx, cnclFn := context.WithCancelCause(ctx) |
| 174 | + uploadCtx, _ = context.WithTimeoutCause(uploadCtx, time.Minute*5, errors.WithStack(context.DeadlineExceeded)) |
| 175 | + defer cnclFn(errors.WithStack(context.Canceled)) |
| 176 | + |
| 177 | + // Only upload if the blob doesn't exist |
| 178 | + _, err := blobClient.UploadStream(uploadCtx, reader, &blockblob.UploadStreamOptions{ |
| 179 | + BlockSize: IOChunkSize, |
| 180 | + Concurrency: IOConcurrency, |
| 181 | + AccessConditions: &blob.AccessConditions{ |
| 182 | + ModifiedAccessConditions: &blob.ModifiedAccessConditions{ |
| 183 | + IfNoneMatch: to.Ptr(azcore.ETagAny), |
| 184 | + }, |
| 185 | + }, |
| 186 | + }) |
| 187 | + |
| 188 | + if err == nil { |
| 189 | + return nil |
| 190 | + } |
| 191 | + |
| 192 | + if bloberror.HasCode(err, bloberror.BlobAlreadyExists) { |
| 193 | + return nil |
| 194 | + } |
| 195 | + |
| 196 | + return errors.Wrapf(err, "failed to upload blob %s: %v", blobKey, err) |
| 197 | +} |
| 198 | + |
| 199 | +var _ io.ReadSeekCloser = &readSeekCloser{} |
| 200 | + |
| 201 | +type readSeekCloser struct { |
| 202 | + io.Reader |
| 203 | + io.Seeker |
| 204 | + io.Closer |
| 205 | +} |
| 206 | + |
| 207 | +func bytesToReadSeekCloser(dt []byte) io.ReadSeekCloser { |
| 208 | + bytesReader := bytes.NewReader(dt) |
| 209 | + return &readSeekCloser{ |
| 210 | + Reader: bytesReader, |
| 211 | + Seeker: bytesReader, |
| 212 | + Closer: io.NopCloser(bytesReader), |
| 213 | + } |
| 214 | +} |
0 commit comments