parse.go 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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) (*porterv1.PorterApp, error) {
  20. ctx, span := telemetry.NewSpan(ctx, "porter-app-parse-yaml")
  21. defer span.End()
  22. if porterYaml == nil {
  23. return 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, telemetry.Error(ctx, span, err, "error unmarshaling porter yaml")
  29. }
  30. var appProto *porterv1.PorterApp
  31. switch version.Version {
  32. case PorterYamlVersion_V2:
  33. appProto, err = v2.AppProtoFromYaml(ctx, porterYaml)
  34. if err != nil {
  35. return nil, telemetry.Error(ctx, span, err, "error converting v2 yaml to proto")
  36. }
  37. // backwards compatibility for old porter.yaml files
  38. // track this span in telemetry and reach out to customers who are still using old porter.yaml if they exist.
  39. // once no one is converting from old porter.yaml, we can remove this code
  40. case PorterYamlVersion_V1, "":
  41. appProto, err = v1.AppProtoFromYaml(ctx, porterYaml)
  42. if err != nil {
  43. return nil, telemetry.Error(ctx, span, err, "error converting v1 yaml to proto")
  44. }
  45. default:
  46. return nil, telemetry.Error(ctx, span, nil, "porter yaml version not supported")
  47. }
  48. if appProto == nil {
  49. return nil, telemetry.Error(ctx, span, nil, "porter yaml output is nil")
  50. }
  51. return appProto, nil
  52. }
  53. // yamlVersion is a struct used to unmarshal the version field of a Porter YAML file
  54. type yamlVersion struct {
  55. Version PorterYamlVersion `yaml:"version"`
  56. }