parse.go 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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, 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, appName)
  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. if appName == "" {
  42. return nil, telemetry.Error(ctx, span, nil, "v1 porter yaml requires externally-provided app name")
  43. }
  44. appProto, err = v1.AppProtoFromYaml(ctx, porterYaml, appName)
  45. if err != nil {
  46. return nil, telemetry.Error(ctx, span, err, "error converting v1 yaml to proto")
  47. }
  48. default:
  49. return nil, telemetry.Error(ctx, span, nil, "porter yaml version not supported")
  50. }
  51. if appProto == nil {
  52. return nil, telemetry.Error(ctx, span, nil, "porter yaml output is nil")
  53. }
  54. return appProto, nil
  55. }
  56. // yamlVersion is a struct used to unmarshal the version field of a Porter YAML file
  57. type yamlVersion struct {
  58. Version PorterYamlVersion `yaml:"version"`
  59. }