OCITarReader return an io.ReadCloser to read the image as a v1 tarball whose contents match OCI v1 layout spec
(overrideName string)
| 56 | |
| 57 | // OCITarReader return an io.ReadCloser to read the image as a v1 tarball whose contents match OCI v1 layout spec |
| 58 | func (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) |
nothing calls this directly
no test coverage detected