2
0

parse.go 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. package porter_app
  2. import (
  3. "context"
  4. v1 "github.com/porter-dev/porter/internal/porter_app/v1"
  5. v2 "github.com/porter-dev/porter/internal/porter_app/v2"
  6. "sigs.k8s.io/yaml"
  7. "github.com/porter-dev/porter/internal/telemetry"
  8. )
  9. // PorterYamlVersion is a string type for the version of the porter yaml
  10. type PorterYamlVersion string
  11. const (
  12. // PorterYamlVersion_V2 is the v2 version of the porter yaml
  13. PorterYamlVersion_V2 PorterYamlVersion = "v2"
  14. // PorterYamlVersion_V1 is the v1, legacy version of the porter yaml
  15. PorterYamlVersion_V1 PorterYamlVersion = "v1stack"
  16. )
  17. // ParseYAML converts a Porter YAML file into a PorterApp proto object
  18. func ParseYAML(ctx context.Context, porterYaml []byte, appName string) (v2.AppWithPreviewOverrides, error) {
  19. ctx, span := telemetry.NewSpan(ctx, "porter-app-parse-yaml")
  20. defer span.End()
  21. var appDefinition v2.AppWithPreviewOverrides
  22. if porterYaml == nil {
  23. return appDefinition, telemetry.Error(ctx, span, nil, "porter yaml input is nil")
  24. }
  25. version := &YamlVersion{}
  26. err := yaml.Unmarshal(porterYaml, version)
  27. if err != nil {
  28. return appDefinition, telemetry.Error(ctx, span, err, "error unmarshaling porter yaml")
  29. }
  30. switch version.Version {
  31. case PorterYamlVersion_V2:
  32. appDefinition, err = v2.AppProtoFromYaml(ctx, porterYaml)
  33. if err != nil {
  34. return appDefinition, telemetry.Error(ctx, span, err, "error converting v2 yaml to proto")
  35. }
  36. // backwards compatibility for old porter.yaml files
  37. // track this span in telemetry and reach out to customers who are still using old porter.yaml if they exist.
  38. // once no one is converting from old porter.yaml, we can remove this code
  39. case PorterYamlVersion_V1, "":
  40. appProto, envVariables, err := v1.AppProtoFromYaml(ctx, porterYaml)
  41. if err != nil {
  42. return appDefinition, telemetry.Error(ctx, span, err, "error converting v1 yaml to proto")
  43. }
  44. appDefinition.AppProto = appProto
  45. appDefinition.EnvVariables = envVariables
  46. default:
  47. return appDefinition, telemetry.Error(ctx, span, nil, "porter yaml version not supported")
  48. }
  49. if appDefinition.AppProto == nil {
  50. return appDefinition, telemetry.Error(ctx, span, nil, "porter yaml output is nil")
  51. }
  52. if appName != "" {
  53. telemetry.WithAttributes(span, telemetry.AttributeKV{Key: "override-name", Value: appName})
  54. if appDefinition.AppProto.Name != "" && appDefinition.AppProto.Name != appName {
  55. telemetry.WithAttributes(span, telemetry.AttributeKV{Key: "parsed-name", Value: appDefinition.AppProto.Name})
  56. return appDefinition, telemetry.Error(ctx, span, nil, "name specified in porter.yaml does not match app name")
  57. }
  58. appDefinition.AppProto.Name = appName
  59. }
  60. if appDefinition.PreviewApp != nil && appDefinition.PreviewApp.AppProto != nil {
  61. appDefinition.PreviewApp.AppProto.Name = appDefinition.AppProto.Name
  62. }
  63. return appDefinition, nil
  64. }
  65. // yamlVersion is a struct used to unmarshal the version field of a Porter YAML file
  66. type YamlVersion struct {
  67. Version PorterYamlVersion `yaml:"version"`
  68. }