|
| 1 | +package docker |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/base64" |
| 6 | + "encoding/json" |
| 7 | + "fmt" |
| 8 | + |
| 9 | + "github.com/covexo/devspace/pkg/util/log" |
| 10 | + "github.com/docker/docker/api/types" |
| 11 | + "github.com/docker/docker/client" |
| 12 | + "github.com/docker/docker/registry" |
| 13 | +) |
| 14 | + |
| 15 | +func getOfficialServer(ctx context.Context, client client.CommonAPIClient) string { |
| 16 | + // The daemon `/info` endpoint informs us of the default registry being |
| 17 | + // used. This is essential in cross-platforms environment, where for |
| 18 | + // example a Linux client might be interacting with a Windows daemon, hence |
| 19 | + // the default registry URL might be Windows specific. |
| 20 | + serverAddress := registry.IndexServer |
| 21 | + if info, err := client.Info(ctx); err != nil { |
| 22 | + // Only report the warning if we're in debug mode to prevent nagging during engine initialization workflows |
| 23 | + fmt.Fprintf(log.GetInstance(), "Warning: failed to get default registry endpoint from daemon (%v). Using system default: %s\n", err, serverAddress) |
| 24 | + } else if info.IndexServerAddress == "" { |
| 25 | + fmt.Fprintf(log.GetInstance(), "Warning: Empty registry endpoint from daemon. Using system default: %s\n", serverAddress) |
| 26 | + } else { |
| 27 | + serverAddress = info.IndexServerAddress |
| 28 | + } |
| 29 | + |
| 30 | + return serverAddress |
| 31 | +} |
| 32 | + |
| 33 | +func encodeAuthToBase64(authConfig types.AuthConfig) (string, error) { |
| 34 | + buf, err := json.Marshal(authConfig) |
| 35 | + if err != nil { |
| 36 | + return "", err |
| 37 | + } |
| 38 | + return base64.URLEncoding.EncodeToString(buf), nil |
| 39 | +} |
| 40 | + |
| 41 | +func getDefaultAuthConfig(client client.CommonAPIClient, checkCredStore bool, serverAddress string, isDefaultRegistry bool) (*types.AuthConfig, error) { |
| 42 | + var authconfig types.AuthConfig |
| 43 | + var err error |
| 44 | + |
| 45 | + configfile, _ := loadDockerConfig() |
| 46 | + |
| 47 | + if !isDefaultRegistry { |
| 48 | + serverAddress = registry.ConvertToHostname(serverAddress) |
| 49 | + } |
| 50 | + |
| 51 | + if checkCredStore { |
| 52 | + authconfig, err = configfile.GetAuthConfig(serverAddress) |
| 53 | + } else { |
| 54 | + authconfig = types.AuthConfig{} |
| 55 | + } |
| 56 | + |
| 57 | + authconfig.ServerAddress = serverAddress |
| 58 | + authconfig.IdentityToken = "" |
| 59 | + return &authconfig, err |
| 60 | +} |
0 commit comments