Debian Codegraph @

ipfs/converter.go

raw
1 /*
2 Copyright The containerd Authors.
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15 */
16
17 package ipfs
18
19 import (
20 "bytes"
21 "context"
22 "encoding/json"
23 "fmt"
24 "strings"
25 "os"
26
27 "github.com/containerd/containerd"
28 "github.com/containerd/containerd/content"
29 "github.com/containerd/containerd/images/converter"
30 "github.com/containerd/containerd/platforms"
31 ipfsclient "github.com/containerd/stargz-snapshotter/ipfs/client"
32 ocispec "github.com/opencontainers/image-spec/specs-go/v1"
33 )
34
35 // Push pushes the provided image ref to IPFS with converting it to IPFS-enabled format.
36 func Push(ctx context.Context, client *containerd.Client, ref string, layerConvert converter.ConvertFunc, platformMC platforms.MatchComparer) (cidV1 string, _ error) {
37 return PushWithIPFSPath(ctx, client, ref, layerConvert, platformMC, nil)
38 }
39
40 func PushWithIPFSPath(ctx context.Context, client *containerd.Client, ref string, layerConvert converter.ConvertFunc, platformMC platforms.MatchComparer, ipfsPath *string) (cidV1 string, _ error) {
41 ctx, done, err := client.WithLease(ctx)
42 if err != nil {
43 return "", err
44 }
45 defer done(ctx)
46 img, err := client.ImageService().Get(ctx, ref)
47 if err != nil {
48 return "", err
49 }
50 var ipath string
51 if idir := os.Getenv("IPFS_PATH"); idir != "" {
52 ipath = idir
53 }
54 if ipfsPath != nil {
55 ipath = *ipfsPath
56 }
57 // HTTP is only supported as of now. We can add https support here if needed (e.g. for connecting to it via proxy, etc)
58 iurl, err := ipfsclient.GetIPFSAPIAddress(ipath, "http")
59 if err != nil {
60 return "", err
61 }
62 iclient := ipfsclient.New(iurl)
63 desc, err := converter.IndexConvertFuncWithHook(layerConvert, true, platformMC, converter.ConvertHooks{
64 PostConvertHook: pushBlobHook(iclient),
65 })(ctx, client.ContentStore(), img.Target)
66 if err != nil {
67 return "", err
68 }
69 root, err := json.Marshal(desc)
70 if err != nil {
71 return "", err
72 }
73 return iclient.Add(bytes.NewReader(root))
74 }
75
76 func pushBlobHook(client *ipfsclient.Client) converter.ConvertHookFunc {
77 return func(ctx context.Context, cs content.Store, desc ocispec.Descriptor, newDesc *ocispec.Descriptor) (*ocispec.Descriptor, error) {
78 resultDesc := newDesc
79 if resultDesc == nil {
80 descCopy := desc
81 resultDesc = &descCopy
82 }
83 ra, err := cs.ReaderAt(ctx, *resultDesc)
84 if err != nil {
85 return nil, err
86 }
87 cidv1, err := client.Add(content.NewReader(ra))
88 if err != nil {
89 return nil, err
90 }
91 resultDesc.URLs = []string{"ipfs://" + cidv1}
92 return resultDesc, nil
93 }
94 }
95
96 func GetCID(desc ocispec.Descriptor) (string, error) {
97 for _, u := range desc.URLs {
98 if strings.HasPrefix(u, "ipfs://") {
99 return u[7:], nil
100 }
101 }
102 return "", fmt.Errorf("no CID is recorded")
103 }