-
Notifications
You must be signed in to change notification settings - Fork 109
/
base.go
306 lines (256 loc) · 8.69 KB
/
base.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
// Copyright 2021 Flant JSC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package config
import (
"context"
"encoding/json"
"errors"
"fmt"
"os"
"strings"
"time"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/yaml"
"github.com/deckhouse/deckhouse/dhctl/pkg/kubernetes/client"
"github.com/deckhouse/deckhouse/dhctl/pkg/log"
"github.com/deckhouse/deckhouse/dhctl/pkg/util/input"
"github.com/deckhouse/deckhouse/dhctl/pkg/util/retry"
)
const (
candiDir = "/deckhouse/candi"
modulesDir = "/deckhouse/modules"
globalHooksModule = "/deckhouse/global-hooks"
// don't forget to update the version in release requirements (release.yaml) 'autoK8sVersion' key
DefaultKubernetesVersion = "1.27"
)
const (
versionMap = "/deckhouse/candi/version_map.yml"
imagesDigestsJSON = "/deckhouse/candi/images_digests.json"
)
func LoadConfigFromFile(paths []string, opts ...ValidateOption) (*MetaConfig, error) {
metaConfig, err := ParseConfig(paths, opts...)
if err != nil {
return nil, err
}
if metaConfig.ClusterConfig == nil {
return nil, fmt.Errorf("ClusterConfiguration must be provided")
}
err = metaConfig.LoadVersionMap(versionMap)
if err != nil {
return nil, err
}
err = metaConfig.LoadImagesDigests(imagesDigestsJSON)
if err != nil {
return nil, err
}
err = metaConfig.LoadInstallerVersion()
if err != nil {
return nil, err
}
if metaConfig.ClusterType == CloudClusterType && len(metaConfig.ProviderClusterConfig) == 0 {
return nil, fmt.Errorf("ProviderClusterConfiguration section is required for a Cloud cluster.")
}
return metaConfig, nil
}
func numerateManifestLines(manifest []byte) string {
manifestLines := strings.Split(string(manifest), "\n")
builder := strings.Builder{}
for index, line := range manifestLines {
builder.WriteString(fmt.Sprintf("%d\t%s\n", index+1, line))
}
return builder.String()
}
func ParseConfig(paths []string, opts ...ValidateOption) (*MetaConfig, error) {
content := ""
for _, path := range paths {
log.DebugF("Have config file %s\n", path)
fileContent, err := os.ReadFile(path)
if err != nil {
return nil, fmt.Errorf("loading config file: %v", err)
}
content = content + "\n\n---\n\n" + string(fileContent)
}
return ParseConfigFromData(content, opts...)
}
func ParseConfigFromCluster(kubeCl *client.KubernetesClient) (*MetaConfig, error) {
var metaConfig *MetaConfig
var err error
err = log.Process("common", "Get Cluster configuration", func() error {
return retry.NewLoop("Get Cluster configuration from Kubernetes cluster", 10, 5*time.Second).Run(func() error {
metaConfig, err = parseConfigFromCluster(kubeCl)
return err
})
})
if err != nil {
return nil, err
}
return metaConfig, nil
}
func ParseConfigInCluster(kubeCl *client.KubernetesClient) (*MetaConfig, error) {
var metaConfig *MetaConfig
var err error
err = retry.NewSilentLoop("Get Cluster configuration from inside Kubernetes cluster", 5, 5*time.Second).Run(func() error {
metaConfig, err = parseConfigFromCluster(kubeCl)
return err
})
if err != nil {
return nil, err
}
return metaConfig, nil
}
func parseConfigFromCluster(kubeCl *client.KubernetesClient) (*MetaConfig, error) {
metaConfig := MetaConfig{}
schemaStore := NewSchemaStore()
clusterConfig, err := kubeCl.CoreV1().Secrets("kube-system").Get(context.TODO(), "d8-cluster-configuration", metav1.GetOptions{})
if err != nil {
return nil, err
}
clusterConfigData := clusterConfig.Data["cluster-configuration.yaml"]
_, err = schemaStore.Validate(&clusterConfigData)
if err != nil {
return nil, err
}
var parsedClusterConfig map[string]json.RawMessage
if err := yaml.Unmarshal(clusterConfigData, &parsedClusterConfig); err != nil {
return nil, err
}
metaConfig.ClusterConfig = parsedClusterConfig
var clusterType string
if err := json.Unmarshal(parsedClusterConfig["clusterType"], &clusterType); err != nil {
return nil, err
}
if clusterType == CloudClusterType {
providerClusterConfig, err := kubeCl.CoreV1().Secrets("kube-system").Get(context.TODO(), "d8-provider-cluster-configuration", metav1.GetOptions{})
if err != nil {
return nil, err
}
providerClusterConfigData := providerClusterConfig.Data["cloud-provider-cluster-configuration.yaml"]
_, err = schemaStore.Validate(&providerClusterConfigData)
if err != nil {
return nil, err
}
var parsedProviderClusterConfig map[string]json.RawMessage
if err := yaml.Unmarshal(providerClusterConfigData, &parsedProviderClusterConfig); err != nil {
return nil, err
}
metaConfig.ProviderClusterConfig = parsedProviderClusterConfig
}
return metaConfig.Prepare()
}
// parseDocument
//
// parse and validate one document of
//
// InitConfiguration
// ClusterConfiguration
// StaticClusterConfiguration
// ClusterConfiguration
// ModuleConfig
//
// if validation schema for ModuleConfig or another resources not found returns ErrSchemaNotFound error
func parseDocument(doc string, metaConfig *MetaConfig, schemaStore *SchemaStore, opts ...ValidateOption) (bool, error) {
doc = strings.TrimSpace(doc)
if doc == "" {
return false, nil
}
docData := []byte(doc)
var index SchemaIndex
err := yaml.Unmarshal(docData, &index)
if err != nil {
return false, fmt.Errorf("Config document unmarshal failed: %v\ndata: \n%s\n", err, numerateManifestLines(docData))
}
if index.Kind == ModuleConfigKind {
moduleConfig := ModuleConfig{}
err = yaml.Unmarshal(docData, &moduleConfig)
if err != nil {
return false, fmt.Errorf("Module config document unmarshal failed: %v\ndata: \n%s\n", err, numerateManifestLines(docData))
}
log.DebugF("Found ModuleConfig in config file %s\n", moduleConfig.Name)
_, err = schemaStore.Validate(&docData, opts...)
if err != nil {
if errors.Is(err, ErrSchemaNotFound) {
return false, nil
}
return false, fmt.Errorf("Module config validation failed: %w\ndata: \n%s\n", err, numerateManifestLines(docData))
}
metaConfig.ModuleConfigs = append(metaConfig.ModuleConfigs, &moduleConfig)
return true, nil
}
_, err = schemaStore.Validate(&docData, opts...)
if err != nil {
if errors.Is(err, ErrSchemaNotFound) {
return false, nil
}
return false, fmt.Errorf("Config document validation failed: %v\ndata: \n%s\n", err, numerateManifestLines(docData))
}
var data map[string]json.RawMessage
if err = yaml.Unmarshal(docData, &data); err != nil {
return false, fmt.Errorf("Config document unmarshal failed: %v\ndata: \n%s\n", err, numerateManifestLines(docData))
}
found := false
switch {
case index.Kind == "InitConfiguration":
log.DebugLn("Found InitConfiguration")
metaConfig.InitClusterConfig = data
found = true
case index.Kind == "ClusterConfiguration":
log.DebugLn("Found ClusterConfiguration")
metaConfig.ClusterConfig = data
found = true
case index.Kind == "StaticClusterConfiguration":
log.DebugLn("Found StaticClusterConfiguration")
metaConfig.StaticClusterConfig = data
found = true
case strings.HasSuffix(index.Kind, "ClusterConfiguration"):
log.DebugF("Found %s\n", index.Kind)
metaConfig.ProviderClusterConfig = data
found = true
}
return found, nil
}
func ParseConfigFromData(configData string, opts ...ValidateOption) (*MetaConfig, error) {
schemaStore := NewSchemaStore()
bigFileTmp := strings.TrimSpace(configData)
docs := input.YAMLSplitRegexp.Split(bigFileTmp, -1)
resourcesDocs := make([]string, 0, len(docs))
metaConfig := MetaConfig{}
for _, doc := range docs {
found, err := parseDocument(doc, &metaConfig, schemaStore, opts...)
if err != nil {
return nil, err
}
if !found && strings.TrimSpace(doc) != "" {
resourcesDocs = append(resourcesDocs, doc)
}
}
metaConfig.ResourcesYAML = strings.TrimSpace(strings.Join(resourcesDocs, "\n\n---\n\n"))
log.DebugF("Collected ResourcesYAML:\n%s\n\n", metaConfig.ResourcesYAML)
// init configuration can be empty, but we need default from openapi spec
if len(metaConfig.InitClusterConfig) == 0 {
doc := `
apiVersion: deckhouse.io/v1
kind: InitConfiguration
deckhouse: {}
`
log.DebugF("Init configuration not found use empty: %s", doc)
found, err := parseDocument(doc, &metaConfig, schemaStore, opts...)
if err != nil {
return nil, err
}
if !found {
return nil, fmt.Errorf("init configuration index not found")
}
}
return metaConfig.Prepare()
}