parse.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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 PorterYamlVersion = "v1stack"
  16. )
  17. // ParseYAML converts a Porter YAML file into a PorterApp proto object
  18. func ParseYAML(ctx context.Context, porterYaml []byte) (*porterv1.PorterApp, error) {
  19. ctx, span := telemetry.NewSpan(ctx, "porter-app-parse-yaml")
  20. defer span.End()
  21. if porterYaml == nil {
  22. return nil, telemetry.Error(ctx, span, nil, "porter yaml input is nil")
  23. }
  24. version := &yamlVersion{}
  25. err := yaml.Unmarshal(porterYaml, version)
  26. if err != nil {
  27. return nil, telemetry.Error(ctx, span, err, "error unmarshaling porter yaml")
  28. }
  29. var appProto *porterv1.PorterApp
  30. switch version.Version {
  31. case PorterYamlVersion_V2:
  32. appProto, err = v2.AppProtoFromYaml(ctx, porterYaml)
  33. if err != nil {
  34. return nil, 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, err = v1.AppProtoFromYaml(ctx, porterYaml)
  41. if err != nil {
  42. return nil, telemetry.Error(ctx, span, err, "error converting v1 yaml to proto")
  43. }
  44. default:
  45. return nil, telemetry.Error(ctx, span, nil, "porter yaml version not supported")
  46. }
  47. if appProto == nil {
  48. return nil, telemetry.Error(ctx, span, nil, "porter yaml output is nil")
  49. }
  50. return appProto, nil
  51. }
  52. // yamlVersion is a struct used to unmarshal the version field of a Porter YAML file
  53. type yamlVersion struct {
  54. Version PorterYamlVersion `yaml:"version"`
  55. }