app_resource.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. package v2beta1
  2. import (
  3. "fmt"
  4. "strings"
  5. "github.com/mitchellh/mapstructure"
  6. apiTypes "github.com/porter-dev/porter/api/types"
  7. "github.com/porter-dev/porter/internal/integrations/preview"
  8. "github.com/porter-dev/switchboard/pkg/types"
  9. )
  10. func (a *AppResource) GetName() string {
  11. if a == nil || a.Name == nil {
  12. return ""
  13. }
  14. return *a.Name
  15. }
  16. func (a *AppResource) GetDependsOn() []string {
  17. var dependsOn []string
  18. if a == nil || a.DependsOn == nil {
  19. return dependsOn
  20. }
  21. for _, d := range a.DependsOn {
  22. if d == nil {
  23. continue
  24. }
  25. dependsOn = append(dependsOn, *d)
  26. }
  27. return dependsOn
  28. }
  29. func (a *AppResource) GetBuildRef() string {
  30. if a == nil || a.BuildRef == nil {
  31. return ""
  32. }
  33. return *a.BuildRef
  34. }
  35. func (a *AppResource) getV1Resource(b *Build) (*types.Resource, error) {
  36. config := &preview.ApplicationConfig{}
  37. config.Build.Method = "registry"
  38. config.Build.Image = fmt.Sprintf("\"{ .%s.image }\"", b.GetName())
  39. config.Build.Env = b.GetRawEnv()
  40. config.Values = a.HelmValues
  41. for _, eg := range b.GetEnvGroups() {
  42. ns, name, _ := strings.Cut(eg, "/")
  43. config.EnvGroups = append(config.EnvGroups, apiTypes.EnvGroupMeta{
  44. Name: name,
  45. Namespace: ns,
  46. })
  47. }
  48. rawConfig := make(map[string]any)
  49. err := mapstructure.Decode(config, &rawConfig)
  50. if err != nil {
  51. return nil, err
  52. }
  53. return &types.Resource{
  54. Name: a.GetName(),
  55. DependsOn: append([]string{b.GetName()}, a.GetDependsOn()...),
  56. Source: map[string]any{
  57. "name": a.Chart.GetName(),
  58. "repo": a.Chart.GetURL(),
  59. "version": a.Chart.GetVersion(),
  60. },
  61. Config: rawConfig,
  62. }, nil
  63. }