This repository has been archived by the owner on Feb 5, 2021. It is now read-only.
/
patch_components.go
373 lines (349 loc) · 11.4 KB
/
patch_components.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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
/*
* Copyright (c) 2019 WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
*
* WSO2 Inc. licenses this file to you 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 instance
import (
"fmt"
"os"
"path/filepath"
"strings"
"github.com/ghodss/yaml"
"cellery.io/cellery/components/cli/cli"
errorpkg "cellery.io/cellery/components/cli/pkg/error"
"cellery.io/cellery/components/cli/pkg/kubernetes"
"cellery.io/cellery/components/cli/pkg/util"
)
const k8sYamlSpec = "spec"
const k8sYamlComponents = "components"
const k8sYamlMetadata = "metadata"
const k8sYamlName = "name"
const k8sYamlContainers = "containers"
const k8sYamlImage = "image"
const k8sYamlImageEnvVars = "env"
const k8sYamlContainerName = "name"
const k8sContainerTemplate = "template"
func RunPatchForSingleComponent(cli cli.Cli, instance string, component string, containerImage string, containerName string, envVars []string) error {
//spinner := util.StartNewSpinner(fmt.Sprintf("Patching component %s in instance %s", component, instance))
var canBeComposite bool
_, err := cli.KubeCli().GetCell(instance)
if err != nil {
if notFound, _ := errorpkg.IsCellInstanceNotFoundError(instance, err); notFound {
// could be a composite
canBeComposite = true
} else {
return err
}
}
artifactFile := filepath.Join("./", fmt.Sprintf("%s.yaml", instance))
defer func() error {
return os.Remove(artifactFile)
}()
if canBeComposite {
_, err = cli.KubeCli().GetComposite(instance)
if err != nil {
if notFound, _ := errorpkg.IsCompositeInstanceNotFoundError(instance, err); notFound {
// the given instance is neither a cell or a composite
return fmt.Errorf("unable to find a running instance with name: %s", instance)
} else {
return err
}
}
if err = patchSingleComponentinComposite(cli, instance, component, containerImage, containerName, envVars, artifactFile); err != nil {
return fmt.Errorf("error patching single component in composite")
}
} else {
if err = patchSingleComponentinCell(cli, instance, component, containerImage, containerName, envVars, artifactFile); err != nil {
return fmt.Errorf("error patching single component in cell, %v", err)
}
}
if err = cli.KubeCli().ApplyFile(artifactFile); err != nil {
return fmt.Errorf("error applying yaml %s", artifactFile)
}
util.PrintSuccessMessage(fmt.Sprintf("Successfully patched the component %s in instance %s with container image %s", component, instance, containerImage))
return nil
}
func patchSingleComponentinComposite(cli cli.Cli, instance string, component string, containerImage string, containerName string, envVars []string, artifactFile string) error {
compositeInst, err := getPatchedCompositeInstanceForSingleComponent(cli, instance, component, containerImage, containerName, envVars)
if err != nil {
return err
}
patchedCompInstContents, err := yaml.Marshal(compositeInst)
if err != nil {
return err
}
err = writeToFile(patchedCompInstContents, artifactFile)
if err != nil {
return err
}
return nil
}
func patchSingleComponentinCell(cli cli.Cli, instance string, component string, containerImage string, containerName string, envVars []string, artifactFile string) error {
cellInst, err := getPatchededCellInstanceForSingleComponent(cli, instance, component, containerImage, containerName, envVars)
if err != nil {
return err
}
patchedCellInstContents, err := yaml.Marshal(cellInst)
if err != nil {
return err
}
err = writeToFile(patchedCellInstContents, artifactFile)
if err != nil {
return err
}
return nil
}
func getPatchededCellInstanceForSingleComponent(cli cli.Cli, instance string, componentName string, containerImage string, containerName string, newEnvVars []string) (map[string]interface{}, error) {
err := validateEnvVars(newEnvVars)
if err != nil {
return nil, err
}
cellInstance, err := cli.KubeCli().GetCellInstanceAsMapInterface(instance)
if err != nil {
return nil, err
}
cellSpec, err := getModifiedSpecForComponent(cellInstance, componentName, containerImage, containerName, newEnvVars)
if err != nil {
return nil, err
}
cellInstance[k8sYamlSpec] = cellSpec
return cellInstance, nil
}
func getPatchedCompositeInstanceForSingleComponent(cli cli.Cli, instance string, componentName string, containerImage string, containerName string, newEnvVars []string) (map[string]interface{}, error) {
err := validateEnvVars(newEnvVars)
if err != nil {
return nil, err
}
compositeInst, err := cli.KubeCli().GetCompositeInstanceAsMapInterface(instance)
if err != nil {
return nil, err
}
compSpec, err := getModifiedSpecForComponent(compositeInst, componentName, containerImage, containerName, newEnvVars)
if err != nil {
return nil, err
}
compositeInst[k8sYamlSpec] = compSpec
return compositeInst, nil
}
func convertToPodSpecEnvVars(envVars []string) []kubernetes.Env {
var podSpecEnvVars []kubernetes.Env
for _, envVar := range envVars {
key, value := getEnvVarKeyValue(envVar)
podSpecEnvVars = append(podSpecEnvVars, kubernetes.Env{
Name: key,
Value: value,
})
}
return podSpecEnvVars
}
func validateEnvVars(envVars []string) error {
for _, envVar := range envVars {
if len(strings.Split(envVar, "=")) < 2 {
return fmt.Errorf("expected env variables as <key>=<value> tuples, got %s", envVar)
}
}
return nil
}
func getMergedEnvVars(currentContainerSpec interface{}, newEnvVars []kubernetes.Env) ([]kubernetes.Env, error) {
mergedEnvVarsMap := make(map[string]string)
envVarBytes, err := yaml.Marshal(currentContainerSpec)
if err != nil {
return nil, err
}
var envVars []kubernetes.Env
err = yaml.Unmarshal(envVarBytes, &envVars)
if err != nil {
return nil, err
}
// add existing env vars
for _, value := range envVars {
mergedEnvVarsMap[value.Name] = value.Value
}
// add new vars
for _, newEnvVar := range newEnvVars {
// will override any existing env var with the same name
mergedEnvVarsMap[newEnvVar.Name] = newEnvVar.Value
}
// now create an array of kind []kubectl.Env from the map
var mergedEnvVars []kubernetes.Env
for mapKey, mapVal := range mergedEnvVarsMap {
mergedEnvVars = append(mergedEnvVars, kubernetes.Env{
Name: mapKey,
Value: mapVal,
})
}
return mergedEnvVars, nil
}
func getEnvVarKeyValue(tuple string) (string, string) {
keyValue := strings.Split(tuple, "=")
return keyValue[0], keyValue[1]
}
func getModifiedSpecForComponent(instance map[string]interface{}, componentName string, containerImage string,
containerName string, newEnvVars []string) (map[string]interface{}, error) {
cellSpecByteArr, err := yaml.Marshal(instance[k8sYamlSpec])
if err != nil {
return nil, err
}
var cellSpec map[string]interface{}
err = yaml.Unmarshal(cellSpecByteArr, &cellSpec)
if err != nil {
return nil, err
}
components, err := getComponentSpecs(cellSpec)
if err != nil {
return nil, err
}
var componentFound bool
for _, component := range components {
// metadata
compMetadata, err := getComponentMetadate(component)
if err != nil {
return nil, err
}
// if the component name matches, update the container
if componentName == compMetadata[k8sYamlName] {
componentFound = true
componentSpec, err := getComponentSpec(component)
if err != nil {
return nil, err
}
template, err := getTemplate(componentSpec)
if err != nil {
return nil, err
}
containerSpecs, err := getContainerSpecs(template)
if err != nil {
return nil, err
}
if containerName == "" {
// container name not provided, use the first container and update it.
if len(newEnvVars) > 0 {
mergedEnvVars, err := getMergedEnvVars(containerSpecs[0][k8sYamlImageEnvVars], convertToPodSpecEnvVars(newEnvVars))
if err != nil {
return nil, err
}
containerSpecs[0][k8sYamlImageEnvVars] = mergedEnvVars
}
containerSpecs[0][k8sYamlImage] = containerImage
} else {
containerFound := false
for i, spec := range containerSpecs {
containerSpec, err := getContainerSpec(spec)
if err != nil {
return nil, err
}
if containerName == containerSpec[k8sYamlContainerName] {
containerFound = true
if len(newEnvVars) > 0 {
mergedEnvVars, err := getMergedEnvVars(containerSpec[k8sYamlImageEnvVars], convertToPodSpecEnvVars(newEnvVars))
if err != nil {
return nil, err
}
containerSpec[k8sYamlImageEnvVars] = mergedEnvVars
}
containerSpec[k8sYamlImage] = containerImage
containerSpecs[i] = containerSpec
break
}
}
if !containerFound {
return nil, fmt.Errorf("no container with name %s found in component %s", containerName, componentName)
}
}
template[k8sYamlContainers] = containerSpecs
componentSpec[k8sContainerTemplate] = template
component[k8sYamlSpec] = componentSpec
break
}
}
if !componentFound {
return instance, fmt.Errorf("no component with name %s found in instance %s", componentName, instance)
}
cellSpec[k8sYamlComponents] = components
return cellSpec, nil
}
func getComponentSpecs(cellSpec map[string]interface{}) ([]map[string]interface{}, error) {
componentBytes, err := yaml.Marshal(cellSpec[k8sYamlComponents])
if err != nil {
return nil, err
}
var components []map[string]interface{}
err = yaml.Unmarshal(componentBytes, &components)
if err != nil {
return nil, err
}
return components, nil
}
func getComponentSpec(component map[string]interface{}) (map[string]interface{}, error) {
compSpecBytes, err := yaml.Marshal(component[k8sYamlSpec])
if err != nil {
return nil, err
}
var componentSpec map[string]interface{}
err = yaml.Unmarshal(compSpecBytes, &componentSpec)
if err != nil {
return nil, err
}
return componentSpec, nil
}
func getContainerSpecs(template map[string]interface{}) ([]map[string]interface{}, error) {
containerSpecBytes, err := yaml.Marshal(template[k8sYamlContainers])
if err != nil {
return nil, err
}
var containerSpecs []map[string]interface{}
err = yaml.Unmarshal(containerSpecBytes, &containerSpecs)
if err != nil {
return nil, err
}
return containerSpecs, nil
}
func getContainerSpec(o interface{}) (map[string]interface{}, error) {
var containerSpec map[string]interface{}
containerYamlBytes, err := yaml.Marshal(o)
if err != nil {
return nil, err
}
err = yaml.Unmarshal(containerYamlBytes, &containerSpec)
if err != nil {
return nil, err
}
return containerSpec, err
}
func getComponentMetadate(component map[string]interface{}) (map[string]interface{}, error) {
compMetadataBytes, err := yaml.Marshal(component[k8sYamlMetadata])
if err != nil {
return nil, err
}
var compMetadata map[string]interface{}
err = yaml.Unmarshal(compMetadataBytes, &compMetadata)
if err != nil {
return nil, err
}
return compMetadata, nil
}
func getTemplate(componentSpec map[string]interface{}) (map[string]interface{}, error) {
templateBytes, err := yaml.Marshal(componentSpec[k8sContainerTemplate])
if err != nil {
return nil, err
}
var template map[string]interface{}
err = yaml.Unmarshal(templateBytes, &template)
if err != nil {
return nil, err
}
return template, nil
}