github.com/docker/buildx@v0.14.1-0.20240514123050-afcb609966dc/store/storeutil/storeutil.go (about) 1 package storeutil 2 3 import ( 4 "bytes" 5 "os" 6 "strings" 7 8 "github.com/docker/buildx/store" 9 "github.com/docker/buildx/util/confutil" 10 "github.com/docker/buildx/util/dockerutil" 11 "github.com/docker/buildx/util/imagetools" 12 "github.com/docker/buildx/util/resolver" 13 "github.com/docker/cli/cli/command" 14 buildkitdconfig "github.com/moby/buildkit/cmd/buildkitd/config" 15 "github.com/pkg/errors" 16 ) 17 18 // GetStore returns current builder instance store 19 func GetStore(dockerCli command.Cli) (*store.Txn, func(), error) { 20 s, err := store.New(confutil.ConfigDir(dockerCli)) 21 if err != nil { 22 return nil, nil, err 23 } 24 return s.Txn() 25 } 26 27 func GetProxyConfig(dockerCli command.Cli) map[string]string { 28 cfg := dockerCli.ConfigFile() 29 host := dockerCli.Client().DaemonHost() 30 31 proxy, ok := cfg.Proxies[host] 32 if !ok { 33 proxy = cfg.Proxies["default"] 34 } 35 36 m := map[string]string{} 37 38 if v := proxy.HTTPProxy; v != "" { 39 m["HTTP_PROXY"] = v 40 } 41 if v := proxy.HTTPSProxy; v != "" { 42 m["HTTPS_PROXY"] = v 43 } 44 if v := proxy.NoProxy; v != "" { 45 m["NO_PROXY"] = v 46 } 47 if v := proxy.FTPProxy; v != "" { 48 m["FTP_PROXY"] = v 49 } 50 return m 51 } 52 53 // GetCurrentInstance finds the current builder instance 54 func GetCurrentInstance(txn *store.Txn, dockerCli command.Cli) (*store.NodeGroup, error) { 55 ep, err := dockerutil.GetCurrentEndpoint(dockerCli) 56 if err != nil { 57 return nil, err 58 } 59 ng, err := txn.Current(ep) 60 if err != nil { 61 return nil, err 62 } 63 if ng == nil { 64 ng, err = GetNodeGroup(txn, dockerCli, dockerCli.CurrentContext()) 65 if err != nil { 66 return nil, err 67 } 68 } 69 70 return ng, nil 71 } 72 73 // GetNodeGroup returns nodegroup based on the name 74 func GetNodeGroup(txn *store.Txn, dockerCli command.Cli, name string) (*store.NodeGroup, error) { 75 ng, err := txn.NodeGroupByName(name) 76 if err != nil { 77 if !os.IsNotExist(errors.Cause(err)) && !store.IsErrInvalidName(err) { 78 return nil, err 79 } 80 } 81 if ng != nil { 82 return ng, nil 83 } 84 85 list, err := dockerCli.ContextStore().List() 86 if err != nil { 87 return nil, err 88 } 89 for _, l := range list { 90 if l.Name == name { 91 ng = &store.NodeGroup{ 92 Name: name, 93 Nodes: []store.Node{ 94 { 95 Name: name, 96 Endpoint: name, 97 }, 98 }, 99 DockerContext: true, 100 } 101 if ng.LastActivity, err = txn.GetLastActivity(ng); err != nil { 102 return nil, err 103 } 104 return ng, nil 105 } 106 } 107 108 return nil, errors.Errorf("no builder %q found", name) 109 } 110 111 func GetImageConfig(dockerCli command.Cli, ng *store.NodeGroup) (opt imagetools.Opt, err error) { 112 opt.Auth = dockerCli.ConfigFile() 113 114 if ng == nil || len(ng.Nodes) == 0 { 115 return opt, nil 116 } 117 118 files := ng.Nodes[0].Files 119 120 dt, ok := files["buildkitd.toml"] 121 if !ok { 122 return opt, nil 123 } 124 125 config, err := buildkitdconfig.Load(bytes.NewReader(dt)) 126 if err != nil { 127 return opt, err 128 } 129 130 regconfig := make(map[string]resolver.RegistryConfig) 131 132 for k, v := range config.Registries { 133 rc := resolver.RegistryConfig{ 134 Mirrors: v.Mirrors, 135 PlainHTTP: v.PlainHTTP, 136 Insecure: v.Insecure, 137 } 138 for _, ca := range v.RootCAs { 139 dt, ok := files[strings.TrimPrefix(ca, confutil.DefaultBuildKitConfigDir+"/")] 140 if ok { 141 rc.RootCAs = append(rc.RootCAs, dt) 142 } 143 } 144 145 for _, kp := range v.KeyPairs { 146 key, keyok := files[strings.TrimPrefix(kp.Key, confutil.DefaultBuildKitConfigDir+"/")] 147 cert, certok := files[strings.TrimPrefix(kp.Certificate, confutil.DefaultBuildKitConfigDir+"/")] 148 if keyok && certok { 149 rc.KeyPairs = append(rc.KeyPairs, resolver.TLSKeyPair{ 150 Key: key, 151 Certificate: cert, 152 }) 153 } 154 } 155 regconfig[k] = rc 156 } 157 158 opt.RegistryConfig = regconfig 159 160 return opt, nil 161 }