-
Notifications
You must be signed in to change notification settings - Fork 61
/
index.go
197 lines (161 loc) · 5.2 KB
/
index.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
// SPDX-License-Identifier: BSD-3-Clause
// Copyright (c) 2022, Unikraft GmbH and The KraftKit Authors.
// Licensed under the BSD-3-Clause License (the "License").
// You may not use this file except in compliance with the License.
package oci
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"time"
"github.com/containerd/containerd/errdefs"
"github.com/containerd/containerd/images"
"github.com/google/go-containerregistry/pkg/name"
specs "github.com/opencontainers/image-spec/specs-go"
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
"oras.land/oras-go/v2/content"
"kraftkit.sh/oci/handler"
)
type Index struct {
handle handler.Handler
index *ocispec.Index
desc *ocispec.Descriptor
manifests []*Manifest
annotations map[string]string
}
// NewIndex instantiates a new index using the provided handler.
func NewIndex(_ context.Context, handle handler.Handler) (*Index, error) {
if handle == nil {
return nil, fmt.Errorf("cannot use `NewIndex` without handler")
}
index := Index{
handle: handle,
}
return &index, nil
}
// NewIndexFromSpec instantiates a new index using the provided handler as well
// as a reference
func NewIndexFromSpec(ctx context.Context, handle handler.Handler, spec *ocispec.Index) (*Index, error) {
index, err := NewIndex(ctx, handle)
if err != nil {
return nil, err
}
index.index = spec
for _, desc := range spec.Manifests {
manifest, err := NewManifestFromDigest(ctx, handle, desc.Digest)
if err != nil {
return nil, fmt.Errorf("could not instantiate manifest from structure: %w", err)
}
index.manifests = append(index.manifests, manifest)
}
return index, nil
}
// NewIndexFromRef instantiates a new index using the provided reference which
// is used by the handle to look up any local existing indexes.
func NewIndexFromRef(ctx context.Context, handle handler.Handler, ref string) (*Index, error) {
indexes, err := handle.ListIndexes(ctx)
if err != nil {
return nil, fmt.Errorf("could not retrieve list of existing indexes: %w", err)
}
for fullref, index := range indexes {
if ref == fullref {
return NewIndexFromSpec(ctx, handle, index)
}
}
return nil, fmt.Errorf("cannot instantiate index from unknown reference '%s'", ref)
}
// SetAnnotation sets an anootation of the image with the provided key.
func (index *Index) SetAnnotation(_ context.Context, key, val string) {
if index.annotations == nil {
index.annotations = make(map[string]string)
}
index.annotations[key] = val
}
// Annotations returns the map of annotations for the index.
func (index *Index) Annotations() map[string]string {
return index.annotations
}
// IndexDesc returns the descriptor of the index.
func (index *Index) Descriptor() (*ocispec.Descriptor, error) {
indexJson, err := json.Marshal(index.index)
if err != nil {
return nil, fmt.Errorf("failed to marshal manifest: %w", err)
}
desc := content.NewDescriptorFromBytes(
ocispec.MediaTypeImageIndex,
indexJson,
)
desc.Annotations = index.Annotations()
return &desc, nil
}
// AddManifest adds a manifest based an previously instantiated Manifest
// structure.
func (index *Index) AddManifest(_ context.Context, manifest *Manifest) error {
if index.manifests == nil {
index.manifests = []*Manifest{}
}
index.manifests = append(index.manifests, manifest)
return nil
}
// Save the index.
func (index *Index) Save(ctx context.Context, fullref string, onProgress func(float64)) (ocispec.Descriptor, error) {
ref, err := name.ParseReference(fullref,
name.WithDefaultRegistry(""),
)
if err != nil {
return ocispec.Descriptor{}, err
}
// Pack the given blobs which generates an image manifest for the pack, and
// pushes it to a content storage.
if index.annotations == nil {
index.annotations = make(map[string]string)
}
// General annotations
index.annotations[ocispec.AnnotationRefName] = ref.Context().String()
index.annotations[ocispec.AnnotationCreated] = time.Now().UTC().Format(time.RFC3339)
// containerd compatibility annotations
index.annotations[images.AnnotationImageName] = ref.String()
manifestDescs := make([]ocispec.Descriptor, len(index.manifests))
for i, manifest := range index.manifests {
desc := manifest.desc
if !manifest.saved {
desc, err = manifest.Save(ctx, fullref, nil)
if err != nil {
return ocispec.Descriptor{}, fmt.Errorf("could not save manifest: %w", err)
}
}
manifestDescs[i] = *desc
}
// Generate the final manifest
index.index = &ocispec.Index{
MediaType: ocispec.MediaTypeImageIndex,
Versioned: specs.Versioned{
SchemaVersion: 2,
},
Manifests: manifestDescs,
Annotations: index.annotations,
}
indexJson, err := json.Marshal(index.index)
if err != nil {
return ocispec.Descriptor{}, fmt.Errorf("failed to marshal manifest: %w", err)
}
indexDesc := content.NewDescriptorFromBytes(
ocispec.MediaTypeImageIndex,
indexJson,
)
indexDesc.Annotations = index.index.Annotations
// save the manifest digest
if err := index.handle.SaveDescriptor(
ctx,
fullref,
indexDesc,
bytes.NewReader(indexJson),
onProgress,
); err != nil && !errors.Is(err, errdefs.ErrAlreadyExists) {
return ocispec.Descriptor{}, fmt.Errorf("failed to push manifest: %w", err)
}
index.desc = &indexDesc
return indexDesc, nil
}