-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfactory.go
85 lines (73 loc) · 1.99 KB
/
factory.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package docker
import (
"crypto/tls"
"crypto/x509"
"fmt"
"net/http"
"strings"
"github.com/docker/docker/client"
log "go.arcalot.io/log/v2"
"go.flow.arcalot.io/deployer"
"go.flow.arcalot.io/pluginsdk/schema"
)
// NewFactory creates a new factory for the Docker deployer.
func NewFactory() deployer.ConnectorFactory[*Config] {
return &factory{}
}
type factory struct {
}
func (f factory) Name() string {
return "docker"
}
func (f factory) DeploymentType() deployer.DeploymentType {
return "image"
}
func (f factory) ConfigurationSchema() *schema.TypedScopeSchema[*Config] {
return Schema
}
func (f factory) Create(config *Config, logger log.Logger) (deployer.Connector, error) {
httpClient, err := f.getHTTPClient(config)
if err != nil {
return nil, err
}
cli, err := client.NewClientWithOpts(
client.WithAPIVersionNegotiation(),
client.WithHost(config.Connection.Host),
client.WithHTTPClient(httpClient),
)
if err != nil {
return nil, fmt.Errorf("failed to create Docker container (%w)", err)
}
return &connector{
cli,
config,
logger,
}, nil
}
func (f factory) getHTTPClient(config *Config) (*http.Client, error) {
var httpClient *http.Client
if config.Connection.CACert != "" && config.Connection.Key != "" && config.Connection.Cert != "" {
tlsConfig := &tls.Config{
MinVersion: tls.VersionTLS13,
}
caCertPool := x509.NewCertPool()
caCertPool.AppendCertsFromPEM([]byte(config.Connection.CACert))
tlsConfig.RootCAs = caCertPool
keyPair, err := tls.X509KeyPair([]byte(config.Connection.Cert), []byte(config.Connection.Key))
if err != nil {
return nil, err
}
tlsConfig.Certificates = []tls.Certificate{keyPair}
transport := &http.Transport{TLSClientConfig: tlsConfig}
httpClient = &http.Client{
Transport: transport,
Timeout: config.Timeouts.HTTP,
}
} else if strings.HasPrefix(config.Connection.Host, "http://") {
httpClient = &http.Client{
Transport: http.DefaultTransport,
Timeout: config.Timeouts.HTTP,
}
}
return httpClient, nil
}