-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
227 lines (172 loc) · 4.88 KB
/
util.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
package configuration
import (
"fmt"
api "github.com/dodo-cli/dodo-core/api/v1alpha3"
"github.com/dodo-cli/dodo-core/pkg/plugin"
log "github.com/hashicorp/go-hclog"
"github.com/hashicorp/go-multierror"
)
type ErrConfigNotFound struct {
Name string
Reason error
}
func (e ErrConfigNotFound) Error() string {
if e.Reason == nil {
return fmt.Sprintf(
"could not find any configuration for '%s'", e.Name,
)
}
return fmt.Sprintf(
"could not find any configuration for '%s': %s",
e.Name,
e.Reason.Error(),
)
}
type ErrInvalidConfig struct {
Name string
Message string
}
func (e ErrInvalidConfig) Error() string {
return fmt.Sprintf(
"invalid configuration for '%s': %s",
e.Name,
e.Message,
)
}
func AssembleBackdropConfig(m plugin.Manager, name string, overrides *api.Backdrop) (*api.Backdrop, error) {
var errs error
config := &api.Backdrop{Entrypoint: &api.Entrypoint{}}
foundSomething := false
for n, p := range m.GetPlugins(Type.String()) {
log.L().Debug("Fetching configuration from plugin", "name", n)
conf, err := p.(Configuration).GetBackdrop(name)
if err != nil {
errs = multierror.Append(errs, err)
continue
}
foundSomething = true
MergeBackdrop(config, conf)
}
if !foundSomething {
return nil, ErrConfigNotFound{Name: name, Reason: errs}
}
MergeBackdrop(config, overrides)
log.L().Debug("assembled configuration", "backdrop", config)
err := ValidateBackdrop(config)
return config, err
}
func FindBuildConfig(m plugin.Manager, name string, overrides *api.BuildInfo) (*api.BuildInfo, error) {
var errs error
for n, p := range m.GetPlugins(Type.String()) {
log.L().Debug("Fetching configuration from plugin", "name", n)
confs, err := p.(Configuration).ListBackdrops()
if err != nil {
errs = multierror.Append(errs, err)
continue
}
for _, conf := range confs {
if conf.BuildInfo != nil && conf.BuildInfo.ImageName == name {
config := &api.BuildInfo{}
MergeBuildInfo(config, conf.BuildInfo)
MergeBuildInfo(config, overrides)
if err := ValidateBuildInfo(config); err != nil {
errs = multierror.Append(errs, err)
continue
}
return config, nil
}
}
}
return nil, ErrConfigNotFound{Name: name, Reason: errs}
}
func MergeBackdrop(target *api.Backdrop, source *api.Backdrop) {
if len(source.Name) > 0 {
target.Name = source.Name
}
target.Aliases = append(target.Aliases, source.Aliases...)
if len(source.ImageId) > 0 {
target.ImageId = source.ImageId
}
if len(source.Runtime) > 0 {
target.Runtime = source.Runtime
}
if source.Entrypoint != nil {
if source.Entrypoint.Interactive {
target.Entrypoint.Interactive = true
}
if len(source.Entrypoint.Interpreter) > 0 {
target.Entrypoint.Interpreter = source.Entrypoint.Interpreter
}
if len(source.Entrypoint.Script) > 0 {
target.Entrypoint.Script = source.Entrypoint.Script
}
if len(source.Entrypoint.Arguments) > 0 {
target.Entrypoint.Arguments = source.Entrypoint.Arguments
}
}
if len(source.ContainerName) > 0 {
target.ContainerName = source.ContainerName
}
target.Environment = append(target.Environment, source.Environment...)
if len(source.User) > 0 {
target.User = source.User
}
target.Volumes = append(target.Volumes, source.Volumes...)
target.Devices = append(target.Devices, source.Devices...)
target.Ports = append(target.Ports, source.Ports...)
target.Capabilities = append(target.Capabilities, source.Capabilities...)
if len(source.WorkingDir) > 0 {
target.WorkingDir = source.WorkingDir
}
if source.BuildInfo != nil {
if target.BuildInfo == nil {
target.BuildInfo = source.BuildInfo
} else {
MergeBuildInfo(target.BuildInfo, source.BuildInfo)
}
}
}
func MergeBuildInfo(target *api.BuildInfo, source *api.BuildInfo) {
if len(source.Builder) > 0 {
target.Builder = source.Builder
}
if len(source.ImageName) > 0 {
target.ImageName = source.ImageName
}
if len(source.Context) > 0 {
target.Context = source.Context
}
if len(source.Dockerfile) > 0 {
target.Dockerfile = source.Dockerfile
}
if len(source.InlineDockerfile) > 0 {
target.InlineDockerfile = source.InlineDockerfile
}
target.Arguments = append(target.Arguments, source.Arguments...)
target.Secrets = append(target.Secrets, source.Secrets...)
target.SshAgents = append(target.SshAgents, source.SshAgents...)
if source.NoCache {
target.NoCache = true
}
if source.ForceRebuild {
target.ForceRebuild = true
}
if source.ForcePull {
target.ForcePull = true
}
target.Dependencies = append(target.Dependencies, source.Dependencies...)
}
func ValidateBackdrop(b *api.Backdrop) error {
if b.ImageId == "" && b.BuildInfo == nil {
return ErrInvalidConfig{Name: b.Name, Message: "neither image nor build configured"}
}
if b.BuildInfo != nil {
if err := ValidateBuildInfo(b.BuildInfo); err != nil {
return err
}
}
return nil
}
func ValidateBuildInfo(b *api.BuildInfo) error {
return nil
}