MCPcopy Index your code
hub / github.com/linuxkit/linuxkit / OCITarReader

Method OCITarReader

src/cmd/linuxkit/cache/indexsource.go:58–156  ·  view source on GitHub ↗

OCITarReader return an io.ReadCloser to read the image as a v1 tarball whose contents match OCI v1 layout spec

(overrideName string)

Source from the content-addressed store, hash-verified

56
57// OCITarReader return an io.ReadCloser to read the image as a v1 tarball whose contents match OCI v1 layout spec
58func (c IndexSource) OCITarReader(overrideName string) (io.ReadCloser, error) {
59 imageName := c.ref.String()
60 saveName := imageName
61 if overrideName != "" {
62 saveName = overrideName
63 }
64 refName, err := name.ParseReference(saveName)
65 if err != nil {
66 return nil, fmt.Errorf("error parsing image name: %v", err)
67 }
68 // get a reference to the image
69 index, err := c.provider.findIndex(c.ref.String())
70 if err != nil {
71 return nil, err
72 }
73 // convert the writer to a reader
74 r, w := io.Pipe()
75 go func() {
76 defer func() { _ = w.Close() }()
77 tw := tar.NewWriter(w)
78 defer func() { _ = tw.Close() }()
79 if err := writeLayoutHeader(tw); err != nil {
80 _ = w.CloseWithError(err)
81 return
82 }
83
84 manifests, err := index.IndexManifest()
85 if err != nil {
86 _ = w.CloseWithError(err)
87 return
88 }
89 // for each manifest, write the manifest blob, then go through each manifest and find the image for it
90 // and write its blobs
91 for _, manifest := range manifests.Manifests {
92 // if we restricted this image source to certain platforms, we should only write those
93 if len(c.platforms) > 0 {
94 found := false
95 for _, platform := range c.platforms {
96 if platform.Architecture == manifest.Platform.Architecture && platform.OS == manifest.Platform.OS &&
97 (platform.Variant == "" || platform.Variant == manifest.Platform.Variant) {
98 found = true
99 break
100 }
101 }
102 if !found {
103 continue
104 }
105 }
106 switch manifest.MediaType {
107 case types.OCIManifestSchema1, types.DockerManifestSchema2:
108 // this is an image manifest
109 image, err := index.Image(manifest.Digest)
110 if err != nil {
111 _ = w.CloseWithError(err)
112 return
113 }
114 if err := writeLayoutImage(tw, image); err != nil {
115 _ = w.CloseWithError(err)

Callers

nothing calls this directly

Calls 9

CloseMethod · 0.95
writeLayoutHeaderFunction · 0.85
writeLayoutImageFunction · 0.85
writeLayoutBlobFunction · 0.85
writeLayoutIndexFunction · 0.85
findIndexMethod · 0.80
StringMethod · 0.65
ImageMethod · 0.65
DigestMethod · 0.65

Tested by

no test coverage detected