-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathfactory.go
93 lines (81 loc) · 2.46 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
86
87
88
89
90
91
92
93
package kubernetes
import (
"fmt"
log "go.arcalot.io/log/v2"
"go.flow.arcalot.io/deployer"
"go.flow.arcalot.io/pluginsdk/schema"
core "k8s.io/api/core/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/kubernetes/scheme"
restclient "k8s.io/client-go/rest"
)
// 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 "kubernetes"
}
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) {
connectionConfig := f.createConnectionConfig(config)
cli, err := kubernetes.NewForConfig(&connectionConfig)
if err != nil {
return nil, fmt.Errorf("failed to create Kubernetes config (%w)", err)
}
restClient, err := restclient.RESTClientFor(&connectionConfig)
if err != nil {
return nil, fmt.Errorf("failed to create Kubernetes REST client (%w)", err)
}
return &connector{
cli: cli,
restClient: restClient,
config: config,
connectionConfig: connectionConfig,
logger: logger,
}, nil
}
func (f factory) createConnectionConfig(config *Config) restclient.Config {
caData := []byte("")
keyData := []byte("")
certData := []byte("")
if config.Connection.CAData != nil {
caData = []byte(*config.Connection.CAData)
}
if config.Connection.KeyData != nil {
keyData = []byte(*config.Connection.KeyData)
}
if config.Connection.CertData != nil {
certData = []byte(*config.Connection.CertData)
}
return restclient.Config{
Host: config.Connection.Host,
APIPath: config.Connection.APIPath,
ContentConfig: restclient.ContentConfig{
GroupVersion: &core.SchemeGroupVersion,
NegotiatedSerializer: scheme.Codecs.WithoutConversion(),
},
Username: config.Connection.Username,
Password: config.Connection.Password,
BearerToken: config.Connection.BearerToken,
Impersonate: restclient.ImpersonationConfig{},
TLSClientConfig: restclient.TLSClientConfig{
ServerName: config.Connection.ServerName,
CertData: certData,
KeyData: keyData,
CAData: caData,
Insecure: config.Connection.Insecure,
},
UserAgent: "Arcaflow",
QPS: float32(config.Connection.QPS),
Burst: int(config.Connection.Burst),
Timeout: config.Timeouts.HTTP,
}
}