New returns a new *composer.Composer.
(client *containerd.Client, globalOptions types.GlobalCommandOptions, options composer.Options, stdout, stderr io.Writer)
| 48 | |
| 49 | // New returns a new *composer.Composer. |
| 50 | func New(client *containerd.Client, globalOptions types.GlobalCommandOptions, options composer.Options, stdout, stderr io.Writer) (*composer.Composer, error) { |
| 51 | if err := composer.Lock(globalOptions.DataRoot, globalOptions.Address); err != nil { |
| 52 | return nil, err |
| 53 | } |
| 54 | |
| 55 | cniEnv, err := netutil.NewCNIEnv(globalOptions.CNIPath, globalOptions.CNINetConfPath, netutil.WithNamespace(globalOptions.Namespace), netutil.WithDefaultNetwork(globalOptions.BridgeIP)) |
| 56 | if err != nil { |
| 57 | return nil, err |
| 58 | } |
| 59 | networkConfigs, err := cniEnv.NetworkList() |
| 60 | if err != nil { |
| 61 | return nil, err |
| 62 | } |
| 63 | options.NetworkExists = func(netName string) (bool, error) { |
| 64 | for _, f := range networkConfigs { |
| 65 | if f.Name == netName { |
| 66 | return true, nil |
| 67 | } |
| 68 | } |
| 69 | return false, nil |
| 70 | } |
| 71 | |
| 72 | options.NetworkInUse = func(ctx context.Context, netName string) (bool, error) { |
| 73 | networkUsedByNsMap, err := netutil.UsedNetworks(ctx, client) |
| 74 | if err != nil { |
| 75 | return false, err |
| 76 | } |
| 77 | for _, v := range networkUsedByNsMap { |
| 78 | if strutil.InStringSlice(v, netName) { |
| 79 | return true, nil |
| 80 | } |
| 81 | } |
| 82 | return false, nil |
| 83 | } |
| 84 | |
| 85 | volStore, err := volume.Store(globalOptions.Namespace, globalOptions.DataRoot, globalOptions.Address) |
| 86 | if err != nil { |
| 87 | return nil, err |
| 88 | } |
| 89 | // FIXME: this is racy. See note in up_volume.go |
| 90 | options.VolumeExists = volStore.Exists |
| 91 | |
| 92 | options.ImageExists = func(ctx context.Context, rawRef string) (bool, error) { |
| 93 | parsedReference, err := referenceutil.Parse(rawRef) |
| 94 | if err != nil { |
| 95 | return false, err |
| 96 | } |
| 97 | ref := parsedReference.String() |
| 98 | if _, err := client.ImageService().Get(ctx, ref); err != nil { |
| 99 | if errors.Is(err, errdefs.ErrNotFound) { |
| 100 | return false, nil |
| 101 | } |
| 102 | return false, err |
| 103 | } |
| 104 | return true, nil |
| 105 | } |
| 106 | |
| 107 | options.EnsureImage = func(ctx context.Context, imageName, pullMode, platform string, ps *serviceparser.Service, quiet bool) error { |
no test coverage detected