parse.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. porterv1 "github.com/porter-dev/api-contracts/generated/go/porter/v1"
  8. "github.com/porter-dev/porter/internal/telemetry"
  9. )
  10. // PorterYamlVersion is a string type for the version of the porter yaml
  11. type PorterYamlVersion string
  12. const (
  13. // PorterYamlVersion_V2 is the v2 version of the porter yaml
  14. PorterYamlVersion_V2 PorterYamlVersion = "v2"
  15. // PorterYamlVersion_V1 is the v1, legacy version of the porter yaml
  16. PorterYamlVersion_V1 PorterYamlVersion = "v1stack"
  17. )
  18. // ParseYAML converts a Porter YAML file into a PorterApp proto object
  19. func ParseYAML(ctx context.Context, porterYaml []byte, appName string) (*porterv1.PorterApp, map[string]string, error) {
  20. ctx, span := telemetry.NewSpan(ctx, "porter-app-parse-yaml")
  21. defer span.End()
  22. if porterYaml == nil {
  23. return nil, nil, 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 nil, nil, telemetry.Error(ctx, span, err, "error unmarshaling porter yaml")
  29. }
  30. var appProto *porterv1.PorterApp
  31. var envVariables map[string]string
  32. switch version.Version {
  33. case PorterYamlVersion_V2:
  34. appProto, envVariables, err = v2.AppProtoFromYaml(ctx, porterYaml)
  35. if err != nil {
  36. return nil, nil, telemetry.Error(ctx, span, err, "error converting v2 yaml to proto")
  37. }
  38. // backwards compatibility for old porter.yaml files
  39. // track this span in telemetry and reach out to customers who are still using old porter.yaml if they exist.
  40. // once no one is converting from old porter.yaml, we can remove this code
  41. case PorterYamlVersion_V1, "":
  42. appProto, envVariables, err = v1.AppProtoFromYaml(ctx, porterYaml)
  43. if err != nil {
  44. return nil, nil, telemetry.Error(ctx, span, err, "error converting v1 yaml to proto")
  45. }
  46. default:
  47. return nil, nil, telemetry.Error(ctx, span, nil, "porter yaml version not supported")
  48. }
  49. if appProto == nil {
  50. return nil, nil, 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 appProto.Name != "" && appProto.Name != appName {
  55. telemetry.WithAttributes(span, telemetry.AttributeKV{Key: "parsed-name", Value: appProto.Name})
  56. return nil, nil, telemetry.Error(ctx, span, nil, "name specified in porter.yaml does not match app name")
  57. }
  58. appProto.Name = appName
  59. }
  60. return appProto, envVariables, nil
  61. }
  62. // yamlVersion is a struct used to unmarshal the version field of a Porter YAML file
  63. type yamlVersion struct {
  64. Version PorterYamlVersion `yaml:"version"`
  65. }