-
Notifications
You must be signed in to change notification settings - Fork 453
/
container.go
309 lines (240 loc) · 8 KB
/
container.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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
// Package container implements a wrapper which wraps all configuration documents into a single container.
package container
import (
"bytes"
"fmt"
"slices"
"github.com/hashicorp/go-multierror"
"github.com/siderolabs/gen/xslices"
coreconfig "github.com/siderolabs/talos/pkg/machinery/config"
"github.com/siderolabs/talos/pkg/machinery/config/config"
"github.com/siderolabs/talos/pkg/machinery/config/encoder"
"github.com/siderolabs/talos/pkg/machinery/config/types/v1alpha1"
"github.com/siderolabs/talos/pkg/machinery/config/validation"
)
// Container wraps all configuration documents into a single container.
type Container struct {
v1alpha1Config *v1alpha1.Config
documents []config.Document
bytes []byte
readonly bool
}
var _ coreconfig.Provider = &Container{}
// New creates a container out of the list of documents.
func New(documents ...config.Document) (*Container, error) {
container := &Container{
documents: make([]config.Document, 0, len(documents)),
}
seenDocuments := make(map[string]struct{})
for _, doc := range documents {
switch d := doc.(type) {
case *v1alpha1.Config:
if container.v1alpha1Config != nil {
return nil, fmt.Errorf("duplicate v1alpha1.Config")
}
container.v1alpha1Config = d
default:
documentID := d.Kind() + "/"
if named, ok := d.(config.NamedDocument); ok {
documentID += named.Name()
}
if _, alreadySeen := seenDocuments[documentID]; alreadySeen {
return nil, fmt.Errorf("duplicate document: %s", documentID)
}
seenDocuments[documentID] = struct{}{}
container.documents = append(container.documents, d)
}
}
return container, nil
}
// NewReadonly creates a read-only container which preserves byte representation of contents.
func NewReadonly(bytes []byte, documents ...config.Document) (*Container, error) {
c, err := New(documents...)
if err != nil {
return nil, err
}
c.bytes = bytes
c.readonly = true
return c, nil
}
// NewV1Alpha1 creates a container with (only) v1alpha1.Config document.
func NewV1Alpha1(config *v1alpha1.Config) *Container {
return &Container{
v1alpha1Config: config,
}
}
// Clone the container.
//
// Cloned container is not readonly.
func (container *Container) Clone() coreconfig.Provider {
return &Container{
v1alpha1Config: container.v1alpha1Config.DeepCopy(),
documents: xslices.Map(container.documents, config.Document.Clone),
}
}
// PatchV1Alpha1 patches the container's v1alpha1.Config while preserving other config documents.
func (container *Container) PatchV1Alpha1(patcher func(*v1alpha1.Config) error) (coreconfig.Provider, error) {
cfg := container.RawV1Alpha1()
if cfg == nil {
return nil, fmt.Errorf("v1alpha1.Config is not present in the container")
}
cfg = cfg.DeepCopy()
if err := patcher(cfg); err != nil {
return nil, err
}
otherDocs := xslices.Filter(container.Documents(), func(doc config.Document) bool {
_, ok := doc.(*v1alpha1.Config)
return !ok
})
return New(append([]config.Document{cfg}, otherDocs...)...)
}
// Readonly implements config.Container interface.
func (container *Container) Readonly() bool {
return container.readonly
}
// Debug implements config.Config interface.
func (container *Container) Debug() bool {
if container.v1alpha1Config == nil {
return false
}
return container.v1alpha1Config.Debug()
}
// Machine implements config.Config interface.
func (container *Container) Machine() config.MachineConfig {
if container.v1alpha1Config == nil {
return nil
}
return container.v1alpha1Config.Machine()
}
// Cluster implements config.Config interface.
func (container *Container) Cluster() config.ClusterConfig {
if container.v1alpha1Config == nil {
return nil
}
return container.v1alpha1Config.Cluster()
}
func findMatchingDocs[T any](documents []config.Document) []T {
var result []T
for _, doc := range documents {
if c, ok := doc.(T); ok {
result = append(result, c)
}
}
return result
}
// SideroLink implements config.Config interface.
func (container *Container) SideroLink() config.SideroLinkConfig {
matching := findMatchingDocs[config.SideroLinkConfig](container.documents)
if len(matching) == 0 {
return nil
}
return matching[0]
}
// Runtime implements config.Config interface.
func (container *Container) Runtime() config.RuntimeConfig {
return config.WrapRuntimeConfigList(findMatchingDocs[config.RuntimeConfig](container.documents)...)
}
// NetworkRules implements config.Config interface.
func (container *Container) NetworkRules() config.NetworkRuleConfig {
return config.WrapNetworkRuleConfigList(findMatchingDocs[config.NetworkRuleConfigSignal](container.documents)...)
}
// Bytes returns source YAML representation (if available) or does default encoding.
func (container *Container) Bytes() ([]byte, error) {
if !container.readonly {
return container.EncodeBytes()
}
if container.bytes == nil {
panic("container.Bytes() called on a readonly container without bytes")
}
return container.bytes, nil
}
// EncodeString configuration to YAML using the provided options.
func (container *Container) EncodeString(encoderOptions ...encoder.Option) (string, error) {
b, err := container.EncodeBytes(encoderOptions...)
if err != nil {
return "", err
}
return string(b), nil
}
// EncodeBytes configuration to YAML using the provided options.
func (container *Container) EncodeBytes(encoderOptions ...encoder.Option) ([]byte, error) {
var buf bytes.Buffer
if container.v1alpha1Config != nil {
b, err := encoder.NewEncoder(container.v1alpha1Config, encoderOptions...).Encode()
if err != nil {
return nil, err
}
buf.Write(b)
}
for _, doc := range container.documents {
if buf.Len() > 0 {
buf.WriteString("---\n")
}
b, err := encoder.NewEncoder(doc, encoderOptions...).Encode()
if err != nil {
return nil, err
}
buf.Write(b)
}
return buf.Bytes(), nil
}
// Validate checks configuration and returns warnings and fatal errors (as multierror).
func (container *Container) Validate(mode validation.RuntimeMode, opt ...validation.Option) ([]string, error) {
var (
warnings []string
err error
)
if container.v1alpha1Config != nil {
warnings, err = container.v1alpha1Config.Validate(mode, opt...)
}
var multiErr *multierror.Error
if err != nil {
multiErr = multierror.Append(multiErr, err)
}
for _, doc := range container.documents {
if validatableDoc, ok := doc.(config.Validator); ok {
docWarnings, docErr := validatableDoc.Validate(mode, opt...)
warnings = append(warnings, docWarnings...)
multiErr = multierror.Append(multiErr, docErr)
}
}
return warnings, multiErr.ErrorOrNil()
}
// RedactSecrets returns a copy of the Provider with all secrets replaced with the given string.
func (container *Container) RedactSecrets(replacement string) coreconfig.Provider {
clone := container.Clone().(*Container) //nolint:forcetypeassert,errcheck
if clone.v1alpha1Config != nil {
clone.v1alpha1Config.Redact(replacement)
}
for _, doc := range clone.documents {
if secretDoc, ok := doc.(config.SecretDocument); ok {
secretDoc.Redact(replacement)
}
}
return clone
}
// RawV1Alpha1 returns internal config representation for v1alpha1.Config.
func (container *Container) RawV1Alpha1() *v1alpha1.Config {
if container.readonly {
return container.v1alpha1Config.DeepCopy()
}
return container.v1alpha1Config
}
// Documents returns all documents in the container.
//
// Documents should not be modified.
func (container *Container) Documents() []config.Document {
docs := slices.Clone(container.documents)
if container.v1alpha1Config != nil {
docs = append([]config.Document{container.v1alpha1Config}, docs...)
}
return docs
}
// CompleteForBoot return true if the machine config is enough to proceed with the boot process.
func (container *Container) CompleteForBoot() bool {
// for now, v1alpha1 config is required
return container.v1alpha1Config != nil
}