github.com/containerd/nerdctl@v1.7.7/pkg/clientutil/client.go (about) 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 clientutil 18 19 import ( 20 "context" 21 "fmt" 22 "os" 23 "path/filepath" 24 "runtime" 25 "strings" 26 27 "github.com/containerd/containerd" 28 "github.com/containerd/containerd/namespaces" 29 "github.com/containerd/log" 30 "github.com/containerd/nerdctl/pkg/platformutil" 31 "github.com/containerd/nerdctl/pkg/systemutil" 32 "github.com/containerd/platforms" 33 "github.com/opencontainers/go-digest" 34 ) 35 36 func NewClient(ctx context.Context, namespace, address string, opts ...containerd.ClientOpt) (*containerd.Client, context.Context, context.CancelFunc, error) { 37 38 ctx = namespaces.WithNamespace(ctx, namespace) 39 40 address = strings.TrimPrefix(address, "unix://") 41 const dockerContainerdaddress = "/var/run/docker/containerd/containerd.sock" 42 if err := systemutil.IsSocketAccessible(address); err != nil { 43 if systemutil.IsSocketAccessible(dockerContainerdaddress) == nil { 44 err = fmt.Errorf("cannot access containerd socket %q (hint: try running with `--address %s` to connect to Docker-managed containerd): %w", address, dockerContainerdaddress, err) 45 } else { 46 err = fmt.Errorf("cannot access containerd socket %q: %w", address, err) 47 } 48 return nil, nil, nil, err 49 } 50 client, err := containerd.New(address, opts...) 51 if err != nil { 52 return nil, nil, nil, err 53 } 54 var cancel context.CancelFunc 55 ctx, cancel = context.WithCancel(ctx) 56 return client, ctx, cancel, nil 57 } 58 59 func NewClientWithPlatform(ctx context.Context, namespace, address, platform string, clientOpts ...containerd.ClientOpt) (*containerd.Client, context.Context, context.CancelFunc, error) { 60 if platform != "" { 61 if canExec, canExecErr := platformutil.CanExecProbably(platform); !canExec { 62 warn := fmt.Sprintf("Platform %q seems incompatible with the host platform %q. If you see \"exec format error\", see https://github.com/containerd/nerdctl/blob/main/docs/multi-platform.md", 63 platform, platforms.DefaultString()) 64 if canExecErr != nil { 65 log.L.WithError(canExecErr).Warn(warn) 66 } else { 67 log.L.Warn(warn) 68 } 69 } 70 platformParsed, err := platforms.Parse(platform) 71 if err != nil { 72 return nil, nil, nil, err 73 } 74 platformM := platforms.Only(platformParsed) 75 clientOpts = append(clientOpts, containerd.WithDefaultPlatform(platformM)) 76 } 77 return NewClient(ctx, namespace, address, clientOpts...) 78 } 79 80 // DataStore returns a string like "/var/lib/nerdctl/1935db59". 81 // "1935db9" is from `$(echo -n "/run/containerd/containerd.sock" | sha256sum | cut -c1-8)` 82 // on Windows it will return "%PROGRAMFILES%/nerdctl/1935db59" 83 func DataStore(dataRoot, address string) (string, error) { 84 if err := os.MkdirAll(dataRoot, 0700); err != nil { 85 return "", err 86 } 87 addrHash, err := getAddrHash(address) 88 if err != nil { 89 return "", err 90 } 91 dataStore := filepath.Join(dataRoot, addrHash) 92 if err := os.MkdirAll(dataStore, 0700); err != nil { 93 return "", err 94 } 95 return dataStore, nil 96 } 97 98 func getAddrHash(addr string) (string, error) { 99 const addrHashLen = 8 100 101 if runtime.GOOS != "windows" { 102 addr = strings.TrimPrefix(addr, "unix://") 103 104 var err error 105 addr, err = filepath.EvalSymlinks(addr) 106 if err != nil { 107 return "", err 108 } 109 } 110 111 d := digest.SHA256.FromString(addr) 112 h := d.Encoded()[0:addrHashLen] 113 return h, nil 114 }