Debian Codegraph @

docs/Targets.md

raw
1 # Targets and Content Stores in `oras-go` v2
2
3 > [!IMPORTANT]
4 > Prerequisite reading: [Modeling Artifact](./Modeling-Artifacts.md)
5
6 In `oras-go` v2, artifacts are modeled as [Directed Acyclic Graphs (DAGs)](https://en.wikipedia.org/wiki/Directed_acyclic_graph) stored in [Content-Addressable Storages (CASs)](https://en.wikipedia.org/wiki/Content-addressable_storage). Each node in the graph represents a [descriptor](https://github.com/opencontainers/image-spec/blob/v1.1.1/descriptor.md) of the content, which must include the following three properties:
7
8 - `mediaType`: The media type of the referenced content.
9 - `digest`: The digest of the targeted content.
10 - `size`: The size, in bytes, of the raw content.
11
12 An example of a descriptor for an [OCI Image Manifest](https://github.com/opencontainers/image-spec/blob/v1.1.1/manifest.md) is shown below:
13
14 ```json
15 {
16 "mediaType": "application/vnd.oci.image.manifest.v1+json",
17 "size": 7682,
18 "digest": "sha256:5b0bcabd1ed22e9fb1310cf6c2dec7cdef19f0ad69efa1f392e94a4333501270"
19 }
20 ```
21
22 ## Interfaces
23
24 `oras-go` v2 defines four major interfaces based on the graph-modeling concepts and descriptors: [`Storage`](#storage), [`GraphStorage`](#graphstorage), [`Target`](#target), and [`GraphTarget`](#graphtarget).
25
26 ### Storage
27
28 The [`Storage`](https://pkg.go.dev/oras.land/oras-go/v2/content#Storage) interface represents a content-addressable storage (CAS) where content is accessed via descriptors. It provides the following functions:
29
30 - `Fetch`: Retrieves the content identified by the descriptor from the CAS.
31 - `Exists`: Checks whether the described content is present in the CAS.
32 - `Push`: Adds content matching the expected descriptor to the CAS.
33
34 For example, consider the following graph stored in a `Storage`, where node names are aliases for descriptors:
35
36 ```mermaid
37 graph TD;
38
39 M0["Manifest m0"]--config-->Blob0["Blob b0"]
40 M0--layers-->Blob1["Blob b1"]
41 M0--layers-->Blob2["Blob b2"]
42 ```
43
44 The effects of the `Fetch` and `Exists` functions would be:
45
46 ```
47 Fetch(m0) == content_m0
48
49 Exists(b0) == true
50 Exists(b3) == false
51 ```
52
53 If a new blob `b3` is pushed to the storage, the graph would update as follows:
54
55 ```mermaid
56 graph TD;
57
58 M0["Manifest m0"]--config-->Blob0["Blob b0"]
59 M0--layers-->Blob1["Blob b1"]
60 M0--layers-->Blob2["Blob b2"]
61 Blob3["Blob b3"]
62 ```
63
64 ### GraphStorage
65
66 The [`GraphStorage`](https://pkg.go.dev/oras.land/oras-go/v2/content#GraphStorage) interface extends [`Storage`](#storage) by adding support for predecessor finding. It provides the following functions:
67
68 - `Fetch`
69 - `Exists`
70 - `Push`
71 - **`Predecessors`**: Finds the nodes directly pointing to a given node in the graph.
72
73 For the [same graph](#storage), the `Predecessors` function would act as follows:
74
75 ```
76 Predecessors(b0) == [m0]
77 Predecessors(m0) == []
78 ```
79
80 ### Target
81
82 The [`Target`](https://pkg.go.dev/oras.land/oras-go/v2#Target) interface represents a CAS with tagging capability. It provides the following functions:
83
84 - `Fetch`
85 - `Exists`
86 - `Push`
87 - **`Resolve`**: Resolves a tag string to a descriptor.
88 - **`Tag`**: Associates a descriptor with a tag string.
89
90 For example, consider a graph stored in a Target where `m0` is associated with two tags, `"foo"` and `"bar"`:
91
92 ```mermaid
93 graph TD;
94
95 M0["Manifest m0"]--config-->Blob0["Blob b0"]
96 M0--layers-->Blob1["Blob b1"]
97 M0--layers-->Blob2["Blob b2"]
98
99 TagFoo>"Tag: foo"]-.->M0
100 TagBar>"Tag: bar"]-.->M0
101 ```
102
103 The effects of the `Resolve` function would be:
104
105 ```
106 Resolve("foo") == m0
107 Resolve("bar") == m0
108 Resolve("hello") == nil
109 ```
110
111 If a new tag `"v1"` is added to `m0`, the graph would update as follows:
112
113 ```mermaid
114 graph TD;
115
116 M0["Manifest m0"]--config-->Blob0["Blob b0"]
117 M0--layers-->Blob1["Blob b1"]
118 M0--layers-->Blob2["Blob b2"]
119
120 TagFoo>"Tag: foo"]-.->M0
121 TagBar>"Tag: bar"]-.->M0
122 TagV1>"Tag: v1"]-.->M0
123 ```
124
125 A descriptor can have multiple tags, but each tag is associated with only one descriptor. Reusing a tag for a different descriptor removes its previous association.
126
127 If the tag `"bar"` is reused on another manifest `m1`, the graph would update as follows:
128
129 ```mermaid
130 graph TD;
131
132 M0["Manifest m0"]--config-->Blob0["Blob b0"]
133 M0--layers-->Blob1["Blob b1"]
134 M0--layers-->Blob2["Blob b2"]
135 M1["Manifest m1"]
136
137 TagFoo>"Tag: foo"]-.->M0
138 TagV1>"Tag: v1"]-.->M0
139 TagBar>"Tag: bar"]-.->M1
140 ```
141
142 ### GraphTarget
143
144 The [`GraphTarget`](https://pkg.go.dev/oras.land/oras-go/v2#GraphTarget) interface combines the capabilities of [`GraphStorage`](#graphstorage) and [`Target`](#target). It provides the following functions:
145
146 - `Fetch`
147 - `Exists`
148 - `Push`
149 - `Resolve`
150 - `Tag`
151 - `Predecessors`
152
153 ## Content Stores
154
155 In `oras-go` v2, a content store is an implementation of the [`Target`](#target) interface—specifically, the [`GraphTarget`](#graphtarget) interface.
156
157 The library provides four built-in content stores:
158
159 - [Memory Store](#memory-store): Stores everything in memory.
160 - [OCI Store](#oci-store): Stores content in the [OCI-Image layout](https://github.com/opencontainers/image-spec/blob/v1.1.1/image-layout.md) on the file system.
161 - [File Store](#file-store): Stores location-addressable content on the file system.
162 - [Repository Store](#repository-store): Communicates with remote artifact repositories (e.g. `ghcr.io`, `docker.io`).
163
164 ### Memory Store
165
166 The memory store, available in the [`content/memory`](https://pkg.go.dev/oras.land/oras-go/v2/content/memory) package, stores all content in memory, where each blob's content is mapped to its corresponding descriptor.
167
168 > [!TIP]
169 > The memory store is often used for building and storing artifacts in memory before copying them to other stores, such as remote repositories.
170
171 ### OCI Store
172
173 The OCI store, available in the [`content/oci`](https://pkg.go.dev/oras.land/oras-go/v2/content/oci) package, follows the OCI [`image-spec v1.1.1`](https://github.com/opencontainers/image-spec/blob/v1.1.1/image-layout.md) to store blob content on the file system.
174
175 For example, consider an artifact and its signature represented by the following graph:
176
177 ```mermaid
178 graph TD;
179
180 SignatureManifest["Signature Manifest<br>(sha256:e5727b...)"]--subject-->Manifest
181 SignatureManifest--config-->Config
182 SignatureManifest--layers-->SignatureBlob["Signature blob<br>(sha256:37f884)"]
183
184 Manifest["Manifest<br>(sha256:314c7f...)"]--config-->Config["Config blob<br>(sha256:44136f...)"]
185 Manifest--layers-->Layer0["Layer blob 0<br>(sha256:b5bb9d...)"]
186 Manifest--layers-->Layer1["Layer blob 1<br>(sha256:7d865e...)"]
187 ```
188
189 The corresponding directory structure on the file system would look like this:
190
191 ```bash
192 $ tree repo
193 repo/
194 ├── blobs
195 │   └── sha256
196 │   ├── 314c7f20dd44ee1cca06af399a67f7c463a9f586830d630802d9e365933da9fb
197 │   ├── 37f88486592fd90ace303ee38f8d1ff698193e76c76d3c1fef8627a39e677696
198 │   ├── 44136fa355b3678a1146ad16f7e8649e94fb4fc21fe77e8310c060f61caaff8a
199 │   ├── 7d865e959b2466918c9863afca942d0fb89d7c9ac0c99bafc3749504ded97730
200 │   ├── b5bb9d8014a0f9b1d61e21e796d78dccdf1352f23cd32812f4850b878ae4944c
201 │   └── e5727bebbcbbd9996446c34622ca96af67a54219edd58d261112f1af06e2537c
202 ├── index.json
203 ├── ingest
204 └── oci-layout
205 ```
206
207 In the layout:
208
209 - All content, whether manifests or layer blobs, are all placed under the `blobs` directory.
210 - The path to each piece of content is determined by its digest.
211 - The `index.json` file is an [Image Index](https://github.com/opencontainers/image-spec/blob/v1.1.1/image-layout.md#index-example) JSON object. It serves as the entry point for the graph and provides tagging functionality.
212 - The `ingest` directory is used temporarily during blob processing. It can be safely removed after the push operation and should be cleaned up before creating a tar archive of the OCI layout. This directory is not defined in the OCI specification.
213 - The `oci-layout` file is a marker of the base of the OCI Layout.
214
215 The OCI Layout offers several advantages:
216
217 - It is fully compliant with OCI `image-spec v1.1.1`, ensuring compatibility with tools beyond ORAS.
218 - Its clean and straightforward structure makes it easy to manage and replicate.
219
220 > [!TIP]
221 > The OCI store is a practical option for maintaining a local copy of a remote repository.
222
223 ### File Store
224
225 The file store, available in the [`content/file`](https://pkg.go.dev/oras.land/oras-go/v2/content/file) package, supports both content-addressable and location-addressable storage. It is designed for packaging arbitrary files or directories and allows adding them directly from the local file system.
226
227 When a file or directory is added, the file store creates a descriptor containing annotations with essential metadata. The process differs depending on whether a file or a directory is added:
228
229 - **File Addition**: When a file is added, its contents are stored as a blob. A descriptor is generated from the blob with an `"org.opencontainers.image.title"` annotation indicating the original file name.
230 - **Directory Addition**: When a directory is added, it is first tar-archived and compressed into a blob. The descriptor generated for a directory includes multiple annotations:
231 - `"org.opencontainers.image.title"`: Indicates the original directory name.
232 - `"io.deis.oras.content.digest"`**(ORAS-specific)**: Represents the digest of the tar'ed content before compression.
233 - `"io.deis.oras.content.unpack"`**(ORAS-specific)**: A flag indicating that the blob represents a directory that needs to be decompressed and un-tar'ed.
234
235 For example, consider the following directory structure on disk:
236
237 ```bash
238 $ tree
239 .
240 ├── hello.txt
241 └── mydir
242 ├── bar.txt
243 └── foo.txt
244
245 $ cat hello.txt
246 hello
247
248 $ cat mydir/foo.txt
249 foo
250
251 $ cat mydir/bar.txt
252 bar
253 ```
254
255 Adding the file `hello.txt` results in a blob with a descriptor similar to this:
256
257 ```json
258 {
259 "mediaType": "application/vnd.custom",
260 "digest": "sha256:5891b5b522d5df086d0ff0b110fbd9d21bb4fc7163af34d08286a2e846f6be03",
261 "size": 6,
262 "annotations": {
263 "org.opencontainers.image.title": "hello.txt"
264 }
265 }
266 ```
267
268 Adding the directory `mydir` results in a tar-archived and compressed blob with a descriptor similar to this:
269
270 ```json
271 {
272 "mediaType": "application/vnd.custom.tar+gzip",
273 "digest": "sha256:b14fa80f5afd3822dce52a711566586c8f89e5dc211e4b0d2819f219b102fe7a",
274 "size": 164,
275 "annotations": {
276 "io.deis.oras.content.digest": "sha256:9d418a0549e5bc45d195aacec889d30e520b9f2bd9feab57d57de6d9cdd66172",
277 "io.deis.oras.content.unpack": "true",
278 "org.opencontainers.image.title": "mydir"
279 }
280 }
281 ```
282
283 To create an artifact, a manifest needs to be [packed](https://pkg.go.dev/oras.land/oras-go/v2#PackManifest) to reference the two blobs and will also be stored in the file store. The manifest content might look like this:
284
285 ```json
286 {
287 "schemaVersion": 2,
288 "mediaType": "application/vnd.oci.image.manifest.v1+json",
289 "artifactType": "application/vnd.example",
290 "config": {
291 "mediaType": "application/vnd.oci.empty.v1+json",
292 "digest": "sha256:44136fa355b3678a1146ad16f7e8649e94fb4fc21fe77e8310c060f61caaff8a",
293 "size": 2,
294 "data": "e30="
295 },
296 "layers": [
297 {
298 "mediaType": "application/vnd.custom",
299 "digest": "sha256:5891b5b522d5df086d0ff0b110fbd9d21bb4fc7163af34d08286a2e846f6be03",
300 "size": 6,
301 "annotations": {
302 "org.opencontainers.image.title": "hello.txt"
303 }
304 },
305 {
306 "mediaType": "application/vnd.custom.tar+gzip",
307 "digest": "sha256:b14fa80f5afd3822dce52a711566586c8f89e5dc211e4b0d2819f219b102fe7a",
308 "size": 164,
309 "annotations": {
310 "io.deis.oras.content.digest": "sha256:9d418a0549e5bc45d195aacec889d30e520b9f2bd9feab57d57de6d9cdd66172",
311 "io.deis.oras.content.unpack": "true",
312 "org.opencontainers.image.title": "mydir"
313 }
314 }
315 ],
316 "annotations": {
317 "org.opencontainers.image.created": "2025-03-07T08:34:23Z"
318 }
319 }
320 ```
321
322 In the file store, blobs with names are location-addressed by file paths, while other content (e.g., manifests and config blobs) is maintained in fallback storage. By default, the fallback storage is a limited in-memory CAS store.
323
324 For the above example, the graph stored in the file store would be like this:
325
326 ```mermaid
327 graph TD;
328
329 Manifest["Manifest<br>(in memory)"]--config-->Config["Config blob<br>(in memory)"]
330 Manifest--layers-->Layer0["hello.txt<br>(on disk)"]
331 Manifest--layers-->Layer1["mydir<br>(on disk)"]
332 ```
333
334 Unlike the OCI store, the file store only persists named contents (e.g., `hello.txt` and `mydir`) on disk, while all metadata is stored in memory.
335
336 > [!IMPORTANT]
337 > Once the file store is terminated, it cannot be restored to its original state from the file system.
338
339 ### Repository Store
340
341 The repository store, available in the [`registry/remote`](https://pkg.go.dev/oras.land/oras-go/v2/registry/remote) package, communicates with remote artifact repositories using APIs defined in the [OCI distribution-spec v1.1.1](https://github.com/opencontainers/distribution-spec/blob/v1.1.1/spec.md).
342
343 Unlike other content stores, the repository store handles manifests and non-manifest blobs separately. This distinction exists because the URI paths for manifests and blobs differ, with manifests accessed via `/v2/<name>/manifests/` and blobs accessed via `/v2/<name>/blobs/`.
344
345 The repository store manages manifests through the `ManifestStore` sub-store and handles blobs through the `BlobStore` sub-store. It automatically determines which sub-store to use based on the media type specified in the descriptor.
346
347 It is important to note that, the `ManifestStore` implements the `Predecessors` function based on the [Referrers API](https://github.com/opencontainers/distribution-spec/blob/v1.1.1/spec.md#listing-referrers) or, when the Referrers API is unavailable, [`Referrers Tag Schema`](https://github.com/opencontainers/distribution-spec/blob/v1.1.1/spec.md#unavailable-referrers-api). However, both approaches only support referrer discovery and do not provide generic predecessor finding.
348
349 Below is a mapping of major repository functions to their corresponding registry API endpoints:
350
351 #### Manifest Store Mappings
352
353 | Function Name | API endpoint |
354 | -------------- | -------------------------------------------------------------------------------------------------------------------- |
355 | `Fetch` | GET `/v2/<name>/manifests/<reference>` |
356 | `Exists` | HEAD `/v2/<name>/manifests/<reference>` |
357 | `Push` | PUT `/v2/<name>/manifests/<reference>` |
358 | `Resolve` | HEAD `/v2/<name>/manifests/<reference>` |
359 | `Tag` | PUT `/v2/<name>/manifests/<reference>` |
360 | `Predecessors` | GET `/v2/<name>/referrers/<digest>?artifactType=<artifactType>`<br>Fallback to `Referrers Tag Schema` if unavailable |
361
362 #### Blob Store Mappings
363
364 | Function Name | API endpoint |
365 | ------------- | ---------------------------------------------------------------------------------------------- |
366 | `Fetch` | GET `/v2/<name>/blobs/<reference>` |
367 | `Exists` | HEAD `/v2/<name>/blobs/<reference>` |
368 | `Push` | POST `/v2/<name>/blobs/uploads/`<br>PUT `/v2/<name>/blobs/uploads/<reference>?digest=<digest>` |
369 | `Resolve` | HEAD `/v2/<name>/blobs/<reference>` |
370
371 ### Summary of Content Stores
372
373 | Name | Description | Persistent Storage | Predecessors Support | Scenarios |
374 | ---------------- | ---------------------------------------------------------------------------------- | ------------------------------ | --------------------------- | ------------------------------------------ |
375 | Memory Store | Stores everything in memory | No | Yes | Memory caching, testing |
376 | OCI Store | Stores content in OCI-Image layout on the file system | Yes | Yes | Local cache or copy of remote repositories |
377 | File Store | Stores location-addressable content on file system | Partial (For named blobs only) | Yes | Packaging arbitrary files |
378 | Repository Store | Communicates with remote artifact repositories (e.g. `ghcr.io`, `docker.io`, etc.) | Yes | Partial (via Referrers API) | Accessing remote repositories |
379
380 ### How to Choose the Appropriate Content Store
381
382 ```mermaid
383 flowchart TD;
384
385 Q1{"Access remote repository?"}
386 Q1--Y-->Repository["Repository Store"]
387 Q1--N-->Q2{"Reading/writing arbitrary files?"}
388 Q2--Y-->File["File Store"]
389 Q2--N-->Q3{"Need persistent storage?"}
390 Q3--Y-->OCI["OCI Store"]
391 Q3--N-->Memory["Memory Store"]
392 ```