forked from konveyor/move2kube
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcloudfoundrytransformer.go
453 lines (434 loc) · 18 KB
/
cloudfoundrytransformer.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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
/*
* Copyright IBM Corporation 2021
*
* 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 transformer
import (
"encoding/json"
"fmt"
"os"
"path/filepath"
"regexp"
"strings"
"code.cloudfoundry.org/cli/util/manifest"
"github.com/cloudfoundry/bosh-cli/director/template"
collector "github.com/konveyor/move2kube/collector"
"github.com/konveyor/move2kube/common"
"github.com/konveyor/move2kube/environment"
"github.com/konveyor/move2kube/qaengine"
irtypes "github.com/konveyor/move2kube/types/ir"
transformertypes "github.com/konveyor/move2kube/types/transformer"
"github.com/konveyor/move2kube/types/transformer/artifacts"
"github.com/sirupsen/logrus"
"gopkg.in/yaml.v3"
"k8s.io/apimachinery/pkg/api/resource"
core "k8s.io/kubernetes/pkg/apis/core"
"k8s.io/kubernetes/pkg/apis/networking"
)
const (
// ResourceRequestKey is the config key for resource requests
ResourceRequestKey = "ResourceRequest"
)
// variableLiteralPattern to identify variable literals in environment names
var variableLiteralPattern = regexp.MustCompile(`[-.+~\x60!@#$%^&*(){}\[\]:;"',?<>/]`)
// CloudFoundry implements Transformer interface
type CloudFoundry struct {
Config transformertypes.Transformer
Env *environment.Environment
}
// Init Initializes the transformer
func (t *CloudFoundry) Init(tc transformertypes.Transformer, env *environment.Environment) (err error) {
t.Config = tc
t.Env = env
return nil
}
// GetConfig returns the transformer config
func (t *CloudFoundry) GetConfig() (transformertypes.Transformer, *environment.Environment) {
return t.Config, t.Env
}
// DirectoryDetect detects cloud foundry projects in various directories
func (t *CloudFoundry) DirectoryDetect(dir string) (map[string][]transformertypes.Artifact, error) {
filePaths, err := common.GetFilesByExt(dir, []string{".yml", ".yaml"})
if err != nil {
return nil, fmt.Errorf("failed to look for yaml files in the directory %s . Error: %q", dir, err)
}
services := map[string][]transformertypes.Artifact{}
// Load instance apps, if available
cfInstanceApps := map[string][]collector.CfApp{} //path
for _, filePath := range filePaths {
fileCfInstanceApps := collector.CfApps{}
if err := common.ReadMove2KubeYaml(filePath, &fileCfInstanceApps); err != nil {
logrus.Debugf("Failed to read the yaml file at path %q Error: %q", filePath, err)
continue
}
if fileCfInstanceApps.Kind != string(collector.CfAppsMetadataKind) {
logrus.Debugf("%q is not a valid apps file. Expected kind: %s Actual Kind: %s", filePath, string(collector.CfAppsMetadataKind), fileCfInstanceApps.Kind)
continue
}
cfInstanceApps[filePath] = append(cfInstanceApps[filePath], fileCfInstanceApps.Spec.CfApps...)
}
logrus.Debugf("Cf Instances %+v", cfInstanceApps)
for _, filePath := range filePaths {
applications, _, err := t.readApplicationManifest(filePath, "")
if err != nil {
logrus.Debugf("Failed to parse the manifest file at path %q Error: %q", filePath, err)
continue
}
for _, application := range applications {
servicedirectory := filepath.Dir(filePath)
buildArtifactDirectory := ""
if application.Path != "" {
artifactDirectory := filepath.Join(filepath.Dir(filePath), application.Path)
if _, err := os.Stat(artifactDirectory); !os.IsNotExist(err) {
servicedirectory = artifactDirectory
} else {
buildArtifactDirectory = artifactDirectory
logrus.Debugf("Path to app directory %s does not exist, assuming manifest directory as app path", artifactDirectory)
}
}
applicationName := application.Name
if applicationName == "" {
basename := filepath.Base(filePath)
applicationName = strings.TrimSuffix(basename, filepath.Ext(basename))
}
ct := transformertypes.Artifact{
Configs: map[transformertypes.ConfigType]interface{}{
artifacts.CloudFoundryConfigType: artifacts.CloudFoundryConfig{
ServiceName: applicationName,
}},
Paths: map[transformertypes.PathType][]string{
artifacts.CfManifestPathType: {filePath},
artifacts.ServiceDirPathType: {servicedirectory},
},
}
if buildArtifactDirectory != "" {
ct.Paths[artifacts.BuildArtifactPathType] = []string{buildArtifactDirectory}
}
containerizationOptions := getContainerizationOptions(servicedirectory)
if len(containerizationOptions) != 0 {
ct.Configs[artifacts.ContainerizationOptionsConfigType] = artifacts.ContainerizationOptionsConfig(containerizationOptions)
}
runningManifestPath, appinstance := getCfInstanceApp(cfInstanceApps, applicationName)
if application.DockerImage != "" || appinstance.Application.DockerImage != "" {
dockerImageName := application.DockerImage
if dockerImageName == "" {
dockerImageName = appinstance.Application.DockerImage
}
ctConfig := ct.Configs[artifacts.CloudFoundryConfigType].(artifacts.CloudFoundryConfig)
ctConfig.ImageName = dockerImageName
ct.Configs[artifacts.CloudFoundryConfigType] = ctConfig
}
if runningManifestPath != "" {
ct.Paths[artifacts.CfRunningManifestPathType] = append(ct.Paths[artifacts.CfRunningManifestPathType], runningManifestPath)
}
normalizedServiceName := common.MakeStringK8sServiceNameCompliant(applicationName)
services[normalizedServiceName] = []transformertypes.Artifact{ct}
}
}
return services, nil
}
// Transform transforms the artifacts
func (t *CloudFoundry) Transform(newArtifacts []transformertypes.Artifact, alreadySeenArtifacts []transformertypes.Artifact) ([]transformertypes.PathMapping, []transformertypes.Artifact, error) {
artifactsCreated := []transformertypes.Artifact{}
for _, a := range newArtifacts {
cfConfig := artifacts.CloudFoundryConfig{}
err := a.GetConfig(artifacts.CloudFoundryConfigType, &cfConfig)
if err != nil {
logrus.Errorf("unable to load config for Transformer into %T : %s", cfConfig, err)
continue
}
serviceConfig := artifacts.ServiceConfig{}
if err := a.GetConfig(artifacts.ServiceConfigType, &serviceConfig); err != nil {
logrus.Errorf("unable to load config for Transformer into %T : %s", serviceConfig, err)
continue
}
containerizationOptionsConfig := artifacts.ContainerizationOptionsConfig{}
if err := a.GetConfig(artifacts.ContainerizationOptionsConfigType, &containerizationOptionsConfig); err != nil {
logrus.Debugf("Unable to get containerization config : %s", err)
}
ir := irtypes.NewIR()
cfinstanceapp := collector.CfApp{}
logrus.Debugf("Transforming %s", cfConfig.ServiceName)
if runninginstancefile, ok := a.Paths[artifacts.CfRunningManifestPathType]; ok {
cfinstanceapp, err = getCfAppInstance(runninginstancefile[0], cfConfig.ServiceName)
if err != nil {
logrus.Debugf("The file at path %s is not a valid cf apps file. Error: %q", runninginstancefile[0], err)
}
}
if paths, ok := a.Paths[artifacts.CfManifestPathType]; ok {
path := paths[0] // TODO: what about the rest of the manifests?
applications, _, err := t.readApplicationManifest(path, cfConfig.ServiceName)
if err != nil {
logrus.Debugf("Error while trying to parse manifest : %s", err)
continue
}
logrus.Debugf("Using cf manifest file at path %s to transform service %s", path, cfConfig.ServiceName)
application := applications[0]
irService := irtypes.Service{Name: serviceConfig.ServiceName}
rList := core.ResourceList{"memory": resource.MustParse(fmt.Sprintf("%dM", cfinstanceapp.Application.Memory)),
"ephemeral-storage": resource.MustParse(fmt.Sprintf("%dM", cfinstanceapp.Application.DiskQuota))}
serviceContainer := core.Container{Name: serviceConfig.ServiceName,
Resources: core.ResourceRequirements{Requests: rList}}
serviceContainer.Image = cfConfig.ImageName
if serviceContainer.Image == "" {
serviceContainer.Image = serviceConfig.ServiceName
}
if cfinstanceapp.Application.Instances != 0 {
irService.Replicas = cfinstanceapp.Application.Instances
} else if application.Instances.IsSet {
irService.Replicas = application.Instances.Value
}
secretName := cfConfig.ServiceName + common.VcapCfSecretSuffix
envList, vcapEnvMap := t.prioritizeAndAddEnvironmentVariables(cfinstanceapp, application.EnvironmentVariables,
secretName, cfConfig.ServiceName)
serviceContainer.Env = append(serviceContainer.Env, envList...)
ir.Storages = append(ir.Storages, irtypes.Storage{Name: secretName,
StorageType: irtypes.SecretKind,
Content: vcapEnvMap})
for _, port := range cfinstanceapp.Application.Ports {
// Add the port to the k8s pod.
serviceContainer.Ports = append(serviceContainer.Ports, core.ContainerPort{ContainerPort: int32(port)})
// Forward the port on the k8s service to the k8s pod.
podPort := networking.ServiceBackendPort{Number: int32(port)}
servicePort := podPort
irService.AddPortForwarding(servicePort, podPort, "")
}
irService.Containers = []core.Container{serviceContainer}
ir.Services[serviceConfig.ServiceName] = irService
}
if len(containerizationOptionsConfig) != 0 {
quesKey := common.JoinQASubKeys(common.ConfigServicesKey, `"`+serviceConfig.ServiceName+`"`, common.ConfigContainerizationOptionServiceKeySegment)
containerizationOptions := qaengine.FetchMultiSelectAnswer(
quesKey,
fmt.Sprintf("Select the transformer to use for containerizing the '%s' service :", serviceConfig.ServiceName),
nil,
[]string{containerizationOptionsConfig[0]},
containerizationOptionsConfig,
nil,
)
secondaryArtifactsGenerated := false
for _, containerizationOption := range containerizationOptions {
containerizationArtifact := getContainerizationConfig(serviceConfig.ServiceName,
a.Paths[artifacts.ServiceDirPathType],
a.Paths[artifacts.BuildArtifactPathType],
containerizationOption)
if containerizationArtifact.Type == "" {
if cfConfig.ImageName == "" {
logrus.Errorf("No containerization option found for service %s", serviceConfig.ServiceName)
}
} else {
containerizationArtifact.Name = serviceConfig.ServiceName
if containerizationArtifact.Configs == nil {
containerizationArtifact.Configs = map[transformertypes.ConfigType]interface{}{}
}
containerizationArtifact.Configs[irtypes.IRConfigType] = ir
containerizationArtifact.Configs[artifacts.ServiceConfigType] = serviceConfig
artifactsCreated = append(artifactsCreated, containerizationArtifact)
secondaryArtifactsGenerated = true
}
}
if secondaryArtifactsGenerated {
continue
}
}
artifactsCreated = append(artifactsCreated, transformertypes.Artifact{
Name: t.Env.GetProjectName(),
Type: irtypes.IRArtifactType,
Configs: map[transformertypes.ConfigType]interface{}{
irtypes.IRConfigType: ir,
},
})
}
return nil, artifactsCreated, nil
}
// prioritizeAndAddEnvironmentVariables adds relevant environment variables relevant to the application deployment
func (t *CloudFoundry) prioritizeAndAddEnvironmentVariables(cfApp collector.CfApp,
manifestEnvMap map[string]string, secretName string, serviceName string) ([]core.EnvVar, map[string][]byte) {
vcapEnvMap := map[string][]byte{}
envOrderMap := map[string]core.EnvVar{}
// Manifest
for varname, value := range manifestEnvMap {
envOrderMap[varname] = core.EnvVar{Name: varname, Value: value}
}
for varname, value := range cfApp.Environment.StagingEnv {
envOrderMap[varname] = core.EnvVar{Name: varname, Value: fmt.Sprintf("%s", value)}
}
for varname, value := range cfApp.Environment.RunningEnv {
envOrderMap[varname] = core.EnvVar{Name: varname, Value: fmt.Sprintf("%s", value)}
}
for varname, value := range cfApp.Environment.SystemEnv {
valueStr := fmt.Sprintf("%s", value)
if varname == common.VcapServiceEnvName && valueStr != "" {
flattenedEnvList := flattenVcapServiceVariables(valueStr, serviceName)
for _, env := range flattenedEnvList {
env.Name = common.NormalizeForEnvironmentVariableName(env.Name)
envOrderMap[env.Name] = env
vcapEnvMap[env.Name] = []byte(env.Value)
}
vcapEnvMap[varname] = []byte(valueStr)
}
envOrderMap[varname] = core.EnvVar{Name: varname, Value: valueStr}
}
for varname, value := range cfApp.Environment.ApplicationEnv {
valueStr := fmt.Sprintf("%s", value)
envOrderMap[varname] = core.EnvVar{Name: varname, Value: valueStr}
vcapEnvMap[varname] = []byte(valueStr)
}
for varname, value := range cfApp.Environment.Environment {
envOrderMap[varname] = core.EnvVar{Name: varname, Value: fmt.Sprintf("%s", value)}
}
var envList []core.EnvVar
for _, env := range envOrderMap {
if _, ok := vcapEnvMap[env.Name]; ok {
vcapEnvMap[env.Name] = []byte(env.Value)
secretKeyRef := core.SecretKeySelector{}
secretKeyRef.Name = secretName
secretKeyRef.Key = env.Name
envList = append(envList,
core.EnvVar{Name: env.Name,
ValueFrom: &core.EnvVarSource{SecretKeyRef: &secretKeyRef}})
} else {
envList = append(envList, env)
}
}
// envList = append(envList, core.EnvVar{Name: "DATABASE_URL", })
return envList, vcapEnvMap
}
// flattenVariable flattens a given variable defined by <name, credential>
func flattenVariable(prefix string, credential interface{}) []core.EnvVar {
var credentialList []core.EnvVar
switch cred := credential.(type) {
case []interface{}:
for index, value := range cred {
envName := fmt.Sprintf("%s_%v", prefix, index)
credentialList = append(credentialList, flattenVariable(envName, value)...)
}
case map[string]interface{}:
for name, value := range cred {
envName := fmt.Sprintf("%s_%s", prefix, name)
credentialList = append(credentialList, flattenVariable(envName, value)...)
}
default:
return []core.EnvVar{{Name: strings.ToUpper(variableLiteralPattern.ReplaceAllLiteralString(prefix, "_")),
Value: fmt.Sprintf("%#v", credential)}}
}
return credentialList
}
// flattenVcapServiceVariables flattens the variables specified in the "credentials" field of VCAP_SERVICES
func flattenVcapServiceVariables(vcapService string, serviceName string) []core.EnvVar {
var flattenedEnvList []core.EnvVar
var serviceInstanceMap map[string][]artifacts.VCAPService
err := json.Unmarshal([]byte(vcapService), &serviceInstanceMap)
if err != nil {
logrus.Errorf("Could not unmarshal the service map instance (%s) in VCAP_SERVICES during CF flattening: %s", vcapService, err)
return nil
}
for _, serviceInstances := range serviceInstanceMap {
for _, serviceInstance := range serviceInstances {
if serviceInstance.ServiceName == serviceName {
if uriValue, ok := serviceInstance.ServiceCredentials["uri"].(string); ok {
flattenedEnvList = append(flattenedEnvList, core.EnvVar{Name: "DATABASE_URL", Value: uriValue})
} else {
logrus.Errorf("uri field is not available in service credential for the service VCAP_SERVICES")
}
}
flattenedEnvList = append(flattenedEnvList, flattenVariable(serviceInstance.ServiceName, serviceInstance.ServiceCredentials)...)
}
}
return flattenedEnvList
}
// readApplicationManifest reads an application manifest
func (t *CloudFoundry) readApplicationManifest(path string, serviceName string) ([]manifest.Application, []string, error) { // manifest, parameters
trimmedvariables, err := getMissingVariables(path)
if err != nil {
logrus.Debugf("Unable to read as cf manifest %s : %s", path, err)
return nil, nil, err
}
rawManifest, err := os.ReadFile(path)
if err != nil {
logrus.Errorf("Unable to read manifest file at path %q Error: %q", path, err)
return nil, nil, err
}
tpl := template.NewTemplate(rawManifest)
fileVars := template.StaticVariables{}
for _, variable := range trimmedvariables {
fileVars[variable] = "{{ index .Values " + `"globalvariables" "` + variable + `"}}`
}
rawManifest, err = tpl.Evaluate(fileVars, nil, template.EvaluateOpts{ExpectAllKeys: true})
if err != nil {
logrus.Debugf("Interpolation Error %s", err)
return nil, nil, err
}
var m manifest.Manifest
err = yaml.Unmarshal(rawManifest, &m)
if err != nil {
logrus.Debugf("UnMarshalling error %s", err)
return nil, nil, err
}
if len(m.Applications) == 1 {
//If the service name is missing, use the directory name
return m.Applications, trimmedvariables, nil
}
applications := []manifest.Application{}
if serviceName != "" {
for _, application := range m.Applications {
if application.Name == serviceName {
applications = append(applications, application)
}
}
} else {
applications = m.Applications
}
return applications, trimmedvariables, nil
}
func getMissingVariables(path string) ([]string, error) {
trimmedvariables := []string{}
_, err := manifest.ReadAndInterpolateManifest(path, []string{}, []template.VarKV{})
if err != nil {
errstring := err.Error()
if strings.Contains(errstring, "Expected to find variables:") {
variablesstr := strings.Split(errstring, ":")[1]
variables := strings.Split(variablesstr, ",")
for _, variable := range variables {
trimmedvariables = append(trimmedvariables, strings.TrimSpace(variable))
}
} else {
logrus.Debugf("Error %s", err)
return []string{}, err
}
}
return trimmedvariables, nil
}
func getCfInstanceApp(fileApps map[string][]collector.CfApp, name string) (string, collector.CfApp) {
for path, apps := range fileApps {
for _, app := range apps {
if app.Application.Name == name {
return path, app
}
}
}
return "", collector.CfApp{}
}
func getCfAppInstance(path string, appname string) (collector.CfApp, error) {
cfinstanceappsfile := collector.CfApps{}
if err := common.ReadMove2KubeYaml(path, &cfinstanceappsfile); err != nil {
return collector.CfApp{}, err
}
for _, app := range cfinstanceappsfile.Spec.CfApps {
if app.Application.Name == appname {
return app, nil
}
}
return collector.CfApp{}, fmt.Errorf("failed to find the app %s in the cf apps file at path %s", appname, path)
}