-
Notifications
You must be signed in to change notification settings - Fork 464
/
valueshelper.go
334 lines (292 loc) · 11.8 KB
/
valueshelper.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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
// Copyright 2021 SAP SE or an SAP affiliate company. All rights reserved. This file is licensed under the Apache Software License, v. 2 except as noted otherwise in the LICENSE file
//
// 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 managedseed
import (
"os"
"strings"
"k8s.io/component-base/version"
"k8s.io/utils/pointer"
"github.com/gardener/gardener/imagevector"
gardencorev1beta1 "github.com/gardener/gardener/pkg/apis/core/v1beta1"
seedmanagementv1alpha1 "github.com/gardener/gardener/pkg/apis/seedmanagement/v1alpha1"
"github.com/gardener/gardener/pkg/gardenlet/apis/config"
gardenlethelper "github.com/gardener/gardener/pkg/gardenlet/apis/config/helper"
gardenletv1alpha1 "github.com/gardener/gardener/pkg/gardenlet/apis/config/v1alpha1"
"github.com/gardener/gardener/pkg/utils"
imagevectorutils "github.com/gardener/gardener/pkg/utils/imagevector"
"github.com/gardener/gardener/pkg/utils/secrets"
)
// ValuesHelper provides methods for merging GardenletDeployment and GardenletConfiguration with parent,
// as well as computing the values to be used when applying the gardenlet chart.
type ValuesHelper interface {
// MergeGardenletDeployment merges the given GardenletDeployment with the values from the parent gardenlet.
MergeGardenletDeployment(*seedmanagementv1alpha1.GardenletDeployment, *gardencorev1beta1.Shoot) (*seedmanagementv1alpha1.GardenletDeployment, error)
// MergeGardenletConfiguration merges the given GardenletConfiguration with the parent GardenletConfiguration.
MergeGardenletConfiguration(config *gardenletv1alpha1.GardenletConfiguration) (*gardenletv1alpha1.GardenletConfiguration, error)
// GetGardenletChartValues computes the values to be used when applying the gardenlet chart.
GetGardenletChartValues(*seedmanagementv1alpha1.GardenletDeployment, *gardenletv1alpha1.GardenletConfiguration, string) (map[string]interface{}, error)
}
// valuesHelper is a concrete implementation of ValuesHelper
type valuesHelper struct {
config *config.GardenletConfiguration
}
// NewValuesHelper creates a new ValuesHelper with the given parent GardenletConfiguration and image vector.
func NewValuesHelper(config *config.GardenletConfiguration) ValuesHelper {
return &valuesHelper{
config: config,
}
}
// MergeGardenletDeployment merges the given GardenletDeployment with the values from the parent gardenlet.
func (vp *valuesHelper) MergeGardenletDeployment(deployment *seedmanagementv1alpha1.GardenletDeployment, _ *gardencorev1beta1.Shoot) (*seedmanagementv1alpha1.GardenletDeployment, error) {
// Convert deployment object to values
deploymentValues, err := utils.ToValuesMap(deployment)
if err != nil {
return nil, err
}
// Get parent deployment values
parentDeployment, err := getParentGardenletDeployment()
if err != nil {
return nil, err
}
parentDeploymentValues, err := utils.ToValuesMap(parentDeployment)
if err != nil {
return nil, err
}
// Merge with parent
deploymentValues = utils.MergeMaps(parentDeploymentValues, deploymentValues)
// Convert deployment values back to an object
var deploymentObj *seedmanagementv1alpha1.GardenletDeployment
if err := utils.FromValuesMap(deploymentValues, &deploymentObj); err != nil {
return nil, err
}
return deploymentObj, nil
}
// MergeGardenletConfiguration merges the given GardenletConfiguration with the parent GardenletConfiguration.
func (vp *valuesHelper) MergeGardenletConfiguration(config *gardenletv1alpha1.GardenletConfiguration) (*gardenletv1alpha1.GardenletConfiguration, error) {
// Convert configuration object to values
configValues, err := utils.ToValuesMap(config)
if err != nil {
return nil, err
}
// Get parent config values
parentConfig, err := gardenlethelper.ConvertGardenletConfigurationExternal(vp.config)
if err != nil {
return nil, err
}
parentConfigValues, err := utils.ToValuesMap(parentConfig)
if err != nil {
return nil, err
}
// Delete gardenClientConnection.bootstrapKubeconfig, seedClientConnection.kubeconfig, and seedConfig in parent config values
parentConfigValues, err = utils.DeleteFromValuesMap(parentConfigValues, "gardenClientConnection", "bootstrapKubeconfig")
if err != nil {
return nil, err
}
parentConfigValues, err = utils.DeleteFromValuesMap(parentConfigValues, "seedClientConnection", "kubeconfig")
if err != nil {
return nil, err
}
parentConfigValues, err = utils.DeleteFromValuesMap(parentConfigValues, "seedConfig")
if err != nil {
return nil, err
}
// Merge with parent
configValues = utils.MergeMaps(parentConfigValues, configValues)
// Convert config values back to an object
var configObj *gardenletv1alpha1.GardenletConfiguration
if err := utils.FromValuesMap(configValues, &configObj); err != nil {
return nil, err
}
return configObj, nil
}
// GetGardenletChartValues computes the values to be used when applying the gardenlet chart.
func (vp *valuesHelper) GetGardenletChartValues(
deployment *seedmanagementv1alpha1.GardenletDeployment,
config *gardenletv1alpha1.GardenletConfiguration,
bootstrapKubeconfig string,
) (map[string]interface{}, error) {
var err error
// Get deployment values
deploymentValues, err := vp.getGardenletDeploymentValues(deployment)
if err != nil {
return nil, err
}
// Get config values
configValues, err := vp.getGardenletConfigurationValues(config, bootstrapKubeconfig)
if err != nil {
return nil, err
}
// Set gardenlet values to deployment values
gardenletValues := deploymentValues
// Ensure gardenlet values is a non-nil map
gardenletValues = utils.InitValuesMap(gardenletValues)
// Set config values in gardenlet values
return utils.SetToValuesMap(gardenletValues, configValues, "config")
}
// getGardenletDeploymentValues computes and returns the gardenlet deployment values from the given GardenletDeployment.
func (vp *valuesHelper) getGardenletDeploymentValues(deployment *seedmanagementv1alpha1.GardenletDeployment) (map[string]interface{}, error) {
// Convert deployment object to values
deploymentValues, err := utils.ToValuesMap(deployment)
if err != nil {
return nil, err
}
// make sure map is initialized
deploymentValues = utils.InitValuesMap(deploymentValues)
// Set imageVectorOverwrite and componentImageVectorOverwrites from parent
parentImageVectorOverwrite, err := getParentImageVectorOverwrite()
if err != nil {
return nil, err
}
if parentImageVectorOverwrite != nil {
deploymentValues["imageVectorOverwrite"] = *parentImageVectorOverwrite
}
parentComponentImageVectorOverwrites, err := getParentComponentImageVectorOverwrites()
if err != nil {
return nil, err
}
if parentComponentImageVectorOverwrites != nil {
deploymentValues["componentImageVectorOverwrites"] = *parentComponentImageVectorOverwrites
}
return deploymentValues, nil
}
// getGardenletConfigurationValues computes and returns the gardenlet configuration values from the given GardenletConfiguration.
func (vp *valuesHelper) getGardenletConfigurationValues(config *gardenletv1alpha1.GardenletConfiguration, bootstrapKubeconfig string) (map[string]interface{}, error) {
// Convert configuration object to values
configValues, err := utils.ToValuesMap(config)
if err != nil {
return nil, err
}
// If bootstrap kubeconfig is specified, set it in gardenClientConnection
// Otherwise, if kubeconfig path is specified in gardenClientConnection, read it and store its contents
if bootstrapKubeconfig != "" {
configValues, err = utils.SetToValuesMap(configValues, bootstrapKubeconfig, "gardenClientConnection", "bootstrapKubeconfig", "kubeconfig")
if err != nil {
return nil, err
}
} else {
kubeconfigPath, err := utils.GetFromValuesMap(configValues, "gardenClientConnection", "kubeconfig")
if err != nil {
return nil, err
}
if kubeconfigPath != nil && kubeconfigPath.(string) != "" {
kubeconfig, err := os.ReadFile(kubeconfigPath.(string))
if err != nil {
return nil, err
}
configValues, err = utils.SetToValuesMap(configValues, string(kubeconfig), "gardenClientConnection", "kubeconfig")
if err != nil {
return nil, err
}
}
}
// If kubeconfig path is specified in seedClientConnection, read it and store its contents
kubeconfigPath, err := utils.GetFromValuesMap(configValues, "seedClientConnection", "kubeconfig")
if err != nil {
return nil, err
}
if kubeconfigPath != nil && kubeconfigPath.(string) != "" {
kubeconfig, err := os.ReadFile(kubeconfigPath.(string))
if err != nil {
return nil, err
}
configValues, err = utils.SetToValuesMap(configValues, string(kubeconfig), "seedClientConnection", "kubeconfig")
if err != nil {
return nil, err
}
}
// Read server certificate file and store its contents
certPath, err := utils.GetFromValuesMap(configValues, "server", "https", "tls", "serverCertPath")
if err != nil {
return nil, err
}
if certPath != nil && certPath.(string) != "" && !strings.Contains(certPath.(string), secrets.TemporaryDirectoryForSelfGeneratedTLSCertificatesPattern) {
cert, err := os.ReadFile(certPath.(string))
if err != nil {
return nil, err
}
configValues, err = utils.SetToValuesMap(configValues, string(cert), "server", "https", "tls", "crt")
if err != nil {
return nil, err
}
}
// Read server key file and store its contents
keyPath, err := utils.GetFromValuesMap(configValues, "server", "https", "tls", "serverKeyPath")
if err != nil {
return nil, err
}
if keyPath != nil && keyPath.(string) != "" && !strings.Contains(keyPath.(string), secrets.TemporaryDirectoryForSelfGeneratedTLSCertificatesPattern) {
key, err := os.ReadFile(keyPath.(string))
if err != nil {
return nil, err
}
configValues, err = utils.SetToValuesMap(configValues, string(key), "server", "https", "tls", "key")
if err != nil {
return nil, err
}
}
// Delete server certificate and key paths
configValues, err = utils.DeleteFromValuesMap(configValues, "server", "https", "tls", "serverCertPath")
if err != nil {
return nil, err
}
configValues, err = utils.DeleteFromValuesMap(configValues, "server", "https", "tls", "serverKeyPath")
if err != nil {
return nil, err
}
return configValues, nil
}
func getParentGardenletDeployment() (*seedmanagementv1alpha1.GardenletDeployment, error) {
// Get image repository and tag
var imageRepository, imageTag string
gardenletImage, err := imagevector.ImageVector().FindImage("gardenlet")
if err != nil {
return nil, err
}
if gardenletImage.Tag != nil {
imageRepository = gardenletImage.Repository
imageTag = *gardenletImage.Tag
} else {
imageRepository = gardenletImage.String()
imageTag = version.Get().GitVersion
}
// Create and return result
return &seedmanagementv1alpha1.GardenletDeployment{
Image: &seedmanagementv1alpha1.Image{
Repository: &imageRepository,
Tag: &imageTag,
},
}, nil
}
func getParentImageVectorOverwrite() (*string, error) {
var imageVectorOverwrite *string
if overWritePath := os.Getenv(imagevectorutils.OverrideEnv); len(overWritePath) > 0 {
data, err := os.ReadFile(overWritePath)
if err != nil {
return nil, err
}
imageVectorOverwrite = pointer.String(string(data))
}
return imageVectorOverwrite, nil
}
func getParentComponentImageVectorOverwrites() (*string, error) {
var componentImageVectorOverwrites *string
if overWritePath := os.Getenv(imagevectorutils.ComponentOverrideEnv); len(overWritePath) > 0 {
data, err := os.ReadFile(overWritePath)
if err != nil {
return nil, err
}
componentImageVectorOverwrites = pointer.String(string(data))
}
return componentImageVectorOverwrites, nil
}