parse.go 2.5 KB

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