2015-02-17 11:28:33 -05:00
|
|
|
package docker
|
|
|
|
|
|
2015-03-27 18:18:52 -04:00
|
|
|
import (
|
2016-11-22 07:18:09 -05:00
|
|
|
"fmt"
|
2015-03-27 18:18:52 -04:00
|
|
|
"path/filepath"
|
2017-11-21 04:14:07 -05:00
|
|
|
"strings"
|
2015-03-27 18:18:52 -04:00
|
|
|
|
|
|
|
|
dc "github.com/fsouza/go-dockerclient"
|
|
|
|
|
)
|
2015-02-17 11:28:33 -05:00
|
|
|
|
2017-11-21 04:14:07 -05:00
|
|
|
// DockerConfig is the structure that stores the configuration to talk to a
|
2015-03-28 21:37:20 -04:00
|
|
|
// Docker API compatible host.
|
2017-11-21 04:14:07 -05:00
|
|
|
type DockerConfig struct {
|
2015-03-27 18:18:52 -04:00
|
|
|
Host string
|
2016-11-22 07:18:09 -05:00
|
|
|
Ca string
|
|
|
|
|
Cert string
|
|
|
|
|
Key string
|
2015-03-27 18:18:52 -04:00
|
|
|
CertPath string
|
2015-02-17 11:28:33 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// NewClient() returns a new Docker client.
|
2017-11-21 04:14:07 -05:00
|
|
|
func (c *DockerConfig) NewClient() (*dc.Client, error) {
|
2016-11-22 07:18:09 -05:00
|
|
|
if c.Ca != "" || c.Cert != "" || c.Key != "" {
|
|
|
|
|
if c.Ca == "" || c.Cert == "" || c.Key == "" {
|
|
|
|
|
return nil, fmt.Errorf("ca_material, cert_material, and key_material must be specified")
|
|
|
|
|
}
|
|
|
|
|
|
2016-12-17 07:41:08 -05:00
|
|
|
if c.CertPath != "" {
|
|
|
|
|
return nil, fmt.Errorf("cert_path must not be specified")
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-22 07:18:09 -05:00
|
|
|
return dc.NewTLSClientFromBytes(c.Host, []byte(c.Cert), []byte(c.Key), []byte(c.Ca))
|
2015-03-27 18:18:52 -04:00
|
|
|
}
|
|
|
|
|
|
2016-11-22 07:18:09 -05:00
|
|
|
if c.CertPath != "" {
|
|
|
|
|
// If there is cert information, load it and use it.
|
|
|
|
|
ca := filepath.Join(c.CertPath, "ca.pem")
|
|
|
|
|
cert := filepath.Join(c.CertPath, "cert.pem")
|
|
|
|
|
key := filepath.Join(c.CertPath, "key.pem")
|
|
|
|
|
return dc.NewTLSClient(c.Host, cert, key, ca)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// If there is no cert information, then just return the direct client
|
|
|
|
|
return dc.NewClient(c.Host)
|
2015-02-17 11:28:33 -05:00
|
|
|
}
|
|
|
|
|
|
2017-11-21 04:14:07 -05:00
|
|
|
// Data structure for holding data that we fetch from Docker.
|
2015-03-28 21:37:20 -04:00
|
|
|
type Data struct {
|
|
|
|
|
DockerImages map[string]*dc.APIImages
|
2015-02-17 11:28:33 -05:00
|
|
|
}
|
2017-11-21 04:14:07 -05:00
|
|
|
|
|
|
|
|
// ProviderConfig for the custom registry provider
|
|
|
|
|
type ProviderConfig struct {
|
|
|
|
|
DockerClient *dc.Client
|
|
|
|
|
AuthConfigs *dc.AuthConfigurations
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// The registry address can be referenced in various places (registry auth, docker config file, image name)
|
|
|
|
|
// with or without the http(s):// prefix; this function is used to standardize the inputs
|
|
|
|
|
func normalizeRegistryAddress(address string) string {
|
|
|
|
|
if !strings.HasPrefix(address, "https://") && !strings.HasPrefix(address, "http://") {
|
|
|
|
|
return "https://" + address
|
|
|
|
|
}
|
|
|
|
|
return address
|
|
|
|
|
}
|